From d68b821be45b7208cf0e1d41812f656f93695690 Mon Sep 17 00:00:00 2001 From: mwojcik Date: Fri, 17 May 2024 15:26:49 +0800 Subject: [PATCH] get packet len to copy less data --- src/libboard_artiq/src/drtioaux.rs | 7 ++- src/libboard_artiq/src/drtioaux_async.rs | 7 ++- src/libboard_artiq/src/drtioaux_proto.rs | 60 +++++++++++++++++++++++- 3 files changed, 65 insertions(+), 9 deletions(-) diff --git a/src/libboard_artiq/src/drtioaux.rs b/src/libboard_artiq/src/drtioaux.rs index 4e9185d2..caede661 100644 --- a/src/libboard_artiq/src/drtioaux.rs +++ b/src/libboard_artiq/src/drtioaux.rs @@ -77,10 +77,9 @@ where F: FnOnce(&[u8]) -> Result { let read_ptr = (DRTIOAUX[linkidx].aux_read_pointer_read)() as usize; let ptr = (DRTIOAUX_MEM[linkidx].base + DRTIOAUX_MEM[linkidx].size / 2 + read_ptr * 0x400) as *mut u32; // work buffer to accomodate axi burst reads - // potentially: get length by checking the first byte to cut down on unnecessary copying - const LEN: usize = 1024; - let mut buf: [u8; LEN] = [0; LEN]; - copy_work_buffer(ptr, buf.as_mut_ptr() as *mut u32, LEN as isize); + let len = Packet::get_aligned_size(*(ptr as *mut u8)); + let mut buf: [u8; 1024] = [0; 1024]; + copy_work_buffer(ptr, buf.as_mut_ptr() as *mut u32, len); let result = f(&buf); (DRTIOAUX[linkidx].aux_rx_present_write)(1); Ok(Some(result?)) diff --git a/src/libboard_artiq/src/drtioaux_async.rs b/src/libboard_artiq/src/drtioaux_async.rs index 6de3d57d..2a00ba6c 100644 --- a/src/libboard_artiq/src/drtioaux_async.rs +++ b/src/libboard_artiq/src/drtioaux_async.rs @@ -41,10 +41,9 @@ where F: FnOnce(&[u8]) -> Result { let read_ptr = (DRTIOAUX[linkidx].aux_read_pointer_read)() as usize; let ptr = (DRTIOAUX_MEM[linkidx].base + DRTIOAUX_MEM[linkidx].size / 2 + read_ptr * 0x400) as *mut u32; // work buffer to accomodate axi burst reads - // potentially: get length by checking the first byte to cut down on unnecessary copying - const LEN: usize = 1024; - let mut buf: [u8; LEN] = [0; LEN]; - copy_work_buffer(ptr, buf.as_mut_ptr() as *mut u32, LEN as isize); + let len = Packet::get_aligned_size(*(ptr as *mut u8)); + let mut buf: [u8; 1024] = [0; 1024]; + copy_work_buffer(ptr, buf.as_mut_ptr() as *mut u32, len); let result = f(&buf); (DRTIOAUX[linkidx].aux_rx_present_write)(1); Ok(Some(result?)) diff --git a/src/libboard_artiq/src/drtioaux_proto.rs b/src/libboard_artiq/src/drtioaux_proto.rs index d754c914..1a010876 100644 --- a/src/libboard_artiq/src/drtioaux_proto.rs +++ b/src/libboard_artiq/src/drtioaux_proto.rs @@ -1,9 +1,11 @@ use core_io::{Error as IoError, Read, Write}; use io::proto::{ProtoRead, ProtoWrite}; +const MAX_PACKET: usize = 1024; + // maximum size of arbitrary payloads // used by satellite -> master analyzer, subkernel exceptions -pub const SAT_PAYLOAD_MAX_SIZE: usize = /*max size*/1024 - /*CRC*/4 - /*packet ID*/1 - /*last*/1 - /*length*/2; +pub const SAT_PAYLOAD_MAX_SIZE: usize = /*max size*/MAX_PACKET - /*CRC*/4 - /*packet ID*/1 - /*last*/1 - /*length*/2; // used by DDMA, subkernel program data (need to provide extra ID and destination) pub const MASTER_PAYLOAD_MAX_SIZE: usize = SAT_PAYLOAD_MAX_SIZE - /*source*/1 - /*destination*/1 - /*ID*/4; @@ -962,4 +964,60 @@ impl Packet { _ => true, } } + + pub fn get_aligned_size(first_byte: u8) -> isize { + // returns length of the packet given first byte, for work buffer + let unaligned_size = match first_byte { + 0x00 => 1, // Packet::EchoRequest + 0x01 => 1, // Packet::EchoReply + 0x02 => 1, // Packet::ResetRequest + 0x03 => 1, // Packet::ResetAck + 0x04 => 1, // Packet::TSCAck + 0x20 => 2, // Packet::DestinationStatusRequest + 0x21 => 1, // Packet::DestinationDownReply + 0x22 => 1, // Packet::DestinationOkReply + 0x23 => 3, // Packet::DestinationSequenceErrorReply + 0x24 => 3, // Packet::DestinationCollisionReply + 0x25 => 3, // Packet::DestinationBusyReply + 0x30 => 34, // Packet::RoutingSetPath + 0x31 => 2, // Packet::RoutingSetRank + 0x32 => 1, // Packet::RoutingAck + 0x40 => 5, // Packet::MonitorRequest + 0x41 => 9, // Packet::MonitorReply + 0x50 => 6, // Packet::InjectionRequest + 0x51 => 5, // Packet::InjectionStatusRequest + 0x52 => 2, // Packet::InjectionStatusReply + 0x80 => 3, // Packet::I2cStartRequest + 0x81 => 3, // Packet::I2cRestartRequest + 0x82 => 3, // Packet::I2cStopRequest + 0x83 => 4, // Packet::I2cWriteRequest + 0x84 => 3, // Packet::I2cWriteReply + 0x85 => 4, // Packet::I2cReadRequest + 0x86 => 3, // Packet::I2cReadReply + 0x87 => 2, // Packet::I2cBasicReply + 0x88 => 5, // Packet::I2cSwitchSelectRequest + 0x90 => 7, // Packet::SpiSetConfigRequest + 0x92 => 7, // Packet::SpiWriteRequest + 0x93 => 3, // Packet::SpiReadRequest + 0x94 => 6, // Packet::SpiReadReply + 0x95 => 2, // Packet::SpiBasicReply + 0xa0 => 2, // Packet::AnalyzerHeaderRequest + 0xa1 => 14, // Packet::AnalyzerHeader + 0xa2 => 2, // Packet::AnalyzerDataRequest + 0xb1 => 8, // Packet::DmaAddTraceReply + 0xb2 => 7, // Packet::DmaRemoveTraceRequest + 0xb3 => 3, // Packet::DmaRemoveTraceReply + 0xb4 => 15, // Packet::DmaPlaybackRequest + 0xb5 => 3, // Packet::DmaPlaybackReply + 0xb6 => 20, // Packet::DmaPlaybackStatus + 0xc1 => 2, // Packet::SubkernelAddDataReply + 0xc4 => 8, // Packet::SubkernelLoadRunRequest + 0xc5 => 3, // Packet::SubkernelLoadRunReply + 0xc8 => 8, // Packet::SubkernelFinished + 0xc9 => 2, // Packet::SubkernelExceptionRequest + 0xcc => 2, // Packet::SubkernelMessageAck + _ => MAX_PACKET-4 + } + 4 /*CRC*/; + (unaligned_size + 7) as isize & -8 as isize + } }