From 2659fc08941a5dec45b8e5a233f87594422bff67 Mon Sep 17 00:00:00 2001 From: Sam Chin Date: Tue, 10 Jun 2025 14:56:04 +0800 Subject: [PATCH 1/3] 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(); } From 0412a401d83405e9a745f4af1bd38d010436767c Mon Sep 17 00:00:00 2001 From: Sam Chin Date: Tue, 10 Jun 2025 21:51:33 +0800 Subject: [PATCH 2/3] no more deadcode --- libs/cramium-hal/src/bmp180.rs | 47 ++++++++++++++++++--------- services/bao-console/src/cmds/test.rs | 23 +++++-------- 2 files changed, 41 insertions(+), 29 deletions(-) diff --git a/libs/cramium-hal/src/bmp180.rs b/libs/cramium-hal/src/bmp180.rs index f29b87535..e32e968b0 100644 --- a/libs/cramium-hal/src/bmp180.rs +++ b/libs/cramium-hal/src/bmp180.rs @@ -1,6 +1,7 @@ use cramium_api::{I2cApi, I2cResult}; pub const BMP180_ADDR: u8 = 0x77; +#[cfg(feature = "std")] const REG_CALIB_START: u8 = 0xAA; const REG_CTRL: u8 = 0xF4; const REG_DATA_START: u8 = 0xF6; @@ -8,11 +9,19 @@ const CMD_READ_TEMP: u8 = 0x2E; #[derive(Debug, Clone, Copy)] +#[allow(dead_code)] struct Bmp180Calibration { - ac1: i16, ac2: i16, ac3: i16, - ac4: u16, ac5: u16, ac6: u16, - b1: i16, b2: i16, - mb: i16, mc: i16, md: i16, + 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 { @@ -32,6 +41,7 @@ impl Bmp180 { Err(_) => return Err(I2cResult::InternalError), } + // note: calibration data is Big Endian, hence the from_be_bytes 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]]), @@ -39,19 +49,26 @@ impl Bmp180 { 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]]), + 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. + 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 Err(I2cResult::InternalError); } diff --git a/services/bao-console/src/cmds/test.rs b/services/bao-console/src/cmds/test.rs index c08c5952f..408ff8ecf 100644 --- a/services/bao-console/src/cmds/test.rs +++ b/services/bao-console/src/cmds/test.rs @@ -1,7 +1,6 @@ use String; - use cram_hal_service::I2c; -use cramium_hal::bmp180::Bmp180; // Correctly import from the library +use cramium_hal::bmp180::Bmp180; use crate::{CommonEnv, ShellCmdApi}; @@ -23,27 +22,23 @@ 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(); - } + Ok(sensor) => 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(); } From 04bf756b8e85a8812c750697d75b14a4e9c15494 Mon Sep 17 00:00:00 2001 From: Sam Chin Date: Wed, 11 Jun 2025 15:05:59 +0800 Subject: [PATCH 3/3] theoretically clean version which has been tested with and without the feature --- libs/cramium-hal/Cargo.toml | 3 +++ libs/cramium-hal/src/bmp180.rs | 14 ++++++++++---- libs/cramium-hal/src/lib.rs | 1 + services/bao-console/Cargo.toml | 4 ++++ services/bao-console/src/cmds/test.rs | 11 +++++++++-- services/cram-hal-service/Cargo.toml | 1 + 6 files changed, 28 insertions(+), 6 deletions(-) diff --git a/libs/cramium-hal/Cargo.toml b/libs/cramium-hal/Cargo.toml index 1c8bcb558..34f71ca98 100644 --- a/libs/cramium-hal/Cargo.toml +++ b/libs/cramium-hal/Cargo.toml @@ -32,11 +32,13 @@ camera-ov2640 = [] camera-gc2145 = [] display-sh1107 = [] axp2101 = [] +bmp180 = [] board-baosec = [ "std", "camera-gc2145", "display-sh1107", "axp2101", + "bmp180", "utralib/cramium-soc", "ux-api/board-baosec", ] # USB form factor token @@ -45,6 +47,7 @@ loader-baosec = [ "camera-gc2145", "display-sh1107", "axp2101", + "bmp180", "ux-api/loader-baosec", ] kernel-baosec = ["utralib/cramium-soc"] diff --git a/libs/cramium-hal/src/bmp180.rs b/libs/cramium-hal/src/bmp180.rs index e32e968b0..db0e60d7b 100644 --- a/libs/cramium-hal/src/bmp180.rs +++ b/libs/cramium-hal/src/bmp180.rs @@ -1,15 +1,14 @@ use cramium_api::{I2cApi, I2cResult}; pub const BMP180_ADDR: u8 = 0x77; -#[cfg(feature = "std")] const REG_CALIB_START: u8 = 0xAA; const REG_CTRL: u8 = 0xF4; const REG_DATA_START: u8 = 0xF6; const CMD_READ_TEMP: u8 = 0x2E; +const REG_CHIP_ID: u8 = 0xD0; #[derive(Debug, Clone, Copy)] -#[allow(dead_code)] struct Bmp180Calibration { ac1: i16, ac2: i16, @@ -29,10 +28,17 @@ pub struct Bmp180 { } impl Bmp180 { - #[cfg(feature = "std")] pub fn new(i2c: &mut dyn I2cApi) -> Result { - let mut cal_buf = [0u8; 22]; + // dummy read to wake the chip + let mut chip_id_buf = [0u8; 1]; + match i2c.i2c_read(BMP180_ADDR, REG_CHIP_ID, &mut chip_id_buf, true) { + Ok(I2cResult::Ack(_)) => (), + Ok(I2cResult::Nack) => (), + Ok(other) => return Err(other), + Err(_) => return Err(I2cResult::InternalError), + } + 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(_) => (), diff --git a/libs/cramium-hal/src/lib.rs b/libs/cramium-hal/src/lib.rs index 6439628f4..04f142641 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; +#[cfg(feature = "bmp180")] pub mod bmp180; pub mod board; #[cfg(feature = "camera-gc2145")] diff --git a/services/bao-console/Cargo.toml b/services/bao-console/Cargo.toml index ba2b1900a..e685c7e2f 100644 --- a/services/bao-console/Cargo.toml +++ b/services/bao-console/Cargo.toml @@ -49,6 +49,10 @@ hosted-baosec = [ "pddb/hosted-baosec", "cramium-hal/hosted-baosec", ] +bmp180 = [ + "cramium-hal/bmp180", + "cram-hal-service", +] usb = [] battery-readout = [] with-pddb = [] diff --git a/services/bao-console/src/cmds/test.rs b/services/bao-console/src/cmds/test.rs index 408ff8ecf..ebc3a376a 100644 --- a/services/bao-console/src/cmds/test.rs +++ b/services/bao-console/src/cmds/test.rs @@ -1,6 +1,4 @@ use String; -use cram_hal_service::I2c; -use cramium_hal::bmp180::Bmp180; use crate::{CommonEnv, ShellCmdApi}; @@ -15,13 +13,22 @@ impl<'a> ShellCmdApi<'a> for Test { fn process(&mut self, args: String, _env: &mut CommonEnv) -> Result, xous::Error> { use core::fmt::Write; let mut ret = String::new(); + + #[allow(unused_variables)] let helpstring = "Test commands. See code for options."; + #[cfg(feature = "bmp180")] + let helpstring = "Usage: + temp - reads temperature from bmp180."; + let mut tokens = args.split(' '); if let Some(sub_cmd) = tokens.next() { match sub_cmd { + #[cfg(feature = "bmp180")] "temp" => { + use cram_hal_service::I2c; + use cramium_hal::bmp180::Bmp180; let mut i2c = I2c::new(); match Bmp180::new(&mut i2c) { diff --git a/services/cram-hal-service/Cargo.toml b/services/cram-hal-service/Cargo.toml index f2e78731c..03134c29c 100644 --- a/services/cram-hal-service/Cargo.toml +++ b/services/cram-hal-service/Cargo.toml @@ -45,6 +45,7 @@ rawserial = [] pinger = [] swap = [] mpw = ["cramium-hal/mpw"] +bmp180 = ["cramium-hal/bmp180"] board-baosec = ["cramium-hal/board-baosec"] board-baosor = ["cramium-hal/board-baosor"] board-dabao = ["cramium-hal/board-dabao"]