1#[cfg(ledc_version = "1")]
13use super::HighSpeed;
14use super::{LowSpeed, Speed, low_level};
15use crate::{pac, time::Rate};
16
17const LEDC_TIMER_DIV_NUM_MAX: u64 = 0x3FFFF;
18
19#[derive(Debug, Clone, Copy, PartialEq)]
21#[cfg_attr(feature = "defmt", derive(defmt::Format))]
22pub enum Error {
23 Divisor,
25 FrequencyUnset,
27}
28
29#[cfg(ledc_version = "1")]
30#[derive(PartialEq, Eq, Copy, Clone, Debug)]
32#[cfg_attr(feature = "defmt", derive(defmt::Format))]
33pub enum HSClockSource {
34 APBClk,
36 }
38
39#[derive(PartialEq, Eq, Copy, Clone, Debug)]
41#[cfg_attr(feature = "defmt", derive(defmt::Format))]
42pub enum LSClockSource {
43 APBClk,
45 }
47
48#[derive(PartialEq, Eq, Copy, Clone, Debug)]
50#[cfg_attr(feature = "defmt", derive(defmt::Format))]
51pub enum Number {
52 Timer0 = 0,
54 Timer1 = 1,
56 Timer2 = 2,
58 Timer3 = 3,
60}
61
62pub mod config {
64 use crate::time::Rate;
65
66 #[derive(PartialEq, Eq, Copy, Clone, Debug)]
68 #[cfg_attr(feature = "defmt", derive(defmt::Format))]
69 #[allow(clippy::enum_variant_names)] pub enum Duty {
71 Duty1Bit = 1,
73 Duty2Bit,
75 Duty3Bit,
77 Duty4Bit,
79 Duty5Bit,
81 Duty6Bit,
83 Duty7Bit,
85 Duty8Bit,
87 Duty9Bit,
89 Duty10Bit,
91 Duty11Bit,
93 Duty12Bit,
95 Duty13Bit,
97 Duty14Bit,
99 #[cfg(ledc_version = "1")]
100 Duty15Bit,
102 #[cfg(ledc_version = "1")]
103 Duty16Bit,
105 #[cfg(ledc_version = "1")]
106 Duty17Bit,
108 #[cfg(ledc_version = "1")]
109 Duty18Bit,
111 #[cfg(ledc_version = "1")]
112 Duty19Bit,
114 #[cfg(ledc_version = "1")]
115 Duty20Bit,
117 }
118
119 impl TryFrom<u32> for Duty {
120 type Error = ();
121
122 fn try_from(value: u32) -> Result<Self, Self::Error> {
123 Ok(match value {
124 1 => Self::Duty1Bit,
125 2 => Self::Duty2Bit,
126 3 => Self::Duty3Bit,
127 4 => Self::Duty4Bit,
128 5 => Self::Duty5Bit,
129 6 => Self::Duty6Bit,
130 7 => Self::Duty7Bit,
131 8 => Self::Duty8Bit,
132 9 => Self::Duty9Bit,
133 10 => Self::Duty10Bit,
134 11 => Self::Duty11Bit,
135 12 => Self::Duty12Bit,
136 13 => Self::Duty13Bit,
137 14 => Self::Duty14Bit,
138 #[cfg(ledc_version = "1")]
139 15 => Self::Duty15Bit,
140 #[cfg(ledc_version = "1")]
141 16 => Self::Duty16Bit,
142 #[cfg(ledc_version = "1")]
143 17 => Self::Duty17Bit,
144 #[cfg(ledc_version = "1")]
145 18 => Self::Duty18Bit,
146 #[cfg(ledc_version = "1")]
147 19 => Self::Duty19Bit,
148 #[cfg(ledc_version = "1")]
149 20 => Self::Duty20Bit,
150 _ => Err(())?,
151 })
152 }
153 }
154
155 #[derive(Copy, Clone)]
157 pub struct Config<CS> {
158 pub duty: Duty,
160 pub clock_source: CS,
162 pub frequency: Rate,
164 }
165}
166
167pub trait TimerSpeed: Speed {
169 type ClockSourceType;
171}
172
173impl TimerSpeed for LowSpeed {
175 type ClockSourceType = LSClockSource;
177}
178
179#[cfg(ledc_version = "1")]
180impl TimerSpeed for HighSpeed {
182 type ClockSourceType = HSClockSource;
184}
185
186pub trait TimerIFace<S: TimerSpeed> {
188 fn freq(&self) -> Option<Rate>;
190
191 fn configure(&mut self, config: config::Config<S::ClockSourceType>) -> Result<(), Error>;
193
194 fn is_configured(&self) -> bool;
196
197 fn duty(&self) -> Option<config::Duty>;
199
200 fn number(&self) -> Number;
202
203 fn frequency(&self) -> u32;
205}
206
207pub trait TimerHW<S: TimerSpeed> {
209 fn freq_hw(&self) -> Option<Rate>;
211
212 fn configure_hw(&self, divisor: u32);
214
215 fn update_hw(&self);
217}
218
219pub struct Timer<'a, S: TimerSpeed> {
221 ledc: &'a pac::ledc::RegisterBlock,
222 number: Number,
223 duty: Option<config::Duty>,
224 frequency: u32,
225 configured: bool,
226 #[cfg(soc_has_clock_node_ref_tick)]
227 use_ref_tick: bool,
228 clock_source: Option<S::ClockSourceType>,
229}
230
231impl<'a, S: TimerSpeed> TimerIFace<S> for Timer<'a, S>
232where
233 Timer<'a, S>: TimerHW<S>,
234{
235 fn freq(&self) -> Option<Rate> {
237 self.freq_hw()
238 }
239
240 fn configure(&mut self, config: config::Config<S::ClockSourceType>) -> Result<(), Error> {
242 self.duty = Some(config.duty);
243 self.clock_source = Some(config.clock_source);
244
245 let src_freq: u32 = self.freq().ok_or(Error::FrequencyUnset)?.as_hz();
246 let precision = 1 << config.duty as u32;
247 let frequency: u32 = config.frequency.as_hz();
248 self.frequency = frequency;
249
250 #[cfg_attr(not(soc_has_clock_node_ref_tick), expect(unused_mut))]
251 let mut divisor = ((src_freq as u64) << 8) / frequency as u64 / precision as u64;
252
253 #[cfg(soc_has_clock_node_ref_tick)]
254 if divisor > LEDC_TIMER_DIV_NUM_MAX {
255 self.use_ref_tick = true;
258 divisor = (1_000_000u64 << 8) / frequency as u64 / precision as u64;
259 }
260
261 if !(256..=LEDC_TIMER_DIV_NUM_MAX).contains(&divisor) {
262 return Err(Error::Divisor);
263 }
264
265 self.configure_hw(divisor as u32);
266 self.update_hw();
267
268 self.configured = true;
269
270 Ok(())
271 }
272
273 fn is_configured(&self) -> bool {
275 self.configured
276 }
277
278 fn duty(&self) -> Option<config::Duty> {
280 self.duty
281 }
282
283 fn number(&self) -> Number {
285 self.number
286 }
287
288 fn frequency(&self) -> u32 {
290 self.frequency
291 }
292}
293
294impl<'a, S: TimerSpeed> Timer<'a, S> {
295 pub fn new(ledc: &'a pac::ledc::RegisterBlock, number: Number) -> Self {
297 Timer {
298 ledc,
299 number,
300 duty: None,
301 frequency: 0u32,
302 configured: false,
303 #[cfg(soc_has_clock_node_ref_tick)]
304 use_ref_tick: false,
305 clock_source: None,
306 }
307 }
308}
309
310impl TimerHW<LowSpeed> for Timer<'_, LowSpeed> {
312 fn freq_hw(&self) -> Option<Rate> {
314 self.clock_source.map(low_level::ls_freq_hw)
315 }
316
317 fn configure_hw(&self, divisor: u32) {
319 let duty = unwrap!(self.duty) as u8;
320 #[cfg(soc_has_clock_node_ref_tick)]
321 let use_ref_tick = self.use_ref_tick;
322 #[cfg(not(soc_has_clock_node_ref_tick))]
323 let use_ref_tick = false;
324 low_level::ls_configure_hw(self.ledc, self.number, divisor, duty, use_ref_tick);
325 }
326
327 fn update_hw(&self) {
329 low_level::ls_update_hw(self.ledc, self.number);
330 }
331}
332
333#[cfg(ledc_version = "1")]
334impl TimerHW<HighSpeed> for Timer<'_, HighSpeed> {
336 fn freq_hw(&self) -> Option<Rate> {
338 self.clock_source.map(low_level::hs_freq_hw)
339 }
340
341 fn configure_hw(&self, divisor: u32) {
343 let duty = unwrap!(self.duty) as u8;
344 low_level::hs_configure_hw(
345 self.ledc,
346 self.number,
347 divisor,
348 duty,
349 unwrap!(self.clock_source),
350 );
351 }
352
353 fn update_hw(&self) {
355 low_level::hs_update_hw();
356 }
357}