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 Ok(unsafe { self.set_auto_reload_unchecked(arr) })
62 } else {
63 Err(Error::WrongAutoReload)
64 }
65 }
66
67 #[inline(always)]
68 fn read_auto_reload(&self) -> u32 {
69 self.arr().read().bits()
70 }
71
72 #[inline(always)]
73 fn set_prescaler(&mut self, psc: u16) {
74 self.psc().write(|w| w.psc().set(psc));
75 }
76
77 #[inline(always)]
78 fn read_prescaler(&self) -> u16 {
79 self.psc().read().psc().bits()
80 }
81
82 #[inline(always)]
83 fn read_count(&self) -> u32 {
84 self.cnt().read().bits() as u32
85 }
86
87 #[inline(always)]
88 fn trigger_update(&mut self) {
89 self.cr1().modify(|_, w| w.urs().set_bit());
92 self.egr().write(|w| w.ug().set_bit());
93 self.cr1().modify(|_, w| w.urs().clear_bit());
94 }
95
96 #[inline]
97 fn config_freq(&mut self, clock: Hertz, update_freq: Hertz) {
98 let (prescaler, arr) = compute_prescaler_arr(clock.raw(), update_freq.raw());
99 self.set_prescaler(prescaler as u16);
100 self.set_auto_reload(arr).unwrap();
101 self.trigger_update();
103 }
104
105 #[inline(always)]
106 fn clear_interrupt_flag(&mut self, event: Event) {
107 self.sr()
108 .write(|w| unsafe { w.bits(0xffff & !event.bits()) });
109 }
110
111 #[inline(always)]
112 fn listen_interrupt(&mut self, event: Event, b: bool) {
113 self.dier().modify(|r, w| unsafe {
114 w.bits(if b {
115 r.bits() | event.bits()
116 } else {
117 r.bits() & !event.bits()
118 })
119 });
120 }
121
122 #[inline(always)]
123 fn get_interrupt_flag(&self) -> Event {
124 Event::from_bits_truncate(self.sr().read().bits())
125 }
126
127 #[inline(always)]
128 fn start_one_pulse(&mut self) {
129 self.cr1().modify(|_, w| w.opm().set_bit().cen().set_bit());
130 }
131
132 #[inline(always)]
133 fn stop_in_debug(&mut self, state: bool) {
134 let dbg = unsafe { DBG::steal() };
135 }
140
141 #[inline(always)]
142 fn enable_preload(&mut self, b: bool) {
143 self.cr1().modify(|_, w| w.arpe().bit(b));
144 }
145}
146
147impl TimerWithPwm for TimerX {
151 fn stop_pwm(&mut self) {
152 self.disable_counter();
153 }
154
155 #[inline(always)]
158 fn start_pwm(&mut self) {
159 self.reset_counter();
160 self.enable_counter();
161 }
162
163 #[inline(always)]
166 fn preload_output_channel_in_mode(&mut self, channel: Channel, mode: PwmMode) {
167 let mode = Ocm::from(mode);
168 match channel {
169 Channel::C1 => {
170 self.ccmr1_output()
171 .modify(|_, w| w.oc1pe().set_bit().oc1m().set(mode as _));
172 }
173 Channel::C2 => {
174 self.ccmr1_output()
175 .modify(|_, w| w.oc2pe().set_bit().oc2m().set(mode as _));
176 }
177 _ => (),
178 }
179 }
180
181 fn set_polarity(&mut self, channel: Channel, polarity: PwmPolarity) {
182 match channel {
183 Channel::C1 => {
184 self.ccer()
185 .modify(|_, w| w.cc1p().bit(polarity == PwmPolarity::ActiveLow));
186 }
187 Channel::C2 => {
188 self.ccer()
189 .modify(|_, w| w.cc2p().bit(polarity == PwmPolarity::ActiveLow));
190 }
191 _ => (),
192 }
193 }
194}
195
196impl TimerWithPwm1Ch for TimerX {
200 #[inline(always)]
201 fn enable_ch1(&mut self, en: bool) {
202 self.ccer().modify(|_, w| w.cc1e().bit(en));
203 }
204
205 #[inline(always)]
206 fn set_ch1_cc_value(&mut self, value: u32) {
207 unsafe { self.ccr1().write(|w| w.bits(value)) };
208 }
209
210 #[inline(always)]
211 fn get_ch1_cc_value(&self) -> u32 {
212 self.ccr1().read().bits()
213 }
214}
215
216impl TimerWithPwm2Ch for TimerX {
219 #[inline(always)]
220 fn enable_ch2(&mut self, en: bool) {
221 self.ccer().modify(|_, w| w.cc2e().bit(en));
222 }
223
224 #[inline(always)]
225 fn set_ch2_cc_value(&mut self, value: u32) {
226 unsafe { self.ccr2().write(|w| w.bits(value)) };
227 }
228
229 #[inline(always)]
230 fn get_ch2_cc_value(&self) -> u32 {
231 self.ccr2().read().bits()
232 }
233}
234
235