libdrtioaux: do not attempt to access non-existent DRTIO gateware

This commit is contained in:
Sebastien Bourdeauducq 2017-02-22 16:45:02 +08:00
parent 257527629a
commit 016743f079
5 changed files with 115 additions and 94 deletions

View File

@ -2,6 +2,7 @@
authors = ["M-Labs"] authors = ["M-Labs"]
name = "drtioaux" name = "drtioaux"
version = "0.0.0" version = "0.0.0"
build = "build.rs"
[lib] [lib]
name = "drtioaux" name = "drtioaux"

View File

@ -0,0 +1,15 @@
use std::env;
use std::path::Path;
use std::io::{BufRead, BufReader};
use std::fs::File;
fn main() {
let out_dir = env::var("BUILDINC_DIRECTORY").unwrap();
let cfg_path = Path::new(&out_dir).join("generated").join("rust-cfg");
println!("cargo:rerun-if-changed={}", cfg_path.to_str().unwrap());
let f = BufReader::new(File::open(&cfg_path).unwrap());
for line in f.lines() {
println!("cargo:rustc-cfg={}", line.unwrap());
}
}

View File

@ -10,7 +10,7 @@ extern crate byteorder;
mod proto; mod proto;
mod crc32; mod crc32;
use std::io::{self, Cursor, Read, Write}; use std::io::{self, Read, Write};
use core::slice; use core::slice;
use proto::*; use proto::*;
@ -40,11 +40,16 @@ impl Packet {
} }
} }
const AUX_TX_BASE: usize = board::mem::DRTIO_AUX_BASE; #[cfg(has_drtio)]
const AUX_TX_SIZE: usize = board::mem::DRTIO_AUX_SIZE/2; pub mod hw {
const AUX_RX_BASE: usize = AUX_TX_BASE + AUX_TX_SIZE; use super::*;
use std::io::Cursor;
fn rx_has_error() -> bool { const AUX_TX_BASE: usize = board::mem::DRTIO_AUX_BASE;
const AUX_TX_SIZE: usize = board::mem::DRTIO_AUX_SIZE/2;
const AUX_RX_BASE: usize = AUX_TX_BASE + AUX_TX_SIZE;
fn rx_has_error() -> bool {
unsafe { unsafe {
let error = board::csr::drtio::aux_rx_error_read() != 0; let error = board::csr::drtio::aux_rx_error_read() != 0;
if error { if error {
@ -52,19 +57,19 @@ fn rx_has_error() -> bool {
} }
error error
} }
} }
pub struct RxBuffer(&'static [u8]); struct RxBuffer(&'static [u8]);
impl Drop for RxBuffer { impl Drop for RxBuffer {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
board::csr::drtio::aux_rx_present_write(1); board::csr::drtio::aux_rx_present_write(1);
} }
} }
} }
fn rx_get_buffer() -> Option<RxBuffer> { fn rx_get_buffer() -> Option<RxBuffer> {
unsafe { unsafe {
if board::csr::drtio::aux_rx_present_read() == 1 { if board::csr::drtio::aux_rx_present_read() == 1 {
let length = board::csr::drtio::aux_rx_length_read(); let length = board::csr::drtio::aux_rx_length_read();
@ -74,9 +79,9 @@ fn rx_get_buffer() -> Option<RxBuffer> {
None None
} }
} }
} }
pub fn recv_packet() -> io::Result<Option<Packet>> { pub fn recv() -> io::Result<Option<Packet>> {
if rx_has_error() { if rx_has_error() {
return Err(io::Error::new(io::ErrorKind::Other, "gateware reported error")) return Err(io::Error::new(io::ErrorKind::Other, "gateware reported error"))
} }
@ -106,23 +111,23 @@ pub fn recv_packet() -> io::Result<Option<Packet>> {
} }
None => Ok(None) None => Ok(None)
} }
} }
fn tx_get_buffer() -> &'static mut [u8] { fn tx_get_buffer() -> &'static mut [u8] {
unsafe { unsafe {
while board::csr::drtio::aux_tx_read() != 0 {} while board::csr::drtio::aux_tx_read() != 0 {}
slice::from_raw_parts_mut(AUX_TX_BASE as *mut u8, AUX_TX_SIZE) slice::from_raw_parts_mut(AUX_TX_BASE as *mut u8, AUX_TX_SIZE)
} }
} }
fn tx_ack_buffer(length: u16) { fn tx_ack_buffer(length: u16) {
unsafe { unsafe {
board::csr::drtio::aux_tx_length_write(length); board::csr::drtio::aux_tx_length_write(length);
board::csr::drtio::aux_tx_write(1) board::csr::drtio::aux_tx_write(1)
} }
} }
pub fn send_packet(packet: &Packet) -> io::Result<()> { pub fn send(packet: &Packet) -> io::Result<()> {
let sl = tx_get_buffer(); let sl = tx_get_buffer();
let mut writer = Cursor::new(sl); let mut writer = Cursor::new(sl);
@ -144,4 +149,5 @@ pub fn send_packet(packet: &Packet) -> io::Result<()> {
tx_ack_buffer(len as u16); tx_ack_buffer(len as u16);
Ok(()) Ok(())
}
} }

View File

@ -113,9 +113,9 @@ mod drtio {
return 0 return 0
} }
count += 1; count += 1;
drtioaux::send_packet(&drtioaux::Packet::EchoRequest).unwrap(); drtioaux::hw::send(&drtioaux::Packet::EchoRequest).unwrap();
io.sleep(100).unwrap(); io.sleep(100).unwrap();
let pr = drtioaux::recv_packet(); let pr = drtioaux::hw::recv();
match pr { match pr {
Ok(Some(drtioaux::Packet::EchoReply)) => return count, Ok(Some(drtioaux::Packet::EchoReply)) => return count,
_ => {} _ => {}

View File

@ -14,14 +14,13 @@ extern crate drtioaux;
fn process_aux_packet(p: drtioaux::Packet) { fn process_aux_packet(p: drtioaux::Packet) {
match p { match p {
drtioaux::Packet::EchoRequest => drtioaux::send_packet(&drtioaux::Packet::EchoReply).unwrap(), drtioaux::Packet::EchoRequest => drtioaux::hw::send(&drtioaux::Packet::EchoReply).unwrap(),
_ => warn!("received unexpected aux packet {:?}", p) _ => warn!("received unexpected aux packet {:?}", p)
} }
} }
fn process_aux_packets() { fn process_aux_packets() {
let pr = drtioaux::recv_packet(); let pr = drtioaux::hw::recv();
match pr { match pr {
Ok(None) => {}, Ok(None) => {},
Ok(Some(p)) => process_aux_packet(p), Ok(Some(p)) => process_aux_packet(p),