From 1b8554b8f2c11a40808390c33ee201437a898cf9 Mon Sep 17 00:00:00 2001 From: Sam Chin Date: Mon, 9 Jun 2025 16:10:42 +0800 Subject: [PATCH] removing the BMP180 code --- services/bao-console/src/cmds/i2cdetect.rs | 195 ++++++--------------- services/bao-console/src/main.rs | 10 +- 2 files changed, 59 insertions(+), 146 deletions(-) diff --git a/services/bao-console/src/cmds/i2cdetect.rs b/services/bao-console/src/cmds/i2cdetect.rs index d949582a4..32e3cf139 100644 --- a/services/bao-console/src/cmds/i2cdetect.rs +++ b/services/bao-console/src/cmds/i2cdetect.rs @@ -1,21 +1,31 @@ use String; use cramium_api::I2cApi; +use cramium_api::I2cResult; use cram_hal_service::I2c; -use std::thread::sleep; -use std::time::Duration; use crate::{CommonEnv, ShellCmdApi}; #[derive(Debug)] -pub struct I2cDetect {} -struct Bmp180Calibration { - ac1: i16, ac2: i16, ac3: i16, - ac4: u16, ac5: u16, ac6: u16, - b1: i16, b2: i16, - mb: i16, mc: i16, md: i16, +enum ProbeStatus { + Ack, + Nack, } +fn probe_address(i2c: &mut I2c, addr: u8) -> ProbeStatus { + let mut buf = [0u8; 1]; + match i2c.i2c_read(addr, 0x00, &mut buf, false) { + Ok(I2cResult::Ack(_)) => ProbeStatus::Ack, + Ok(I2cResult::Nack) => ProbeStatus::Nack, + Ok(I2cResult::Pending) => panic!("Unexpected I2cResult::Pending: this operation should be blocking."), + Ok(I2cResult::InternalError) => panic!("Unexpected Ok(I2cResult::InternalError)."), + Err(e) => panic!("I2C bus error while probing address 0x{:02X}: {:?}", addr, e), + } +} + + +pub struct I2cDetect {} + impl<'a> ShellCmdApi<'a> for I2cDetect { cmd_api!(i2cdetect); @@ -23,154 +33,57 @@ impl<'a> ShellCmdApi<'a> for I2cDetect { use core::fmt::Write; let mut ret = String::new(); let helpstring = "Usage: - i2cdetect cat - prints cat ascii art - i2cdetect bread - prints bread ascii art - i2cdetect breadcat - prints breadcat ascii art - i2cdetect probe - probes a single I2C address (e.g. 34) - i2cdetect bmptemp - reads raw temperature from BMP180"; + i2cdetect probe - probes a single I2C address (e.g. 34) + i2cdetect scan - reads + i2cdetect bmp180_temp - reads raw temperature from BMP180"; let mut tokens = args.split_whitespace(); if let Some(sub_cmd) = tokens.next() { match sub_cmd { - "cat" => { - let cat_art = r#"f - ⠀⠀⠀⠀⢀⠠⠤⠀⢀⣿⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ - ⠀⠀⠐⠀⠐⠀⠀⢀⣾⣿⡇⠀⠀⠀⠀⠀⢀⣼⡇⠀⠀⠀⠀ - ⠀⠀⠀⠀⠀⠀⠀⣸⣿⣿⣿⠀⠀⠀⠀⣴⣿⣿⠇⠀⠀⠀⠀ - ⠀⠀⠀⠀⠀⠀⢠⣿⣿⣿⣇⠀⠀⢀⣾⣿⣿⣿⠀⠀⠀⠀⠀ - ⠀⠀⠀⠀⠀⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡟⠀⠀⠐⠀⡀ - ⠀⠀⠀⠀⢰⡿⠉⠀⡜⣿⣿⣿⡿⠿⢿⣿⣿⡃⠀⠀⠂⠄⠀ - ⠀⠀⠒⠒⠸⣿⣄⡘⣃⣿⣿⡟⢰⠃⠀⢹⣿⡇⠀⠀⠀⠀⠀ - ⠀⠀⠚⠉⠀⠊⠻⣿⣿⣿⣿⣿⣮⣤⣤⣿⡟⠁⠘⠠⠁⠀⠀ - ⠀⠀⠀⠀⠀⠠⠀⠀⠈⠙⠛⠛⠛⠛⠛⠁⠀⠒⠤⠀⠀⠀⠀ - ⠨⠠⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠑⠀⠀⠀⠀⠀⠀ - ⠁⠃⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ - "#; - write!(ret, "{}", cat_art).unwrap(); - } - "bread" => { - let bread_art = r#" - ⠀⠀⠀⠀⣀⣠⣤⣤⣤⣤⣤⣄⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ - ⠀⠀⣴⣿⣿⠿⠛⣉⣁⣠⣤⣤⣤⣬⣀⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ - ⠀⢸⣿⣿⣿⣿⣿⣿⡿⠛⣉⣁⣠⣤⣤⣤⣬⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ - ⠀⢸⣿⣿⣿⣿⣿⣵⣾⣿⡿⠟⠛⠉⣉⣉⣉⣉⠉⠓⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ - ⠀⢸⣿⣿⣿⣿⣿⣿⡟⢁⣤⣶⣿⣿⣿⣿⣿⣿⣿⣿⣶⣄⠀⠀⠀⠀⠀⠀⠀⠀ - ⠀⢸⣿⣿⣿⣿⣿⣿⠀⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⠀⠀⠀⠀⠀⠀⠀ - ⠀⢿⣿⣿⣿⣿⣿⣿⡄⢹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀ - ⠀⠈⠻⣿⣿⣿⣿⣿⣷⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠀⠀⠀⠀⠀⠀⠀⠀ - ⠀⠀⠀⠈⠻⣿⣿⣿⣿⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀ - ⠀⠀⠀⠀⠀⠙⢿⣿⠇⢰⣿⣿⣿⣿⡿⠛⢉⣩⣉⡉⠛⠻⠇⠀⠀⠀⠀⠀⠀⠀ - ⠀⠀⠀⠀⠀⠀⠀⠙⠀⠸⠿⠿⠟⢉⣠⣾⣿⣿⣿⣿⣷⣶⣦⣤⣄⣀⣀⡀⠀⠀ - ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡷⠀ - ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢈⣉⣉⠉⠛⠛⠻⠿⣿⣿⣿⣿⣿⣿⣿⠿⠋⣀⠀ - ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠛⠛⠛⠛⠻⠶⣶⣤⣈⣉⣉⣉⣁⣤⣴⠾⠛⠀ - ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠙⠛⠉⠉⠉⠀⠀⠀⠀ - "#; - write!(ret, "{}", bread_art).unwrap(); - } - "breadcat" => { - let breadcat_art = r#" - ⠀⠀⠀⠰⣦⣤⣤⣀⠀⠀⡀⠀⠀⠀⠀⣀⣀⠀⠠⢤⣶⣶⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ - ⠀⠀⠀⠀⢿⣿⢋⣵⣾⣿⣿⡿⣥⣮⢻⣿⣿⣿⣿⣶⣌⢿⣿⢰⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣦⣤⡀⠀⠀⠀⠀⠀⠀⠀⠀ - ⠀⠀⠀⠀⠘⢡⣿⣿⣿⣿⣿⣷⡹⠟⣵⣿⣿⣿⣿⣿⣿⣦⢃⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀ - ⠀⠀⠀⠀⠀⢺⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢨⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀ - ⠀⠀⠀⠀⠀⠸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⣿⣿⣿⡟⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀ - ⠀⠀⠀⠀⠀⠀⠙⣿⣷⣬⣿⣿⣿⣿⣿⣿⣿⡼⣿⣿⡟⣱⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀ - ⠀⠀⠀⠀⠀⠀⠀⣿⣧⣠⣿⣿⣿⣿⣿⣿⣿⣀⣾⣿⡇⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡀⠀⠀⠀⠀⠀⠀⠀ - ⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣅⣽⣿⣿⣿⣿⣿⣿⡇⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⠀⠀⠀⠀⠀⠀ - ⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡀⠀⠀⠀⠀ - ⠀⠀⠀⠀⠀⠀⠀⠛⠙⠋⠋⠋⠋⠋⠋⠋⠋⠛⠙⠙⠁⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⢿⣿⣿⣿⣿⣿⣧⠀⠀⠀⠀ - ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢈⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀ - ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠⣴⣶⣉⢿⣿⣿⣿⡟⠀⠀⠀⠀ - ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣤⣆⡻⣿⣷⠜⣿⣏⣾⣿⣿⡿⠁⠀⠀⠀⠀ - ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⣿⣿⣿⣇⣿⣿⣽⣿⣿⣿⠿⠋⠀⠀⠀⠀⠀⠀ - ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢿⣿⣿⣿⣿⣿⠿⠟⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀ - ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠉⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ - "#; - write!(ret, "{}", breadcat_art).unwrap(); - } - "probe" => { - if let Some(addr_str) = tokens.next() { - match u8::from_str_radix(addr_str, 16) { - Ok(addr) => { - let mut i2c = I2c::new(); - let mut buf = [0u8; 1]; - match i2c.i2c_read(addr, 0x00, &mut buf, false) { - Ok(_) => { - write!(ret, "Device found at address {:02X} (via read)\n", addr).unwrap(); - } - Err(xous::Error::InternalError) => { - write!(ret, "No device found at address {:02X}\n", addr).unwrap(); - } - Err(e) => { - write!(ret, "Error probing address {:02X}: {:?}\n", addr, e).unwrap(); - } + if let Some(addr_str) = tokens.next() { + match u8::from_str_radix(addr_str, 16) { + Ok(addr) => { + let mut i2c = I2c::new(); + let status = probe_address(&mut i2c, addr); + match status { + ProbeStatus::Ack => write!(ret, "Device found at address {:02X}\n", addr).unwrap(), + ProbeStatus::Nack => write!(ret, "Device at address {:02X} responded with NACK\n", addr).unwrap(), + }; + } + Err(_) => { + write!(ret, "Invalid hex address '{}'. Example: i2cdetect probe 3C\n", addr_str).unwrap(); } } - Err(_) => { - write!(ret, "Invalid hex address '{}'. Example: i2cdetect probe 3C\n", addr_str).unwrap(); - } + } else { + write!(ret, "Usage: i2cdetect probe \n").unwrap(); } - } else { - write!(ret, "Usage: i2cdetect probe \n").unwrap(); } - } - - "bmptemp" => { - 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; + "scan" => { let mut i2c = I2c::new(); - - let mut cal_buf = [0u8; 22]; - if let Err(e) = i2c.i2c_read(BMP180_ADDR, REG_CALIB_START, &mut cal_buf, true) { - write!(ret, "Failed to read calibration data: {:?}\n", e).unwrap(); - return Ok(Some(ret)); + writeln!(&mut ret, " 0 1 2 3 4 5 6 7 8 9 a b c d e f").unwrap(); + for row in 0..8 { + write!(&mut ret, "{:02x}: ", row * 16).unwrap(); + for col in 0..16 { + let addr = (row * 16 + col) as u8; + if addr < 0x03 || addr > 0x77 { + write!(&mut ret, " ").unwrap(); + } else { + let status = probe_address(&mut i2c, addr); + match status { + ProbeStatus::Ack => write!(&mut ret, "{:02x} ", addr).unwrap(), + ProbeStatus::Nack => write!(&mut ret, "-- ").unwrap(), + }; + } + } + writeln!(&mut ret).unwrap(); } - - // note: calibration data is Big Endian, hence the from_be_bytes - let cal = 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 let Err(e) = i2c.i2c_write(BMP180_ADDR, REG_CTRL, &[CMD_READ_TEMP]) { - write!(ret, "Failed to start temp measurement: {:?}\n", e).unwrap(); - return Ok(Some(ret)); - } - - sleep(Duration::from_millis(5)); - - let mut temp_buffer = [0u8; 2]; - if let Err(e) = i2c.i2c_read(BMP180_ADDR, REG_DATA_START, &mut temp_buffer, true) { - write!(ret, "Failed to read temp data: {:?}\n", e).unwrap(); - return Ok(Some(ret)); - } - let ut = i16::from_be_bytes(temp_buffer) as i32; - - 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; - - write!(ret, "BMP180 Temperature: {:.1}°C\n", temp).unwrap(); } + + _ => { write!(ret, "{}\n", helpstring).unwrap(); } diff --git a/services/bao-console/src/main.rs b/services/bao-console/src/main.rs index 40b635705..4f40e6079 100644 --- a/services/bao-console/src/main.rs +++ b/services/bao-console/src/main.rs @@ -110,11 +110,11 @@ fn main() { #[cfg(feature = "battery-readout")] { - use cramium_api::I2cApi; - let mut i2c = cram_hal_service::I2c::new(); - use cramium_hal::axp2101::*; - let measurements = [("VBAT", REG_VBAT_H), ("VBUS", REG_VBUS_H), ("VSYS", REG_VSYS_H)]; - let mut buf = [0u8, 0u8]; + // use cramium_api::I2cApi; + // let mut i2c = cram_hal_service::I2c::new(); + // use cramium_hal::axp2101::*; + // let measurements = [("VBAT", REG_VBAT_H), ("VBUS", REG_VBUS_H), ("VSYS", REG_VSYS_H)]; + // let mut buf = [0u8, 0u8]; loop { tt.sleep_ms(2_000).ok(); // for (name, offset) in measurements {