From 2d10503c203b8822d5bef74081e449be93a8a9f2 Mon Sep 17 00:00:00 2001 From: mwojcik Date: Wed, 24 Apr 2024 17:12:57 +0800 Subject: [PATCH] libboard_artiq: support multiple aux rx buffers --- src/libboard_artiq/src/drtioaux.rs | 21 ++++++++++++--------- src/libboard_artiq/src/drtioaux_async.rs | 21 ++++++++++++--------- src/libboard_artiq/src/drtioaux_proto.rs | 6 ------ 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/libboard_artiq/src/drtioaux.rs b/src/libboard_artiq/src/drtioaux.rs index 6b8077a7..09048e6b 100644 --- a/src/libboard_artiq/src/drtioaux.rs +++ b/src/libboard_artiq/src/drtioaux.rs @@ -74,12 +74,15 @@ where F: FnOnce(&[u8]) -> Result { let linkidx = linkno as usize; unsafe { if (DRTIOAUX[linkidx].aux_rx_present_read)() == 1 { - let ptr = (DRTIOAUX_MEM[linkidx].base + DRTIOAUX_MEM[linkidx].size / 2) as *mut u32; - let len = (DRTIOAUX[linkidx].aux_rx_length_read)() as usize; + let cpu_ptr = (DRTIOAUX[linkidx].aux_read_pointer_read)() as usize; + let ptr = (DRTIOAUX_MEM[linkidx].base + DRTIOAUX_MEM[linkidx].size / 2 + cpu_ptr * 0x400) as *mut u32; // work buffer to accomodate axi burst reads - let mut buf: [u8; 1024] = [0; 1024]; - copy_work_buffer(ptr, buf.as_mut_ptr() as *mut u32, len as isize); - let result = f(&buf[0..len]); + // buffer at maximum proto packet size, not maximum gateware supported size + // to minimize copying time + const LEN: usize = 512; + let mut buf: [u8; LEN] = [0; LEN]; + copy_work_buffer(ptr, buf.as_mut_ptr() as *mut u32, LEN as isize); + let result = f(&buf); (DRTIOAUX[linkidx].aux_rx_present_write)(1); Ok(Some(result?)) } else { @@ -100,15 +103,15 @@ pub fn recv(linkno: u8) -> Result, Error> { let mut reader = Cursor::new(buffer); - let checksum_at = buffer.len() - 4; + let packet = Packet::read_from(&mut reader)?; + let padding = (12 - (reader.position() % 8)) % 8; + let checksum_at = reader.position() + padding; let checksum = crc::crc32::checksum_ieee(&reader.get_ref()[0..checksum_at]); reader.set_position(checksum_at); if reader.read_u32()? != checksum { return Err(Error::CorruptedPacket); } - reader.set_position(0); - - Ok(Packet::read_from(&mut reader)?) + Ok(packet) }) } diff --git a/src/libboard_artiq/src/drtioaux_async.rs b/src/libboard_artiq/src/drtioaux_async.rs index 9a791311..7676cd3e 100644 --- a/src/libboard_artiq/src/drtioaux_async.rs +++ b/src/libboard_artiq/src/drtioaux_async.rs @@ -38,12 +38,15 @@ where F: FnOnce(&[u8]) -> Result { let linkidx = linkno as usize; unsafe { if (DRTIOAUX[linkidx].aux_rx_present_read)() == 1 { - let ptr = (DRTIOAUX_MEM[linkidx].base + DRTIOAUX_MEM[linkidx].size / 2) as *mut u32; - let len = (DRTIOAUX[linkidx].aux_rx_length_read)() as usize; + let cpu_ptr = (DRTIOAUX[linkidx].aux_read_pointer_read)() as usize; + let ptr = (DRTIOAUX_MEM[linkidx].base + DRTIOAUX_MEM[linkidx].size / 2 + cpu_ptr * 0x400) as *mut u32; // work buffer to accomodate axi burst reads - let mut buf: [u8; 1024] = [0; 1024]; - copy_work_buffer(ptr, buf.as_mut_ptr() as *mut u32, len as isize); - let result = f(&buf[0..len]); + // buffer at maximum proto packet size, not maximum gateware supported size + // to minimize required copying time + const LEN: usize = 512; + let mut buf: [u8; LEN] = [0; LEN]; + copy_work_buffer(ptr, buf.as_mut_ptr() as *mut u32, LEN as isize); + let result = f(&buf); (DRTIOAUX[linkidx].aux_rx_present_write)(1); Ok(Some(result?)) } else { @@ -64,15 +67,15 @@ pub async fn recv(linkno: u8) -> Result, Error> { let mut reader = Cursor::new(buffer); - let checksum_at = buffer.len() - 4; + let packet = Packet::read_from(&mut reader)?; + let padding = (12 - (reader.position() % 8)) % 8; + let checksum_at = reader.position() + padding; let checksum = crc::crc32::checksum_ieee(&reader.get_ref()[0..checksum_at]); reader.set_position(checksum_at); if reader.read_u32()? != checksum { return Err(Error::CorruptedPacket); } - reader.set_position(0); - - Ok(Packet::read_from(&mut reader)?) + Ok(packet) }) .await } diff --git a/src/libboard_artiq/src/drtioaux_proto.rs b/src/libboard_artiq/src/drtioaux_proto.rs index 160f49e0..1b5272a3 100644 --- a/src/libboard_artiq/src/drtioaux_proto.rs +++ b/src/libboard_artiq/src/drtioaux_proto.rs @@ -89,8 +89,6 @@ pub enum Packet { RoutingSetRank { rank: u8, }, - RoutingRetrievePackets, - RoutingNoPackets, RoutingAck, MonitorRequest { @@ -325,8 +323,6 @@ impl Packet { rank: reader.read_u8()?, }, 0x32 => Packet::RoutingAck, - 0x33 => Packet::RoutingRetrievePackets, - 0x34 => Packet::RoutingNoPackets, 0x40 => Packet::MonitorRequest { destination: reader.read_u8()?, @@ -602,8 +598,6 @@ impl Packet { writer.write_u8(rank)?; } Packet::RoutingAck => writer.write_u8(0x32)?, - Packet::RoutingRetrievePackets => writer.write_u8(0x33)?, - Packet::RoutingNoPackets => writer.write_u8(0x34)?, Packet::MonitorRequest { destination,