stm32f1_hal/timer/
timer9.rs1#![allow(unused_variables)]
2type TimerX = pac::TIM9;
3type Width = u16;
4
5use super::*;
8use crate::{Mcu, pac};
9
10impl Instance for TimerX {}
11
12impl TimerInit<TimerX> for TimerX {
13 fn constrain(self, mcu: &mut Mcu) -> Timer<TimerX> {
14 Timer::new(self, mcu)
15 }
16}
17
18impl GeneralTimer for TimerX {
19 #[inline(always)]
20 fn reset_config(&mut self) {
21 self.cr1().reset();
22 }
23
24 #[inline(always)]
25 fn enable_counter(&mut self) {
26 self.cr1().modify(|_, w| w.cen().set_bit());
27 }
28
29 #[inline(always)]
30 fn disable_counter(&mut self) {
31 self.cr1().modify(|_, w| w.cen().clear_bit());
32 }
33
34 #[inline(always)]
35 fn is_counter_enabled(&self) -> bool {
36 self.cr1().read().cen().is_enabled()
37 }
38
39 #[inline(always)]
40 fn reset_counter(&mut self) {
41 self.cnt().reset();
42 }
43
44 #[inline(always)]
45 fn max_auto_reload() -> u32 {
46 Width::MAX as u32
47 }
48
49 #[inline(always)]
50 unsafe fn set_auto_reload_unchecked(&mut self, arr: u32) {
51 unsafe {
52 self.arr().write(|w| w.bits(arr));
53 }
54 }
55
56 #[inline(always)]
57 fn set_auto_reload(&mut self, arr: u32) -> Result<(), Error> {
58 if arr > 0 && arr <= Self::max_auto_reload() {
61 unsafe { self.set_auto_reload_unchecked(arr) }
62 Ok(())
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()
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, update_freq: Hertz) {
99 let (prescaler, arr) = compute_prescaler_arr(clock.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 }
141
142 #[inline(always)]
143 fn enable_preload(&mut self, b: bool) {
144 self.cr1().modify(|_, w| w.arpe().bit(b));
145 }
146}
147
148impl TimerWithPwm for TimerX {
152 fn stop_pwm(&mut self) {
153 self.disable_counter();
154 }
155
156 #[inline(always)]
159 fn start_pwm(&mut self) {
160 self.reset_counter();
161 self.enable_counter();
162 }
163
164 #[inline(always)]
167 fn preload_output_channel_in_mode(&mut self, channel: Channel, mode: PwmMode) {
168 let mode = Ocm::from(mode);
169 match channel {
170 Channel::C1 => {
171 self.ccmr1_output()
172 .modify(|_, w| w.oc1pe().set_bit().oc1m().set(mode as _));
173 }
174 Channel::C2 => {
175 self.ccmr1_output()
176 .modify(|_, w| w.oc2pe().set_bit().oc2m().set(mode as _));
177 }
178 _ => (),
179 }
180 }
181
182 fn set_polarity(&mut self, channel: Channel, polarity: PwmPolarity) {
183 match channel {
184 Channel::C1 => {
185 self.ccer()
186 .modify(|_, w| w.cc1p().bit(polarity == PwmPolarity::ActiveLow));
187 }
188 Channel::C2 => {
189 self.ccer()
190 .modify(|_, w| w.cc2p().bit(polarity == PwmPolarity::ActiveLow));
191 }
192 _ => (),
193 }
194 }
195}
196
197impl TimerWithPwm1Ch for TimerX {
201 #[inline(always)]
202 fn enable_ch1(&mut self, en: bool) {
203 self.ccer().modify(|_, w| w.cc1e().bit(en));
204 }
205
206 #[inline(always)]
207 fn set_ch1_cc_value(&mut self, value: u32) {
208 unsafe { self.ccr1().write(|w| w.bits(value)) };
209 }
210
211 #[inline(always)]
212 fn get_ch1_cc_value(&self) -> u32 {
213 self.ccr1().read().bits()
214 }
215}
216
217impl TimerWithPwm2Ch for TimerX {
220 #[inline(always)]
221 fn enable_ch2(&mut self, en: bool) {
222 self.ccer().modify(|_, w| w.cc2e().bit(en));
223 }
224
225 #[inline(always)]
226 fn set_ch2_cc_value(&mut self, value: u32) {
227 unsafe { self.ccr2().write(|w| w.bits(value)) };
228 }
229
230 #[inline(always)]
231 fn get_ch2_cc_value(&self) -> u32 {
232 self.ccr2().read().bits()
233 }
234}
235
236