aboutsummaryrefslogtreecommitdiff
path: root/embassy-stm32/src/timer/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-stm32/src/timer/mod.rs')
-rw-r--r--embassy-stm32/src/timer/mod.rs262
1 files changed, 262 insertions, 0 deletions
diff --git a/embassy-stm32/src/timer/mod.rs b/embassy-stm32/src/timer/mod.rs
index 4ffb2a289..1d642ed37 100644
--- a/embassy-stm32/src/timer/mod.rs
+++ b/embassy-stm32/src/timer/mod.rs
@@ -1,4 +1,5 @@
1pub mod complementary_pwm; 1pub mod complementary_pwm;
2pub mod qei;
2pub mod simple_pwm; 3pub mod simple_pwm;
3 4
4use stm32_metapac::timer::vals; 5use stm32_metapac::timer::vals;
@@ -14,6 +15,7 @@ pub mod low_level {
14} 15}
15 16
16pub(crate) mod sealed { 17pub(crate) mod sealed {
18
17 use super::*; 19 use super::*;
18 pub trait Basic16bitInstance: RccPeripheral { 20 pub trait Basic16bitInstance: RccPeripheral {
19 type Interrupt: interrupt::typelevel::Interrupt; 21 type Interrupt: interrupt::typelevel::Interrupt;
@@ -31,10 +33,16 @@ pub(crate) mod sealed {
31 fn clear_update_interrupt(&mut self) -> bool; 33 fn clear_update_interrupt(&mut self) -> bool;
32 34
33 fn enable_update_interrupt(&mut self, enable: bool); 35 fn enable_update_interrupt(&mut self, enable: bool);
36
37 fn set_autoreload_preload(&mut self, enable: vals::Arpe);
34 } 38 }
35 39
36 pub trait GeneralPurpose16bitInstance: Basic16bitInstance { 40 pub trait GeneralPurpose16bitInstance: Basic16bitInstance {
37 fn regs_gp16() -> crate::pac::timer::TimGp16; 41 fn regs_gp16() -> crate::pac::timer::TimGp16;
42
43 fn set_count_direction(&mut self, direction: vals::Dir);
44
45 fn set_clock_division(&mut self, ckd: vals::Ckd);
38 } 46 }
39 47
40 pub trait GeneralPurpose32bitInstance: GeneralPurpose16bitInstance { 48 pub trait GeneralPurpose32bitInstance: GeneralPurpose16bitInstance {
@@ -48,6 +56,18 @@ pub(crate) mod sealed {
48 } 56 }
49 57
50 pub trait CaptureCompare16bitInstance: GeneralPurpose16bitInstance { 58 pub trait CaptureCompare16bitInstance: GeneralPurpose16bitInstance {
59 fn set_input_capture_filter(&mut self, channel: Channel, icf: vals::Icf);
60
61 fn clear_input_interrupt(&mut self, channel: Channel);
62
63 fn enable_input_interrupt(&mut self, channel: Channel, enable: bool);
64
65 fn set_input_capture_prescaler(&mut self, channel: Channel, val: u8);
66
67 fn set_input_ti_selection(&mut self, channel: Channel, tisel: InputTISelection);
68
69 fn set_input_capture_mode(&mut self, channel: Channel, mode: InputCaptureMode);
70
51 /// Global output enable. Does not do anything on non-advanced timers. 71 /// Global output enable. Does not do anything on non-advanced timers.
52 fn enable_outputs(&mut self, enable: bool); 72 fn enable_outputs(&mut self, enable: bool);
53 73
@@ -59,6 +79,8 @@ pub(crate) mod sealed {
59 79
60 fn set_compare_value(&mut self, channel: Channel, value: u16); 80 fn set_compare_value(&mut self, channel: Channel, value: u16);
61 81
82 fn get_capture_value(&mut self, channel: Channel) -> u16;
83
62 fn get_max_compare_value(&self) -> u16; 84 fn get_max_compare_value(&self) -> u16;
63 } 85 }
64 86
@@ -73,6 +95,18 @@ pub(crate) mod sealed {
73 } 95 }
74 96
75 pub trait CaptureCompare32bitInstance: GeneralPurpose32bitInstance { 97 pub trait CaptureCompare32bitInstance: GeneralPurpose32bitInstance {
98 fn set_input_capture_filter(&mut self, channel: Channel, icf: vals::Icf);
99
100 fn clear_input_interrupt(&mut self, channel: Channel);
101
102 fn enable_input_interrupt(&mut self, channel: Channel, enable: bool);
103
104 fn set_input_capture_prescaler(&mut self, channel: Channel, val: u8);
105
106 fn set_input_ti_selection(&mut self, channel: Channel, tisel: InputTISelection);
107
108 fn set_input_capture_mode(&mut self, channel: Channel, mode: InputCaptureMode);
109
76 fn set_output_compare_mode(&mut self, channel: Channel, mode: OutputCompareMode); 110 fn set_output_compare_mode(&mut self, channel: Channel, mode: OutputCompareMode);
77 111
78 fn set_output_polarity(&mut self, channel: Channel, polarity: OutputPolarity); 112 fn set_output_polarity(&mut self, channel: Channel, polarity: OutputPolarity);
@@ -81,6 +115,8 @@ pub(crate) mod sealed {
81 115
82 fn set_compare_value(&mut self, channel: Channel, value: u32); 116 fn set_compare_value(&mut self, channel: Channel, value: u32);
83 117
118 fn get_capture_value(&mut self, channel: Channel) -> u32;
119
84 fn get_max_compare_value(&self) -> u32; 120 fn get_max_compare_value(&self) -> u32;
85 } 121 }
86} 122}
@@ -105,6 +141,30 @@ impl Channel {
105} 141}
106 142
107#[derive(Clone, Copy)] 143#[derive(Clone, Copy)]
144pub enum InputCaptureMode {
145 Rising,
146 Falling,
147 BothEdges,
148}
149
150#[derive(Clone, Copy)]
151pub enum InputTISelection {
152 Normal,
153 Alternate,
154 TRC,
155}
156
157impl From<InputTISelection> for stm32_metapac::timer::vals::CcmrInputCcs {
158 fn from(tisel: InputTISelection) -> Self {
159 match tisel {
160 InputTISelection::Normal => stm32_metapac::timer::vals::CcmrInputCcs::TI4,
161 InputTISelection::Alternate => stm32_metapac::timer::vals::CcmrInputCcs::TI3,
162 InputTISelection::TRC => stm32_metapac::timer::vals::CcmrInputCcs::TRC,
163 }
164 }
165}
166
167#[derive(Clone, Copy)]
108pub enum OutputCompareMode { 168pub enum OutputCompareMode {
109 Frozen, 169 Frozen,
110 ActiveOnMatch, 170 ActiveOnMatch,
@@ -211,6 +271,7 @@ macro_rules! impl_basic_16bit_timer {
211 use core::convert::TryInto; 271 use core::convert::TryInto;
212 let f = frequency.0; 272 let f = frequency.0;
213 let timer_f = Self::frequency().0; 273 let timer_f = Self::frequency().0;
274 assert!(f > 0);
214 let pclk_ticks_per_timer_period = timer_f / f; 275 let pclk_ticks_per_timer_period = timer_f / f;
215 let psc: u16 = unwrap!(((pclk_ticks_per_timer_period - 1) / (1 << 16)).try_into()); 276 let psc: u16 = unwrap!(((pclk_ticks_per_timer_period - 1) / (1 << 16)).try_into());
216 let arr: u16 = unwrap!((pclk_ticks_per_timer_period / (u32::from(psc) + 1)).try_into()); 277 let arr: u16 = unwrap!((pclk_ticks_per_timer_period / (u32::from(psc) + 1)).try_into());
@@ -240,6 +301,10 @@ macro_rules! impl_basic_16bit_timer {
240 fn enable_update_interrupt(&mut self, enable: bool) { 301 fn enable_update_interrupt(&mut self, enable: bool) {
241 Self::regs().dier().write(|r| r.set_uie(enable)); 302 Self::regs().dier().write(|r| r.set_uie(enable));
242 } 303 }
304
305 fn set_autoreload_preload(&mut self, enable: vals::Arpe) {
306 Self::regs().cr1().modify(|r| r.set_arpe(enable));
307 }
243 } 308 }
244 }; 309 };
245} 310}
@@ -255,6 +320,7 @@ macro_rules! impl_32bit_timer {
255 fn set_frequency(&mut self, frequency: Hertz) { 320 fn set_frequency(&mut self, frequency: Hertz) {
256 use core::convert::TryInto; 321 use core::convert::TryInto;
257 let f = frequency.0; 322 let f = frequency.0;
323 assert!(f > 0);
258 let timer_f = Self::frequency().0; 324 let timer_f = Self::frequency().0;
259 let pclk_ticks_per_timer_period = (timer_f / f) as u64; 325 let pclk_ticks_per_timer_period = (timer_f / f) as u64;
260 let psc: u16 = unwrap!(((pclk_ticks_per_timer_period - 1) / (1 << 32)).try_into()); 326 let psc: u16 = unwrap!(((pclk_ticks_per_timer_period - 1) / (1 << 32)).try_into());
@@ -276,6 +342,59 @@ macro_rules! impl_32bit_timer {
276macro_rules! impl_compare_capable_16bit { 342macro_rules! impl_compare_capable_16bit {
277 ($inst:ident) => { 343 ($inst:ident) => {
278 impl sealed::CaptureCompare16bitInstance for crate::peripherals::$inst { 344 impl sealed::CaptureCompare16bitInstance for crate::peripherals::$inst {
345 fn set_input_capture_filter(&mut self, channel: Channel, icf: vals::Icf) {
346 use sealed::GeneralPurpose16bitInstance;
347 let raw_channel = channel.raw();
348 Self::regs_gp16()
349 .ccmr_input(raw_channel / 2)
350 .modify(|r| r.set_icf(raw_channel % 2, icf));
351 }
352
353 fn clear_input_interrupt(&mut self, channel: Channel) {
354 use sealed::GeneralPurpose16bitInstance;
355 Self::regs_gp16()
356 .sr()
357 .modify(|r| r.set_ccif(channel.raw(), false));
358 }
359
360 fn enable_input_interrupt(&mut self, channel: Channel, enable: bool) {
361 use sealed::GeneralPurpose16bitInstance;
362 Self::regs_gp16()
363 .dier()
364 .modify(|r| r.set_ccie(channel.raw(), enable));
365 }
366 fn set_input_capture_prescaler(&mut self, channel: Channel, factor: u8) {
367 use sealed::GeneralPurpose16bitInstance;
368 let raw_channel = channel.raw();
369 Self::regs_gp16()
370 .ccmr_input(raw_channel / 2)
371 .modify(|r| r.set_icpsc(raw_channel % 2, factor));
372 }
373
374 fn set_input_ti_selection(&mut self, channel: Channel, tisel: InputTISelection) {
375 use sealed::GeneralPurpose16bitInstance;
376 let raw_channel = channel.raw();
377 Self::regs_gp16()
378 .ccmr_input(raw_channel / 2)
379 .modify(|r| r.set_ccs(raw_channel % 2, tisel.into()));
380 }
381 fn set_input_capture_mode(&mut self, channel: Channel, mode: InputCaptureMode) {
382 use sealed::GeneralPurpose16bitInstance;
383 Self::regs_gp16().ccer().modify(|r| match mode {
384 InputCaptureMode::Rising => {
385 r.set_ccnp(channel.raw(), false);
386 r.set_ccp(channel.raw(), false);
387 }
388 InputCaptureMode::Falling => {
389 r.set_ccnp(channel.raw(), false);
390 r.set_ccp(channel.raw(), true);
391 }
392 InputCaptureMode::BothEdges => {
393 r.set_ccnp(channel.raw(), true);
394 r.set_ccp(channel.raw(), true);
395 }
396 });
397 }
279 fn enable_outputs(&mut self, _enable: bool) {} 398 fn enable_outputs(&mut self, _enable: bool) {}
280 399
281 fn set_output_compare_mode(&mut self, channel: Channel, mode: OutputCompareMode) { 400 fn set_output_compare_mode(&mut self, channel: Channel, mode: OutputCompareMode) {
@@ -305,6 +424,11 @@ macro_rules! impl_compare_capable_16bit {
305 Self::regs_gp16().ccr(channel.raw()).modify(|w| w.set_ccr(value)); 424 Self::regs_gp16().ccr(channel.raw()).modify(|w| w.set_ccr(value));
306 } 425 }
307 426
427 fn get_capture_value(&mut self, channel: Channel) -> u16 {
428 use sealed::GeneralPurpose16bitInstance;
429 Self::regs_gp16().ccr(channel.raw()).read().ccr()
430 }
431
308 fn get_max_compare_value(&self) -> u16 { 432 fn get_max_compare_value(&self) -> u16 {
309 use sealed::GeneralPurpose16bitInstance; 433 use sealed::GeneralPurpose16bitInstance;
310 Self::regs_gp16().arr().read().arr() 434 Self::regs_gp16().arr().read().arr()
@@ -329,6 +453,14 @@ foreach_interrupt! {
329 fn regs_gp16() -> crate::pac::timer::TimGp16 { 453 fn regs_gp16() -> crate::pac::timer::TimGp16 {
330 crate::pac::$inst 454 crate::pac::$inst
331 } 455 }
456
457 fn set_count_direction(&mut self, direction: vals::Dir) {
458 Self::regs_gp16().cr1().modify(|r| r.set_dir(direction));
459 }
460
461 fn set_clock_division(&mut self, ckd: vals::Ckd) {
462 Self::regs_gp16().cr1().modify(|r| r.set_ckd(ckd));
463 }
332 } 464 }
333 }; 465 };
334 466
@@ -343,6 +475,59 @@ foreach_interrupt! {
343 impl GeneralPurpose32bitInstance for crate::peripherals::$inst {} 475 impl GeneralPurpose32bitInstance for crate::peripherals::$inst {}
344 476
345 impl sealed::CaptureCompare32bitInstance for crate::peripherals::$inst { 477 impl sealed::CaptureCompare32bitInstance for crate::peripherals::$inst {
478 fn set_input_capture_filter(&mut self, channel: Channel, icf: vals::Icf) {
479 use sealed::GeneralPurpose32bitInstance;
480 let raw_channel = channel.raw();
481 Self::regs_gp32()
482 .ccmr_input(raw_channel / 2)
483 .modify(|r| r.set_icf(raw_channel % 2, icf));
484 }
485
486 fn clear_input_interrupt(&mut self, channel: Channel) {
487 use sealed::GeneralPurpose32bitInstance;
488 Self::regs_gp32()
489 .sr()
490 .modify(|r| r.set_ccif(channel.raw(), false));
491 }
492 fn enable_input_interrupt(&mut self, channel: Channel, enable: bool) {
493 use sealed::GeneralPurpose32bitInstance;
494 Self::regs_gp32()
495 .dier()
496 .modify(|r| r.set_ccie(channel.raw(), enable));
497 }
498 fn set_input_capture_prescaler(&mut self, channel: Channel, factor: u8) {
499 use crate::timer::sealed::GeneralPurpose32bitInstance;
500 let raw_channel = channel.raw();
501 Self::regs_gp32()
502 .ccmr_input(raw_channel / 2)
503 .modify(|r| r.set_icpsc(raw_channel % 2, factor));
504 }
505
506 fn set_input_ti_selection(&mut self, channel: Channel, tisel: InputTISelection) {
507 use crate::timer::sealed::GeneralPurpose32bitInstance;
508 let raw_channel = channel.raw();
509 Self::regs_gp32()
510 .ccmr_input(raw_channel / 2)
511 .modify(|r| r.set_ccs(raw_channel % 2, tisel.into()));
512 }
513
514 fn set_input_capture_mode(&mut self, channel: Channel, mode: InputCaptureMode) {
515 use crate::timer::sealed::GeneralPurpose32bitInstance;
516 Self::regs_gp32().ccer().modify(|r| match mode {
517 InputCaptureMode::Rising => {
518 r.set_ccnp(channel.raw(), false);
519 r.set_ccp(channel.raw(), false);
520 }
521 InputCaptureMode::Falling => {
522 r.set_ccnp(channel.raw(), false);
523 r.set_ccp(channel.raw(), true);
524 }
525 InputCaptureMode::BothEdges => {
526 r.set_ccnp(channel.raw(), true);
527 r.set_ccp(channel.raw(), true);
528 }
529 });
530 }
346 fn set_output_compare_mode( 531 fn set_output_compare_mode(
347 &mut self, 532 &mut self,
348 channel: Channel, 533 channel: Channel,
@@ -370,6 +555,11 @@ foreach_interrupt! {
370 Self::regs_gp32().ccr(channel.raw()).modify(|w| w.set_ccr(value)); 555 Self::regs_gp32().ccr(channel.raw()).modify(|w| w.set_ccr(value));
371 } 556 }
372 557
558 fn get_capture_value(&mut self, channel: Channel) -> u32 {
559 use crate::timer::sealed::GeneralPurpose32bitInstance;
560 Self::regs_gp32().ccr(channel.raw()).read().ccr()
561 }
562
373 fn get_max_compare_value(&self) -> u32 { 563 fn get_max_compare_value(&self) -> u32 {
374 use crate::timer::sealed::GeneralPurpose32bitInstance; 564 use crate::timer::sealed::GeneralPurpose32bitInstance;
375 Self::regs_gp32().arr().read().arr() as u32 565 Self::regs_gp32().arr().read().arr() as u32
@@ -380,6 +570,14 @@ foreach_interrupt! {
380 fn regs_gp16() -> crate::pac::timer::TimGp16 { 570 fn regs_gp16() -> crate::pac::timer::TimGp16 {
381 unsafe { crate::pac::timer::TimGp16::from_ptr(crate::pac::$inst.as_ptr()) } 571 unsafe { crate::pac::timer::TimGp16::from_ptr(crate::pac::$inst.as_ptr()) }
382 } 572 }
573
574 fn set_count_direction(&mut self, direction: vals::Dir) {
575 Self::regs_gp16().cr1().modify(|r| r.set_dir(direction));
576 }
577
578 fn set_clock_division(&mut self, ckd: vals::Ckd) {
579 Self::regs_gp16().cr1().modify(|r| r.set_ckd(ckd));
580 }
383 } 581 }
384 }; 582 };
385 583
@@ -396,6 +594,14 @@ foreach_interrupt! {
396 fn regs_gp16() -> crate::pac::timer::TimGp16 { 594 fn regs_gp16() -> crate::pac::timer::TimGp16 {
397 unsafe { crate::pac::timer::TimGp16::from_ptr(crate::pac::$inst.as_ptr()) } 595 unsafe { crate::pac::timer::TimGp16::from_ptr(crate::pac::$inst.as_ptr()) }
398 } 596 }
597
598 fn set_count_direction(&mut self, direction: vals::Dir) {
599 Self::regs_gp16().cr1().modify(|r| r.set_dir(direction));
600 }
601
602 fn set_clock_division(&mut self, ckd: vals::Ckd) {
603 Self::regs_gp16().cr1().modify(|r| r.set_ckd(ckd));
604 }
399 } 605 }
400 606
401 impl sealed::AdvancedControlInstance for crate::peripherals::$inst { 607 impl sealed::AdvancedControlInstance for crate::peripherals::$inst {
@@ -405,6 +611,57 @@ foreach_interrupt! {
405 } 611 }
406 612
407 impl sealed::CaptureCompare16bitInstance for crate::peripherals::$inst { 613 impl sealed::CaptureCompare16bitInstance for crate::peripherals::$inst {
614 fn set_input_capture_filter(&mut self, channel: Channel, icf: vals::Icf) {
615 use crate::timer::sealed::AdvancedControlInstance;
616 let raw_channel = channel.raw();
617 Self::regs_advanced()
618 .ccmr_input(raw_channel / 2)
619 .modify(|r| r.set_icf(raw_channel % 2, icf));
620 }
621
622 fn clear_input_interrupt(&mut self, channel: Channel) {
623 use crate::timer::sealed::AdvancedControlInstance;
624 Self::regs_advanced()
625 .sr()
626 .modify(|r| r.set_ccif(channel.raw(), false));
627 }
628 fn enable_input_interrupt(&mut self, channel: Channel, enable: bool) {
629 use crate::timer::sealed::AdvancedControlInstance;
630 Self::regs_advanced()
631 .dier()
632 .modify(|r| r.set_ccie(channel.raw(), enable));
633 }
634 fn set_input_capture_prescaler(&mut self, channel: Channel, factor: u8) {
635 use crate::timer::sealed::AdvancedControlInstance;
636 let raw_channel = channel.raw();
637 Self::regs_advanced()
638 .ccmr_input(raw_channel / 2)
639 .modify(|r| r.set_icpsc(raw_channel % 2, factor));
640 }
641 fn set_input_ti_selection(&mut self, channel: Channel, tisel: InputTISelection) {
642 use crate::timer::sealed::AdvancedControlInstance;
643 let raw_channel = channel.raw();
644 Self::regs_advanced()
645 .ccmr_input(raw_channel / 2)
646 .modify(|r| r.set_ccs(raw_channel % 2, tisel.into()));
647 }
648 fn set_input_capture_mode(&mut self, channel: Channel, mode: InputCaptureMode) {
649 use crate::timer::sealed::AdvancedControlInstance;
650 Self::regs_advanced().ccer().modify(|r| match mode {
651 InputCaptureMode::Rising => {
652 r.set_ccnp(channel.raw(), false);
653 r.set_ccp(channel.raw(), false);
654 }
655 InputCaptureMode::Falling => {
656 r.set_ccnp(channel.raw(), false);
657 r.set_ccp(channel.raw(), true);
658 }
659 InputCaptureMode::BothEdges => {
660 r.set_ccnp(channel.raw(), true);
661 r.set_ccp(channel.raw(), true);
662 }
663 });
664 }
408 fn enable_outputs(&mut self, enable: bool) { 665 fn enable_outputs(&mut self, enable: bool) {
409 use crate::timer::sealed::AdvancedControlInstance; 666 use crate::timer::sealed::AdvancedControlInstance;
410 let r = Self::regs_advanced(); 667 let r = Self::regs_advanced();
@@ -437,6 +694,11 @@ foreach_interrupt! {
437 .modify(|w| w.set_cce(channel.raw(), enable)); 694 .modify(|w| w.set_cce(channel.raw(), enable));
438 } 695 }
439 696
697 fn get_capture_value(&mut self, channel: Channel) -> u16 {
698 use crate::timer::sealed::AdvancedControlInstance;
699 Self::regs_advanced().ccr(channel.raw()).read().ccr()
700 }
701
440 fn set_compare_value(&mut self, channel: Channel, value: u16) { 702 fn set_compare_value(&mut self, channel: Channel, value: u16) {
441 use crate::timer::sealed::AdvancedControlInstance; 703 use crate::timer::sealed::AdvancedControlInstance;
442 Self::regs_advanced() 704 Self::regs_advanced()