move i2c in bao1x-hal-services into bao1x-hal

this makes i2c accessible without having to rely explicitly
on the server itself, making the library callable in more contexts.
necessitated avoiding a name space collision and moving the
I2C hardware drivers to a stucture named I2cDriver; the I2C
object now unambiguously refers to the userspace interface
to I2C.
This commit is contained in:
bunnie 2025-12-11 02:13:21 +08:00
parent 8269f9f759
commit 8950a24327
8 changed files with 20 additions and 12 deletions

View file

@ -189,7 +189,7 @@ pub fn early_init(mut board_type: bao1x_api::BoardTypeCoding) -> (bao1x_api::Boa
)
};
let mut i2c = unsafe {
bao1x_hal::udma::I2c::new_with_ifram(
bao1x_hal::udma::I2cDriver::new_with_ifram(
i2c_channel,
400_000,
100_000_000,

View file

@ -46,6 +46,7 @@ rand_chacha = { version = "0.3.1", default-features = false }
# [target.'cfg(target_os = "xous")'.dependencies]
xous = { version = "0.9.67", features = ["v2p"] }
xous-ipc = { version = "0.10.7", optional = true }
bitflags = "1.2.1"
# TODO: migrate from bitflags to this crate
@ -117,6 +118,7 @@ std = [
"usb-device",
"xous-api-ticktimer",
"bao1x-api/std",
"xous-ipc",
]
derive-rkyv = ["rkyv"]

View file

@ -176,6 +176,7 @@ pub fn setup_memory_pins(iox: &dyn IoSetup) -> SpimChannel {
SPI_MEM_CHANNEL
}
const I2C_CHANNEL: I2cChannel = I2cChannel::Channel0;
/// This also sets up I2C-adjacent interrupt inputs as well
pub fn setup_i2c_pins(iox: &dyn IoSetup) -> I2cChannel {
// I2C_SCL_B[0]
@ -211,9 +212,12 @@ pub fn setup_i2c_pins(iox: &dyn IoSetup) -> I2cChannel {
None,
None,
);
I2cChannel::Channel0
I2C_CHANNEL
}
/// A function just to get the channel.
pub fn get_i2c_channel() -> I2cChannel { I2C_CHANNEL }
/// returns the power-down port and pin number
pub fn setup_camera_pins<T: IoSetup + IoGpio>(iox: &T) -> (IoxPort, u8) {
iox.setup_pin(

View file

@ -10,9 +10,9 @@ pub struct I2c {
impl I2c {
pub fn new() -> Self {
REFCOUNT.fetch_add(1, Ordering::Relaxed);
let xns = xous_names::XousNames::new().unwrap();
let xns = xous_api_names::XousNames::new().unwrap();
let conn = xns
.request_connection(crate::SERVER_NAME_BAO1X_HAL)
.request_connection(bao1x_api::SERVER_NAME_BAO1X_HAL)
.expect("Couldn't connect to bao1x HAL server");
I2c { conn }
}

View file

@ -38,6 +38,8 @@ pub mod clocks;
pub mod coreuser;
#[cfg(all(not(feature = "hosted-baosec"), not(feature = "kernel")))]
pub mod hardening;
#[cfg(feature = "std")]
pub mod i2c;
#[cfg(all(not(feature = "hosted-baosec"), feature = "std"))]
pub mod kpc_aoint;
#[cfg(not(feature = "hosted-baosec"))]

View file

@ -68,7 +68,7 @@ const MAX_I2C_TXLEN: usize = 512;
const MAX_I2C_RXLEN: usize = 512;
const MAX_I2C_CMDLEN: usize = 512;
pub struct I2c<'a> {
pub struct I2cDriver<'a> {
csr: CSR<u32>,
#[allow(dead_code)] // used in bao1x
udma_global: &'a dyn UdmaGlobalConfig,
@ -87,13 +87,13 @@ pub struct I2c<'a> {
pending: I2cPending,
}
impl Udma for I2c<'_> {
impl Udma for I2cDriver<'_> {
fn csr_mut(&mut self) -> &mut CSR<u32> { &mut self.csr }
fn csr(&self) -> &CSR<u32> { &self.csr }
}
impl<'a> I2c<'a> {
impl<'a> I2cDriver<'a> {
/// 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.
#[cfg(feature = "std")]
@ -105,7 +105,7 @@ impl<'a> I2c<'a> {
) -> Option<Self> {
// one page is the minimum size we can request
if let Some(ifram) = IframRange::request(4096, None) {
Some(I2c::new_with_ifram(channel, i2c_freq, perclk_freq, ifram, udma_global))
Some(I2cDriver::new_with_ifram(channel, i2c_freq, perclk_freq, ifram, udma_global))
} else {
None
}
@ -159,7 +159,7 @@ impl<'a> I2c<'a> {
// one page is the minimum size we can request
let ifram_base = ifram.virt_range.as_ptr() as usize;
let ifram_base_phys = ifram.phys_range.as_ptr() as usize;
let mut i2c = I2c {
let mut i2c = I2cDriver {
csr,
udma_global,
channel,
@ -495,7 +495,7 @@ impl<'a> I2c<'a> {
}
}
impl I2cApi for I2c<'_> {
impl I2cApi for I2cDriver<'_> {
fn i2c_read(
&mut self,
dev: u8,

View file

@ -4,7 +4,7 @@ use bao1x_api::I2cResult;
#[cfg(feature = "hosted-baosec")]
use bao1x_emu::i2c::I2c;
#[cfg(not(feature = "hosted-baosec"))]
use bao1x_hal_service::I2c;
use bao1x_hal::i2c::I2c;
use crate::{CommonEnv, ShellCmdApi};

View file

@ -258,7 +258,7 @@ pub(crate) fn main_hw() -> ! {
let iox = bao1x_api::IoxHal::new();
#[cfg(feature = "board-baosec")]
let mut i2c = bao1x_hal_service::I2c::new();
let mut i2c = bao1x_hal::i2c::I2c::new();
#[cfg(feature = "board-baosec")]
let pmic = {
log::info!("Registering PMIC handler to detect USB plug/unplug events");