Merge pull request #656 from betrusted-io/sam.hello-world

adding bao-console functions (`i2cdetect` and `cute`)
This commit is contained in:
bunnie 2025-06-09 18:52:28 +08:00 committed by GitHub
commit 0e1cebf329
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 215 additions and 7 deletions

17
Cargo.lock generated
View file

@ -1308,26 +1308,25 @@ dependencies = [
[[package]]
name = "curve25519-dalek"
version = "4.1.2"
source = "git+https://github.com/betrusted-io/curve25519-dalek.git?branch=main#bef542a781348054c32a9beaf6bd1195814cace2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a677b8922c94e01bdbb12126b0bc852f00447528dee1782229af9c720c3f348"
dependencies = [
"cfg-if",
"cpufeatures",
"curve25519-dalek-derive",
"digest 0.10.7",
"engine25519-as",
"fiat-crypto 0.2.7",
"log",
"platforms",
"rustc_version 0.4.0",
"subtle",
"utralib 0.1.25",
"xous 0.9.66 (registry+https://github.com/rust-lang/crates.io-index)",
"zeroize",
]
[[package]]
name = "curve25519-dalek-derive"
version = "0.1.1"
source = "git+https://github.com/betrusted-io/curve25519-dalek.git?branch=main#bef542a781348054c32a9beaf6bd1195814cace2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3"
dependencies = [
"proc-macro2",
"quote",
@ -4075,6 +4074,12 @@ version = "0.3.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2900ede94e305130c13ddd391e0ab7cbaeb783945ae07a279c268cb05109c6cb"
[[package]]
name = "platforms"
version = "3.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b02ffed1bc8c2234bb6f8e760e34613776c5102a041f25330b869a78153a68c"
[[package]]
name = "plotters"
version = "0.3.5"

View file

@ -91,6 +91,10 @@ use usb::*;
mod pddb;
#[cfg(feature = "with-pddb")]
use pddb::*;
mod i2cdetect;
use i2cdetect::*;
mod cute;
use cute::*;
pub struct CmdEnv {
common_env: CommonEnv,
@ -138,11 +142,16 @@ impl CmdEnv {
let mut echo_cmd = Echo {}; // this command has no persistent storage, so we can "create" it every time we call dispatch (but it's a zero-cost absraction so this doesn't actually create any instructions)
let mut ver_cmd = Ver {};
let mut console_cmd = Test {};
let mut i2cdetect_cmd = I2cDetect {};
let mut cute_cmd = Cute {};
let commands: &mut [&mut dyn ShellCmdApi] = &mut [
///// 4. add your command to this array, so that it can be looked up and dispatched
&mut echo_cmd,
&mut ver_cmd,
&mut self.trng_cmd,
&mut i2cdetect_cmd,
&mut cute_cmd,
&mut console_cmd,
#[cfg(feature = "usb")]
&mut self.usb,

View file

@ -0,0 +1,90 @@
use String;
use crate::{CommonEnv, ShellCmdApi};
#[derive(Debug)]
pub struct Cute {}
impl<'a> ShellCmdApi<'a> for Cute {
cmd_api!(cute);
fn process(&mut self, args: String, _env: &mut CommonEnv) -> Result<Option<String>, xous::Error> {
use core::fmt::Write;
let mut ret = String::new();
let helpstring = "Usage:
cute cat - prints cat ascii art
cute bread - prints bread ascii art
cute breadcat - prints breadcat ascii art";
let mut tokens = args.split_whitespace();
if let Some(sub_cmd) = tokens.next() {
match sub_cmd {
"cat" => {
let cat_art = r#"
⠀⠀⠀⠀⢀⠠⠤⠀⢀⣿⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠐⠀⠐⠀⠀⢀⣾⣿⡇⠀⠀⠀⠀⠀⢀⣼⡇⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣸⣿⣿⣿⠀⠀⠀⠀⣴⣿⣿⠇⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⢠⣿⣿⣿⣇⠀⠀⢀⣾⣿⣿⣿⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡟⠀⠀⠐⠀⡀
⠀⠀⠀⠀⢰⡿⠉⠀⡜⣿⣿⣿⡿⠿⢿⣿⣿⡃⠀⠀⠂⠄⠀
⠀⠀⠒⠒⠸⣿⣄⡘⣃⣿⣿⡟⢰⠃⠀⢹⣿⡇⠀⠀⠀⠀⠀
⠀⠀⠚⠉⠀⠊⠻⣿⣿⣿⣿⣿⣮⣤⣤⣿⡟⠁⠘⠠⠁⠀⠀
⠀⠀⠀⠀⠀⠠⠀⠀⠈⠙⠛⠛⠛⠛⠛⠁⠀⠒⠤⠀⠀⠀⠀
⠨⠠⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠑⠀⠀⠀⠀⠀⠀
⠁⠃⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
"#;
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();
}
_ => {
write!(ret, "{}\n", helpstring).unwrap();
}
}
} else {
write!(ret, "{}\n", helpstring).unwrap();
}
Ok(Some(ret))
}
}

View file

@ -0,0 +1,103 @@
use String;
use cram_hal_service::I2c;
use cramium_api::I2cApi;
use cramium_api::I2cResult;
use crate::{CommonEnv, ShellCmdApi};
#[derive(Debug)]
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);
fn process(&mut self, args: String, _env: &mut CommonEnv) -> Result<Option<String>, xous::Error> {
use core::fmt::Write;
let mut ret = String::new();
let helpstring = "Usage:
i2cdetect probe <hex> - probes a single I2C address (e.g. 34)
i2cdetect scan - reads all addresses on the bus (use with caution!)";
let mut tokens = args.split_whitespace();
if let Some(sub_cmd) = tokens.next() {
match sub_cmd {
"probe" => {
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();
}
}
} else {
write!(ret, "Usage: i2cdetect probe <hexaddr>\n").unwrap();
}
}
"scan" => {
let mut i2c = I2c::new();
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();
}
}
_ => {
write!(ret, "{}\n", helpstring).unwrap();
}
}
} else {
write!(ret, "{}\n", helpstring).unwrap();
}
Ok(Some(ret))
}
}

View file

@ -612,7 +612,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// - [planned] vault application
let bao_rram_pkgs =
["xous-ticktimer", "xous-log", "xous-names", "keystore" /* "usb-cramium" */].to_vec(); /* "usb-cramium" */
let bao_swap_pkgs = ["cram-hal-service", "bao-console", "modals", "pddb", "bao-video"].to_vec(); /* "bao-video" */
let bao_swap_pkgs =
["cram-hal-service", "bao-console", /* "modals", "pddb", */ "bao-video"].to_vec(); /* "bao-video" */
if !builder.is_swap_set() {
builder.set_swap(0, 8 * 1024 * 1024);
}