diff options
| author | Felipe Balbi <[email protected]> | 2025-11-13 13:17:44 -0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-11-13 13:17:44 -0800 |
| commit | 77b2c602a60e41c7c977003a6d40367ac285930e (patch) | |
| tree | 6a6490c883f84658c992af4351b8d8a8d8e1c1e4 /examples/src/bin/uart_interrupt.rs | |
| parent | f4b8ae36bec40a15bedd3c0493e4822f9c5238dd (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/uart_interrupt.rs')
| -rw-r--r-- | examples/src/bin/uart_interrupt.rs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/examples/src/bin/uart_interrupt.rs b/examples/src/bin/uart_interrupt.rs new file mode 100644 index 000000000..100588727 --- /dev/null +++ b/examples/src/bin/uart_interrupt.rs | |||
| @@ -0,0 +1,66 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use embassy_executor::Spawner; | ||
| 5 | use embassy_mcxa::bind_interrupts; | ||
| 6 | use embassy_mcxa_examples::init_uart2; | ||
| 7 | use hal::interrupt::typelevel::Handler; | ||
| 8 | use hal::uart; | ||
| 9 | use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _}; | ||
| 10 | |||
| 11 | // Bind LPUART2 interrupt to our handler | ||
| 12 | bind_interrupts!(struct Irqs { | ||
| 13 | LPUART2 => hal::uart::UartInterruptHandler; | ||
| 14 | }); | ||
| 15 | |||
| 16 | #[used] | ||
| 17 | #[no_mangle] | ||
| 18 | static KEEP_LPUART2: unsafe extern "C" fn() = LPUART2; | ||
| 19 | |||
| 20 | // Wrapper function for the interrupt handler | ||
| 21 | unsafe extern "C" fn lpuart2_handler() { | ||
| 22 | hal::uart::UartInterruptHandler::on_interrupt(); | ||
| 23 | } | ||
| 24 | |||
| 25 | #[embassy_executor::main] | ||
| 26 | async fn main(_spawner: Spawner) { | ||
| 27 | let _p = hal::init(hal::config::Config::default()); | ||
| 28 | |||
| 29 | // Enable/clock UART2 before touching its registers | ||
| 30 | unsafe { | ||
| 31 | init_uart2(hal::pac()); | ||
| 32 | } | ||
| 33 | let src = unsafe { hal::clocks::uart2_src_hz(hal::pac()) }; | ||
| 34 | let uart = uart::Uart::<uart::Lpuart2>::new(_p.LPUART2, uart::Config::new(src)); | ||
| 35 | |||
| 36 | // Configure LPUART2 interrupt for UART operation BEFORE any UART usage | ||
| 37 | hal::interrupt::LPUART2.configure_for_uart(hal::interrupt::Priority::from(3)); | ||
| 38 | |||
| 39 | // Manually install the interrupt handler and enable RX IRQs in the peripheral | ||
| 40 | unsafe { | ||
| 41 | hal::interrupt::LPUART2.install_handler(lpuart2_handler); | ||
| 42 | // Enable RX interrupts so the handler actually fires on incoming bytes | ||
| 43 | uart.enable_rx_interrupts(); | ||
| 44 | } | ||
| 45 | |||
| 46 | // Print welcome message | ||
| 47 | uart.write_str_blocking("UART interrupt echo demo starting...\r\n"); | ||
| 48 | uart.write_str_blocking("Type characters to echo them back.\r\n"); | ||
| 49 | |||
| 50 | // Log using defmt if enabled | ||
| 51 | defmt::info!("UART interrupt echo demo starting..."); | ||
| 52 | |||
| 53 | loop { | ||
| 54 | // Check if we have received any data | ||
| 55 | if uart.rx_data_available() { | ||
| 56 | if let Some(byte) = uart.try_read_byte() { | ||
| 57 | // Echo it back | ||
| 58 | uart.write_byte(byte); | ||
| 59 | uart.write_str_blocking(" (received)\r\n"); | ||
| 60 | } | ||
| 61 | } else { | ||
| 62 | // No data available, wait a bit before checking again | ||
| 63 | cortex_m::asm::delay(12_000_000); // ~1 second at 12MHz | ||
| 64 | } | ||
| 65 | } | ||
| 66 | } | ||
