|
| 1 | +// Licensed under the Apache-2.0 license |
| 2 | + |
| 3 | +use core::fmt; |
| 4 | +use core::marker::PhantomData; |
| 5 | +use embedded_hal_old::timer::{Cancel, CountDown, Periodic}; |
| 6 | +use fugit::MicrosDurationU32 as MicroSeconds; |
| 7 | + |
| 8 | +/// Timer type: One-shot or periodic |
| 9 | +#[derive(Debug, Clone, Copy, PartialEq)] |
| 10 | +pub enum TimerType { |
| 11 | + OneShot, |
| 12 | + Periodic, |
| 13 | +} |
| 14 | + |
| 15 | +/// Timer error type |
| 16 | +#[derive(Debug)] |
| 17 | +pub enum TimerError { |
| 18 | + TimeoutTooLarge, |
| 19 | + InvalidConfig, |
| 20 | +} |
| 21 | + |
| 22 | +const MAX_TIMEOUT_MS: u32 = 4_294_967; |
| 23 | +const MATCH_DISABLE: u32 = 0xffff_ffff; |
| 24 | + |
| 25 | +/// Trait to abstract timer register base + index |
| 26 | +pub trait TimerInstance { |
| 27 | + fn cr() -> &'static ast1060_pac::timer::RegisterBlock; |
| 28 | + fn gr() -> &'static ast1060_pac::timerg::RegisterBlock; |
| 29 | + fn index() -> usize; |
| 30 | +} |
| 31 | + |
| 32 | +/// Timer0 instance (only one currently supported) |
| 33 | +impl TimerInstance for ast1060_pac::Timer { |
| 34 | + fn cr() -> &'static ast1060_pac::timer::RegisterBlock { |
| 35 | + unsafe { &*ast1060_pac::Timer::ptr() } |
| 36 | + } |
| 37 | + |
| 38 | + fn gr() -> &'static ast1060_pac::timerg::RegisterBlock { |
| 39 | + unsafe { &*ast1060_pac::Timerg::ptr() } |
| 40 | + } |
| 41 | + |
| 42 | + fn index() -> usize { |
| 43 | + 0 |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/// Timer controller |
| 48 | +pub struct TimerController<T: TimerInstance> { |
| 49 | + cr: &'static ast1060_pac::timer::RegisterBlock, |
| 50 | + gr: &'static ast1060_pac::timerg::RegisterBlock, |
| 51 | + tick_per_us: u32, |
| 52 | + callback: Option<fn()>, |
| 53 | + auto_reload: TimerType, |
| 54 | + _marker: PhantomData<T>, |
| 55 | +} |
| 56 | + |
| 57 | +impl<T: TimerInstance> fmt::Debug for TimerController<T> { |
| 58 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 59 | + f.write_str("TimerController") |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl<T: TimerInstance> TimerController<T> { |
| 64 | + /// Create a new timer controller |
| 65 | + #[must_use] |
| 66 | + pub fn new(tick_per_us: u32) -> Self { |
| 67 | + Self { |
| 68 | + cr: T::cr(), |
| 69 | + gr: T::gr(), |
| 70 | + tick_per_us, |
| 71 | + callback: None, |
| 72 | + auto_reload: TimerType::OneShot, |
| 73 | + _marker: PhantomData, |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /// Get current counter value |
| 78 | + #[must_use] |
| 79 | + pub fn counter(&self) -> u32 { |
| 80 | + self.cr.timer000().read().bits() |
| 81 | + } |
| 82 | + |
| 83 | + /// Stop the timer and clear reload |
| 84 | + pub fn stop(&mut self) { |
| 85 | + let index = T::index(); |
| 86 | + self.gr |
| 87 | + .timerg03c() |
| 88 | + .write(|w| unsafe { w.bits(1 << (4 * index)) }); |
| 89 | + self.cr.timer004().write(|w| unsafe { w.bits(0) }); |
| 90 | + } |
| 91 | + |
| 92 | + /// Handle timer interrupt (user calls this in IRQ handler) |
| 93 | + pub fn handle_interrupt(&mut self) { |
| 94 | + let index = T::index(); |
| 95 | + self.gr.timerg034().write(|w| unsafe { w.bits(1 << index) }); |
| 96 | + |
| 97 | + if self.auto_reload == TimerType::OneShot { |
| 98 | + self.stop(); |
| 99 | + } |
| 100 | + |
| 101 | + if let Some(cb) = self.callback { |
| 102 | + cb(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + pub fn set_callback(&mut self, cb: Option<fn()>, periodic: TimerType) { |
| 107 | + self.callback = cb; |
| 108 | + self.auto_reload = periodic; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +impl<T: TimerInstance> CountDown for TimerController<T> { |
| 113 | + type Time = MicroSeconds; |
| 114 | + type Error = TimerError; |
| 115 | + |
| 116 | + fn try_start<Time>(&mut self, count: Time) -> Result<(), Self::Error> |
| 117 | + where |
| 118 | + Time: Into<MicroSeconds>, |
| 119 | + { |
| 120 | + let us = count.into().ticks(); |
| 121 | + |
| 122 | + if u64::from(us) >= u64::from(MAX_TIMEOUT_MS) * 1000 { |
| 123 | + return Err(TimerError::TimeoutTooLarge); |
| 124 | + } |
| 125 | + |
| 126 | + let reload = us * self.tick_per_us; |
| 127 | + let index = T::index(); |
| 128 | + |
| 129 | + self.gr |
| 130 | + .timerg03c() |
| 131 | + .write(|w| unsafe { w.bits(1 << (4 * index)) }); |
| 132 | + self.cr.timer004().write(|w| unsafe { w.bits(reload) }); |
| 133 | + self.cr |
| 134 | + .timer008() |
| 135 | + .write(|w| unsafe { w.bits(MATCH_DISABLE) }); |
| 136 | + self.cr |
| 137 | + .timer00c() |
| 138 | + .write(|w| unsafe { w.bits(MATCH_DISABLE) }); |
| 139 | + |
| 140 | + let ctrl_val = (1 << (4 * index)) | (1 << (4 * index + 2)); |
| 141 | + self.gr.timerg030().write(|w| unsafe { w.bits(ctrl_val) }); |
| 142 | + |
| 143 | + Ok(()) |
| 144 | + } |
| 145 | + |
| 146 | + fn try_wait(&mut self) -> nb::Result<(), Self::Error> { |
| 147 | + let index = T::index(); |
| 148 | + let status = self.gr.timerg034().read().bits(); |
| 149 | + |
| 150 | + if (status & (1 << index)) != 0 { |
| 151 | + self.gr.timerg034().write(|w| unsafe { w.bits(1 << index) }); |
| 152 | + Ok(()) |
| 153 | + } else { |
| 154 | + Err(nb::Error::WouldBlock) |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +impl<T: TimerInstance> Cancel for TimerController<T> { |
| 160 | + fn try_cancel(&mut self) -> Result<(), Self::Error> { |
| 161 | + self.stop(); |
| 162 | + Ok(()) |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +impl<T: TimerInstance> Periodic for TimerController<T> {} |
0 commit comments