mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-17 08:40:52 +00:00
commit
4bc762e9ae
12 changed files with 187 additions and 42 deletions
|
|
@ -36,6 +36,8 @@ pub enum HalOpcode {
|
|||
ConfigureUdmaClock = 7,
|
||||
// blocking scalar
|
||||
ConfigureUdmaEvent = 8,
|
||||
// blocking scalar
|
||||
UdmaIrqStatusBits = 16,
|
||||
|
||||
/// I2C operations
|
||||
I2c = 9,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
pub trait I2cApi {
|
||||
fn i2c_write(&mut self, dev: u8, adr: u8, data: &[u8]) -> Result<usize, xous::Error>;
|
||||
fn i2c_write(&mut self, dev: u8, adr: u8, data: &[u8]) -> Result<I2cResult, xous::Error>;
|
||||
|
||||
/// initiate an i2c read. The read buffer is passed during the await.
|
||||
fn i2c_read(
|
||||
|
|
@ -8,7 +8,7 @@ pub trait I2cApi {
|
|||
adr: u8,
|
||||
buf: &mut [u8],
|
||||
repeated_start: bool,
|
||||
) -> Result<usize, xous::Error>;
|
||||
) -> Result<I2cResult, xous::Error>;
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "derive-rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
|
||||
|
|
@ -20,7 +20,7 @@ pub enum I2cTransactionType {
|
|||
}
|
||||
|
||||
#[cfg_attr(feature = "derive-rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
||||
pub enum I2cResult {
|
||||
/// For the outbound message holder
|
||||
Pending,
|
||||
|
|
@ -28,6 +28,8 @@ pub enum I2cResult {
|
|||
Ack(usize),
|
||||
/// An error occurred.
|
||||
Nack,
|
||||
/// An unhandled error has occurred.
|
||||
InternalError,
|
||||
}
|
||||
#[cfg_attr(feature = "derive-rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
|
||||
#[cfg(feature = "std")]
|
||||
|
|
|
|||
|
|
@ -233,6 +233,12 @@ pub enum EventChannel {
|
|||
Channel3 = 24,
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, num_derive::FromPrimitive)]
|
||||
pub enum IrqBank {
|
||||
I2c,
|
||||
I2cErr,
|
||||
}
|
||||
|
||||
/// Use a trait that will allow us to share code between both `std` and `no-std` implementations
|
||||
pub trait UdmaGlobalConfig {
|
||||
fn clock(&self, peripheral: PeriphId, enable: bool);
|
||||
|
|
@ -243,4 +249,5 @@ pub trait UdmaGlobalConfig {
|
|||
to_channel: EventChannel,
|
||||
);
|
||||
fn reset(&self, peripheral: PeriphId);
|
||||
fn irq_status_bits(&self, bank: IrqBank) -> u32;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -213,6 +213,8 @@ impl Axp2101 {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn get_dcdc(&mut self, which: WhichDcDc) -> (f32, bool) { self.dcdc_v_dvm[which as usize] }
|
||||
|
||||
pub fn set_ldo(
|
||||
&mut self,
|
||||
i2c: &mut dyn I2cApi,
|
||||
|
|
@ -237,6 +239,10 @@ impl Axp2101 {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn get_ldo(&mut self, which: WhichLdo) -> (f32, bool) {
|
||||
(self.ldo_v[which as usize], self.ldo_ena[which as usize])
|
||||
}
|
||||
|
||||
fn encode_dcdc_ena(&self) -> u8 {
|
||||
let mut code = 0;
|
||||
for (i, &ena) in self.dcdc_ena.iter().enumerate() {
|
||||
|
|
|
|||
|
|
@ -263,6 +263,50 @@ impl<'a> I2c<'a> {
|
|||
xous::yield_slice();
|
||||
}
|
||||
}
|
||||
let nack_bits = self.udma_global.irq_status_bits(IrqBank::I2cErr);
|
||||
match self.channel {
|
||||
I2cChannel::Channel0 => {
|
||||
if (nack_bits
|
||||
& (utralib::utra::irqarray12::EV_STATUS_I2C0_NACK.mask()
|
||||
<< utralib::utra::irqarray12::EV_STATUS_I2C0_NACK.offset())
|
||||
as u32)
|
||||
!= 0
|
||||
{
|
||||
return Err(xous::Error::Timeout);
|
||||
}
|
||||
}
|
||||
I2cChannel::Channel1 => {
|
||||
if (nack_bits
|
||||
& (utralib::utra::irqarray12::EV_STATUS_I2C1_NACK.mask()
|
||||
<< utralib::utra::irqarray12::EV_STATUS_I2C1_NACK.offset())
|
||||
as u32)
|
||||
!= 0
|
||||
{
|
||||
return Err(xous::Error::Timeout);
|
||||
}
|
||||
}
|
||||
I2cChannel::Channel2 => {
|
||||
if (nack_bits
|
||||
& (utralib::utra::irqarray12::EV_STATUS_I2C2_NACK.mask()
|
||||
<< utralib::utra::irqarray12::EV_STATUS_I2C2_NACK.offset())
|
||||
as u32)
|
||||
!= 0
|
||||
{
|
||||
return Err(xous::Error::Timeout);
|
||||
}
|
||||
}
|
||||
I2cChannel::Channel3 => {
|
||||
if (nack_bits
|
||||
& (utralib::utra::irqarray12::EV_STATUS_I2C3_NACK.mask()
|
||||
<< utralib::utra::irqarray12::EV_STATUS_I2C3_NACK.offset())
|
||||
as u32)
|
||||
!= 0
|
||||
{
|
||||
return Err(xous::Error::Timeout);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let ret = match self.pending.take() {
|
||||
I2cPending::Read(len) => {
|
||||
if let Some(buf) = rx_buf {
|
||||
|
|
@ -458,13 +502,25 @@ impl I2cApi for I2c<'_> {
|
|||
adr: u8,
|
||||
buf: &mut [u8],
|
||||
repeated_start: bool,
|
||||
) -> Result<usize, xous::Error> {
|
||||
self.i2c_read_async(dev, adr, buf.len(), repeated_start)?;
|
||||
self.i2c_await(Some(buf), true)
|
||||
) -> Result<I2cResult, xous::Error> {
|
||||
match self.i2c_read_async(dev, adr, buf.len(), repeated_start) {
|
||||
Err(_) => return Err(xous::Error::InternalError),
|
||||
_ => (),
|
||||
};
|
||||
match self.i2c_await(Some(buf), true) {
|
||||
Ok(b) => Ok(I2cResult::Ack(b)),
|
||||
_ => Ok(I2cResult::Nack),
|
||||
}
|
||||
}
|
||||
|
||||
fn i2c_write(&mut self, dev: u8, adr: u8, data: &[u8]) -> Result<usize, xous::Error> {
|
||||
self.i2c_write_async(dev, adr, data)?;
|
||||
self.i2c_await(None, true)
|
||||
fn i2c_write(&mut self, dev: u8, adr: u8, data: &[u8]) -> Result<I2cResult, xous::Error> {
|
||||
match self.i2c_write_async(dev, adr, data) {
|
||||
Err(_) => return Err(xous::Error::InternalError),
|
||||
_ => (),
|
||||
};
|
||||
match self.i2c_await(None, true) {
|
||||
Ok(b) => Ok(I2cResult::Ack(b)),
|
||||
_ => Ok(I2cResult::Nack),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,9 +37,53 @@ impl Into<usize> for GlobalReg {
|
|||
|
||||
pub struct GlobalConfig {
|
||||
csr: SharedCsr<u32>,
|
||||
i2c_irq: SharedCsr<u32>,
|
||||
i2c_err_irq: SharedCsr<u32>,
|
||||
}
|
||||
impl GlobalConfig {
|
||||
pub fn new(base_addr: *mut u32) -> Self { GlobalConfig { csr: SharedCsr::new(base_addr) } }
|
||||
pub fn new() -> Self {
|
||||
#[cfg(target_os = "xous")]
|
||||
let base_addr = xous::syscall::map_memory(
|
||||
xous::MemoryAddress::new(utralib::generated::HW_UDMA_CTRL_BASE),
|
||||
None,
|
||||
4096,
|
||||
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
||||
)
|
||||
.expect("couldn't map UDMA global control")
|
||||
.as_mut_ptr() as *mut u32;
|
||||
#[cfg(not(target_os = "xous"))]
|
||||
let base_addr = utra::udma_ctrl::HW_UDMA_CTRL_BASE as *mut u32;
|
||||
|
||||
#[cfg(target_os = "xous")]
|
||||
let i2c_irq_addr = xous::syscall::map_memory(
|
||||
xous::MemoryAddress::new(utralib::generated::HW_IRQARRAY7_BASE),
|
||||
None,
|
||||
4096,
|
||||
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
||||
)
|
||||
.expect("couldn't map I2C IRQ range")
|
||||
.as_mut_ptr() as *mut u32;
|
||||
#[cfg(not(target_os = "xous"))]
|
||||
let i2c_irq_addr = utralib::generated::HW_IRQARRAY7_BASE as *mut u32;
|
||||
|
||||
#[cfg(target_os = "xous")]
|
||||
let i2c_err_irq_addr = xous::syscall::map_memory(
|
||||
xous::MemoryAddress::new(utralib::generated::HW_IRQARRAY12_BASE),
|
||||
None,
|
||||
4096,
|
||||
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
||||
)
|
||||
.expect("couldn't map I2C IRQ range")
|
||||
.as_mut_ptr() as *mut u32;
|
||||
#[cfg(not(target_os = "xous"))]
|
||||
let i2c_err_irq_addr = utralib::generated::HW_IRQARRAY12_BASE as *mut u32;
|
||||
|
||||
GlobalConfig {
|
||||
csr: SharedCsr::new(base_addr),
|
||||
i2c_irq: SharedCsr::new(i2c_irq_addr),
|
||||
i2c_err_irq: SharedCsr::new(i2c_err_irq_addr),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clock_on(&self, peripheral: PeriphId) {
|
||||
// Safety: only safe when used in the context of UDMA registers.
|
||||
|
|
@ -138,6 +182,21 @@ impl UdmaGlobalConfig for GlobalConfig {
|
|||
}
|
||||
|
||||
fn reset(&self, peripheral: PeriphId) { self.reset(peripheral); }
|
||||
|
||||
fn irq_status_bits(&self, bank: IrqBank) -> u32 {
|
||||
match bank {
|
||||
IrqBank::I2c => {
|
||||
let pending = self.i2c_irq.r(utralib::utra::irqarray7::EV_PENDING);
|
||||
self.i2c_irq.wo(utralib::utra::irqarray7::EV_PENDING, pending);
|
||||
pending
|
||||
}
|
||||
IrqBank::I2cErr => {
|
||||
let pending = self.i2c_err_irq.r(utralib::utra::irqarray12::EV_PENDING);
|
||||
self.i2c_err_irq.wo(utralib::utra::irqarray12::EV_PENDING, pending);
|
||||
pending
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// --------------------------------- DMA channel ------------------------------------
|
||||
pub(crate) const CFG_EN: u32 = 0b01_0000; // start a transfer
|
||||
|
|
|
|||
|
|
@ -235,7 +235,7 @@ pub fn early_init() -> u32 {
|
|||
|
||||
// Set up the UDMA_UART block to the correct baud rate and enable status
|
||||
#[allow(unused_mut)] // some configs require mut
|
||||
let mut udma_global = GlobalConfig::new(utra::udma_ctrl::HW_UDMA_CTRL_BASE as *mut u32);
|
||||
let mut udma_global = GlobalConfig::new();
|
||||
udma_global.clock_on(PeriphId::Uart1);
|
||||
udma_global.map_event(
|
||||
PeriphId::Uart1,
|
||||
|
|
@ -409,7 +409,7 @@ pub fn early_init() -> u32 {
|
|||
// show the boot logo
|
||||
use ux_api::minigfx::FrameBuffer;
|
||||
|
||||
let mut udma_global = GlobalConfig::new(utra::udma_ctrl::HW_UDMA_CTRL_BASE as *mut u32);
|
||||
let mut udma_global = GlobalConfig::new();
|
||||
let mut sh1107 = cramium_hal::sh1107::Oled128x128::new(
|
||||
cramium_hal::sh1107::MainThreadToken::new(),
|
||||
perclk,
|
||||
|
|
@ -456,7 +456,7 @@ pub fn early_init() -> u32 {
|
|||
use crate::platform::cramium::qr;
|
||||
|
||||
let iox = Iox::new(utra::iox::HW_IOX_BASE as *mut u32);
|
||||
let udma_global = GlobalConfig::new(utra::udma_ctrl::HW_UDMA_CTRL_BASE as *mut u32);
|
||||
let udma_global = GlobalConfig::new();
|
||||
|
||||
// setup camera pins
|
||||
let (cam_pdwn_bnk, cam_pdwn_pin) = cramium_hal::board::setup_camera_pins(&iox);
|
||||
|
|
@ -962,7 +962,7 @@ pub fn early_init() -> u32 {
|
|||
|
||||
// setup the I/O pins
|
||||
let mut iox = Iox::new(utralib::generated::HW_IOX_BASE as *mut u32);
|
||||
let mut udma_global = GlobalConfig::new(utralib::generated::HW_UDMA_CTRL_BASE as *mut u32);
|
||||
let mut udma_global = GlobalConfig::new();
|
||||
let channel = cramium_hal::board::setup_memory_pins(&iox);
|
||||
udma_global.clock_off(PeriphId::from(channel));
|
||||
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ pub struct SwapHal {
|
|||
impl SwapHal {
|
||||
pub fn new(cfg: &BootConfig, perclk_freq: u32) -> Option<SwapHal> {
|
||||
if let Some(swap) = cfg.swap {
|
||||
let udma_global = GlobalConfig::new(utralib::generated::HW_UDMA_CTRL_BASE as *mut u32);
|
||||
let udma_global = GlobalConfig::new();
|
||||
|
||||
// setup the I/O pins
|
||||
let iox = Iox::new(utralib::generated::HW_IOX_BASE as *mut u32);
|
||||
|
|
@ -413,8 +413,7 @@ pub fn userspace_maps(cfg: &mut BootConfig) {
|
|||
iox.set_gpio_dir(IoxPort::PD, 3, IoxDir::Output);
|
||||
|
||||
// Set up the UDMA_UART block to the correct baud rate and enable status
|
||||
let udma_global =
|
||||
cramium_hal::udma::GlobalConfig::new(utralib::utra::udma_ctrl::HW_UDMA_CTRL_BASE as *mut u32);
|
||||
let udma_global = cramium_hal::udma::GlobalConfig::new();
|
||||
udma_global.clock_on(PeriphId::Uart0);
|
||||
udma_global.map_event(
|
||||
PeriphId::Uart0,
|
||||
|
|
|
|||
|
|
@ -110,7 +110,7 @@ pub fn process_update(perclk: u32) {
|
|||
|
||||
crate::println!("waiting for button press");
|
||||
let mut iox = Iox::new(utra::iox::HW_IOX_BASE as *mut u32);
|
||||
let mut udma_global = udma::GlobalConfig::new(utra::udma_ctrl::HW_UDMA_CTRL_BASE as *mut u32);
|
||||
let mut udma_global = udma::GlobalConfig::new();
|
||||
|
||||
let iox_kbd = iox.clone();
|
||||
let mut sh1107 = cramium_hal::sh1107::Oled128x128::new(
|
||||
|
|
@ -394,9 +394,7 @@ pub fn process_update(perclk: u32) {
|
|||
);
|
||||
progress_bar(&mut sh1107, 0);
|
||||
|
||||
let udma_global = GlobalConfig::new(
|
||||
utralib::generated::HW_UDMA_CTRL_BASE as *mut u32,
|
||||
);
|
||||
let udma_global = GlobalConfig::new();
|
||||
|
||||
// setup the I/O pins
|
||||
let iox = Iox::new(utralib::generated::HW_IOX_BASE as *mut u32);
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ impl I2cApi for I2c {
|
|||
adr: u8,
|
||||
buf: &mut [u8],
|
||||
repeated_start: bool,
|
||||
) -> Result<usize, xous::Error> {
|
||||
) -> Result<I2cResult, xous::Error> {
|
||||
let r = I2cTransaction {
|
||||
i2c_type: if repeated_start {
|
||||
I2cTransactionType::ReadRepeatedStart
|
||||
|
|
@ -46,16 +46,14 @@ impl I2cApi for I2c {
|
|||
result: I2cResult::Pending,
|
||||
};
|
||||
let result = self.i2c_transactions(I2cTransactions::from(vec![r]))?;
|
||||
match result.transactions[0].result {
|
||||
I2cResult::Ack(b) => {
|
||||
buf.copy_from_slice(&result.transactions[0].data);
|
||||
Ok(b)
|
||||
}
|
||||
_ => Err(xous::Error::InternalError),
|
||||
if result.transactions[0].result == I2cResult::InternalError {
|
||||
Err(xous::Error::InternalError)
|
||||
} else {
|
||||
Ok(result.transactions[0].result)
|
||||
}
|
||||
}
|
||||
|
||||
fn i2c_write(&mut self, dev: u8, adr: u8, data: &[u8]) -> Result<usize, xous::Error> {
|
||||
fn i2c_write(&mut self, dev: u8, adr: u8, data: &[u8]) -> Result<I2cResult, xous::Error> {
|
||||
let w = I2cTransaction {
|
||||
i2c_type: I2cTransactionType::Write,
|
||||
device: dev,
|
||||
|
|
@ -64,9 +62,10 @@ impl I2cApi for I2c {
|
|||
result: I2cResult::Pending,
|
||||
};
|
||||
let result = self.i2c_transactions(I2cTransactions::from(vec![w]))?;
|
||||
match result.transactions[0].result {
|
||||
I2cResult::Ack(b) => Ok(b),
|
||||
_ => Err(xous::Error::InternalError),
|
||||
if result.transactions[0].result == I2cResult::InternalError {
|
||||
Err(xous::Error::InternalError)
|
||||
} else {
|
||||
Ok(result.transactions[0].result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -86,4 +86,20 @@ impl UdmaGlobalConfig for UdmaGlobal {
|
|||
}
|
||||
|
||||
fn reset(&self, peripheral: PeriphId) { self.reset(peripheral); }
|
||||
|
||||
fn irq_status_bits(&self, bank: IrqBank) -> u32 {
|
||||
match xous::send_message(
|
||||
self.conn,
|
||||
xous::Message::new_blocking_scalar(
|
||||
HalOpcode::UdmaIrqStatusBits.to_usize().unwrap(),
|
||||
bank as u32 as usize,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
),
|
||||
) {
|
||||
Ok(xous::Result::Scalar5(_, value, _, _, _)) => value as u32,
|
||||
_ => panic!("Unhandled response on irq_status_bits"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ use bitfield::*;
|
|||
use cramium_api::*;
|
||||
use cramium_hal::{iox::Iox, udma::GlobalConfig};
|
||||
use num_traits::*;
|
||||
use udma::UdmaGlobalConfig;
|
||||
use utralib::CSR;
|
||||
#[cfg(feature = "quantum-timer")]
|
||||
use utralib::utra;
|
||||
|
|
@ -185,14 +186,7 @@ fn main() {
|
|||
.expect("couldn't claim the IOX hardware page");
|
||||
let iox = Iox::new(iox_page.as_ptr() as *mut u32);
|
||||
|
||||
let udma_global_csr = xous::syscall::map_memory(
|
||||
xous::MemoryAddress::new(utralib::generated::HW_UDMA_CTRL_BASE),
|
||||
None,
|
||||
4096,
|
||||
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
||||
)
|
||||
.expect("couldn't map UDMA global control");
|
||||
let udma_global = GlobalConfig::new(udma_global_csr.as_mut_ptr() as *mut u32);
|
||||
let udma_global = GlobalConfig::new();
|
||||
|
||||
// Note: the I2C handler can be put into a separate thread if we need the main
|
||||
// HAL server to not block while a large I2C transaction is being handled. For
|
||||
|
|
@ -533,6 +527,13 @@ fn main() {
|
|||
udma_global.map_event_with_offset(periph, event_offset, to_channel);
|
||||
}
|
||||
}
|
||||
HalOpcode::UdmaIrqStatusBits => {
|
||||
if let Some(scalar) = msg.body.scalar_message_mut() {
|
||||
let bank = num_traits::FromPrimitive::from_usize(scalar.arg1)
|
||||
.expect("Bad argument to UdmaIrqStatusBits");
|
||||
scalar.arg1 = udma_global.irq_status_bits(bank) as usize;
|
||||
}
|
||||
}
|
||||
HalOpcode::PeriphReset => {
|
||||
if let Some(scalar) = msg.body.scalar_message() {
|
||||
let periph: PeriphId = num_traits::FromPrimitive::from_usize(scalar.arg1).unwrap();
|
||||
|
|
@ -548,7 +549,7 @@ fn main() {
|
|||
match transaction.i2c_type {
|
||||
I2cTransactionType::Write => {
|
||||
match i2c.i2c_write(transaction.device, transaction.address, &transaction.data) {
|
||||
Ok(b) => transaction.result = I2cResult::Ack(b),
|
||||
Ok(result) => transaction.result = result,
|
||||
_ => transaction.result = I2cResult::Nack,
|
||||
}
|
||||
}
|
||||
|
|
@ -559,7 +560,7 @@ fn main() {
|
|||
&mut transaction.data,
|
||||
transaction.i2c_type == I2cTransactionType::ReadRepeatedStart,
|
||||
) {
|
||||
Ok(b) => transaction.result = I2cResult::Ack(b),
|
||||
Ok(result) => transaction.result = result,
|
||||
_ => transaction.result = I2cResult::Nack,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue