1
0
Fork 0

proto fw: add rx packet

This commit is contained in:
morgan 2024-10-03 15:45:22 +08:00
parent 6511bfc637
commit 0310793181
1 changed files with 68 additions and 5 deletions

View File

@ -1,6 +1,6 @@
use core::slice; use core::slice;
use core_io::{Error as IoError, Write}; use core_io::{Error as IoError, Read, Write};
use crc::crc32; use crc::crc32;
use io::Cursor; use io::Cursor;
use libboard_zynq::println; use libboard_zynq::println;
@ -27,6 +27,7 @@ impl From<IoError> for Error {
} }
pub enum Packet { pub enum Packet {
// Upconn packet
CtrlRead { CtrlRead {
addr: u32, addr: u32,
length: u8, length: u8,
@ -51,9 +52,55 @@ pub enum Packet {
packet_tag: u8, packet_tag: u8,
}, },
TestPacket, TestPacket,
// Downconn packet
CtrlAck {
ackcode: u8,
length: u8,
data: [u8; DATA_MAXSIZE],
},
CtrlAckWithTag {
tag: u8,
ackcode: u8,
length: u8,
data: [u8; DATA_MAXSIZE],
},
CtrlAckNoData {
ackcode: u8,
},
CtrlAckNoDataWithTag {
tag: u8,
ackcode: u8,
},
// DEBUG: Loopback message
CtrlAckLoopback {
ackcode: u8,
length: u8,
data: [u8; DATA_MAXSIZE],
},
}
fn read_word(reader: &mut Cursor<&mut [u8]>) -> Result<u32, Error> {
let mut bytes = [0; 4];
reader.read(&mut bytes)?;
Ok(u32::from_be_bytes(bytes))
} }
impl Packet { impl Packet {
pub fn read_from(reader: &mut Cursor<&mut [u8]>, packet_type: u8) -> Result<(), Error> {
match packet_type {
0x03 => {
let ackcode4x = read_word(reader);
println!("ackcode4x = {:#010X}", ackcode4x.unwrap());
let len = read_word(reader);
println!("len = {:#010X}", len.unwrap());
}
0x06 => {}
_ => {}
}
Ok(())
}
pub fn write_to(&self, writer: &mut Cursor<&mut [u8]>) -> Result<(), Error> { pub fn write_to(&self, writer: &mut Cursor<&mut [u8]>) -> Result<(), Error> {
// CoaXpress use big endian // CoaXpress use big endian
match *self { match *self {
@ -90,7 +137,20 @@ impl Packet {
writer.write(&[0x08; 4])?; writer.write(&[0x08; 4])?;
writer.write(&[packet_tag; 4])?; writer.write(&[packet_tag; 4])?;
} }
_ => {}
// DEBUG: Loopback message
Packet::CtrlAckLoopback { ackcode, length, data } => {
writer.write(&[0x03; 4])?;
writer.write(&[ackcode; 4])?;
if ackcode == 0x00 || ackcode == 0x04 {
writer.write(&[0x00, 0x00, 0x00, length])?;
writer.write(&data[0..length as usize])?;
let checksum = crc32::checksum_ieee(&writer.get_ref()[4..writer.position()]);
writer.write(&(!checksum).to_le_bytes())?;
}
}
_ => return Err(Error::UnknownPacket(0)),
} }
// Section 9.2.2.2 (CXP-001-2021) // Section 9.2.2.2 (CXP-001-2021)
// Only Control packet need CRC32 appended in the end of the packet // Only Control packet need CRC32 appended in the end of the packet
@ -113,10 +173,13 @@ impl Packet {
pub fn receive(channel: usize) -> Result<(), Error> { pub fn receive(channel: usize) -> Result<(), Error> {
unsafe { unsafe {
// let ptr = CXP_LOOPBACK_MEM[0].base as *mut u32; // let ptr = CXP_LOOPBACK_MEM[0].base as *mut u32;
let ptr = CXP_RX_MEM[0].base as *mut u32; 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)); let mut reader = Cursor::new(slice::from_raw_parts_mut(ptr as *mut u8, MEM_LEN));
let packet_type = (CXP[channel].downconn_packet_type_read)();
print_packet(slice::from_raw_parts_mut(ptr as *mut u8, MEM_LEN)); Packet::read_from(&mut reader, packet_type);
// print_packet(slice::from_raw_parts_mut(ptr as *mut u8, MEM_LEN));
} }
Ok(()) Ok(())
} }