diff options
| author | James Munns <[email protected]> | 2025-11-14 19:03:26 +0100 |
|---|---|---|
| committer | James Munns <[email protected]> | 2025-11-14 19:03:26 +0100 |
| commit | 0bae6aa5aaab5d0f3a3e7e1ec83a0cee909de115 (patch) | |
| tree | 1740876a3af2f06bcc129b62e3a6e87f4472d5f3 /examples/src/bin/hello.rs | |
| parent | 8cdccae3c6c4a805cf5003b1a859734c105d76e8 (diff) | |
| parent | 77b2c602a60e41c7c977003a6d40367ac285930e (diff) | |
Merge remote-tracking branch 'origin/main' into james/impl-clocks
Diffstat (limited to 'examples/src/bin/hello.rs')
| -rw-r--r-- | examples/src/bin/hello.rs | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/examples/src/bin/hello.rs b/examples/src/bin/hello.rs new file mode 100644 index 000000000..ff7afa01d --- /dev/null +++ b/examples/src/bin/hello.rs | |||
| @@ -0,0 +1,120 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use embassy_executor::Spawner; | ||
| 5 | use hal::lpuart::{Blocking, Config, Lpuart}; | ||
| 6 | use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _}; | ||
| 7 | |||
| 8 | /// Simple helper to write a byte as hex to UART | ||
| 9 | fn write_hex_byte(uart: &mut Lpuart<'_, Blocking>, byte: u8) { | ||
| 10 | const HEX_DIGITS: &[u8] = b"0123456789ABCDEF"; | ||
| 11 | uart.write_byte(HEX_DIGITS[(byte >> 4) as usize]); | ||
| 12 | uart.write_byte(HEX_DIGITS[(byte & 0xF) as usize]); | ||
| 13 | } | ||
| 14 | |||
| 15 | #[embassy_executor::main] | ||
| 16 | async fn main(_spawner: Spawner) { | ||
| 17 | let p = hal::init(hal::config::Config::default()); | ||
| 18 | |||
| 19 | defmt::info!("boot"); | ||
| 20 | |||
| 21 | // Create UART configuration | ||
| 22 | let config = Config { | ||
| 23 | baudrate_bps: 115_200, | ||
| 24 | enable_tx: true, | ||
| 25 | enable_rx: true, | ||
| 26 | ..Default::default() | ||
| 27 | }; | ||
| 28 | |||
| 29 | // Create UART instance using LPUART2 with PIO2_2 as TX and PIO2_3 as RX | ||
| 30 | unsafe { | ||
| 31 | embassy_mcxa_examples::init_uart2(hal::pac()); | ||
| 32 | } | ||
| 33 | let mut uart = Lpuart::new_blocking( | ||
| 34 | p.LPUART2, // Peripheral | ||
| 35 | p.PIO2_2, // TX pin | ||
| 36 | p.PIO2_3, // RX pin | ||
| 37 | config, | ||
| 38 | ) | ||
| 39 | .unwrap(); | ||
| 40 | |||
| 41 | // Print welcome message before any async delays to guarantee early console output | ||
| 42 | uart.write_str_blocking("\r\n=== MCXA276 UART Echo Demo ===\r\n"); | ||
| 43 | uart.write_str_blocking("Available commands:\r\n"); | ||
| 44 | uart.write_str_blocking(" help - Show this help\r\n"); | ||
| 45 | uart.write_str_blocking(" echo <text> - Echo back the text\r\n"); | ||
| 46 | uart.write_str_blocking(" hex <byte> - Display byte in hex (0-255)\r\n"); | ||
| 47 | uart.write_str_blocking("Type a command: "); | ||
| 48 | |||
| 49 | let mut buffer = [0u8; 64]; | ||
| 50 | let mut buf_idx = 0; | ||
| 51 | |||
| 52 | loop { | ||
| 53 | // Read a byte from UART | ||
| 54 | let byte = uart.read_byte_blocking(); | ||
| 55 | |||
| 56 | // Echo the character back | ||
| 57 | if byte == b'\r' || byte == b'\n' { | ||
| 58 | // Enter pressed - process command | ||
| 59 | uart.write_str_blocking("\r\n"); | ||
| 60 | |||
| 61 | if buf_idx > 0 { | ||
| 62 | let command = &buffer[0..buf_idx]; | ||
| 63 | |||
| 64 | if command == b"help" { | ||
| 65 | uart.write_str_blocking("Available commands:\r\n"); | ||
| 66 | uart.write_str_blocking(" help - Show this help\r\n"); | ||
| 67 | uart.write_str_blocking(" echo <text> - Echo back the text\r\n"); | ||
| 68 | uart.write_str_blocking(" hex <byte> - Display byte in hex (0-255)\r\n"); | ||
| 69 | } else if command.starts_with(b"echo ") && command.len() > 5 { | ||
| 70 | uart.write_str_blocking("Echo: "); | ||
| 71 | uart.write_str_blocking(core::str::from_utf8(&command[5..]).unwrap_or("")); | ||
| 72 | uart.write_str_blocking("\r\n"); | ||
| 73 | } else if command.starts_with(b"hex ") && command.len() > 4 { | ||
| 74 | // Parse the byte value | ||
| 75 | let num_str = &command[4..]; | ||
| 76 | if let Ok(num) = parse_u8(num_str) { | ||
| 77 | uart.write_str_blocking("Hex: 0x"); | ||
| 78 | write_hex_byte(&mut uart, num); | ||
| 79 | uart.write_str_blocking("\r\n"); | ||
| 80 | } else { | ||
| 81 | uart.write_str_blocking("Invalid number for hex command\r\n"); | ||
| 82 | } | ||
| 83 | } else if !command.is_empty() { | ||
| 84 | uart.write_str_blocking("Unknown command: "); | ||
| 85 | uart.write_str_blocking(core::str::from_utf8(command).unwrap_or("")); | ||
| 86 | uart.write_str_blocking("\r\n"); | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | // Reset buffer and prompt | ||
| 91 | buf_idx = 0; | ||
| 92 | uart.write_str_blocking("Type a command: "); | ||
| 93 | } else if byte == 8 || byte == 127 { | ||
| 94 | // Backspace | ||
| 95 | if buf_idx > 0 { | ||
| 96 | buf_idx -= 1; | ||
| 97 | uart.write_str_blocking("\x08 \x08"); // Erase character | ||
| 98 | } | ||
| 99 | } else if buf_idx < buffer.len() - 1 { | ||
| 100 | // Regular character | ||
| 101 | buffer[buf_idx] = byte; | ||
| 102 | buf_idx += 1; | ||
| 103 | uart.write_byte(byte); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | /// Simple parser for u8 from ASCII bytes | ||
| 109 | fn parse_u8(bytes: &[u8]) -> Result<u8, ()> { | ||
| 110 | let mut result = 0u8; | ||
| 111 | for &b in bytes { | ||
| 112 | if b.is_ascii_digit() { | ||
| 113 | result = result.checked_mul(10).ok_or(())?; | ||
| 114 | result = result.checked_add(b - b'0').ok_or(())?; | ||
| 115 | } else { | ||
| 116 | return Err(()); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | Ok(result) | ||
| 120 | } | ||
