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"]