cxp fw: refactor

This commit is contained in:
morgan 2025-01-24 13:36:11 +08:00
parent d9e0a6a990
commit d221874806

View File

@ -31,7 +31,6 @@ const VERSION_SUPPORTED: u32 = 0x4044;
const VERSION_USED: u32 = 0x4048;
// Setup const
const CXP_CHANNELS: u8 = CXP_LEN as u8;
// TEST: result
// Currently, multilane/channel is not working properly in gateware due to buffer overflow issue
// only single lane @ 0x800 max pak size or dual lane 256 max pak size is working
@ -97,9 +96,8 @@ pub fn setup() -> Result<bool, Error> {
fn scan_active_channels() -> u8 {
let mut active_channels: u8 = 0;
for ch in 0..CXP_CHANNELS {
if unsafe { (CXP[ch as usize].rx_ready_read)() } == 1 {
// info!("ch#{} is up <---------------------------------", ch);
for ch in 0..CXP_LEN {
if unsafe { (CXP[ch].rx_ready_read)() } == 1 {
active_channels += 1;
}
}
@ -115,8 +113,8 @@ fn discover_camera(timer: &mut GlobalTimer) -> Result<(), Error> {
// Section 12.1.2 (CXP-001-2021)
// send ConnectionReset on all channels -> wait 200ms -> scan for active channels
for ch in 0..CXP_CHANNELS {
write_bytes_no_ack(ch, CONNECTION_RESET, &1_u32.to_be_bytes(), false)?;
for ch in 0..CXP_LEN {
write_bytes_no_ack(ch as u8, CONNECTION_RESET, &1_u32.to_be_bytes(), false)?;
}
timer.delay_ms(200);
@ -155,8 +153,8 @@ fn check_connection_topology(active_channels: u8) -> Result<(), Error> {
}
fn negotiate_active_channels(timer: &mut GlobalTimer) -> Result<u8, Error> {
let mut available_chs = read_u32(MASTER_CHANNEL, CONNECTION_CFG_DEFAULT, false)? >> 16;
available_chs = available_chs.min(CXP_CHANNELS as u32);
let max_camera_chs = read_u32(MASTER_CHANNEL, CONNECTION_CFG_DEFAULT, false)? >> 16;
let available_chs = max_camera_chs.min(CXP_LEN as u32);
// activate channels on camera but preserve the discovery linerate
let current_cfg = read_u32(MASTER_CHANNEL, CONNECTION_CFG, false)?;
@ -168,35 +166,33 @@ fn negotiate_active_channels(timer: &mut GlobalTimer) -> Result<u8, Error> {
)?;
timer.delay_ms(200);
let active_channels = scan_active_channels();
check_connection_topology(active_channels)?;
let active_chs = scan_active_channels();
check_connection_topology(active_chs)?;
if available_chs > active_channels as u32 {
if available_chs > active_chs as u32 {
info!(
"Only detected {} channel(s), disabling excess channels on camera",
active_channels
active_chs
);
write_u32(
MASTER_CHANNEL,
CONNECTION_CFG,
current_cfg & 0xFFFF | ((active_channels as u32) << 16),
current_cfg & 0xFFFF | ((active_chs as u32) << 16),
false,
)?;
timer.delay_ms(200);
// check no active channels are down after the cfg change
if active_channels != scan_active_channels() {
if active_chs != scan_active_channels() {
return Err(Error::ConnectionLost);
}
}
Ok(active_channels)
Ok(active_chs)
}
fn set_host_connection_id() -> Result<(), Error> {
// TODO: not mandatory??
info!("setting host connection id to = {}", HOST_CONNECTION_ID);
write_u32(MASTER_CHANNEL, MASTER_HOST_CONNECTION_ID, HOST_CONNECTION_ID, false)?;
let reg = read_u32(MASTER_CHANNEL, MASTER_HOST_CONNECTION_ID, false)?;
info!("host connection id set as = {}", reg);
Ok(())
}
@ -234,7 +230,7 @@ fn negotiate_cxp_version() -> Result<bool, Error> {
}
fn negotiate_pak_max_size(with_tag: bool) -> Result<(), Error> {
// TODO: set MAX CONTROL packet size?
// DEBUG: print control & stream packet size
let reg = read_u32(MASTER_CHANNEL, CONTROL_PACKET_SIZE_MAX, with_tag)?;
info!("Max CTRL PAK size = {:#010X}", reg);
@ -388,11 +384,5 @@ fn camera_setup(timer: &mut GlobalTimer) -> Result<bool, Error> {
test_channels_stability(active_channels, cxp_v2_or_greater, timer)?;
// unsafe{
// // don't set two identical routingid, the router cannot handle it
// // it can handle it after adding the priority decoder
// cxp_frame_pipeline::buffer_1_routingid_write(0x00);
// }
Ok(cxp_v2_or_greater)
}