diff options
Diffstat (limited to 'examples/src/bin/ostimer_async.rs')
| -rw-r--r-- | examples/src/bin/ostimer_async.rs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/examples/src/bin/ostimer_async.rs b/examples/src/bin/ostimer_async.rs new file mode 100644 index 000000000..4e692a744 --- /dev/null +++ b/examples/src/bin/ostimer_async.rs | |||
| @@ -0,0 +1,63 @@ | |||
| 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 embassy_time::{Duration, Timer}; | ||
| 8 | use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _}; | ||
| 9 | |||
| 10 | // Bind only OS_EVENT, and retain the symbol explicitly so it can’t be GC’ed. | ||
| 11 | bind_interrupts!(struct Irqs { | ||
| 12 | OS_EVENT => hal::ostimer::time_driver::OsEventHandler; | ||
| 13 | }); | ||
| 14 | |||
| 15 | #[used] | ||
| 16 | #[no_mangle] | ||
| 17 | static KEEP_OS_EVENT: unsafe extern "C" fn() = OS_EVENT; | ||
| 18 | |||
| 19 | #[embassy_executor::main] | ||
| 20 | async fn main(_spawner: Spawner) { | ||
| 21 | let p = hal::init(hal::config::Config::default()); | ||
| 22 | |||
| 23 | // Create UART configuration | ||
| 24 | let config = hal::lpuart::Config { | ||
| 25 | baudrate_bps: 115_200, | ||
| 26 | enable_tx: true, | ||
| 27 | enable_rx: true, | ||
| 28 | ..Default::default() | ||
| 29 | }; | ||
| 30 | |||
| 31 | // Create UART instance using LPUART2 with PIO2_2 as TX and PIO2_3 as RX | ||
| 32 | unsafe { | ||
| 33 | init_uart2(hal::pac()); | ||
| 34 | } | ||
| 35 | let mut uart = hal::lpuart::Lpuart::new_blocking( | ||
| 36 | p.LPUART2, // Peripheral | ||
| 37 | p.PIO2_2, // TX pin | ||
| 38 | p.PIO2_3, // RX pin | ||
| 39 | config, | ||
| 40 | ) | ||
| 41 | .unwrap(); | ||
| 42 | uart.blocking_write(b"boot\n").unwrap(); | ||
| 43 | |||
| 44 | // Avoid mass NVIC writes here; DefaultHandler now safely returns. | ||
| 45 | |||
| 46 | // Initialize embassy-time global driver backed by OSTIMER0 (re-enables OS_EVENT with priority) | ||
| 47 | // The bind_interrupts! macro handles handler binding automatically | ||
| 48 | |||
| 49 | // Initialize OSTIMER with default 1MHz frequency | ||
| 50 | // Adjust this value to match your actual OSTIMER clock frequency | ||
| 51 | hal::ostimer::time_driver::init(hal::config::Config::default().time_interrupt_priority, 1_000_000); | ||
| 52 | |||
| 53 | // Removed force-pend; rely on real hardware match to trigger OS_EVENT. | ||
| 54 | |||
| 55 | // Log using defmt if enabled | ||
| 56 | defmt::info!("OSTIMER async example starting..."); | ||
| 57 | |||
| 58 | loop { | ||
| 59 | defmt::info!("tick"); | ||
| 60 | uart.write_str_blocking("tick\n"); | ||
| 61 | Timer::after(Duration::from_millis(1000)).await; | ||
| 62 | } | ||
| 63 | } | ||
