1
0
Fork 0

proto fw: handle ring buffer

This commit is contained in:
morgan 2024-10-07 13:15:46 +08:00
parent f9da1dddf9
commit 9305336ba4
1 changed files with 18 additions and 22 deletions

View File

@ -22,19 +22,6 @@ pub enum Error {
UnknownPacket(u8), UnknownPacket(u8),
} }
#[derive(Debug)]
pub enum CtrlError {
AddressReadOnly,
AddressWriteOnly,
CRCFailed,
DataSizeDifferent,
InvalidAddress,
InvalidData,
InvalidOpcode,
MalformedPacket,
SizeToolarge,
}
impl From<IoError> for Error { impl From<IoError> for Error {
fn from(_: IoError) -> Error { fn from(_: IoError) -> Error {
Error::BufferError Error::BufferError
@ -137,17 +124,25 @@ impl DownConnPacket {
} }
} }
pub fn receive(channel: usize) -> Result<(), Error> { pub fn receive(channel: usize) -> Result<Option<DownConnPacket>, Error> {
unsafe { unsafe {
let ptr = CXP_RX_MEM[channel].base as *mut u32; if (CXP[channel].downconn_pending_packet_read)() == 1 {
let mut reader = Cursor::new(slice::from_raw_parts_mut(ptr as *mut u8, MEM_LEN)); let read_buffer_ptr = (CXP[channel].downconn_read_ptr_read)() as usize;
let packet_type = (CXP[channel].downconn_packet_type_read)(); println!("buffer ptr = {}", read_buffer_ptr);
let ptr = (CXP_RX_MEM[channel].base + read_buffer_ptr * MEM_LEN) as *mut u8;
let packet = DownConnPacket::read_from(&mut reader, packet_type); let mut reader = Cursor::new(slice::from_raw_parts_mut(ptr, MEM_LEN));
let packet_type = (CXP[channel].downconn_packet_type_read)();
println!("{:?}", packet); let packet = DownConnPacket::read_from(&mut reader, packet_type);
println!("{:?}", packet);
(CXP[channel].downconn_pending_packet_write)(1);
Ok(Some(packet?))
} else {
Ok(None)
}
} }
Ok(())
} }
trait CxpWrite { trait CxpWrite {
@ -405,8 +400,9 @@ pub fn downconn_debug_send(channel: usize, packet: &UpConnPacket) -> Result<(),
pub fn downconn_debug_mem_print(channel: usize) { pub fn downconn_debug_mem_print(channel: usize) {
unsafe { unsafe {
let ptr = CXP_RX_MEM[channel].base as *mut u32; let ptr = CXP_RX_MEM[channel].base as *mut u8;
print_packet(slice::from_raw_parts_mut(ptr as *mut u8, MEM_LEN)); let arr = slice::from_raw_parts_mut(ptr, MEM_LEN * 4);
print_packet(arr);
} }
} }