From 2659fc08941a5dec45b8e5a233f87594422bff67 Mon Sep 17 00:00:00 2001 From: Sam Chin Date: Tue, 10 Jun 2025 14:56:04 +0800 Subject: [PATCH] wip' ' --- libs/cramium-hal/Cargo.toml | 5 +- libs/cramium-hal/src/bmp180.rs | 111 ++++++++++++++++++++++++++ libs/cramium-hal/src/lib.rs | 1 + services/bao-console/src/cmds/test.rs | 25 ++++++ 4 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 libs/cramium-hal/src/bmp180.rs diff --git a/libs/cramium-hal/Cargo.toml b/libs/cramium-hal/Cargo.toml index b5bc80f75..1c8bcb558 100644 --- a/libs/cramium-hal/Cargo.toml +++ b/libs/cramium-hal/Cargo.toml @@ -33,6 +33,7 @@ camera-gc2145 = [] display-sh1107 = [] axp2101 = [] board-baosec = [ + "std", "camera-gc2145", "display-sh1107", "axp2101", @@ -50,7 +51,9 @@ kernel-baosec = ["utralib/cramium-soc"] kernel-baosor = ["utralib/cramium-soc"] kernel-dabao = ["utralib/cramium-soc"] test-baosec = ["ux-api/loader-baosec", "camera-gc2145"] -hosted-baosec = [] # emulated hardware on x86 platform +hosted-baosec = [ + "std" +] # emulated hardware on x86 platform board-baosor = [ "camera-gc2145", "axp2101", diff --git a/libs/cramium-hal/src/bmp180.rs b/libs/cramium-hal/src/bmp180.rs new file mode 100644 index 000000000..f29b87535 --- /dev/null +++ b/libs/cramium-hal/src/bmp180.rs @@ -0,0 +1,111 @@ +use cramium_api::{I2cApi, I2cResult}; + +pub const BMP180_ADDR: u8 = 0x77; +const REG_CALIB_START: u8 = 0xAA; +const REG_CTRL: u8 = 0xF4; +const REG_DATA_START: u8 = 0xF6; +const CMD_READ_TEMP: u8 = 0x2E; + +#[derive(Debug, Clone, Copy)] + +struct Bmp180Calibration { + ac1: i16, ac2: i16, ac3: i16, + ac4: u16, ac5: u16, ac6: u16, + b1: i16, b2: i16, + mb: i16, mc: i16, md: i16, +} + +pub struct Bmp180 { + calibration: Bmp180Calibration, +} + +impl Bmp180 { + #[cfg(feature = "std")] + pub fn new(i2c: &mut dyn I2cApi) -> Result { + let mut cal_buf = [0u8; 22]; + + match i2c.i2c_read(BMP180_ADDR, REG_CALIB_START, &mut cal_buf, true) { + Ok(i2c_result) => match i2c_result { + I2cResult::Ack(_) => (), + other => return Err(other), + }, + Err(_) => return Err(I2cResult::InternalError), + } + + let calibration = Bmp180Calibration { + ac1: i16::from_be_bytes([cal_buf[0], cal_buf[1]]), + ac2: i16::from_be_bytes([cal_buf[2], cal_buf[3]]), + ac3: i16::from_be_bytes([cal_buf[4], cal_buf[5]]), + ac4: u16::from_be_bytes([cal_buf[6], cal_buf[7]]), + ac5: u16::from_be_bytes([cal_buf[8], cal_buf[9]]), + ac6: u16::from_be_bytes([cal_buf[10], cal_buf[11]]), + b1: i16::from_be_bytes([cal_buf[12], cal_buf[13]]), + b2: i16::from_be_bytes([cal_buf[14], cal_buf[15]]), + mb: i16::from_be_bytes([cal_buf[16], cal_buf[17]]), + mc: i16::from_be_bytes([cal_buf[18], cal_buf[19]]), + md: i16::from_be_bytes([cal_buf[20], cal_buf[21]]), + }; + + if calibration.ac1 == 0 || calibration.ac2 == 0 || calibration.ac3 == 0 || + calibration.ac4 == 0 || calibration.ac5 == 0 || calibration.ac6 == 0 || + calibration.b1 == 0 || calibration.b2 == 0 || + calibration.mb == 0 || calibration.mc == 0 || calibration.md == 0 || + calibration.ac1 == -1 { + // Return an error indicating the data from the sensor is invalid. + return Err(I2cResult::InternalError); + } + + Ok(Bmp180 { calibration }) + } + + pub fn read_temperature(&self, i2c: &mut dyn I2cApi) -> Result { + match i2c.i2c_write(BMP180_ADDR, REG_CTRL, &[CMD_READ_TEMP]) { + Ok(I2cResult::Ack(_)) => (), + Ok(other) => return Err(other), + Err(_) => return Err(I2cResult::InternalError), + } + + self.delay(5); + + let mut temp_buffer = [0u8; 2]; + match i2c.i2c_read(BMP180_ADDR, REG_DATA_START, &mut temp_buffer, true) { + Ok(I2cResult::Ack(_)) => (), + Ok(other) => return Err(other), + Err(_) => return Err(I2cResult::InternalError), + } + + let ut = i16::from_be_bytes(temp_buffer) as i32; + + let cal = &self.calibration; + let x1 = (ut - cal.ac6 as i32) * cal.ac5 as i32 >> 15; + let x2 = (cal.mc as i32 * 2048) / (x1 + cal.md as i32); + let b5 = x1 + x2; + let temp = ((b5 + 8) >> 4) as f32 / 10.0; + + Ok(temp) + } + + pub fn delay(&self, quantum: usize) { + #[cfg(feature = "std")] + { + let tt = xous_api_ticktimer::Ticktimer::new().unwrap(); + tt.sleep_ms(quantum).ok(); + } + #[cfg(not(feature = "std"))] + { + use utralib::{CSR, utra}; + // abuse the d11ctime timer to create some time-out like thing + let mut d11c = CSR::new(utra::d11ctime::HW_D11CTIME_BASE as *mut u32); + d11c.wfo(utra::d11ctime::CONTROL_COUNT, 333_333); // 1.0ms per interval + let mut polarity = d11c.rf(utra::d11ctime::HEARTBEAT_BEAT); + for _ in 0..quantum { + while polarity == d11c.rf(utra::d11ctime::HEARTBEAT_BEAT) {} + polarity = d11c.rf(utra::d11ctime::HEARTBEAT_BEAT); + } + // we have to split this because we don't know where we caught the previous interval + if quantum == 1 { + while polarity == d11c.rf(utra::d11ctime::HEARTBEAT_BEAT) {} + } + } + } +} diff --git a/libs/cramium-hal/src/lib.rs b/libs/cramium-hal/src/lib.rs index f3a4e7fef..6439628f4 100644 --- a/libs/cramium-hal/src/lib.rs +++ b/libs/cramium-hal/src/lib.rs @@ -5,6 +5,7 @@ pub mod debug; #[cfg(feature = "axp2101")] pub mod axp2101; +pub mod bmp180; pub mod board; #[cfg(feature = "camera-gc2145")] pub mod gc2145; diff --git a/services/bao-console/src/cmds/test.rs b/services/bao-console/src/cmds/test.rs index 4a76ccea8..c08c5952f 100644 --- a/services/bao-console/src/cmds/test.rs +++ b/services/bao-console/src/cmds/test.rs @@ -1,5 +1,8 @@ use String; +use cram_hal_service::I2c; +use cramium_hal::bmp180::Bmp180; // Correctly import from the library + use crate::{CommonEnv, ShellCmdApi}; #[derive(Debug)] @@ -19,6 +22,28 @@ impl<'a> ShellCmdApi<'a> for Test { if let Some(sub_cmd) = tokens.next() { match sub_cmd { + "temp" => { + // Initialize I2C connection + let mut i2c = I2c::new(); + + // Attempt to initialize the BMP180 sensor + match Bmp180::new(&mut i2c) { + Ok(sensor) => { + // If successful, attempt to read the temperature + match sensor.read_temperature(&mut i2c) { + Ok(temp) => { + write!(ret, "BMP180 Temperature: {:.1}°C", temp).unwrap(); + } + Err(e) => { + write!(ret, "Failed to read temperature: {:?}", e).unwrap(); + } + } + } + Err(e) => { + write!(ret, "Failed to initialize BMP180 sensor: {:?}", e).unwrap(); + } + } + } _ => { write!(ret, "{}", helpstring).unwrap(); }