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),
}
#[derive(Debug)]
pub enum CtrlError {
AddressReadOnly,
AddressWriteOnly,
CRCFailed,
DataSizeDifferent,
InvalidAddress,
InvalidData,
InvalidOpcode,
MalformedPacket,
SizeToolarge,
}
impl From<IoError> for Error {
fn from(_: IoError) -> Error {
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 {
let ptr = CXP_RX_MEM[channel].base as *mut u32;
let mut reader = Cursor::new(slice::from_raw_parts_mut(ptr as *mut u8, MEM_LEN));
if (CXP[channel].downconn_pending_packet_read)() == 1 {
let read_buffer_ptr = (CXP[channel].downconn_read_ptr_read)() as usize;
println!("buffer ptr = {}", read_buffer_ptr);
let ptr = (CXP_RX_MEM[channel].base + read_buffer_ptr * MEM_LEN) as *mut u8;
let mut reader = Cursor::new(slice::from_raw_parts_mut(ptr, MEM_LEN));
let packet_type = (CXP[channel].downconn_packet_type_read)();
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 {
@ -405,8 +400,9 @@ pub fn downconn_debug_send(channel: usize, packet: &UpConnPacket) -> Result<(),
pub fn downconn_debug_mem_print(channel: usize) {
unsafe {
let ptr = CXP_RX_MEM[channel].base as *mut u32;
print_packet(slice::from_raw_parts_mut(ptr as *mut u8, MEM_LEN));
let ptr = CXP_RX_MEM[channel].base as *mut u8;
let arr = slice::from_raw_parts_mut(ptr, MEM_LEN * 4);
print_packet(arr);
}
}