1#[cfg(feature = "hrtim_v2")]
2use crate::pac::HRTIM_TIMF;
3use crate::{
4 pac::{HRTIM_MASTER, HRTIM_TIMA, HRTIM_TIMB, HRTIM_TIMC, HRTIM_TIMD, HRTIM_TIME},
5 DacResetTrigger, NoDacTrigger,
6};
7use core::{marker::PhantomData, ops::Deref};
8
9pub use super::ext::{Chan, Cmp};
10#[derive(Debug, PartialEq, Clone, Copy)]
11#[cfg_attr(feature = "defmt", derive(defmt::Format))]
12pub enum SwapPins {
13 Normal,
14 Swapped,
15}
16
17use super::{
18 capture::{self, HrCapt, HrCapture},
19 control::HrPwmCtrl,
20 ext::{MasterDierW, MasterExt, MasterIcr, TimExt},
21 HrtimPrescaler,
22};
23
24pub struct HrTim<TIM, PSCL, CPT1, CPT2, DacRst: DacResetTrigger = NoDacTrigger> {
25 _timer: PhantomData<TIM>,
26 _prescaler: PhantomData<PSCL>,
27 _dac_trg: PhantomData<DacRst>,
28 capture_ch1: CPT1,
29 capture_ch2: CPT2,
30}
31
32pub struct Ch1;
33pub struct Ch2;
34
35pub trait ChExt {
36 const CH: Chan;
37}
38
39impl ChExt for Ch1 {
40 const CH: Chan = Chan::Ch1;
41}
42
43impl ChExt for Ch2 {
44 const CH: Chan = Chan::Ch2;
45}
46
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48#[repr(u8)]
49pub enum Timer {
50 Master,
51 Tim(TimX),
52}
53
54#[derive(Debug, Clone, Copy, PartialEq, Eq)]
55#[repr(u8)]
56pub enum TimX {
57 A = 0,
58 B = 1,
59 C = 2,
60 D = 3,
61 E = 4,
62 #[cfg(feature = "hrtim_v2")]
63 F = 5,
64}
65
66pub trait Instance: Deref<Target = Self::RB> {
68 type RB: MasterExt;
69 const TIMX: Timer;
70 fn ptr() -> *const Self::RB;
71}
72
73pub trait InstanceX: Instance<RB: TimExt> {
75 const T_X: TimX;
76}
77
78impl Instance for HRTIM_MASTER {
79 type RB = crate::pac::hrtim_master::RegisterBlock;
80 const TIMX: Timer = Timer::Master;
81 fn ptr() -> *const Self::RB {
82 Self::ptr()
83 }
84}
85
86macro_rules! hrtim_timer {
87 ($(
88 $TIMX:ident:
89 $timx:ident,
90 $T:ident,
91 )+) => {$(
92 impl Instance for $TIMX {
93 type RB = crate::pac::$timx::RegisterBlock;
94 const TIMX: Timer = Timer::Tim(Self::T_X);
95 fn ptr() -> *const Self::RB {
96 Self::ptr()
97 }
98 }
99 impl InstanceX for $TIMX {
100 const T_X: TimX = TimX::$T;
101 }
102
103 )+}
104}
105
106hrtim_timer! {
107 HRTIM_TIMA: hrtim_tima, A,
108 HRTIM_TIMB: hrtim_timb, B,
109 HRTIM_TIMC: hrtim_timc, C,
110 HRTIM_TIMD: hrtim_timd, D,
111 HRTIM_TIME: hrtim_time, E,
112}
113
114#[cfg(feature = "hrtim_v2")]
115hrtim_timer! {HRTIM_TIMF: hrtim_timf, F,}
116
117pub struct DmaChannel<TIM> {
121 _x: PhantomData<TIM>,
122}
123
124pub trait HrTimer {
125 type Timer: Instance;
126 type Prescaler: HrtimPrescaler;
127 type DacResetTrigger: DacResetTrigger;
128
129 fn get_period(&self) -> u16;
137
138 fn set_period(&mut self, period: u16);
142
143 fn get_counter_value(&self) -> u16;
147
148 fn start(&mut self, _hr_control: &mut HrPwmCtrl);
150
151 fn stop(&mut self, _hr_control: &mut HrPwmCtrl);
153
154 fn stop_and_reset(&mut self, _hr_control: &mut HrPwmCtrl);
156
157 fn clear_repetition_interrupt(&mut self);
158
159 fn as_reset_adc_trigger(&self) -> super::adc_trigger::TimerReset<Self::Timer>;
161
162 fn as_period_adc_trigger(&self) -> super::adc_trigger::TimerPeriod<Self::Timer>;
164
165 fn disable_register_updates(&mut self, _hr_control: &mut HrPwmCtrl);
171
172 fn enable_register_updates(&mut self, _hr_control: &mut HrPwmCtrl);
179}
180
181pub trait HrSlaveTimer: HrTimer {
182 type CptCh1;
183 type CptCh2;
184
185 fn enable_reset_event<E: super::event::TimerResetEventSource<Self::Timer, Self::Prescaler>>(
187 &mut self,
188 _event: &E,
189 );
190
191 fn disable_reset_event<E: super::event::TimerResetEventSource<Self::Timer, Self::Prescaler>>(
193 &mut self,
194 _event: &E,
195 );
196
197 #[cfg(feature = "stm32g4")]
198 unsafe fn swap_outputs(&self, _hr_control: &mut HrPwmCtrl, swap: SwapPins);
200}
201
202pub struct TimerSplitCapture<T, PSCL, CH1, CH2, DacRst: DacResetTrigger> {
203 pub timer: HrTim<T, PSCL, (), (), DacRst>,
204 pub ch1: HrCapt<T, PSCL, CH1, capture::NoDma>,
205 pub ch2: HrCapt<T, PSCL, CH2, capture::NoDma>,
206}
207
208pub trait HrSlaveTimerCpt: HrSlaveTimer {
210 type CaptureCh1: HrCapture;
211 type CaptureCh2: HrCapture;
212
213 fn capture_ch1(&mut self) -> &mut Self::CaptureCh1;
214 fn capture_ch2(&mut self) -> &mut Self::CaptureCh2;
215 fn split_capture(
216 self,
217 ) -> TimerSplitCapture<Self::Timer, Self::Prescaler, Ch1, Ch2, Self::DacResetTrigger>;
218}
219
220impl<TIM: Instance, PSCL: HrtimPrescaler, CPT1, CPT2, DacRst: DacResetTrigger> HrTimer
221 for HrTim<TIM, PSCL, CPT1, CPT2, DacRst>
222{
223 type Prescaler = PSCL;
224 type Timer = TIM;
225 type DacResetTrigger = DacRst;
226
227 fn get_period(&self) -> u16 {
228 let tim = unsafe { &*TIM::ptr() };
229
230 tim.perr().read().per().bits()
231 }
232 fn set_period(&mut self, period: u16) {
233 let tim = unsafe { &*TIM::ptr() };
234
235 tim.perr().write(|w| unsafe { w.per().bits(period as u16) });
236 }
237
238 fn get_counter_value(&self) -> u16 {
239 let tim = unsafe { &*TIM::ptr() };
240 tim.cntr().read().cnt().bits()
241 }
242
243 fn start(&mut self, _hr_control: &mut HrPwmCtrl) {
245 let master = unsafe { &*HRTIM_MASTER::ptr() };
249 master.cr().modify(|_, w| match TIM::TIMX {
250 Timer::Master => w.mcen().set_bit(),
251 Timer::Tim(v) => w.tcen(v as _).set_bit(),
252 });
253 }
254
255 fn stop(&mut self, _hr_control: &mut HrPwmCtrl) {
257 let master = unsafe { &*HRTIM_MASTER::ptr() };
260 master.cr().modify(|_, w| match TIM::TIMX {
261 Timer::Master => w.mcen().clear_bit(),
262 Timer::Tim(v) => w.tcen(v as _).clear_bit(),
263 });
264 }
265
266 fn stop_and_reset(&mut self, _hr_control: &mut HrPwmCtrl) {
268 self.stop(_hr_control);
269
270 let tim = unsafe { &*TIM::ptr() };
272 unsafe {
273 tim.cntr().write(|w| w.cnt().bits(0));
274 }
275 }
276
277 fn as_reset_adc_trigger(&self) -> super::adc_trigger::TimerReset<Self::Timer> {
279 super::adc_trigger::TimerReset(PhantomData)
280 }
281
282 fn as_period_adc_trigger(&self) -> super::adc_trigger::TimerPeriod<Self::Timer> {
284 super::adc_trigger::TimerPeriod(PhantomData)
285 }
286
287 fn clear_repetition_interrupt(&mut self) {
288 let tim = unsafe { &*TIM::ptr() };
289
290 tim.icr().write(|w| w.repc().clear());
291 }
292
293 fn disable_register_updates(&mut self, _hr_control: &mut HrPwmCtrl) {
299 use super::HRTIM_COMMON;
300 let common = unsafe { &*HRTIM_COMMON::ptr() };
301 common.cr1().modify(|_, w| match TIM::TIMX {
302 Timer::Master => w.mudis().set_bit(),
303 Timer::Tim(v) => w.tudis(v as _).set_bit(),
304 });
305 }
306
307 fn enable_register_updates<'a>(&mut self, _hr_control: &mut HrPwmCtrl) {
314 use super::HRTIM_COMMON;
315 let common = unsafe { &*HRTIM_COMMON::ptr() };
316 common.cr1().modify(|_, w| match TIM::TIMX {
317 Timer::Master => w.mudis().clear_bit(),
318 Timer::Tim(v) => w.tudis(v as _).clear_bit(),
319 });
320 }
321}
322
323impl<TIM: Instance, PSCL, CPT1, CPT2, DacRst> HrTim<TIM, PSCL, CPT1, CPT2, DacRst>
324where
325 DacRst: DacResetTrigger,
326{
327 pub fn set_repetition_counter(&mut self, repetition_counter: u8) {
328 let tim = unsafe { &*TIM::ptr() };
329
330 unsafe {
331 tim.repr().write(|w| w.rep().bits(repetition_counter));
332 }
333 }
334
335 pub fn enable_repetition_interrupt(&mut self, enable: bool) {
336 let tim = unsafe { &*TIM::ptr() };
337
338 tim.dier().modify(|_r, w| w.repie().bit(enable));
339 }
340}
341
342impl<TIM: InstanceX, PSCL: HrtimPrescaler, CPT1, CPT2, DacRst> HrSlaveTimer
343 for HrTim<TIM, PSCL, CPT1, CPT2, DacRst>
344where
345 DacRst: DacResetTrigger,
346{
347 type CptCh1 = HrCapt<Self::Timer, Self::Prescaler, Ch1, capture::NoDma>;
348 type CptCh2 = HrCapt<Self::Timer, Self::Prescaler, Ch2, capture::NoDma>;
349
350 fn enable_reset_event<E: super::event::TimerResetEventSource<Self::Timer, Self::Prescaler>>(
366 &mut self,
367 _event: &E,
368 ) {
369 let tim = unsafe { &*TIM::ptr() };
370
371 unsafe {
372 tim.rstr().modify(|r, w| w.bits(r.bits() | E::BITS));
373 }
374 }
375
376 fn disable_reset_event<E: super::event::TimerResetEventSource<Self::Timer, Self::Prescaler>>(
378 &mut self,
379 _event: &E,
380 ) {
381 let tim = unsafe { &*TIM::ptr() };
382
383 unsafe {
384 tim.rstr().modify(|r, w| w.bits(r.bits() & !E::BITS));
385 }
386 }
387
388 #[cfg(feature = "stm32g4")]
389 unsafe fn swap_outputs(&self, _hr_control: &mut HrPwmCtrl, swap: SwapPins) {
391 use super::HRTIM_COMMON;
392
393 let common = unsafe { &*HRTIM_COMMON::ptr() };
395 common
396 .cr2()
397 .modify(|_r, w| w.swp(TIM::T_X as u8).bit(swap == SwapPins::Swapped));
398 }
399}
400
401impl<TIM: InstanceX, PSCL, DacRst> HrSlaveTimerCpt
402 for HrTim<
403 TIM,
404 PSCL,
405 HrCapt<TIM, PSCL, Ch1, capture::NoDma>,
406 HrCapt<TIM, PSCL, Ch2, capture::NoDma>,
407 DacRst,
408 >
409where
410 PSCL: HrtimPrescaler,
411 DacRst: DacResetTrigger,
412 HrCapt<TIM, PSCL, Ch1, capture::NoDma>: HrCapture,
413 HrCapt<TIM, PSCL, Ch2, capture::NoDma>: HrCapture,
414{
415 type CaptureCh1 = <Self as HrSlaveTimer>::CptCh1;
416 type CaptureCh2 = <Self as HrSlaveTimer>::CptCh2;
417
418 fn capture_ch1(&mut self) -> &mut Self::CaptureCh1 {
420 &mut self.capture_ch1
421 }
422
423 fn capture_ch2(&mut self) -> &mut Self::CaptureCh2 {
425 &mut self.capture_ch2
426 }
427
428 fn split_capture(self) -> TimerSplitCapture<TIM, PSCL, Ch1, Ch2, DacRst> {
429 let HrTim {
430 _timer,
431 _prescaler,
432 _dac_trg,
433 capture_ch1,
434 capture_ch2,
435 } = self;
436
437 TimerSplitCapture {
438 timer: HrTim {
439 _timer,
440 _prescaler,
441 _dac_trg,
442 capture_ch1: (),
443 capture_ch2: (),
444 },
445 ch1: capture_ch1,
446 ch2: capture_ch2,
447 }
448 }
449}
450
451impl<TIM: InstanceX, DST, PSCL, CPT1, CPT2, DacRst> super::event::EventSource<DST, PSCL>
453 for HrTim<TIM, PSCL, CPT1, CPT2, DacRst>
454where
455 DacRst: DacResetTrigger,
456{
457 const BITS: u32 = 1 << 2;
459}
460
461impl<TIM: InstanceX, PSCL, CPT1, CPT2> super::capture::CaptureEvent<TIM, PSCL>
463 for HrTim<TIM, PSCL, CPT1, CPT2>
464{
465 const BITS: u32 = 1 << 1;
466}
467
468#[cfg(feature = "stm32g4")]
469use super::adc_trigger::{
470 AdcTrigger13 as Adc13, AdcTrigger24 as Adc24, AdcTrigger579 as Adc579,
471 AdcTrigger6810 as Adc6810,
472};
473
474#[cfg(feature = "stm32g4")]
475macro_rules! hrtim_timer_adc_trigger {
476 ($($TIMX:ident:
477 [$(($AdcTrigger:ident: [
478 $((PER: $adc_trigger_bits_period:expr),)*
479 $((RST: $adc_trigger_bits_reset:expr)),*
480 ])),+]
481 ),+) => {
482 $($(
483 $(impl $AdcTrigger for super::adc_trigger::TimerReset<$TIMX> {
484 const BITS: u32 = $adc_trigger_bits_reset;
485 })*
486
487 $(impl $AdcTrigger for super::adc_trigger::TimerPeriod<$TIMX> {
488 const BITS: u32 = $adc_trigger_bits_period;
489 })*
490 )*)*
491 }
492}
493
494#[cfg(feature = "stm32g4")]
495hrtim_timer_adc_trigger! {
496 HRTIM_MASTER: [(Adc13: [(PER: 1 << 4),]), (Adc24: [(PER: 1 << 4),]), (Adc579: [(PER: 4),]), (Adc6810: [(PER: 4),])],
497
498 HRTIM_TIMA: [(Adc13: [(PER: 1 << 13), (RST: 1 << 14)]), (Adc24: [(PER: 1 << 13), ]), (Adc579: [(PER: 12), (RST: 13)]), (Adc6810: [(PER: 12), ])],
499 HRTIM_TIMB: [(Adc13: [(PER: 1 << 18), (RST: 1 << 19)]), (Adc24: [(PER: 1 << 17), ]), (Adc579: [(PER: 16), (RST: 17)]), (Adc6810: [(PER: 15), ])],
500 HRTIM_TIMC: [(Adc13: [(PER: 1 << 23), ]), (Adc24: [(PER: 1 << 21), (RST: 1 << 22)]), (Adc579: [(PER: 20), ]), (Adc6810: [(PER: 18), (RST: 19)])],
501 HRTIM_TIMD: [(Adc13: [(PER: 1 << 27), ]), (Adc24: [(PER: 1 << 26), (RST: 1 << 27)]), (Adc579: [(PER: 23), ]), (Adc6810: [(PER: 22), (RST: 23)])],
502 HRTIM_TIME: [(Adc13: [(PER: 1 << 31), ]), (Adc24: [ (RST: 1 << 31)]), (Adc579: [(PER: 26), ]), (Adc6810: [ ])],
503 HRTIM_TIMF: [(Adc13: [(PER: 1 << 24), (RST: 1 << 28)]), (Adc24: [(PER: 1 << 24), ]), (Adc579: [(PER: 30), (RST: 31)]), (Adc6810: [(PER: 31), ])]
504}
505
506impl<DST, PSCL, CPT1, CPT2> super::event::TimerResetEventSource<DST, PSCL>
508 for HrTim<HRTIM_MASTER, PSCL, CPT1, CPT2, NoDacTrigger>
509{
510 const BITS: u32 = 1 << 4; }
512
513impl<DST, PSCL, CPT1, CPT2> super::event::EventSource<DST, PSCL>
515 for HrTim<HRTIM_MASTER, PSCL, CPT1, CPT2, NoDacTrigger>
516{
517 const BITS: u32 = 1 << 7; }