mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
commit
20140b2cdd
6 changed files with 175 additions and 1 deletions
|
|
@ -32,10 +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
|
||||
|
|
@ -44,13 +47,16 @@ loader-baosec = [
|
|||
"camera-gc2145",
|
||||
"display-sh1107",
|
||||
"axp2101",
|
||||
"bmp180",
|
||||
"ux-api/loader-baosec",
|
||||
]
|
||||
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",
|
||||
|
|
|
|||
134
libs/cramium-hal/src/bmp180.rs
Normal file
134
libs/cramium-hal/src/bmp180.rs
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
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;
|
||||
const REG_CHIP_ID: u8 = 0xD0;
|
||||
|
||||
#[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 {
|
||||
pub fn new(i2c: &mut dyn I2cApi) -> Result<Self, I2cResult> {
|
||||
// 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(_) => (),
|
||||
other => return Err(other),
|
||||
},
|
||||
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]]),
|
||||
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 Err(I2cResult::InternalError);
|
||||
}
|
||||
|
||||
Ok(Bmp180 { calibration })
|
||||
}
|
||||
|
||||
pub fn read_temperature(&self, i2c: &mut dyn I2cApi) -> Result<f32, I2cResult> {
|
||||
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) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -5,6 +5,8 @@
|
|||
pub mod debug;
|
||||
#[cfg(feature = "axp2101")]
|
||||
pub mod axp2101;
|
||||
#[cfg(feature = "bmp180")]
|
||||
pub mod bmp180;
|
||||
pub mod board;
|
||||
#[cfg(feature = "camera-gc2145")]
|
||||
pub mod gc2145;
|
||||
|
|
|
|||
|
|
@ -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 = []
|
||||
|
|
|
|||
|
|
@ -13,12 +13,39 @@ impl<'a> ShellCmdApi<'a> for Test {
|
|||
fn process(&mut self, args: String, _env: &mut CommonEnv) -> Result<Option<String>, 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) {
|
||||
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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"]
|
||||
|
|
|
|||
Loading…
Reference in a new issue