Skip to content

Commit 0e1cebf

Browse files
authored
Merge pull request #656 from betrusted-io/sam.hello-world
adding bao-console functions (`i2cdetect` and `cute`)
2 parents 5108239 + 7d23064 commit 0e1cebf

File tree

5 files changed

+215
-7
lines changed

5 files changed

+215
-7
lines changed

Cargo.lock

Lines changed: 11 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

services/bao-console/src/cmds.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ use usb::*;
9191
mod pddb;
9292
#[cfg(feature = "with-pddb")]
9393
use pddb::*;
94+
mod i2cdetect;
95+
use i2cdetect::*;
96+
mod cute;
97+
use cute::*;
9498

9599
pub struct CmdEnv {
96100
common_env: CommonEnv,
@@ -138,11 +142,16 @@ impl CmdEnv {
138142
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)
139143
let mut ver_cmd = Ver {};
140144
let mut console_cmd = Test {};
145+
let mut i2cdetect_cmd = I2cDetect {};
146+
let mut cute_cmd = Cute {};
147+
141148
let commands: &mut [&mut dyn ShellCmdApi] = &mut [
142149
///// 4. add your command to this array, so that it can be looked up and dispatched
143150
&mut echo_cmd,
144151
&mut ver_cmd,
145152
&mut self.trng_cmd,
153+
&mut i2cdetect_cmd,
154+
&mut cute_cmd,
146155
&mut console_cmd,
147156
#[cfg(feature = "usb")]
148157
&mut self.usb,

services/bao-console/src/cmds/cute.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
use String;
2+
3+
use crate::{CommonEnv, ShellCmdApi};
4+
5+
#[derive(Debug)]
6+
pub struct Cute {}
7+
8+
impl<'a> ShellCmdApi<'a> for Cute {
9+
cmd_api!(cute);
10+
11+
fn process(&mut self, args: String, _env: &mut CommonEnv) -> Result<Option<String>, xous::Error> {
12+
use core::fmt::Write;
13+
let mut ret = String::new();
14+
let helpstring = "Usage:
15+
cute cat - prints cat ascii art
16+
cute bread - prints bread ascii art
17+
cute breadcat - prints breadcat ascii art";
18+
19+
let mut tokens = args.split_whitespace();
20+
21+
if let Some(sub_cmd) = tokens.next() {
22+
match sub_cmd {
23+
"cat" => {
24+
let cat_art = r#"
25+
⠀⠀⠀⠀⢀⠠⠤⠀⢀⣿⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
26+
⠀⠀⠐⠀⠐⠀⠀⢀⣾⣿⡇⠀⠀⠀⠀⠀⢀⣼⡇⠀⠀⠀⠀
27+
⠀⠀⠀⠀⠀⠀⠀⣸⣿⣿⣿⠀⠀⠀⠀⣴⣿⣿⠇⠀⠀⠀⠀
28+
⠀⠀⠀⠀⠀⠀⢠⣿⣿⣿⣇⠀⠀⢀⣾⣿⣿⣿⠀⠀⠀⠀⠀
29+
⠀⠀⠀⠀⠀⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡟⠀⠀⠐⠀⡀
30+
⠀⠀⠀⠀⢰⡿⠉⠀⡜⣿⣿⣿⡿⠿⢿⣿⣿⡃⠀⠀⠂⠄⠀
31+
⠀⠀⠒⠒⠸⣿⣄⡘⣃⣿⣿⡟⢰⠃⠀⢹⣿⡇⠀⠀⠀⠀⠀
32+
⠀⠀⠚⠉⠀⠊⠻⣿⣿⣿⣿⣿⣮⣤⣤⣿⡟⠁⠘⠠⠁⠀⠀
33+
⠀⠀⠀⠀⠀⠠⠀⠀⠈⠙⠛⠛⠛⠛⠛⠁⠀⠒⠤⠀⠀⠀⠀
34+
⠨⠠⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠑⠀⠀⠀⠀⠀⠀
35+
⠁⠃⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
36+
"#;
37+
write!(ret, "{}", cat_art).unwrap();
38+
}
39+
"bread" => {
40+
let bread_art = r#"
41+
⠀⠀⠀⠀⣀⣠⣤⣤⣤⣤⣤⣄⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
42+
⠀⠀⣴⣿⣿⠿⠛⣉⣁⣠⣤⣤⣤⣬⣀⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
43+
⠀⢸⣿⣿⣿⣿⣿⣿⡿⠛⣉⣁⣠⣤⣤⣤⣬⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
44+
⠀⢸⣿⣿⣿⣿⣿⣵⣾⣿⡿⠟⠛⠉⣉⣉⣉⣉⠉⠓⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
45+
⠀⢸⣿⣿⣿⣿⣿⣿⡟⢁⣤⣶⣿⣿⣿⣿⣿⣿⣿⣿⣶⣄⠀⠀⠀⠀⠀⠀⠀⠀
46+
⠀⢸⣿⣿⣿⣿⣿⣿⠀⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⠀⠀⠀⠀⠀⠀⠀
47+
⠀⢿⣿⣿⣿⣿⣿⣿⡄⢹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀
48+
⠀⠈⠻⣿⣿⣿⣿⣿⣷⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠀⠀⠀⠀⠀⠀⠀⠀
49+
⠀⠀⠀⠈⠻⣿⣿⣿⣿⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀
50+
⠀⠀⠀⠀⠀⠙⢿⣿⠇⢰⣿⣿⣿⣿⡿⠛⢉⣩⣉⡉⠛⠻⠇⠀⠀⠀⠀⠀⠀⠀
51+
⠀⠀⠀⠀⠀⠀⠀⠙⠀⠸⠿⠿⠟⢉⣠⣾⣿⣿⣿⣿⣷⣶⣦⣤⣄⣀⣀⡀⠀⠀
52+
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡷⠀
53+
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢈⣉⣉⠉⠛⠛⠻⠿⣿⣿⣿⣿⣿⣿⣿⠿⠋⣀⠀
54+
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠛⠛⠛⠛⠻⠶⣶⣤⣈⣉⣉⣉⣁⣤⣴⠾⠛⠀
55+
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠙⠛⠉⠉⠉⠀⠀⠀⠀
56+
"#;
57+
write!(ret, "{}", bread_art).unwrap();
58+
}
59+
"breadcat" => {
60+
let breadcat_art = r#"
61+
⠀⠀⠀⠰⣦⣤⣤⣀⠀⠀⡀⠀⠀⠀⠀⣀⣀⠀⠠⢤⣶⣶⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
62+
⠀⠀⠀⠀⢿⣿⢋⣵⣾⣿⣿⡿⣥⣮⢻⣿⣿⣿⣿⣶⣌⢿⣿⢰⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣦⣤⡀⠀⠀⠀⠀⠀⠀⠀⠀
63+
⠀⠀⠀⠀⠘⢡⣿⣿⣿⣿⣿⣷⡹⠟⣵⣿⣿⣿⣿⣿⣿⣦⢃⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀
64+
⠀⠀⠀⠀⠀⢺⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢨⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀
65+
⠀⠀⠀⠀⠀⠸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⣿⣿⣿⡟⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀
66+
⠀⠀⠀⠀⠀⠀⠙⣿⣷⣬⣿⣿⣿⣿⣿⣿⣿⡼⣿⣿⡟⣱⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀
67+
⠀⠀⠀⠀⠀⠀⠀⣿⣧⣠⣿⣿⣿⣿⣿⣿⣿⣀⣾⣿⡇⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡀⠀⠀⠀⠀⠀⠀⠀
68+
⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣅⣽⣿⣿⣿⣿⣿⣿⡇⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⠀⠀⠀⠀⠀⠀
69+
⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡀⠀⠀⠀⠀
70+
⠀⠀⠀⠀⠀⠀⠀⠛⠙⠋⠋⠋⠋⠋⠋⠋⠋⠛⠙⠙⠁⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⢿⣿⣿⣿⣿⣿⣧⠀⠀⠀⠀
71+
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢈⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀
72+
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠⣴⣶⣉⢿⣿⣿⣿⡟⠀⠀⠀⠀
73+
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣤⣆⡻⣿⣷⠜⣿⣏⣾⣿⣿⡿⠁⠀⠀⠀⠀
74+
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⣿⣿⣿⣇⣿⣿⣽⣿⣿⣿⠿⠋⠀⠀⠀⠀⠀⠀
75+
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢿⣿⣿⣿⣿⣿⠿⠟⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀
76+
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠉⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
77+
"#;
78+
write!(ret, "{}", breadcat_art).unwrap();
79+
}
80+
_ => {
81+
write!(ret, "{}\n", helpstring).unwrap();
82+
}
83+
}
84+
} else {
85+
write!(ret, "{}\n", helpstring).unwrap();
86+
}
87+
88+
Ok(Some(ret))
89+
}
90+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
use String;
2+
use cram_hal_service::I2c;
3+
use cramium_api::I2cApi;
4+
use cramium_api::I2cResult;
5+
6+
use crate::{CommonEnv, ShellCmdApi};
7+
8+
#[derive(Debug)]
9+
10+
enum ProbeStatus {
11+
Ack,
12+
Nack,
13+
}
14+
15+
fn probe_address(i2c: &mut I2c, addr: u8) -> ProbeStatus {
16+
let mut buf = [0u8; 1];
17+
match i2c.i2c_read(addr, 0x00, &mut buf, false) {
18+
Ok(I2cResult::Ack(_)) => ProbeStatus::Ack,
19+
Ok(I2cResult::Nack) => ProbeStatus::Nack,
20+
Ok(I2cResult::Pending) => panic!("Unexpected I2cResult::Pending: this operation should be blocking."),
21+
Ok(I2cResult::InternalError) => panic!("Unexpected Ok(I2cResult::InternalError)."),
22+
Err(e) => panic!("I2C bus error while probing address 0x{:02X}: {:?}", addr, e),
23+
}
24+
}
25+
26+
pub struct I2cDetect {}
27+
28+
impl<'a> ShellCmdApi<'a> for I2cDetect {
29+
cmd_api!(i2cdetect);
30+
31+
fn process(&mut self, args: String, _env: &mut CommonEnv) -> Result<Option<String>, xous::Error> {
32+
use core::fmt::Write;
33+
let mut ret = String::new();
34+
let helpstring = "Usage:
35+
i2cdetect probe <hex> - probes a single I2C address (e.g. 34)
36+
i2cdetect scan - reads all addresses on the bus (use with caution!)";
37+
38+
let mut tokens = args.split_whitespace();
39+
40+
if let Some(sub_cmd) = tokens.next() {
41+
match sub_cmd {
42+
"probe" => {
43+
if let Some(addr_str) = tokens.next() {
44+
match u8::from_str_radix(addr_str, 16) {
45+
Ok(addr) => {
46+
let mut i2c = I2c::new();
47+
let status = probe_address(&mut i2c, addr);
48+
match status {
49+
ProbeStatus::Ack => {
50+
write!(ret, "Device found at address {:02X}\n", addr).unwrap()
51+
}
52+
ProbeStatus::Nack => {
53+
write!(ret, "Device at address {:02X} responded with NACK\n", addr)
54+
.unwrap()
55+
}
56+
};
57+
}
58+
Err(_) => {
59+
write!(
60+
ret,
61+
"Invalid hex address '{}'. Example: i2cdetect probe 3C\n",
62+
addr_str
63+
)
64+
.unwrap();
65+
}
66+
}
67+
} else {
68+
write!(ret, "Usage: i2cdetect probe <hexaddr>\n").unwrap();
69+
}
70+
}
71+
72+
"scan" => {
73+
let mut i2c = I2c::new();
74+
writeln!(&mut ret, " 0 1 2 3 4 5 6 7 8 9 a b c d e f").unwrap();
75+
for row in 0..8 {
76+
write!(&mut ret, "{:02x}: ", row * 16).unwrap();
77+
for col in 0..16 {
78+
let addr = (row * 16 + col) as u8;
79+
if addr < 0x03 || addr > 0x77 {
80+
write!(&mut ret, " ").unwrap();
81+
} else {
82+
let status = probe_address(&mut i2c, addr);
83+
match status {
84+
ProbeStatus::Ack => write!(&mut ret, "{:02x} ", addr).unwrap(),
85+
ProbeStatus::Nack => write!(&mut ret, "-- ").unwrap(),
86+
};
87+
}
88+
}
89+
writeln!(&mut ret).unwrap();
90+
}
91+
}
92+
93+
_ => {
94+
write!(ret, "{}\n", helpstring).unwrap();
95+
}
96+
}
97+
} else {
98+
write!(ret, "{}\n", helpstring).unwrap();
99+
}
100+
101+
Ok(Some(ret))
102+
}
103+
}

xtask/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
612612
// - [planned] vault application
613613
let bao_rram_pkgs =
614614
["xous-ticktimer", "xous-log", "xous-names", "keystore" /* "usb-cramium" */].to_vec(); /* "usb-cramium" */
615-
let bao_swap_pkgs = ["cram-hal-service", "bao-console", "modals", "pddb", "bao-video"].to_vec(); /* "bao-video" */
615+
let bao_swap_pkgs =
616+
["cram-hal-service", "bao-console", /* "modals", "pddb", */ "bao-video"].to_vec(); /* "bao-video" */
616617
if !builder.is_swap_set() {
617618
builder.set_swap(0, 8 * 1024 * 1024);
618619
}

0 commit comments

Comments
 (0)