working read

This commit is contained in:
Sam Chin 2025-06-07 11:17:00 +08:00
parent b0b29f7d78
commit b35f9cad33

View file

@ -1,4 +1,8 @@
use String; use String;
use cramium_api::I2cApi;
use cram_hal_service::I2c;
use std::thread::sleep;
use std::time::Duration;
use crate::{CommonEnv, ShellCmdApi}; use crate::{CommonEnv, ShellCmdApi};
@ -14,14 +18,16 @@ impl<'a> ShellCmdApi<'a> for I2cDetect {
let helpstring = "Usage: let helpstring = "Usage:
i2cdetect cat - prints cat ascii art i2cdetect cat - prints cat ascii art
i2cdetect bread - prints bread ascii art i2cdetect bread - prints bread ascii art
i2cdetect breadcat - prints breadcat ascii art"; i2cdetect breadcat - prints breadcat ascii art
i2cdetect probe <hex> - probes a single I2C address (e.g. 34)
i2cdetect bmptemp - reads raw temperature from BMP180";
let mut tokens = args.split_whitespace(); let mut tokens = args.split_whitespace();
if let Some(sub_cmd) = tokens.next() { if let Some(sub_cmd) = tokens.next() {
match sub_cmd { match sub_cmd {
"cat" => { "cat" => {
let cat_art = r#" let cat_art = r#"f
⠀⠀⠀⠀⢀⠠⠤⠀⢀⣿⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⢀⠠⠤⠀⢀⣿⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠐⠀⠐⠀⠀⢀⣾⣿⡇⠀⠀⠀⠀⠀⢀⣼⡇⠀⠀⠀⠀ ⠀⠀⠐⠀⠐⠀⠀⢀⣾⣿⡇⠀⠀⠀⠀⠀⢀⣼⡇⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣸⣿⣿⣿⠀⠀⠀⠀⣴⣿⣿⠇⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⣸⣿⣿⣿⠀⠀⠀⠀⣴⣿⣿⠇⠀⠀⠀⠀
@ -77,6 +83,64 @@ impl<'a> ShellCmdApi<'a> for I2cDetect {
"#; "#;
write!(ret, "{}", breadcat_art).unwrap(); 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();
}
}
}
Err(_) => {
write!(ret, "Invalid hex address '{}'. Example: i2cdetect probe 3C\n", addr_str).unwrap();
}
}
} else {
write!(ret, "Usage: i2cdetect probe <hexaddr>\n").unwrap();
}
}
"bmptemp" => {
const BMP180_ADDR: u8 = 0x77;
const REG_CTRL: u8 = 0xF4;
const REG_DATA_START: u8 = 0xF6;
const CMD_READ_TEMP: u8 = 0x2E;
let mut i2c = I2c::new();
match i2c.i2c_write(BMP180_ADDR, REG_CTRL, &[CMD_READ_TEMP]) {
Ok(_) => {
sleep(Duration::from_millis(5));
let mut temp_buffer = [0u8; 2];
match i2c.i2c_read(BMP180_ADDR, REG_DATA_START, &mut temp_buffer, true) {
Ok(_) => {
let uncalibrated_temp = i16::from_be_bytes(temp_buffer);
write!(ret, "BMP180 raw temp: {}\n", uncalibrated_temp).unwrap();
}
Err(e) => {
write!(ret, "Failed to read temp data: {:?}\n", e).unwrap();
}
}
}
Err(e) => {
write!(ret, "Failed to write command to BMP180 (is it connected @ 0x77?): {:?}\n", e).unwrap();
}
}
}
_ => { _ => {
write!(ret, "{}\n", helpstring).unwrap(); write!(ret, "{}\n", helpstring).unwrap();
} }