Moninj: fix working with DRTIO #190

Merged
sb10q merged 5 commits from mwojcik/artiq-zynq:moninj_async into master 2022-05-25 12:17:24 +08:00
4 changed files with 41 additions and 35 deletions

View File

@ -134,7 +134,7 @@
cargoLock = { cargoLock = {
lockFile = src/Cargo.lock; lockFile = src/Cargo.lock;
outputHashes = { outputHashes = {
"libasync-0.0.0" = "sha256-7qRHEHg+CXqqZSLgV4j9XLrLj6mlaeXzCZ8eFkRa0U8="; "libasync-0.0.0" = "sha256-xuwesRrQiccopPTCkwGqQxld74X74q7EVsKIrE0zirc=";
}; };
}; };

12
src/Cargo.lock generated
View File

@ -219,7 +219,7 @@ dependencies = [
[[package]] [[package]]
name = "libasync" name = "libasync"
version = "0.0.0" version = "0.0.0"
source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#56c27e98e4fc2bd719d26abaa98a7628c395b9c0" source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#605c8f73a60cf41a6e2d535c5850933d3154e935"
dependencies = [ dependencies = [
"embedded-hal", "embedded-hal",
"libcortex_a9", "libcortex_a9",
@ -251,7 +251,7 @@ dependencies = [
[[package]] [[package]]
name = "libboard_zynq" name = "libboard_zynq"
version = "0.0.0" version = "0.0.0"
source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#56c27e98e4fc2bd719d26abaa98a7628c395b9c0" source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#605c8f73a60cf41a6e2d535c5850933d3154e935"
dependencies = [ dependencies = [
"bit_field", "bit_field",
"embedded-hal", "embedded-hal",
@ -276,7 +276,7 @@ dependencies = [
[[package]] [[package]]
name = "libconfig" name = "libconfig"
version = "0.1.0" version = "0.1.0"
source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#56c27e98e4fc2bd719d26abaa98a7628c395b9c0" source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#605c8f73a60cf41a6e2d535c5850933d3154e935"
dependencies = [ dependencies = [
"core_io", "core_io",
"fatfs", "fatfs",
@ -287,7 +287,7 @@ dependencies = [
[[package]] [[package]]
name = "libcortex_a9" name = "libcortex_a9"
version = "0.0.0" version = "0.0.0"
source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#56c27e98e4fc2bd719d26abaa98a7628c395b9c0" source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#605c8f73a60cf41a6e2d535c5850933d3154e935"
dependencies = [ dependencies = [
"bit_field", "bit_field",
"libregister", "libregister",
@ -303,7 +303,7 @@ checksum = "33a33a362ce288760ec6a508b94caaec573ae7d3bbbd91b87aa0bad4456839db"
[[package]] [[package]]
name = "libregister" name = "libregister"
version = "0.0.0" version = "0.0.0"
source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#56c27e98e4fc2bd719d26abaa98a7628c395b9c0" source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#605c8f73a60cf41a6e2d535c5850933d3154e935"
dependencies = [ dependencies = [
"bit_field", "bit_field",
"vcell", "vcell",
@ -313,7 +313,7 @@ dependencies = [
[[package]] [[package]]
name = "libsupport_zynq" name = "libsupport_zynq"
version = "0.0.0" version = "0.0.0"
source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#56c27e98e4fc2bd719d26abaa98a7628c395b9c0" source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#605c8f73a60cf41a6e2d535c5850933d3154e935"
dependencies = [ dependencies = [
"cc", "cc",
"compiler_builtins", "compiler_builtins",

View File

@ -20,7 +20,7 @@ use crate::proto_async::*;
pub enum Error { pub enum Error {
NetworkError(smoltcp::Error), NetworkError(smoltcp::Error),
UnexpectedPattern, UnexpectedPattern,
UnrecognizedPacket, UnrecognizedPacket
} }
pub type Result<T> = core::result::Result<T, Error>; pub type Result<T> = core::result::Result<T, Error>;
@ -58,45 +58,47 @@ enum DeviceMessage {
#[cfg(has_drtio)] #[cfg(has_drtio)]
mod remote_moninj { mod remote_moninj {
use super::*; use super::*;
use libboard_artiq::drtioaux; use libboard_artiq::drtioaux_async;
use crate::rtio_mgt::drtio; use crate::rtio_mgt::drtio;
use log::error; use log::error;
pub fn read_probe(aux_mutex: &Rc<Mutex<bool>>, timer: GlobalTimer, linkno: u8, destination: u8, channel: i32, probe: i8) -> i64 { pub async fn read_probe(aux_mutex: &Rc<Mutex<bool>>, timer: GlobalTimer, linkno: u8, destination: u8, channel: i32, probe: i8) -> i64 {
let reply = task::block_on(drtio::aux_transact(aux_mutex, linkno, &drtioaux::Packet::MonitorRequest { let reply = drtio::aux_transact(aux_mutex, linkno, &drtioaux_async::Packet::MonitorRequest {
destination: destination, destination: destination,
channel: channel as _, channel: channel as _,
probe: probe as _}, probe: probe as _},
timer)); timer).await;
match reply { match reply {
Ok(drtioaux::Packet::MonitorReply { value }) => return value as i64, Ok(drtioaux_async::Packet::MonitorReply { value }) => return value as i64,
Ok(packet) => error!("received unexpected aux packet: {:?}", packet), Ok(packet) => error!("received unexpected aux packet: {:?}", packet),
Err("link went down") => { debug!("link is down"); },
Err(e) => error!("aux packet error ({})", e) Err(e) => error!("aux packet error ({})", e)
} }
0 0
} }
pub fn inject(aux_mutex: &Rc<Mutex<bool>>, _timer: GlobalTimer, linkno: u8, destination: u8, channel: i32, overrd: i8, value: i8) { pub async fn inject(aux_mutex: &Rc<Mutex<bool>>, _timer: GlobalTimer, linkno: u8, destination: u8, channel: i32, overrd: i8, value: i8) {
let _lock = aux_mutex.lock(); let _lock = aux_mutex.lock();
drtioaux::send(linkno, &drtioaux::Packet::InjectionRequest { drtioaux_async::send(linkno, &drtioaux_async::Packet::InjectionRequest {
destination: destination, destination: destination,
channel: channel as _, channel: channel as _,
overrd: overrd as _, overrd: overrd as _,
value: value as _ value: value as _
}).unwrap(); }).await.unwrap();
} }
pub fn read_injection_status(aux_mutex: &Rc<Mutex<bool>>, timer: GlobalTimer, linkno: u8, destination: u8, channel: i32, overrd: i8) -> i8 { pub async fn read_injection_status(aux_mutex: &Rc<Mutex<bool>>, timer: GlobalTimer, linkno: u8, destination: u8, channel: i32, overrd: i8) -> i8 {
let reply = task::block_on(drtio::aux_transact(aux_mutex, let reply = drtio::aux_transact(aux_mutex,
linkno, linkno,
&drtioaux::Packet::InjectionStatusRequest { &drtioaux_async::Packet::InjectionStatusRequest {
destination: destination, destination: destination,
channel: channel as _, channel: channel as _,
overrd: overrd as _}, overrd: overrd as _},
timer)); timer).await;
match reply { match reply {
Ok(drtioaux::Packet::InjectionStatusReply { value }) => return value as i8, Ok(drtioaux_async::Packet::InjectionStatusReply { value }) => return value as i8,
Ok(packet) => error!("received unexpected aux packet: {:?}", packet), Ok(packet) => error!("received unexpected aux packet: {:?}", packet),
Err("link went down") => { debug!("link is down"); },
Err(e) => error!("aux packet error ({})", e) Err(e) => error!("aux packet error ({})", e)
} }
0 0
@ -142,7 +144,7 @@ macro_rules! dispatch {
local_moninj::$func(channel.into(), $($param, )*) local_moninj::$func(channel.into(), $($param, )*)
} else { } else {
let linkno = hop - 1 as u8; let linkno = hop - 1 as u8;
remote_moninj::$func($aux_mutex, $timer, linkno, destination, channel, $($param, )*) remote_moninj::$func($aux_mutex, $timer, linkno, destination, channel, $($param, )*).await
} }
}} }}
} }
@ -163,20 +165,21 @@ async fn handle_connection(stream: &TcpStream, timer: GlobalTimer,
let mut probe_watch_list: BTreeMap<(i32, i8), Option<i64>> = BTreeMap::new(); let mut probe_watch_list: BTreeMap<(i32, i8), Option<i64>> = BTreeMap::new();
let mut inject_watch_list: BTreeMap<(i32, i8), Option<i8>> = BTreeMap::new(); let mut inject_watch_list: BTreeMap<(i32, i8), Option<i8>> = BTreeMap::new();
let mut next_check = Milliseconds(0); let mut next_check = timer.get_time();
loop { let timeout = |next_check: Milliseconds| -> nb::Result<(), Void> {
// TODO: we don't need fuse() here. if timer.get_time() < next_check {
// remove after https://github.com/rust-lang/futures-rs/issues/1989 lands
let read_message_f = read_i8(&stream).fuse();
let next_check_c = next_check.clone();
let timeout = || -> nb::Result<(), Void> {
if timer.get_time() < next_check_c {
Err(nb::Error::WouldBlock) Err(nb::Error::WouldBlock)
} else { } else {
Ok(()) Ok(())
} }
}; };
let timeout_f = block_async!(timeout()).fuse(); loop {
// TODO: we don't need fuse() here.
// remove after https://github.com/rust-lang/futures-rs/issues/1989 lands
let read_message_f = read_i8(&stream).fuse();
let next_check_c = next_check.clone();
let timeout_f = block_async!(timeout(next_check_c)).fuse();
pin_mut!(read_message_f, timeout_f); pin_mut!(read_message_f, timeout_f);
select_biased! { select_biased! {
message = read_message_f => { message = read_message_f => {
@ -246,7 +249,7 @@ async fn handle_connection(stream: &TcpStream, timer: GlobalTimer,
*previous = Some(current); *previous = Some(current);
} }
} }
next_check = next_check + Milliseconds(200); next_check = timer.get_time() + Milliseconds(200);
} }
} }
} }

View File

@ -49,7 +49,10 @@ pub mod drtio {
pub async fn aux_transact(aux_mutex: &Mutex<bool>, linkno: u8, request: &Packet, pub async fn aux_transact(aux_mutex: &Mutex<bool>, linkno: u8, request: &Packet,
timer: GlobalTimer) -> Result<Packet, &'static str> { timer: GlobalTimer) -> Result<Packet, &'static str> {
let _lock = aux_mutex.lock(); if !link_rx_up(linkno).await {
return Err("link went down");
}
let _lock = aux_mutex.async_lock().await;
drtioaux_async::send(linkno, request).await.unwrap(); drtioaux_async::send(linkno, request).await.unwrap();
recv_aux_timeout(linkno, 200, timer).await recv_aux_timeout(linkno, 200, timer).await
} }
@ -88,7 +91,7 @@ pub mod drtio {
} }
async fn sync_tsc(aux_mutex: &Rc<Mutex<bool>>, linkno: u8, timer: GlobalTimer) -> Result<(), &'static str> { async fn sync_tsc(aux_mutex: &Rc<Mutex<bool>>, linkno: u8, timer: GlobalTimer) -> Result<(), &'static str> {
let _lock = aux_mutex.lock(); let _lock = aux_mutex.async_lock().await;
unsafe { unsafe {
(csr::DRTIO[linkno as usize].set_time_write)(1); (csr::DRTIO[linkno as usize].set_time_write)(1);
@ -142,7 +145,7 @@ pub mod drtio {
} }
async fn process_unsolicited_aux(aux_mutex: &Rc<Mutex<bool>>, linkno: u8) { async fn process_unsolicited_aux(aux_mutex: &Rc<Mutex<bool>>, linkno: u8) {
let _lock = aux_mutex.lock(); let _lock = aux_mutex.async_lock().await;
match drtioaux_async::recv(linkno).await { match drtioaux_async::recv(linkno).await {
Ok(Some(packet)) => warn!("[LINK#{}] unsolicited aux packet: {:?}", linkno, packet), Ok(Some(packet)) => warn!("[LINK#{}] unsolicited aux packet: {:?}", linkno, packet),
Ok(None) => (), Ok(None) => (),