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