stm32f1_hal/timer/
timer8.rs1type TimerX = pac::TIM8;
2type Width = u16;
3
4use super::*;
9use crate::{Mcu, pac};
10
11impl Instance for TimerX {}
12
13impl TimerInit<TimerX> for TimerX {
14 fn constrain(self, mcu: &mut Mcu) -> Timer<TimerX> {
15 Timer::new(self, mcu)
16 }
17}
18
19impl GeneralTimer for TimerX {
20 #[inline(always)]
21 fn reset_config(&mut self) {
22 self.cr1().reset();
23 }
24
25 #[inline(always)]
26 fn enable_counter(&mut self) {
27 self.cr1().modify(|_, w| w.cen().set_bit());
28 }
29
30 #[inline(always)]
31 fn disable_counter(&mut self) {
32 self.cr1().modify(|_, w| w.cen().clear_bit());
33 }
34
35 #[inline(always)]
36 fn is_counter_enabled(&self) -> bool {
37 self.cr1().read().cen().is_enabled()
38 }
39
40 #[inline(always)]
41 fn reset_counter(&mut self) {
42 self.cnt().reset();
43 }
44
45 #[inline(always)]
46 fn max_auto_reload() -> u32 {
47 Width::MAX as u32
48 }
49
50 #[inline(always)]
51 unsafe fn set_auto_reload_unchecked(&mut self, arr: u32) {
52 unsafe {
53 self.arr().write(|w| w.bits(arr));
54 }
55 }
56
57 #[inline(always)]
58 fn set_auto_reload(&mut self, arr: u32) -> Result<(), Error> {
59 if arr > 0 && arr <= Self::max_auto_reload() {
62 Ok(unsafe { self.set_auto_reload_unchecked(arr) })
63 } else {
64 Err(Error::WrongAutoReload)
65 }
66 }
67
68 #[inline(always)]
69 fn read_auto_reload(&self) -> u32 {
70 self.arr().read().bits()
71 }
72
73 #[inline(always)]
74 fn set_prescaler(&mut self, psc: u16) {
75 self.psc().write(|w| w.psc().set(psc));
76 }
77
78 #[inline(always)]
79 fn read_prescaler(&self) -> u16 {
80 self.psc().read().psc().bits()
81 }
82
83 #[inline(always)]
84 fn read_count(&self) -> u32 {
85 self.cnt().read().bits() as u32
86 }
87
88 #[inline(always)]
89 fn trigger_update(&mut self) {
90 self.cr1().modify(|_, w| w.urs().set_bit());
93 self.egr().write(|w| w.ug().set_bit());
94 self.cr1().modify(|_, w| w.urs().clear_bit());
95 }
96
97 #[inline]
98 fn config_freq(&mut self, clock: Hertz, count_freq: Hertz, update_freq: Hertz) {
99 let (prescaler, arr) = freq_to_presc_arr(clock.raw(), count_freq.raw(), update_freq.raw());
100 self.set_prescaler(prescaler as u16);
101 self.set_auto_reload(arr).unwrap();
102 self.trigger_update();
104 }
105
106 #[inline(always)]
107 fn clear_interrupt_flag(&mut self, event: Event) {
108 self.sr()
109 .write(|w| unsafe { w.bits(0xffff & !event.bits()) });
110 }
111
112 #[inline(always)]
113 fn listen_interrupt(&mut self, event: Event, b: bool) {
114 self.dier().modify(|r, w| unsafe {
115 w.bits(if b {
116 r.bits() | event.bits()
117 } else {
118 r.bits() & !event.bits()
119 })
120 });
121 }
122
123 #[inline(always)]
124 fn get_interrupt_flag(&self) -> Event {
125 Event::from_bits_truncate(self.sr().read().bits())
126 }
127
128 #[inline(always)]
129 fn start_one_pulse(&mut self) {
130 self.cr1().modify(|_, w| w.opm().set_bit().cen().set_bit());
131 }
132
133 #[inline(always)]
134 fn stop_in_debug(&mut self, state: bool) {
135 let dbg = unsafe { DBG::steal() };
136 dbg.cr().modify(|_, w| w.dbg_tim8_stop().bit(state));
138 }
140}
141
142impl GeneralTimerExt for TimerX {
143 #[inline(always)]
144 fn enable_preload(&mut self, b: bool) {
145 self.cr1().modify(|_, w| w.arpe().bit(b));
146 }
147}
148
149impl TimerWithPwm for TimerX {
153 fn stop_pwm(&mut self) {
154 self.disable_counter();
155 }
156
157 #[inline(always)]
160 fn start_pwm(&mut self) {
161 self.bdtr().modify(|_, w| w.aoe().set_bit());
163 self.reset_counter();
164 self.enable_counter();
165 }
166
167 #[inline(always)]
170 fn preload_output_channel_in_mode(&mut self, channel: Channel, mode: PwmMode) {
171 let mode = Ocm::from(mode);
172 match channel {
173 Channel::C1 => {
174 self.ccmr1_output()
175 .modify(|_, w| w.oc1pe().set_bit().oc1m().set(mode as _));
176 }
177 Channel::C2 => {
178 self.ccmr1_output()
179 .modify(|_, w| w.oc2pe().set_bit().oc2m().set(mode as _));
180 }
181 Channel::C3 => {
182 self.ccmr2_output()
183 .modify(|_, w| w.oc3pe().set_bit().oc3m().set(mode as _));
184 }
185 Channel::C4 => {
186 self.ccmr2_output()
187 .modify(|_, w| w.oc4pe().set_bit().oc4m().set(mode as _));
188 }
189 }
190 }
191
192 fn set_polarity(&mut self, channel: Channel, polarity: PwmPolarity) {
193 match channel {
194 Channel::C1 => {
195 self.ccer()
196 .modify(|_, w| w.cc1p().bit(polarity == PwmPolarity::ActiveLow));
197 }
198 Channel::C2 => {
199 self.ccer()
200 .modify(|_, w| w.cc2p().bit(polarity == PwmPolarity::ActiveLow));
201 }
202 Channel::C3 => {
203 self.ccer()
204 .modify(|_, w| w.cc3p().bit(polarity == PwmPolarity::ActiveLow));
205 }
206 Channel::C4 => {
207 self.ccer()
208 .modify(|_, w| w.cc4p().bit(polarity == PwmPolarity::ActiveLow));
209 }
210 }
211 }
212}
213
214impl TimerWithPwm1Ch for TimerX {
218 #[inline(always)]
219 fn enable_ch1(&mut self, en: bool) {
220 self.ccer().modify(|_, w| w.cc1e().bit(en));
221 }
222
223 #[inline(always)]
224 fn set_ch1_cc_value(&mut self, value: u32) {
225 unsafe { self.ccr1().write(|w| w.bits(value)) };
226 }
227
228 #[inline(always)]
229 fn get_ch1_cc_value(&self) -> u32 {
230 self.ccr1().read().bits()
231 }
232}
233
234impl TimerWithPwm2Ch for TimerX {
237 #[inline(always)]
238 fn enable_ch2(&mut self, en: bool) {
239 self.ccer().modify(|_, w| w.cc2e().bit(en));
240 }
241
242 #[inline(always)]
243 fn set_ch2_cc_value(&mut self, value: u32) {
244 unsafe { self.ccr2().write(|w| w.bits(value)) };
245 }
246
247 #[inline(always)]
248 fn get_ch2_cc_value(&self) -> u32 {
249 self.ccr2().read().bits()
250 }
251}
252
253impl TimerWithPwm3Ch for TimerX {
256 #[inline(always)]
257 fn enable_ch3(&mut self, en: bool) {
258 self.ccer().modify(|_, w| w.cc3e().bit(en));
259 }
260
261 #[inline(always)]
262 fn set_ch3_cc_value(&mut self, value: u32) {
263 unsafe { self.ccr3().write(|w| w.bits(value)) };
264 }
265
266 #[inline(always)]
267 fn get_ch3_cc_value(&self) -> u32 {
268 self.ccr3().read().bits()
269 }
270}
271
272impl TimerWithPwm4Ch for TimerX {
273 #[inline(always)]
274 fn enable_ch4(&mut self, en: bool) {
275 self.ccer().modify(|_, w| w.cc4e().bit(en));
276 }
277
278 #[inline(always)]
279 fn set_ch4_cc_value(&mut self, value: u32) {
280 unsafe { self.ccr4().write(|w| w.bits(value)) };
281 }
282
283 #[inline(always)]
284 fn get_ch4_cc_value(&self) -> u32 {
285 self.ccr4().read().bits()
286 }
287}
288
289type Mms = pac::tim1::cr2::MMS;
293impl MasterTimer for TimerX {
295 type Mms = Mms;
296 #[inline(always)]
297 fn master_mode(&mut self, mode: Self::Mms) {
298 self.cr2().modify(|_, w| w.mms().variant(mode));
299 }
300}
301
302impl TimerDirection for TimerX {
305 #[inline(always)]
306 fn set_count_direction(&mut self, dir: CountDirection) {
307 self.cr1()
308 .modify(|_, w| w.dir().bit(dir == CountDirection::Down));
309 }
310}
311
312