Rust: use generated CSR functions.

This commit is contained in:
whitequark 2016-09-29 14:04:42 +00:00
parent fdcb27ccff
commit 3263def5c8
4 changed files with 19 additions and 12 deletions

View File

@ -0,0 +1,15 @@
use core::{cmp, ptr, str};
include!(concat!(env!("BUILDINC_DIRECTORY"), "/generated/mem.rs"));
include!(concat!(env!("BUILDINC_DIRECTORY"), "/generated/csr.rs"));
pub fn ident(buf: &mut [u8]) -> &str {
unsafe {
let len = ptr::read_volatile(csr::IDENTIFIER_MEM_BASE);
let len = cmp::min(len as usize, buf.len());
for i in 0..len {
buf[i] = ptr::read_volatile(csr::IDENTIFIER_MEM_BASE.offset(1 + i as isize)) as u8
}
str::from_utf8_unchecked(&buf[..len])
}
}

View File

@ -48,9 +48,9 @@ impl Log for BufferLogger {
fn log(&self, record: &LogRecord) {
if self.enabled(record.metadata()) {
use core::fmt::Write;
writeln!(self.buffer.borrow_mut(), "{}({}): {}",
writeln!(self.buffer.borrow_mut(), "{:>5}({}): {}",
record.level(), record.location().module_path(), record.args()).unwrap();
println!("{}({}): {}",
println!("{:>5}({}): {}",
record.level(), record.location().module_path(), record.args());
}
}

View File

@ -11,6 +11,7 @@ extern crate byteorder;
use std::prelude::v1::*;
use buffer_logger::BufferLogger;
pub mod board;
pub mod io;
pub mod buffer_logger;
pub mod session;

View File

@ -68,16 +68,7 @@ fn handle_request(stream: &mut ::io::TcpStream,
match try!(read_request(stream)) {
Request::Ident => {
let mut ident: [u8; 256];
let ident = unsafe {
extern { fn get_ident(ident: *mut u8); }
ident = ::core::mem::uninitialized();
get_ident(ident.as_mut_ptr());
&ident[..ident.iter().position(|&c| c == 0).unwrap()]
};
write_reply(stream, Reply::Ident(str::from_utf8(ident).unwrap()))
write_reply(stream, Reply::Ident(::board::ident(&mut [0; 64])))
}
Request::Log => {