From 186534ff016ad966462a70d50d6333128aa1dad1 Mon Sep 17 00:00:00 2001 From: morgan Date: Mon, 30 Sep 2024 16:14:46 +0800 Subject: [PATCH] proto fw: use new csr proto: fix compilation warning proto fw: add rx memory test --- src/libboard_artiq/src/cxp_proto.rs | 114 ++++++++++++++++------------ 1 file changed, 66 insertions(+), 48 deletions(-) diff --git a/src/libboard_artiq/src/cxp_proto.rs b/src/libboard_artiq/src/cxp_proto.rs index 2ce22b7..5289332 100644 --- a/src/libboard_artiq/src/cxp_proto.rs +++ b/src/libboard_artiq/src/cxp_proto.rs @@ -2,21 +2,22 @@ use core::slice; use core_io::{Error as IoError, Write}; use crc::crc32; -use embedded_hal::prelude::_embedded_hal_blocking_delay_DelayUs; use io::Cursor; -use libboard_zynq::{println, timer::GlobalTimer}; +use libboard_zynq::println; -use crate::{mem::mem::{CXP_LOOPBACK_MEM, CXP_MEM}, - pl::csr}; +// TODO: fix the import +use crate::{mem::mem::{CXP_LOOPBACK_MEM, CXP_RX_MEM, CXP_TX_MEM}, + pl::csr::CXP}; const MAX_PACKET: usize = 128; const DATA_MAXSIZE: usize = /*max size*/MAX_PACKET - /*Tag*/4 - /*Op code & length*/4 - /*addr*/4 - /*CRC*/4 ; -const MEM_SIZE: usize = 0x200; +const MEM_LEN: usize = 0x200; #[derive(Debug)] pub enum Error { BufferError, LinkDown, + UnknownPacket(u8), } impl From for Error { @@ -109,72 +110,89 @@ impl Packet { } } -pub fn send(packet: &Packet) -> Result<(), Error> { - if unsafe { csr::cxp::upconn_tx_enable_read() } == 0 { +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)); + + print_packet(slice::from_raw_parts_mut(ptr as *mut u8, MEM_LEN)); + } + Ok(()) +} + +pub fn send(channel: usize, packet: &Packet) -> Result<(), Error> { + if unsafe { (CXP[channel].upconn_tx_enable_read)() } == 0 { Err(Error::LinkDown)? } match *packet { - Packet::TestPacket => send_test_packet(), - _ => send_data_packet(packet), + Packet::TestPacket => send_test_packet(channel), + _ => send_data_packet(channel, packet), } } -fn send_data_packet(packet: &Packet) -> Result<(), Error> { +fn send_data_packet(channel: usize, packet: &Packet) -> Result<(), Error> { unsafe { // TODO: put this in mem group - while csr::cxp::upconn_command_tx_read() == 1 {} - let ptr = CXP_MEM[0].base as *mut u32; - let mut writer = Cursor::new(slice::from_raw_parts_mut(ptr as *mut u8, MEM_SIZE)); + while (CXP[channel].upconn_command_tx_read)() == 1 {} + let ptr = CXP_TX_MEM[0].base as *mut u32; + let mut writer = Cursor::new(slice::from_raw_parts_mut(ptr as *mut u8, MEM_LEN)); packet.write_to(&mut writer)?; - csr::cxp::upconn_command_tx_word_len_write(writer.position() as u8 / 4); - csr::cxp::upconn_command_tx_write(1); + (CXP[channel].upconn_command_tx_word_len_write)(writer.position() as u8 / 4); + (CXP[channel].upconn_command_tx_write)(1); } Ok(()) } -fn send_test_packet() -> Result<(), Error> { +fn send_test_packet(channel: usize) -> Result<(), Error> { unsafe { - while csr::cxp::upconn_testseq_tx_read() == 1 {} - csr::cxp::upconn_tx_testmode_en_write(1); - csr::cxp::upconn_testseq_tx_write(1); + while (CXP[channel].upconn_testseq_tx_read)() == 1 {} + (CXP[channel].upconn_tx_testmode_en_write)(1); + (CXP[channel].upconn_testseq_tx_write)(1); // wait till all test packet is out before switching back - while csr::cxp::upconn_testseq_tx_read() == 1 {} - csr::cxp::upconn_tx_testmode_en_write(0); + while (CXP[channel].upconn_testseq_tx_read)() == 1 {} + (CXP[channel].upconn_tx_testmode_en_write)(0); } Ok(()) } -pub fn write_u32(addr: u32, data: u32) -> Result<(), Error> { +pub fn write_u32(channel: usize, addr: u32, data: u32) -> Result<(), Error> { let mut data_slice: [u8; DATA_MAXSIZE] = [0; DATA_MAXSIZE]; data_slice[..4].clone_from_slice(&data.to_be_bytes()); - send(&Packet::CtrlWrite { - addr, - length: 4, - data: data_slice, - })?; + send( + channel, + &Packet::CtrlWrite { + addr, + length: 4, + data: data_slice, + }, + )?; Ok(()) } -pub fn read_u32(addr: u32) -> Result<(), Error> { - send(&Packet::CtrlRead { addr, length: 4 })?; +pub fn read_u32(channel: usize, addr: u32) -> Result<(), Error> { + send(channel, &Packet::CtrlRead { addr, length: 4 })?; Ok(()) } -pub fn write_u64(addr: u32, data: u64) -> Result<(), Error> { +pub fn write_u64(channel: usize, addr: u32, data: u64) -> Result<(), Error> { let mut data_slice: [u8; DATA_MAXSIZE] = [0; DATA_MAXSIZE]; data_slice[..8].clone_from_slice(&data.to_be_bytes()); - send(&Packet::CtrlWrite { - addr, - length: 8, - data: data_slice, - })?; + send( + channel, + &Packet::CtrlWrite { + addr, + length: 8, + data: data_slice, + }, + )?; Ok(()) } @@ -214,36 +232,36 @@ pub fn print_packetu32(pak: &[u32], k: &[u8]) { println!("============================================"); } -pub fn downconn_debug_send(packet: &Packet) -> Result<(), Error> { +pub fn downconn_debug_send(channel: usize, packet: &Packet) -> Result<(), Error> { unsafe { // TODO: put this in mem group - while csr::cxp::downconn_command_tx_read() == 1 {} + while (CXP[channel].downconn_command_tx_read)() == 1 {} let ptr = CXP_LOOPBACK_MEM[0].base as *mut u32; - let mut writer = Cursor::new(slice::from_raw_parts_mut(ptr as *mut u8, MEM_SIZE)); + let mut writer = Cursor::new(slice::from_raw_parts_mut(ptr as *mut u8, MEM_LEN)); packet.write_to(&mut writer)?; - csr::cxp::downconn_command_tx_word_len_write(writer.position() as u8 / 4); - csr::cxp::downconn_command_tx_write(1); + (CXP[channel].downconn_command_tx_word_len_write)(writer.position() as u8 / 4); + (CXP[channel].downconn_command_tx_write)(1); } Ok(()) } -pub fn downconn_debug_send_trig_ack() { +pub fn downconn_debug_send_trig_ack(channel: usize) { unsafe { - csr::cxp::downconn_ack_write(1); + (CXP[channel].downconn_ack_write)(1); } } -pub fn downconn_send_test_packet() { +pub fn downconn_send_test_packet(channel: usize) { unsafe { - while csr::cxp::downconn_testseq_tx_read() == 1 {} - csr::cxp::downconn_mux_sel_write(1); - csr::cxp::downconn_testseq_tx_write(1); + while (CXP[channel].downconn_testseq_tx_read)() == 1 {} + (CXP[channel].downconn_mux_sel_write)(1); + (CXP[channel].downconn_testseq_tx_write)(1); // wait till all test packet is out before switching back - while csr::cxp::downconn_testseq_tx_read() == 1 {} - csr::cxp::downconn_mux_sel_write(0); + while (CXP[channel].downconn_testseq_tx_read)() == 1 {} + (CXP[channel].downconn_mux_sel_write)(0); } }