diff --git a/src/libboard_artiq/src/cxp_proto.rs b/src/libboard_artiq/src/cxp_proto.rs index 5289332..64fc357 100644 --- a/src/libboard_artiq/src/cxp_proto.rs +++ b/src/libboard_artiq/src/cxp_proto.rs @@ -1,6 +1,6 @@ use core::slice; -use core_io::{Error as IoError, Write}; +use core_io::{Error as IoError, Read, Write}; use crc::crc32; use io::Cursor; use libboard_zynq::println; @@ -27,6 +27,7 @@ impl From for Error { } pub enum Packet { + // Upconn packet CtrlRead { addr: u32, length: u8, @@ -51,9 +52,55 @@ pub enum Packet { packet_tag: u8, }, 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 { + let mut bytes = [0; 4]; + reader.read(&mut bytes)?; + Ok(u32::from_be_bytes(bytes)) } 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> { // CoaXpress use big endian match *self { @@ -90,7 +137,20 @@ impl Packet { writer.write(&[0x08; 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) // 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> { unsafe { // let ptr = CXP_LOOPBACK_MEM[0].base as *mut u32; - let ptr = CXP_RX_MEM[0].base as *mut u32; - // let mut reader = Cursor::new(slice::from_raw_parts_mut(ptr as *mut u8, MEM_LEN)); + 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 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(()) }