aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxoviat <[email protected]>2023-09-30 17:57:22 +0000
committerGitHub <[email protected]>2023-09-30 17:57:22 +0000
commit7ad102695d0b9f285ca13b6dd9ebb37a3b148bf7 (patch)
tree1be7f60392d9ab4aa38581468ec27f5f7d3d4f15
parentfc8f96fea5b0689b2ae9a152f2da2b91dfaea750 (diff)
parent4999de6f82e78c129dddf3d97fc1282b6c7d0e66 (diff)
Merge pull request #1981 from xoviat/fix-traits
Fix traits
-rw-r--r--embassy-stm32/src/timer/mod.rs620
1 files changed, 160 insertions, 460 deletions
diff --git a/embassy-stm32/src/timer/mod.rs b/embassy-stm32/src/timer/mod.rs
index 1d642ed37..d88fbcfdb 100644
--- a/embassy-stm32/src/timer/mod.rs
+++ b/embassy-stm32/src/timer/mod.rs
@@ -5,7 +5,6 @@ pub mod simple_pwm;
5use stm32_metapac::timer::vals; 5use stm32_metapac::timer::vals;
6 6
7use crate::interrupt; 7use crate::interrupt;
8use crate::rcc::sealed::RccPeripheral as __RccPeri;
9use crate::rcc::RccPeripheral; 8use crate::rcc::RccPeripheral;
10use crate::time::Hertz; 9use crate::time::Hertz;
11 10
@@ -22,33 +21,88 @@ pub(crate) mod sealed {
22 21
23 fn regs() -> crate::pac::timer::TimBasic; 22 fn regs() -> crate::pac::timer::TimBasic;
24 23
25 fn start(&mut self); 24 fn start(&mut self) {
25 Self::regs().cr1().modify(|r| r.set_cen(true));
26 }
26 27
27 fn stop(&mut self); 28 fn stop(&mut self) {
29 Self::regs().cr1().modify(|r| r.set_cen(false));
30 }
28 31
29 fn reset(&mut self); 32 fn reset(&mut self) {
33 Self::regs().cnt().write(|r| r.set_cnt(0));
34 }
30 35
31 fn set_frequency(&mut self, frequency: Hertz); 36 fn set_frequency(&mut self, frequency: Hertz) {
37 let f = frequency.0;
38 let timer_f = Self::frequency().0;
39 assert!(f > 0);
40 let pclk_ticks_per_timer_period = timer_f / f;
41 let psc: u16 = unwrap!(((pclk_ticks_per_timer_period - 1) / (1 << 16)).try_into());
42 let arr: u16 = unwrap!((pclk_ticks_per_timer_period / (u32::from(psc) + 1)).try_into());
43
44 let regs = Self::regs();
45 regs.psc().write(|r| r.set_psc(psc));
46 regs.arr().write(|r| r.set_arr(arr));
47
48 regs.cr1().modify(|r| r.set_urs(vals::Urs::COUNTERONLY));
49 regs.egr().write(|r| r.set_ug(true));
50 regs.cr1().modify(|r| r.set_urs(vals::Urs::ANYEVENT));
51 }
32 52
33 fn clear_update_interrupt(&mut self) -> bool; 53 fn clear_update_interrupt(&mut self) -> bool {
54 let regs = Self::regs();
55 let sr = regs.sr().read();
56 if sr.uif() {
57 regs.sr().modify(|r| {
58 r.set_uif(false);
59 });
60 true
61 } else {
62 false
63 }
64 }
34 65
35 fn enable_update_interrupt(&mut self, enable: bool); 66 fn enable_update_interrupt(&mut self, enable: bool) {
67 Self::regs().dier().write(|r| r.set_uie(enable));
68 }
36 69
37 fn set_autoreload_preload(&mut self, enable: vals::Arpe); 70 fn set_autoreload_preload(&mut self, enable: vals::Arpe) {
71 Self::regs().cr1().modify(|r| r.set_arpe(enable));
72 }
38 } 73 }
39 74
40 pub trait GeneralPurpose16bitInstance: Basic16bitInstance { 75 pub trait GeneralPurpose16bitInstance: Basic16bitInstance {
41 fn regs_gp16() -> crate::pac::timer::TimGp16; 76 fn regs_gp16() -> crate::pac::timer::TimGp16;
42 77
43 fn set_count_direction(&mut self, direction: vals::Dir); 78 fn set_count_direction(&mut self, direction: vals::Dir) {
79 Self::regs_gp16().cr1().modify(|r| r.set_dir(direction));
80 }
44 81
45 fn set_clock_division(&mut self, ckd: vals::Ckd); 82 fn set_clock_division(&mut self, ckd: vals::Ckd) {
83 Self::regs_gp16().cr1().modify(|r| r.set_ckd(ckd));
84 }
46 } 85 }
47 86
48 pub trait GeneralPurpose32bitInstance: GeneralPurpose16bitInstance { 87 pub trait GeneralPurpose32bitInstance: GeneralPurpose16bitInstance {
49 fn regs_gp32() -> crate::pac::timer::TimGp32; 88 fn regs_gp32() -> crate::pac::timer::TimGp32;
50 89
51 fn set_frequency(&mut self, frequency: Hertz); 90 fn set_frequency(&mut self, frequency: Hertz) {
91 let f = frequency.0;
92 assert!(f > 0);
93 let timer_f = Self::frequency().0;
94 let pclk_ticks_per_timer_period = (timer_f / f) as u64;
95 let psc: u16 = unwrap!(((pclk_ticks_per_timer_period - 1) / (1 << 32)).try_into());
96 let arr: u32 = unwrap!((pclk_ticks_per_timer_period / (psc as u64 + 1)).try_into());
97
98 let regs = Self::regs_gp32();
99 regs.psc().write(|r| r.set_psc(psc));
100 regs.arr().write(|r| r.set_arr(arr));
101
102 regs.cr1().modify(|r| r.set_urs(vals::Urs::COUNTERONLY));
103 regs.egr().write(|r| r.set_ug(true));
104 regs.cr1().modify(|r| r.set_urs(vals::Urs::ANYEVENT));
105 }
52 } 106 }
53 107
54 pub trait AdvancedControlInstance: GeneralPurpose16bitInstance { 108 pub trait AdvancedControlInstance: GeneralPurpose16bitInstance {
@@ -56,68 +110,115 @@ pub(crate) mod sealed {
56 } 110 }
57 111
58 pub trait CaptureCompare16bitInstance: GeneralPurpose16bitInstance { 112 pub trait CaptureCompare16bitInstance: GeneralPurpose16bitInstance {
59 fn set_input_capture_filter(&mut self, channel: Channel, icf: vals::Icf); 113 fn set_input_capture_filter(&mut self, channel: Channel, icf: vals::Icf) {
60 114 let raw_channel = channel.raw();
61 fn clear_input_interrupt(&mut self, channel: Channel); 115 Self::regs_gp16()
62 116 .ccmr_input(raw_channel / 2)
63 fn enable_input_interrupt(&mut self, channel: Channel, enable: bool); 117 .modify(|r| r.set_icf(raw_channel % 2, icf));
64 118 }
65 fn set_input_capture_prescaler(&mut self, channel: Channel, val: u8);
66 119
67 fn set_input_ti_selection(&mut self, channel: Channel, tisel: InputTISelection); 120 fn clear_input_interrupt(&mut self, channel: Channel) {
121 Self::regs_gp16().sr().modify(|r| r.set_ccif(channel.raw(), false));
122 }
68 123
69 fn set_input_capture_mode(&mut self, channel: Channel, mode: InputCaptureMode); 124 fn enable_input_interrupt(&mut self, channel: Channel, enable: bool) {
125 Self::regs_gp16().dier().modify(|r| r.set_ccie(channel.raw(), enable));
126 }
127 fn set_input_capture_prescaler(&mut self, channel: Channel, factor: u8) {
128 let raw_channel = channel.raw();
129 Self::regs_gp16()
130 .ccmr_input(raw_channel / 2)
131 .modify(|r| r.set_icpsc(raw_channel % 2, factor));
132 }
70 133
71 /// Global output enable. Does not do anything on non-advanced timers. 134 fn set_input_ti_selection(&mut self, channel: Channel, tisel: InputTISelection) {
72 fn enable_outputs(&mut self, enable: bool); 135 let raw_channel = channel.raw();
136 Self::regs_gp16()
137 .ccmr_input(raw_channel / 2)
138 .modify(|r| r.set_ccs(raw_channel % 2, tisel.into()));
139 }
140 fn set_input_capture_mode(&mut self, channel: Channel, mode: InputCaptureMode) {
141 Self::regs_gp16().ccer().modify(|r| match mode {
142 InputCaptureMode::Rising => {
143 r.set_ccnp(channel.raw(), false);
144 r.set_ccp(channel.raw(), false);
145 }
146 InputCaptureMode::Falling => {
147 r.set_ccnp(channel.raw(), false);
148 r.set_ccp(channel.raw(), true);
149 }
150 InputCaptureMode::BothEdges => {
151 r.set_ccnp(channel.raw(), true);
152 r.set_ccp(channel.raw(), true);
153 }
154 });
155 }
156 fn enable_outputs(&mut self, _enable: bool) {}
73 157
74 fn set_output_compare_mode(&mut self, channel: Channel, mode: OutputCompareMode); 158 fn set_output_compare_mode(&mut self, channel: Channel, mode: OutputCompareMode) {
159 let r = Self::regs_gp16();
160 let raw_channel: usize = channel.raw();
161 r.ccmr_output(raw_channel / 2)
162 .modify(|w| w.set_ocm(raw_channel % 2, mode.into()));
163 }
75 164
76 fn set_output_polarity(&mut self, channel: Channel, polarity: OutputPolarity); 165 fn set_output_polarity(&mut self, channel: Channel, polarity: OutputPolarity) {
166 Self::regs_gp16()
167 .ccer()
168 .modify(|w| w.set_ccp(channel.raw(), polarity.into()));
169 }
77 170
78 fn enable_channel(&mut self, channel: Channel, enable: bool); 171 fn enable_channel(&mut self, channel: Channel, enable: bool) {
172 Self::regs_gp16().ccer().modify(|w| w.set_cce(channel.raw(), enable));
173 }
79 174
80 fn set_compare_value(&mut self, channel: Channel, value: u16); 175 fn set_compare_value(&mut self, channel: Channel, value: u16) {
176 Self::regs_gp16().ccr(channel.raw()).modify(|w| w.set_ccr(value));
177 }
81 178
82 fn get_capture_value(&mut self, channel: Channel) -> u16; 179 fn get_capture_value(&mut self, channel: Channel) -> u16 {
180 Self::regs_gp16().ccr(channel.raw()).read().ccr()
181 }
83 182
84 fn get_max_compare_value(&self) -> u16; 183 fn get_max_compare_value(&self) -> u16 {
184 Self::regs_gp16().arr().read().arr()
185 }
85 } 186 }
86 187
87 pub trait ComplementaryCaptureCompare16bitInstance: CaptureCompare16bitInstance { 188 pub trait ComplementaryCaptureCompare16bitInstance: CaptureCompare16bitInstance + AdvancedControlInstance {
88 fn set_complementary_output_polarity(&mut self, channel: Channel, polarity: OutputPolarity); 189 fn set_complementary_output_polarity(&mut self, channel: Channel, polarity: OutputPolarity) {
190 Self::regs_advanced()
191 .ccer()
192 .modify(|w| w.set_ccnp(channel.raw(), polarity.into()));
193 }
89 194
90 fn set_dead_time_clock_division(&mut self, value: vals::Ckd); 195 fn set_dead_time_clock_division(&mut self, value: vals::Ckd) {
196 Self::regs_advanced().cr1().modify(|w| w.set_ckd(value));
197 }
91 198
92 fn set_dead_time_value(&mut self, value: u8); 199 fn set_dead_time_value(&mut self, value: u8) {
200 Self::regs_advanced().bdtr().modify(|w| w.set_dtg(value));
201 }
93 202
94 fn enable_complementary_channel(&mut self, channel: Channel, enable: bool); 203 fn enable_complementary_channel(&mut self, channel: Channel, enable: bool) {
204 Self::regs_advanced()
205 .ccer()
206 .modify(|w| w.set_ccne(channel.raw(), enable));
207 }
95 } 208 }
96 209
97 pub trait CaptureCompare32bitInstance: GeneralPurpose32bitInstance { 210 pub trait CaptureCompare32bitInstance: GeneralPurpose32bitInstance + CaptureCompare16bitInstance {
98 fn set_input_capture_filter(&mut self, channel: Channel, icf: vals::Icf); 211 fn set_compare_value(&mut self, channel: Channel, value: u32) {
99 212 Self::regs_gp32().ccr(channel.raw()).modify(|w| w.set_ccr(value));
100 fn clear_input_interrupt(&mut self, channel: Channel); 213 }
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
110 fn set_output_compare_mode(&mut self, channel: Channel, mode: OutputCompareMode);
111
112 fn set_output_polarity(&mut self, channel: Channel, polarity: OutputPolarity);
113
114 fn enable_channel(&mut self, channel: Channel, enable: bool);
115
116 fn set_compare_value(&mut self, channel: Channel, value: u32);
117 214
118 fn get_capture_value(&mut self, channel: Channel) -> u32; 215 fn get_capture_value(&mut self, channel: Channel) -> u32 {
216 Self::regs_gp32().ccr(channel.raw()).read().ccr()
217 }
119 218
120 fn get_max_compare_value(&self) -> u32; 219 fn get_max_compare_value(&self) -> u32 {
220 Self::regs_gp32().arr().read().arr()
221 }
121 } 222 }
122} 223}
123 224
@@ -254,57 +355,6 @@ macro_rules! impl_basic_16bit_timer {
254 fn regs() -> crate::pac::timer::TimBasic { 355 fn regs() -> crate::pac::timer::TimBasic {
255 unsafe { crate::pac::timer::TimBasic::from_ptr(crate::pac::$inst.as_ptr()) } 356 unsafe { crate::pac::timer::TimBasic::from_ptr(crate::pac::$inst.as_ptr()) }
256 } 357 }
257
258 fn start(&mut self) {
259 Self::regs().cr1().modify(|r| r.set_cen(true));
260 }
261
262 fn stop(&mut self) {
263 Self::regs().cr1().modify(|r| r.set_cen(false));
264 }
265
266 fn reset(&mut self) {
267 Self::regs().cnt().write(|r| r.set_cnt(0));
268 }
269
270 fn set_frequency(&mut self, frequency: Hertz) {
271 use core::convert::TryInto;
272 let f = frequency.0;
273 let timer_f = Self::frequency().0;
274 assert!(f > 0);
275 let pclk_ticks_per_timer_period = timer_f / f;
276 let psc: u16 = unwrap!(((pclk_ticks_per_timer_period - 1) / (1 << 16)).try_into());
277 let arr: u16 = unwrap!((pclk_ticks_per_timer_period / (u32::from(psc) + 1)).try_into());
278
279 let regs = Self::regs();
280 regs.psc().write(|r| r.set_psc(psc));
281 regs.arr().write(|r| r.set_arr(arr));
282
283 regs.cr1().modify(|r| r.set_urs(vals::Urs::COUNTERONLY));
284 regs.egr().write(|r| r.set_ug(true));
285 regs.cr1().modify(|r| r.set_urs(vals::Urs::ANYEVENT));
286 }
287
288 fn clear_update_interrupt(&mut self) -> bool {
289 let regs = Self::regs();
290 let sr = regs.sr().read();
291 if sr.uif() {
292 regs.sr().modify(|r| {
293 r.set_uif(false);
294 });
295 true
296 } else {
297 false
298 }
299 }
300
301 fn enable_update_interrupt(&mut self, enable: bool) {
302 Self::regs().dier().write(|r| r.set_uie(enable));
303 }
304
305 fn set_autoreload_preload(&mut self, enable: vals::Arpe) {
306 Self::regs().cr1().modify(|r| r.set_arpe(enable));
307 }
308 } 358 }
309 }; 359 };
310} 360}
@@ -316,24 +366,6 @@ macro_rules! impl_32bit_timer {
316 fn regs_gp32() -> crate::pac::timer::TimGp32 { 366 fn regs_gp32() -> crate::pac::timer::TimGp32 {
317 crate::pac::$inst 367 crate::pac::$inst
318 } 368 }
319
320 fn set_frequency(&mut self, frequency: Hertz) {
321 use core::convert::TryInto;
322 let f = frequency.0;
323 assert!(f > 0);
324 let timer_f = Self::frequency().0;
325 let pclk_ticks_per_timer_period = (timer_f / f) as u64;
326 let psc: u16 = unwrap!(((pclk_ticks_per_timer_period - 1) / (1 << 32)).try_into());
327 let arr: u32 = unwrap!(((pclk_ticks_per_timer_period / (psc as u64 + 1)).try_into()));
328
329 let regs = Self::regs_gp32();
330 regs.psc().write(|r| r.set_psc(psc));
331 regs.arr().write(|r| r.set_arr(arr));
332
333 regs.cr1().modify(|r| r.set_urs(vals::Urs::COUNTERONLY));
334 regs.egr().write(|r| r.set_ug(true));
335 regs.cr1().modify(|r| r.set_urs(vals::Urs::ANYEVENT));
336 }
337 } 369 }
338 }; 370 };
339} 371}
@@ -341,99 +373,7 @@ macro_rules! impl_32bit_timer {
341#[allow(unused)] 373#[allow(unused)]
342macro_rules! impl_compare_capable_16bit { 374macro_rules! impl_compare_capable_16bit {
343 ($inst:ident) => { 375 ($inst:ident) => {
344 impl sealed::CaptureCompare16bitInstance for crate::peripherals::$inst { 376 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 }
398 fn enable_outputs(&mut self, _enable: bool) {}
399
400 fn set_output_compare_mode(&mut self, channel: Channel, mode: OutputCompareMode) {
401 use sealed::GeneralPurpose16bitInstance;
402 let r = Self::regs_gp16();
403 let raw_channel: usize = channel.raw();
404 r.ccmr_output(raw_channel / 2)
405 .modify(|w| w.set_ocm(raw_channel % 2, mode.into()));
406 }
407
408 fn set_output_polarity(&mut self, channel: Channel, polarity: OutputPolarity) {
409 use sealed::GeneralPurpose16bitInstance;
410 Self::regs_gp16()
411 .ccer()
412 .modify(|w| w.set_ccp(channel.raw(), polarity.into()));
413 }
414
415 fn enable_channel(&mut self, channel: Channel, enable: bool) {
416 use sealed::GeneralPurpose16bitInstance;
417 Self::regs_gp16()
418 .ccer()
419 .modify(|w| w.set_cce(channel.raw(), enable));
420 }
421
422 fn set_compare_value(&mut self, channel: Channel, value: u16) {
423 use sealed::GeneralPurpose16bitInstance;
424 Self::regs_gp16().ccr(channel.raw()).modify(|w| w.set_ccr(value));
425 }
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
432 fn get_max_compare_value(&self) -> u16 {
433 use sealed::GeneralPurpose16bitInstance;
434 Self::regs_gp16().arr().read().arr()
435 }
436 }
437 }; 377 };
438} 378}
439 379
@@ -453,14 +393,6 @@ foreach_interrupt! {
453 fn regs_gp16() -> crate::pac::timer::TimGp16 { 393 fn regs_gp16() -> crate::pac::timer::TimGp16 {
454 crate::pac::$inst 394 crate::pac::$inst
455 } 395 }
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 }
464 } 396 }
465 }; 397 };
466 398
@@ -473,111 +405,12 @@ foreach_interrupt! {
473 impl CaptureCompare32bitInstance for crate::peripherals::$inst {} 405 impl CaptureCompare32bitInstance for crate::peripherals::$inst {}
474 impl GeneralPurpose16bitInstance for crate::peripherals::$inst {} 406 impl GeneralPurpose16bitInstance for crate::peripherals::$inst {}
475 impl GeneralPurpose32bitInstance for crate::peripherals::$inst {} 407 impl GeneralPurpose32bitInstance for crate::peripherals::$inst {}
476 408 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 }
531 fn set_output_compare_mode(
532 &mut self,
533 channel: Channel,
534 mode: OutputCompareMode,
535 ) {
536 use crate::timer::sealed::GeneralPurpose32bitInstance;
537 let raw_channel = channel.raw();
538 Self::regs_gp32().ccmr_output(raw_channel / 2).modify(|w| w.set_ocm(raw_channel % 2, mode.into()));
539 }
540
541 fn set_output_polarity(&mut self, channel: Channel, polarity: OutputPolarity) {
542 use crate::timer::sealed::GeneralPurpose32bitInstance;
543 Self::regs_gp32()
544 .ccer()
545 .modify(|w| w.set_ccp(channel.raw(), polarity.into()));
546 }
547
548 fn enable_channel(&mut self, channel: Channel, enable: bool) {
549 use crate::timer::sealed::GeneralPurpose32bitInstance;
550 Self::regs_gp32().ccer().modify(|w| w.set_cce(channel.raw(), enable));
551 }
552
553 fn set_compare_value(&mut self, channel: Channel, value: u32) {
554 use crate::timer::sealed::GeneralPurpose32bitInstance;
555 Self::regs_gp32().ccr(channel.raw()).modify(|w| w.set_ccr(value));
556 }
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
563 fn get_max_compare_value(&self) -> u32 {
564 use crate::timer::sealed::GeneralPurpose32bitInstance;
565 Self::regs_gp32().arr().read().arr() as u32
566 }
567 }
568 409
569 impl sealed::GeneralPurpose16bitInstance for crate::peripherals::$inst { 410 impl sealed::GeneralPurpose16bitInstance for crate::peripherals::$inst {
570 fn regs_gp16() -> crate::pac::timer::TimGp16 { 411 fn regs_gp16() -> crate::pac::timer::TimGp16 {
571 unsafe { crate::pac::timer::TimGp16::from_ptr(crate::pac::$inst.as_ptr()) } 412 unsafe { crate::pac::timer::TimGp16::from_ptr(crate::pac::$inst.as_ptr()) }
572 } 413 }
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 }
581 } 414 }
582 }; 415 };
583 416
@@ -589,19 +422,12 @@ foreach_interrupt! {
589 impl CaptureCompare16bitInstance for crate::peripherals::$inst {} 422 impl CaptureCompare16bitInstance for crate::peripherals::$inst {}
590 impl ComplementaryCaptureCompare16bitInstance for crate::peripherals::$inst {} 423 impl ComplementaryCaptureCompare16bitInstance for crate::peripherals::$inst {}
591 impl AdvancedControlInstance for crate::peripherals::$inst {} 424 impl AdvancedControlInstance for crate::peripherals::$inst {}
592 425 impl sealed::CaptureCompare16bitInstance for crate::peripherals::$inst {}
426 impl sealed::ComplementaryCaptureCompare16bitInstance for crate::peripherals::$inst {}
593 impl sealed::GeneralPurpose16bitInstance for crate::peripherals::$inst { 427 impl sealed::GeneralPurpose16bitInstance for crate::peripherals::$inst {
594 fn regs_gp16() -> crate::pac::timer::TimGp16 { 428 fn regs_gp16() -> crate::pac::timer::TimGp16 {
595 unsafe { crate::pac::timer::TimGp16::from_ptr(crate::pac::$inst.as_ptr()) } 429 unsafe { crate::pac::timer::TimGp16::from_ptr(crate::pac::$inst.as_ptr()) }
596 } 430 }
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 }
605 } 431 }
606 432
607 impl sealed::AdvancedControlInstance for crate::peripherals::$inst { 433 impl sealed::AdvancedControlInstance for crate::peripherals::$inst {
@@ -610,133 +436,7 @@ foreach_interrupt! {
610 } 436 }
611 } 437 }
612 438
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 439
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 }
665 fn enable_outputs(&mut self, enable: bool) {
666 use crate::timer::sealed::AdvancedControlInstance;
667 let r = Self::regs_advanced();
668 r.bdtr().modify(|w| w.set_moe(enable));
669 }
670
671 fn set_output_compare_mode(
672 &mut self,
673 channel: Channel,
674 mode: OutputCompareMode,
675 ) {
676 use crate::timer::sealed::AdvancedControlInstance;
677 let r = Self::regs_advanced();
678 let raw_channel: usize = channel.raw();
679 r.ccmr_output(raw_channel / 2)
680 .modify(|w| w.set_ocm(raw_channel % 2, mode.into()));
681 }
682
683 fn set_output_polarity(&mut self, channel: Channel, polarity: OutputPolarity) {
684 use crate::timer::sealed::AdvancedControlInstance;
685 Self::regs_advanced()
686 .ccer()
687 .modify(|w| w.set_ccp(channel.raw(), polarity.into()));
688 }
689
690 fn enable_channel(&mut self, channel: Channel, enable: bool) {
691 use crate::timer::sealed::AdvancedControlInstance;
692 Self::regs_advanced()
693 .ccer()
694 .modify(|w| w.set_cce(channel.raw(), enable));
695 }
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
702 fn set_compare_value(&mut self, channel: Channel, value: u16) {
703 use crate::timer::sealed::AdvancedControlInstance;
704 Self::regs_advanced()
705 .ccr(channel.raw())
706 .modify(|w| w.set_ccr(value));
707 }
708
709 fn get_max_compare_value(&self) -> u16 {
710 use crate::timer::sealed::AdvancedControlInstance;
711 Self::regs_advanced().arr().read().arr()
712 }
713 }
714
715 impl sealed::ComplementaryCaptureCompare16bitInstance for crate::peripherals::$inst {
716 fn set_complementary_output_polarity(&mut self, channel: Channel, polarity: OutputPolarity) {
717 use crate::timer::sealed::AdvancedControlInstance;
718 Self::regs_advanced()
719 .ccer()
720 .modify(|w| w.set_ccnp(channel.raw(), polarity.into()));
721 }
722
723 fn set_dead_time_clock_division(&mut self, value: vals::Ckd) {
724 use crate::timer::sealed::AdvancedControlInstance;
725 Self::regs_advanced().cr1().modify(|w| w.set_ckd(value));
726 }
727
728 fn set_dead_time_value(&mut self, value: u8) {
729 use crate::timer::sealed::AdvancedControlInstance;
730 Self::regs_advanced().bdtr().modify(|w| w.set_dtg(value));
731 }
732
733 fn enable_complementary_channel(&mut self, channel: Channel, enable: bool) {
734 use crate::timer::sealed::AdvancedControlInstance;
735 Self::regs_advanced()
736 .ccer()
737 .modify(|w| w.set_ccne(channel.raw(), enable));
738 }
739 }
740 440
741 441
742 }; 442 };