From 4286820f4df945be60a0138f08a2242bf1663140 Mon Sep 17 00:00:00 2001 From: morgan Date: Thu, 10 Oct 2024 13:06:53 +0800 Subject: [PATCH] proto FW: add read/write u32 proto FW: general cleanup & update csr names --- src/libboard_artiq/src/cxp_proto.rs | 103 +++++++++++++--------------- 1 file changed, 48 insertions(+), 55 deletions(-) diff --git a/src/libboard_artiq/src/cxp_proto.rs b/src/libboard_artiq/src/cxp_proto.rs index d8409b3..9203226 100644 --- a/src/libboard_artiq/src/cxp_proto.rs +++ b/src/libboard_artiq/src/cxp_proto.rs @@ -249,9 +249,9 @@ pub fn receive(channel: usize) -> Result, Error> { 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 * BUF_LEN) as *mut u8; + let ptr = (CXP_RX_MEM[channel].base + read_buffer_ptr * BUF_LEN) as *mut u32; - let mut reader = Cursor::new(slice::from_raw_parts_mut(ptr, BUF_LEN)); + let mut reader = Cursor::new(slice::from_raw_parts_mut(ptr as *mut u8, BUF_LEN)); let packet_type = (CXP[channel].downconn_packet_type_read)(); let packet = DownConnPacket::read_from(&mut reader, packet_type); @@ -460,15 +460,14 @@ pub fn send(channel: usize, packet: &UpConnPacket) -> Result<(), Error> { fn send_data_packet(channel: usize, packet: &UpConnPacket) -> Result<(), Error> { unsafe { - // TODO: put this in mem group - while (CXP[channel].upconn_command_tx_read)() == 1 {} + while (CXP[channel].upconn_bootstrap_tx_busy_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, BUF_LEN)); packet.write_to(&mut writer)?; - (CXP[channel].upconn_command_tx_word_len_write)(writer.position() as u16 / 4); - (CXP[channel].upconn_command_tx_write)(1); + (CXP[channel].upconn_bootstrap_tx_word_len_write)(writer.position() as u16 / 4); + (CXP[channel].upconn_bootstrap_tx_write)(1); } Ok(()) @@ -476,50 +475,44 @@ fn send_data_packet(channel: usize, packet: &UpConnPacket) -> Result<(), Error> fn send_test_packet(channel: usize) -> Result<(), Error> { unsafe { - 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 (CXP[channel].upconn_testseq_tx_read)() == 1 {} - (CXP[channel].upconn_tx_testmode_en_write)(0); + while (CXP[channel].upconn_bootstrap_tx_busy_read)() == 1 {} + (CXP[channel].upconn_bootstrap_tx_testseq_write)(1); } Ok(()) } -// -// DEBUG: use only -// -// -// -// 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( -// channel, -// &UpConnPacket::CtrlWrite { -// tag: None, -// addr, -// length: 4, -// data: data_slice, -// }, -// )?; +pub fn write_u32(channel: u8, addr: u32, val: u32) -> Result<(), Error> { + // TODO: add tags after connection & verify it's CXPv2 -// Ok(()) -// } + let mut data: [u8; DATA_MAXSIZE] = [0; DATA_MAXSIZE]; + NetworkEndian::write_u32(&mut data[..4], val); + send( + channel as usize, + &UpConnPacket::CtrlWrite { + tag: None, + addr, + length: 4, + data, + }, + )?; -// pub fn read_u32(channel: usize, addr: u32) -> Result<(), Error> { -// send( -// channel, -// &UpConnPacket::CtrlRead { -// tag: None, -// addr, -// length: 4, -// }, -// )?; + Ok(()) +} -// Ok(()) -// } +pub fn read_u32(channel: u8, addr: u32) -> Result<(), Error> { + // TODO: add tags after connection & verify it's CXPv2 + + send( + channel as usize, + &UpConnPacket::CtrlRead { + tag: None, + addr, + length: 4, + }, + )?; + + Ok(()) +} // pub fn write_u64(channel: usize, addr: u32, data: u64) -> Result<(), Error> { // let mut data_slice: [u8; DATA_MAXSIZE] = [0; DATA_MAXSIZE]; @@ -537,6 +530,11 @@ fn send_test_packet(channel: usize) -> Result<(), Error> { // Ok(()) // } +// +// DEBUG: use only +// +// +// pub fn print_packet(pak: &[u8]) { println!("pak = ["); for i in 0..(pak.len() / 4) { @@ -573,14 +571,14 @@ pub fn print_packetu32(pak: &[u32], k: &[u8]) { pub fn downconn_debug_send(channel: usize, packet: &UpConnPacket) -> Result<(), Error> { unsafe { - while (CXP[channel].downconn_command_tx_read)() == 1 {} + while (CXP[channel].downconn_bootstrap_loopback_tx_busy_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, BUF_LEN)); packet.write_to(&mut writer)?; - (CXP[channel].downconn_command_tx_word_len_write)(writer.position() as u16 / 4); - (CXP[channel].downconn_command_tx_write)(1); + (CXP[channel].downconn_bootstrap_loopback_tx_word_len_write)(writer.position() as u16 / 4); + (CXP[channel].downconn_bootstrap_loopback_tx_write)(1); } Ok(()) @@ -588,8 +586,8 @@ 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 u8; - let arr = slice::from_raw_parts_mut(ptr, BUF_LEN * 4); + let ptr = CXP_RX_MEM[channel].base as *mut u32; + let arr = slice::from_raw_parts_mut(ptr as *mut u8, BUF_LEN * 4); print_packet(arr); } } @@ -602,12 +600,7 @@ pub fn downconn_debug_send_trig_ack(channel: usize) { pub fn downconn_send_test_packet(channel: usize) { unsafe { - 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 (CXP[channel].downconn_testseq_tx_read)() == 1 {} - (CXP[channel].downconn_mux_sel_write)(0); + while (CXP[channel].downconn_bootstrap_loopback_tx_busy_read)() == 1 {} + (CXP[channel].downconn_bootstrap_loopback_tx_testseq_write)(1); } }