aboutsummaryrefslogtreecommitdiff
path: root/examples/src/bin/hello.rs
diff options
context:
space:
mode:
authorFelipe Balbi <[email protected]>2025-11-13 13:17:44 -0800
committerGitHub <[email protected]>2025-11-13 13:17:44 -0800
commit77b2c602a60e41c7c977003a6d40367ac285930e (patch)
tree6a6490c883f84658c992af4351b8d8a8d8e1c1e4 /examples/src/bin/hello.rs
parentf4b8ae36bec40a15bedd3c0493e4822f9c5238dd (diff)
Move examples to a package of their own (#16)
* Move examples to a package of their own * cargo +nightly fmt * Add missing safety doc * cargo clippy examples * fmt again --------- Co-authored-by: Felipe Balbi <[email protected]>
Diffstat (limited to 'examples/src/bin/hello.rs')
-rw-r--r--examples/src/bin/hello.rs111
1 files changed, 111 insertions, 0 deletions
diff --git a/examples/src/bin/hello.rs b/examples/src/bin/hello.rs
new file mode 100644
index 000000000..5c4336d50
--- /dev/null
+++ b/examples/src/bin/hello.rs
@@ -0,0 +1,111 @@
1#![no_std]
2#![no_main]
3
4use embassy_executor::Spawner;
5use embassy_mcxa_examples::init_uart2;
6use hal::uart;
7use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _};
8
9/// Simple helper to write a byte as hex to UART
10fn write_hex_byte(uart: &hal::uart::Uart<hal::uart::Lpuart2>, byte: u8) {
11 const HEX_DIGITS: &[u8] = b"0123456789ABCDEF";
12 uart.write_byte(HEX_DIGITS[(byte >> 4) as usize]);
13 uart.write_byte(HEX_DIGITS[(byte & 0xF) as usize]);
14}
15
16#[embassy_executor::main]
17async fn main(_spawner: Spawner) {
18 let p = hal::init(hal::config::Config::default());
19
20 defmt::info!("boot");
21
22 // Board-level init for UART2 clocks and pins.
23 unsafe {
24 init_uart2(hal::pac());
25 }
26
27 // Get UART source frequency from clock configuration
28 // Using hardcoded frequency for now - dynamic detection may have issues
29 let src = 12_000_000; // FRO_LF_DIV at 12MHz with DIV=0
30 let uart = uart::Uart::<uart::Lpuart2>::new(p.LPUART2, uart::Config::new(src));
31
32 // Print welcome message before any async delays to guarantee early console output
33 uart.write_str_blocking("\r\n=== MCXA276 UART Echo Demo ===\r\n");
34 uart.write_str_blocking("Available commands:\r\n");
35 uart.write_str_blocking(" help - Show this help\r\n");
36 uart.write_str_blocking(" echo <text> - Echo back the text\r\n");
37 uart.write_str_blocking(" hex <byte> - Display byte in hex (0-255)\r\n");
38 uart.write_str_blocking("Type a command: ");
39
40 let mut buffer = [0u8; 64];
41 let mut buf_idx = 0;
42
43 loop {
44 // Read a byte from UART
45 let byte = uart.read_byte_blocking();
46
47 // Echo the character back
48 if byte == b'\r' || byte == b'\n' {
49 // Enter pressed - process command
50 uart.write_str_blocking("\r\n");
51
52 if buf_idx > 0 {
53 let command = &buffer[0..buf_idx];
54
55 if command == b"help" {
56 uart.write_str_blocking("Available commands:\r\n");
57 uart.write_str_blocking(" help - Show this help\r\n");
58 uart.write_str_blocking(" echo <text> - Echo back the text\r\n");
59 uart.write_str_blocking(" hex <byte> - Display byte in hex (0-255)\r\n");
60 } else if command.starts_with(b"echo ") && command.len() > 5 {
61 uart.write_str_blocking("Echo: ");
62 uart.write_str_blocking(core::str::from_utf8(&command[5..]).unwrap_or(""));
63 uart.write_str_blocking("\r\n");
64 } else if command.starts_with(b"hex ") && command.len() > 4 {
65 // Parse the byte value
66 let num_str = &command[4..];
67 if let Ok(num) = parse_u8(num_str) {
68 uart.write_str_blocking("Hex: 0x");
69 write_hex_byte(&uart, num);
70 uart.write_str_blocking("\r\n");
71 } else {
72 uart.write_str_blocking("Invalid number for hex command\r\n");
73 }
74 } else if !command.is_empty() {
75 uart.write_str_blocking("Unknown command: ");
76 uart.write_str_blocking(core::str::from_utf8(command).unwrap_or(""));
77 uart.write_str_blocking("\r\n");
78 }
79 }
80
81 // Reset buffer and prompt
82 buf_idx = 0;
83 uart.write_str_blocking("Type a command: ");
84 } else if byte == 8 || byte == 127 {
85 // Backspace
86 if buf_idx > 0 {
87 buf_idx -= 1;
88 uart.write_str_blocking("\x08 \x08"); // Erase character
89 }
90 } else if buf_idx < buffer.len() - 1 {
91 // Regular character
92 buffer[buf_idx] = byte;
93 buf_idx += 1;
94 uart.write_byte(byte);
95 }
96 }
97}
98
99/// Simple parser for u8 from ASCII bytes
100fn parse_u8(bytes: &[u8]) -> Result<u8, ()> {
101 let mut result = 0u8;
102 for &b in bytes {
103 if b.is_ascii_digit() {
104 result = result.checked_mul(10).ok_or(())?;
105 result = result.checked_add(b - b'0').ok_or(())?;
106 } else {
107 return Err(());
108 }
109 }
110 Ok(result)
111}