From 168c75eaf64b4d73302c60d4dd4033cbf355e52b Mon Sep 17 00:00:00 2001 From: morgan Date: Wed, 4 Sep 2024 14:49:52 +0800 Subject: [PATCH] cxp upconn fw: add crc & packet type insert in fw --- src/libboard_artiq/src/cxp_upconn.rs | 30 +++++++++++++--------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/libboard_artiq/src/cxp_upconn.rs b/src/libboard_artiq/src/cxp_upconn.rs index 0970130..8078104 100644 --- a/src/libboard_artiq/src/cxp_upconn.rs +++ b/src/libboard_artiq/src/cxp_upconn.rs @@ -1,4 +1,5 @@ use core_io::{Error as IoError, Read, Write}; +use crc::crc32; use embedded_hal::prelude::_embedded_hal_blocking_delay_DelayUs; use io::Cursor; use libboard_zynq::{println, timer::GlobalTimer}; @@ -139,12 +140,18 @@ impl Packet { pub fn write_to(&self, writer: &mut W) -> Result<(), IoError> where W: Write { match *self { - Packet::ControlWrite_u32_no_tag { addr, data, .. } => { + Packet::ControlWrite_u32_no_tag { + addr, + data, + packet_type, + } => { + writer.write(&[packet_type; 4])?; writer.write(&[0x01, 0x00, 0x00, 0x04])?; writer.write(&addr.to_be_bytes())?; writer.write(&data.to_be_bytes())?; } - Packet::ControlRead_u32_no_tag { addr, .. } => { + Packet::ControlRead_u32_no_tag { addr, packet_type } => { + writer.write(&[packet_type; 4])?; writer.write(&[0x00, 0x00, 0x00, 0x04])?; writer.write(&addr.to_be_bytes())?; } @@ -170,28 +177,19 @@ pub fn send(packet: &Packet) -> Result<(), IoError> { // writer.write_u8(0)?; // } - // let checksum = crc::crc32::checksum_ieee(&writer.get_ref()[0..writer.position()]); - // writer.write_u32(checksum)?; + // Section 9.2.2.2 (CXP-001-2021) + // CoaXpress use the polynomial of IEEE-802.3 (Ethernet) CRC but the checksum calculation is different + // Also, the calculation does not include the first 4 bytes of packet_type - // let mut res: [u8; LEN] = [0; LEN]; - // res.clone_from_slice(&mut write); + let checksum = crc32::checksum_ieee(&writer.get_ref()[4..writer.position()]); + writer.write(&(!checksum).to_le_bytes())?; unsafe { let len = writer.position(); csr::cxp::upconn_command_din_len_write(len as u8); - match *packet { - Packet::ControlWrite_u32_no_tag { packet_type, .. } - | Packet::ControlRead_u32_no_tag { packet_type, .. } => { - csr::cxp::upconn_command_packet_type_write(packet_type); - } - _ => { - println!("packet_type is not inserted!!!!"); - } - } for data in writer.get_ref()[..len].iter() { while csr::cxp::upconn_command_din_ready_read() == 0 {} csr::cxp::upconn_command_din_data_write(*data); - println!("{:#04X}", *data); } }