modifications to the HAL for DAR-699

This commit is contained in:
bunnie 2024-12-06 03:32:49 +08:00
parent a5ebcf5d0f
commit 785826ca23
3 changed files with 93 additions and 15 deletions

View file

@ -39,6 +39,7 @@ board-baosec = [
board-baosor = ["camera-ov2640", "axp2101"] # Precursor form factor board-baosor = ["camera-ov2640", "axp2101"] # Precursor form factor
board-dabao = [] # Dev board form factor board-dabao = [] # Dev board form factor
verilator-only = [] verilator-only = []
mpw = []
udma-stress-test = [ udma-stress-test = [
] # For RTL simulation testing - aggravates a key corner case ] # For RTL simulation testing - aggravates a key corner case

View file

@ -6,6 +6,15 @@ use crate::udma::{Bank, Udma};
const TIMEOUT_ITERS: usize = 1_000_000; const TIMEOUT_ITERS: usize = 1_000_000;
// MPW had this register:
// pub const REG_SETUP: crate::Register = crate::Register::new(13, 0x1);
// pub const REG_SETUP_R_DO_RST: crate::Field = crate::Field::new(1, 0, REG_SETUP);
// It is gone in NTO and we use the UDMA system reset instead
#[cfg(feature = "mpw")]
const SETUP_OFFSET: usize = 13;
#[cfg(feature = "mpw")]
const DO_RST_MASK: usize = 1;
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
#[repr(u8)] #[repr(u8)]
pub enum I2cCmd { pub enum I2cCmd {
@ -75,9 +84,12 @@ const MAX_I2C_TXLEN: usize = 512;
const MAX_I2C_RXLEN: usize = 512; const MAX_I2C_RXLEN: usize = 512;
const MAX_I2C_CMDLEN: usize = 512; const MAX_I2C_CMDLEN: usize = 512;
pub struct I2c { pub struct I2c<'a> {
csr: CSR<u32>, csr: CSR<u32>,
_channel: I2cChannel, #[allow(dead_code)] // used in NTO
udma_global: &'a dyn UdmaGlobalConfig,
#[allow(dead_code)] // used in NTO
channel: I2cChannel,
divider: u16, divider: u16,
perclk_freq: u32, perclk_freq: u32,
pub ifram: IframRange, pub ifram: IframRange,
@ -91,20 +103,25 @@ pub struct I2c {
pending: I2cPending, pending: I2cPending,
} }
impl Udma for I2c { impl Udma for I2c<'_> {
fn csr_mut(&mut self) -> &mut CSR<u32> { &mut self.csr } fn csr_mut(&mut self) -> &mut CSR<u32> { &mut self.csr }
fn csr(&self) -> &CSR<u32> { &self.csr } fn csr(&self) -> &CSR<u32> { &self.csr }
} }
impl I2c { impl<'a> I2c<'a> {
/// Safety: called only after global clock for I2C channel is enabled. /// Safety: called only after global clock for I2C channel is enabled.
/// It is also unsafe to `Drop` because you have to cleanup the clock manually. /// It is also unsafe to `Drop` because you have to cleanup the clock manually.
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub unsafe fn new(channel: I2cChannel, i2c_freq: u32, perclk_freq: u32) -> Option<Self> { pub unsafe fn new(
channel: I2cChannel,
i2c_freq: u32,
perclk_freq: u32,
udma_global: &'a dyn UdmaGlobalConfig,
) -> Option<Self> {
// one page is the minimum size we can request // one page is the minimum size we can request
if let Some(ifram) = IframRange::request(4096, None) { if let Some(ifram) = IframRange::request(4096, None) {
Some(I2c::new_with_ifram(channel, i2c_freq, perclk_freq, ifram)) Some(I2c::new_with_ifram(channel, i2c_freq, perclk_freq, ifram, udma_global))
} else { } else {
None None
} }
@ -115,6 +132,7 @@ impl I2c {
i2c_freq: u32, i2c_freq: u32,
perclk_freq: u32, perclk_freq: u32,
ifram: IframRange, ifram: IframRange,
udma_global: &'a dyn UdmaGlobalConfig,
) -> Self { ) -> Self {
// divide-by-4 is an empirical observation // divide-by-4 is an empirical observation
let divider: u16 = ((((perclk_freq / 2) / i2c_freq) / 4).min(u16::MAX as u32)) as u16; let divider: u16 = ((((perclk_freq / 2) / i2c_freq) / 4).min(u16::MAX as u32)) as u16;
@ -134,18 +152,33 @@ impl I2c {
) )
.expect("couldn't map i2c port"); .expect("couldn't map i2c port");
#[cfg(target_os = "xous")] #[cfg(target_os = "xous")]
#[allow(unused_mut)] // because it is used when `mpw` feature is selected
let mut csr = CSR::new(csr_range.as_mut_ptr() as *mut u32); let mut csr = CSR::new(csr_range.as_mut_ptr() as *mut u32);
#[cfg(not(target_os = "xous"))] #[cfg(not(target_os = "xous"))]
#[allow(unused_mut)] // because it is used when `mpw` feature is selected
let mut csr = CSR::new(base_addr as *mut u32); let mut csr = CSR::new(base_addr as *mut u32);
// reset the block // reset the block, if MPW. If NTO, this is handled by udma global
csr.wfo(utra::udma_i2c_0::REG_SETUP_R_DO_RST, 1); #[cfg(feature = "mpw")]
csr.wo(utra::udma_i2c_0::REG_SETUP, 0); unsafe {
csr.base().add(SETUP_OFFSET).write_volatile(DO_RST_MASK as u32);
csr.base().add(SETUP_OFFSET).write_volatile(0);
}
#[cfg(not(feature = "mpw"))]
{
udma_global.reset(match channel {
I2cChannel::Channel0 => PeriphId::I2c0,
I2cChannel::Channel1 => PeriphId::I2c1,
I2cChannel::Channel2 => PeriphId::I2c2,
I2cChannel::Channel3 => PeriphId::I2c3,
});
}
// one page is the minimum size we can request // one page is the minimum size we can request
let ifram_base = ifram.virt_range.as_ptr() as usize; let ifram_base = ifram.virt_range.as_ptr() as usize;
let ifram_base_phys = ifram.phys_range.as_ptr() as usize; let ifram_base_phys = ifram.phys_range.as_ptr() as usize;
let mut i2c = I2c { let mut i2c = I2c {
csr, csr,
_channel: channel, udma_global,
channel,
ifram, ifram,
divider, divider,
cmd_buf: unsafe { core::slice::from_raw_parts_mut(ifram_base as *mut u32, MAX_I2C_CMDLEN) }, cmd_buf: unsafe { core::slice::from_raw_parts_mut(ifram_base as *mut u32, MAX_I2C_CMDLEN) },
@ -221,8 +254,22 @@ impl I2c {
self.udma_reset(Bank::Custom); self.udma_reset(Bank::Custom);
self.udma_reset(Bank::Tx); self.udma_reset(Bank::Tx);
self.udma_reset(Bank::Rx); self.udma_reset(Bank::Rx);
self.csr.wfo(utra::udma_i2c_0::REG_SETUP_R_DO_RST, 1); // reset the block, if MPW. If NTO, this needs to be handled by the upper level code with a
self.csr.wo(utra::udma_i2c_0::REG_SETUP, 0); // reset to udma_global
#[cfg(feature = "mpw")]
unsafe {
self.csr.base().add(SETUP_OFFSET).write_volatile(DO_RST_MASK as u32);
self.csr.base().add(SETUP_OFFSET).write_volatile(0);
}
#[cfg(not(feature = "mpw"))]
{
self.udma_global.reset(match self.channel {
I2cChannel::Channel0 => PeriphId::I2c0,
I2cChannel::Channel1 => PeriphId::I2c1,
I2cChannel::Channel2 => PeriphId::I2c2,
I2cChannel::Channel3 => PeriphId::I2c3,
});
}
self.send_cmd_list(&[I2cCmd::Config(self.divider)]); self.send_cmd_list(&[I2cCmd::Config(self.divider)]);
self.pending.take(); self.pending.take();
@ -297,8 +344,20 @@ impl I2c {
// block has to be reset on every start transaction due to a... bug? programming error? // block has to be reset on every start transaction due to a... bug? programming error?
// where the Rx length is mismatched from the actual length of Rx data expected because // where the Rx length is mismatched from the actual length of Rx data expected because
// it seems the Rx buffer pointer increments even during Tx events. // it seems the Rx buffer pointer increments even during Tx events.
self.csr.wfo(utra::udma_i2c_0::REG_SETUP_R_DO_RST, 1); #[cfg(feature = "mpw")]
self.csr.wo(utra::udma_i2c_0::REG_SETUP, 0); unsafe {
self.csr.base().add(SETUP_OFFSET).write_volatile(DO_RST_MASK as u32);
self.csr.base().add(SETUP_OFFSET).write_volatile(0);
}
#[cfg(not(feature = "mpw"))]
{
self.udma_global.reset(match self.channel {
I2cChannel::Channel0 => PeriphId::I2c0,
I2cChannel::Channel1 => PeriphId::I2c1,
I2cChannel::Channel2 => PeriphId::I2c2,
I2cChannel::Channel3 => PeriphId::I2c3,
});
}
// into the pre-allocated Tx buf // into the pre-allocated Tx buf
self.new_tranaction(); self.new_tranaction();
@ -328,7 +387,7 @@ impl I2c {
} }
} }
impl I2cApi for I2c { impl I2cApi for I2c<'_> {
fn i2c_read( fn i2c_read(
&mut self, &mut self,
dev: u8, dev: u8,

View file

@ -28,6 +28,7 @@ use crate::SharedCsr;
enum GlobalReg { enum GlobalReg {
ClockGate = 0, ClockGate = 0,
EventIn = 1, EventIn = 1,
Reset = 2,
} }
impl Into<usize> for GlobalReg { impl Into<usize> for GlobalReg {
fn into(self) -> usize { self as usize } fn into(self) -> usize { self as usize }
@ -241,6 +242,7 @@ pub trait UdmaGlobalConfig {
event_type: PeriphEventType, event_type: PeriphEventType,
to_channel: EventChannel, to_channel: EventChannel,
); );
fn reset(&self, peripheral: PeriphId);
} }
#[repr(u32)] #[repr(u32)]
@ -319,6 +321,20 @@ impl GlobalConfig {
// Safety: only safe when used in the context of UDMA registers. // Safety: only safe when used in the context of UDMA registers.
unsafe { self.csr.base().add(GlobalReg::EventIn.into()).read_volatile() } unsafe { self.csr.base().add(GlobalReg::EventIn.into()).read_volatile() }
} }
pub fn reset(&self, peripheral: PeriphId) {
unsafe {
// assert reset
self.csr.base().add(GlobalReg::Reset.into()).write_volatile(peripheral.into());
// a few nops for the reset to propagate
core::arch::asm!(
"nop", "nop", "nop", "nop", "nop", "nop", "nop", "nop", "nop", "nop", "nop", "nop", "nop",
"nop", "nop", "nop",
);
// de-assert reset
self.csr.base().add(GlobalReg::Reset.into()).write_volatile(0);
}
}
} }
impl UdmaGlobalConfig for GlobalConfig { impl UdmaGlobalConfig for GlobalConfig {
@ -338,6 +354,8 @@ impl UdmaGlobalConfig for GlobalConfig {
) { ) {
self.map_event(peripheral, event_type, to_channel); self.map_event(peripheral, event_type, to_channel);
} }
fn reset(&self, peripheral: PeriphId) { self.reset(peripheral); }
} }
// --------------------------------- DMA channel ------------------------------------ // --------------------------------- DMA channel ------------------------------------
pub(crate) const CFG_EN: u32 = 0b01_0000; // start a transfer pub(crate) const CFG_EN: u32 = 0b01_0000; // start a transfer