aboutsummaryrefslogtreecommitdiff
path: root/examples/src/bin/ostimer_async.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/ostimer_async.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/ostimer_async.rs')
-rw-r--r--examples/src/bin/ostimer_async.rs52
1 files changed, 52 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..34862b61f
--- /dev/null
+++ b/examples/src/bin/ostimer_async.rs
@@ -0,0 +1,52 @@
1#![no_std]
2#![no_main]
3
4use embassy_executor::Spawner;
5use embassy_mcxa::bind_interrupts;
6use embassy_mcxa_examples::{init_ostimer0, init_uart2};
7use embassy_time::{Duration, Timer};
8use hal::uart;
9use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _};
10
11// Bind only OS_EVENT, and retain the symbol explicitly so it can’t be GC’ed.
12bind_interrupts!(struct Irqs {
13 OS_EVENT => hal::ostimer::time_driver::OsEventHandler;
14});
15
16#[used]
17#[no_mangle]
18static KEEP_OS_EVENT: unsafe extern "C" fn() = OS_EVENT;
19
20#[embassy_executor::main]
21async fn main(_spawner: Spawner) {
22 let _p = hal::init(hal::config::Config::default());
23
24 // Enable/clock OSTIMER0 and UART2 before touching their registers
25 unsafe {
26 init_ostimer0(hal::pac());
27 init_uart2(hal::pac());
28 }
29 let src = unsafe { hal::clocks::uart2_src_hz(hal::pac()) };
30 let uart = uart::Uart::<uart::Lpuart2>::new(_p.LPUART2, uart::Config::new(src));
31 uart.write_str_blocking("boot\n");
32
33 // Avoid mass NVIC writes here; DefaultHandler now safely returns.
34
35 // Initialize embassy-time global driver backed by OSTIMER0 (re-enables OS_EVENT with priority)
36 // The bind_interrupts! macro handles handler binding automatically
37
38 // Initialize OSTIMER with default 1MHz frequency
39 // Adjust this value to match your actual OSTIMER clock frequency
40 hal::ostimer::time_driver::init(hal::config::Config::default().time_interrupt_priority, 1_000_000);
41
42 // Removed force-pend; rely on real hardware match to trigger OS_EVENT.
43
44 // Log using defmt if enabled
45 defmt::info!("OSTIMER async example starting...");
46
47 loop {
48 defmt::info!("tick");
49 uart.write_str_blocking("tick\n");
50 Timer::after(Duration::from_millis(1000)).await;
51 }
52}