aboutsummaryrefslogtreecommitdiff
path: root/src/ostimer.rs
diff options
context:
space:
mode:
authorFelipe Balbi <[email protected]>2025-11-07 10:07:33 -0800
committerFelipe Balbi <[email protected]>2025-11-07 10:08:16 -0800
commite75066820ad320495ca70570641c90d75247b19b (patch)
treeda2aeddb9164dbc2829b54185d1f180efbad6daf /src/ostimer.rs
parentcb2ac2790f4b037056f9571abeb4d62360199426 (diff)
cargo +nightly fmt
Signed-off-by: Felipe Balbi <[email protected]>
Diffstat (limited to 'src/ostimer.rs')
-rw-r--r--src/ostimer.rs66
1 files changed, 22 insertions, 44 deletions
diff --git a/src/ostimer.rs b/src/ostimer.rs
index 6a4188db0..a4cab6970 100644
--- a/src/ostimer.rs
+++ b/src/ostimer.rs
@@ -27,9 +27,10 @@
27//! - Immediate wake for timestamps that would cause rollover issues 27//! - Immediate wake for timestamps that would cause rollover issues
28#![allow(dead_code)] 28#![allow(dead_code)]
29 29
30use core::sync::atomic::{AtomicBool, Ordering};
31
30use crate::interrupt::InterruptExt; 32use crate::interrupt::InterruptExt;
31use crate::pac; 33use crate::pac;
32use core::sync::atomic::{AtomicBool, Ordering};
33 34
34// PAC defines the shared RegisterBlock under `ostimer0`. 35// PAC defines the shared RegisterBlock under `ostimer0`.
35type Regs = pac::ostimer0::RegisterBlock; 36type Regs = pac::ostimer0::RegisterBlock;
@@ -129,18 +130,12 @@ pub(super) fn wait_for_match_write_complete(r: &Regs) -> bool {
129 130
130fn prime_match_registers(r: &Regs) { 131fn prime_match_registers(r: &Regs) {
131 // Disable the interrupt, clear any pending flag, then wait until the MATCH registers are writable. 132 // Disable the interrupt, clear any pending flag, then wait until the MATCH registers are writable.
132 r.osevent_ctrl().write(|w| { 133 r.osevent_ctrl()
133 w.ostimer_intrflag() 134 .write(|w| w.ostimer_intrflag().clear_bit_by_one().ostimer_intena().clear_bit());
134 .clear_bit_by_one()
135 .ostimer_intena()
136 .clear_bit()
137 });
138 135
139 if wait_for_match_write_ready(r) { 136 if wait_for_match_write_ready(r) {
140 r.match_l() 137 r.match_l().write(|w| unsafe { w.match_value().bits(MATCH_L_MAX) });
141 .write(|w| unsafe { w.match_value().bits(MATCH_L_MAX) }); 138 r.match_h().write(|w| unsafe { w.match_value().bits(MATCH_H_MAX) });
142 r.match_h()
143 .write(|w| unsafe { w.match_value().bits(MATCH_H_MAX) });
144 let _ = wait_for_match_write_complete(r); 139 let _ = wait_for_match_write_complete(r);
145 } 140 }
146} 141}
@@ -222,10 +217,7 @@ impl<'d, I: Instance> Ostimer<'d, I> {
222 /// Requires clocks for the instance to be enabled by the board before calling. 217 /// Requires clocks for the instance to be enabled by the board before calling.
223 /// Does not enable NVIC or INTENA; use time_driver::init() for async operation. 218 /// Does not enable NVIC or INTENA; use time_driver::init() for async operation.
224 pub fn new(_inst: impl Instance, cfg: Config, _p: &'d crate::pac::Peripherals) -> Self { 219 pub fn new(_inst: impl Instance, cfg: Config, _p: &'d crate::pac::Peripherals) -> Self {
225 assert!( 220 assert!(cfg.clock_frequency_hz > 0, "OSTIMER frequency must be greater than 0");
226 cfg.clock_frequency_hz > 0,
227 "OSTIMER frequency must be greater than 0"
228 );
229 221
230 if cfg.init_match_max { 222 if cfg.init_match_max {
231 let r: &Regs = unsafe { &*I::ptr() }; 223 let r: &Regs = unsafe { &*I::ptr() };
@@ -268,12 +260,8 @@ impl<'d, I: Instance> Ostimer<'d, I> {
268 260
269 // Mask the peripheral interrupt flag before we toggle the reset line so that 261 // Mask the peripheral interrupt flag before we toggle the reset line so that
270 // no new NVIC activity races with the reset sequence. 262 // no new NVIC activity races with the reset sequence.
271 r.osevent_ctrl().write(|w| { 263 r.osevent_ctrl()
272 w.ostimer_intrflag() 264 .write(|w| w.ostimer_intrflag().clear_bit_by_one().ostimer_intena().clear_bit());
273 .clear_bit_by_one()
274 .ostimer_intena()
275 .clear_bit()
276 });
277 265
278 unsafe { 266 unsafe {
279 crate::reset::assert::<crate::reset::line::Ostimer0>(peripherals); 267 crate::reset::assert::<crate::reset::line::Ostimer0>(peripherals);
@@ -287,9 +275,7 @@ impl<'d, I: Instance> Ostimer<'d, I> {
287 crate::reset::release::<crate::reset::line::Ostimer0>(peripherals); 275 crate::reset::release::<crate::reset::line::Ostimer0>(peripherals);
288 } 276 }
289 277
290 while !<crate::reset::line::Ostimer0 as crate::reset::ResetLine>::is_released( 278 while !<crate::reset::line::Ostimer0 as crate::reset::ResetLine>::is_released(&peripherals.mrcc0) {
291 &peripherals.mrcc0,
292 ) {
293 cortex_m::asm::nop(); 279 cortex_m::asm::nop();
294 } 280 }
295 281
@@ -363,12 +349,8 @@ impl<'d, I: Instance> Ostimer<'d, I> {
363 349
364 critical_section::with(|_| { 350 critical_section::with(|_| {
365 // Disable interrupt and clear flag 351 // Disable interrupt and clear flag
366 r.osevent_ctrl().write(|w| { 352 r.osevent_ctrl()
367 w.ostimer_intrflag() 353 .write(|w| w.ostimer_intrflag().clear_bit_by_one().ostimer_intena().clear_bit());
368 .clear_bit_by_one()
369 .ostimer_intena()
370 .clear_bit()
371 });
372 354
373 if !wait_for_match_write_ready(r) { 355 if !wait_for_match_write_ready(r) {
374 prime_match_registers(r); 356 prime_match_registers(r);
@@ -526,15 +508,17 @@ fn gray_to_bin(gray: u64) -> u64 {
526} 508}
527 509
528pub mod time_driver { 510pub mod time_driver {
529 use super::{
530 ALARM_ACTIVE, ALARM_CALLBACK, ALARM_FLAG, ALARM_TARGET_TIME, EVTIMER_HI_MASK,
531 EVTIMER_HI_SHIFT, LOW_32_BIT_MASK, Regs, bin_to_gray, now_ticks_read,
532 };
533 use crate::pac;
534 use core::sync::atomic::Ordering; 511 use core::sync::atomic::Ordering;
535 use core::task::Waker; 512 use core::task::Waker;
513
536 use embassy_sync::waitqueue::AtomicWaker; 514 use embassy_sync::waitqueue::AtomicWaker;
537 use embassy_time_driver as etd; 515 use embassy_time_driver as etd;
516
517 use super::{
518 bin_to_gray, now_ticks_read, Regs, ALARM_ACTIVE, ALARM_CALLBACK, ALARM_FLAG, ALARM_TARGET_TIME,
519 EVTIMER_HI_MASK, EVTIMER_HI_SHIFT, LOW_32_BIT_MASK,
520 };
521 use crate::pac;
538 pub struct Driver; 522 pub struct Driver;
539 static TIMER_WAKER: AtomicWaker = AtomicWaker::new(); 523 static TIMER_WAKER: AtomicWaker = AtomicWaker::new();
540 524
@@ -569,12 +553,8 @@ pub mod time_driver {
569 553
570 critical_section::with(|_| { 554 critical_section::with(|_| {
571 // Mask INTENA and clear flag 555 // Mask INTENA and clear flag
572 r.osevent_ctrl().write(|w| { 556 r.osevent_ctrl()
573 w.ostimer_intrflag() 557 .write(|w| w.ostimer_intrflag().clear_bit_by_one().ostimer_intena().clear_bit());
574 .clear_bit_by_one()
575 .ostimer_intena()
576 .clear_bit()
577 });
578 558
579 // Read back to ensure W1C took effect on hardware 559 // Read back to ensure W1C took effect on hardware
580 let _ = r.osevent_ctrl().read().ostimer_intrflag().bit(); 560 let _ = r.osevent_ctrl().read().ostimer_intrflag().bit();
@@ -690,9 +670,7 @@ pub mod time_driver {
690 670
691 /// Type-level handler to be used with bind_interrupts! for OS_EVENT. 671 /// Type-level handler to be used with bind_interrupts! for OS_EVENT.
692 pub struct OsEventHandler; 672 pub struct OsEventHandler;
693 impl crate::interrupt::typelevel::Handler<crate::interrupt::typelevel::OS_EVENT> 673 impl crate::interrupt::typelevel::Handler<crate::interrupt::typelevel::OS_EVENT> for OsEventHandler {
694 for OsEventHandler
695 {
696 unsafe fn on_interrupt() { 674 unsafe fn on_interrupt() {
697 on_interrupt(); 675 on_interrupt();
698 } 676 }