lpc55s6x_hal/peripherals/
utick.rs

1//! API for the micro-tick timer (UTICK)
2//!
3//! The entry point to this API is [`UTICK`].
4//!
5//! The UTICK peripheral is described in the user manual, chapter 26.
6//! It is driven by the FRO 1Mhz clock and has a microsecond resolution.
7//!
8//! # Examples: led.rs, led_utick.rs
9
10// TODO: move this to drivers section,
11// possibly merge with ctimers when they're implemented
12
13use embedded_hal::timer;
14use nb;
15use void::Void;
16
17use crate::{
18    raw,
19    peripherals::{
20        syscon,
21    },
22    typestates::{
23        init_state,
24        ClocksSupportUtickToken,
25    },
26};
27
28crate::wrap_stateful_peripheral!(Utick, UTICK0);
29
30pub type EnabledUtick = Utick<init_state::Enabled>;
31
32impl<State> Utick<State> {
33    pub fn enabled(
34        mut self,
35        syscon: &mut syscon::Syscon,
36        _clocktree_token: &ClocksSupportUtickToken,
37    ) -> EnabledUtick {
38        syscon.enable_clock(&mut self.raw);
39        syscon.reset(&mut self.raw);
40
41        Utick {
42            raw: self.raw,
43            _state: init_state::Enabled(()),
44        }
45    }
46
47    pub fn disabled(mut self, syscon: &mut syscon::Syscon) -> Utick<init_state::Disabled> {
48        syscon.disable_clock(&mut self.raw);
49
50        Utick {
51            raw: self.raw,
52            _state: init_state::Disabled,
53        }
54    }
55}
56
57// TODO: This does not feel like it belongs here.
58
59impl timer::Cancel for EnabledUtick {
60    type Error = Void;
61
62    fn cancel(&mut self) -> Result<(), Self::Error> {
63        // A value of 0 stops the timer.
64        self.raw.ctrl.write(|w| unsafe { w.delayval().bits(0) });
65        Ok(())
66    }
67}
68
69// TODO: also implement Periodic for UTICK
70impl timer::CountDown for EnabledUtick {
71    type Time = u32;
72
73    fn start<T>(&mut self, timeout: T)
74    where
75        T: Into<Self::Time>,
76    {
77        // The delay will be equal to DELAYVAL + 1 periods of the timer clock.
78        // The minimum usable value is 1, for a delay of 2 timer clocks. A value of 0 stops the timer.
79        let time = timeout.into();
80        // Maybe remove again? Empirically, nothing much happens when
81        // writing 1 to `delayval`.
82        assert!(time >= 2);
83        self.raw
84            .ctrl
85            .write(|w| unsafe { w.delayval().bits(time - 1) });
86        // So... this seems a bit unsafe (what if time is 2?)
87        // But: without it, in --release builds the timer behaves erratically.
88        // The UM says this on the topic: "Note that the Micro-tick Timer operates from a different
89        // (typically slower) clock than the CPU and bus systems.  This means there may be a
90        // synchronization delay when accessing Micro-tick Timer registers."
91        while self.raw.stat.read().active().bit_is_clear() {}
92    }
93
94    fn wait(&mut self) -> nb::Result<(), Void> {
95        if self.raw.stat.read().active().bit_is_clear() {
96            return Ok(());
97        }
98
99        Err(nb::Error::WouldBlock)
100    }
101}
102
103// TODO: Either get rid of `nb` or get rid of this
104impl EnabledUtick {
105    pub fn blocking_wait(&mut self) {
106        while self.raw.stat.read().active().bit_is_set() {}
107    }
108}