-
Notifications
You must be signed in to change notification settings - Fork 774
/
mod.rs
381 lines (323 loc) · 12.5 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//! Timers, PWM, quadrature decoder.
use core::marker::PhantomData;
use embassy_sync::waitqueue::AtomicWaker;
#[cfg(not(stm32l0))]
pub mod complementary_pwm;
pub mod input_capture;
pub mod low_level;
pub mod pwm_input;
pub mod qei;
pub mod simple_pwm;
use crate::interrupt;
use crate::rcc::RccPeripheral;
/// Timer channel.
#[derive(Clone, Copy)]
pub enum Channel {
/// Channel 1.
Ch1,
/// Channel 2.
Ch2,
/// Channel 3.
Ch3,
/// Channel 4.
Ch4,
}
impl Channel {
/// Get the channel index (0..3)
pub fn index(&self) -> usize {
match self {
Channel::Ch1 => 0,
Channel::Ch2 => 1,
Channel::Ch3 => 2,
Channel::Ch4 => 3,
}
}
}
/// Amount of bits of a timer.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum TimerBits {
/// 16 bits.
Bits16,
/// 32 bits.
#[cfg(not(stm32l0))]
Bits32,
}
struct State {
up_waker: AtomicWaker,
cc_waker: [AtomicWaker; 4],
}
impl State {
const fn new() -> Self {
const NEW_AW: AtomicWaker = AtomicWaker::new();
Self {
up_waker: NEW_AW,
cc_waker: [NEW_AW; 4],
}
}
}
trait SealedInstance: RccPeripheral {
/// Async state for this timer
fn state() -> &'static State;
}
/// Core timer instance.
#[allow(private_bounds)]
pub trait CoreInstance: SealedInstance + 'static {
/// Update Interrupt for this timer.
type UpdateInterrupt: interrupt::typelevel::Interrupt;
/// Amount of bits this timer has.
const BITS: TimerBits;
/// Registers for this timer.
///
/// This is a raw pointer to the register block. The actual register block layout varies depending on the timer type.
fn regs() -> *mut ();
}
/// Cut-down basic timer instance.
pub trait BasicNoCr2Instance: CoreInstance {}
/// Basic timer instance.
pub trait BasicInstance: BasicNoCr2Instance {}
/// General-purpose 16-bit timer with 1 channel instance.
pub trait GeneralInstance1Channel: CoreInstance {
/// Capture compare interrupt for this timer.
type CaptureCompareInterrupt: interrupt::typelevel::Interrupt;
}
/// General-purpose 16-bit timer with 2 channels instance.
pub trait GeneralInstance2Channel: GeneralInstance1Channel {
/// Trigger event interrupt for this timer.
type TriggerInterrupt: interrupt::typelevel::Interrupt;
}
// This trait add *extra* methods to GeneralInstance4Channel,
// that GeneralInstance4Channel doesn't use, but the "AdvancedInstance"s need.
// And it's a private trait, so it's content won't leak to outer namespace.
//
// If you want to add a new method to it, please leave a detail comment to explain it.
trait General4ChBlankSealed {
// SimplePwm<'d, T> is implemented for T: GeneralInstance4Channel
// Advanced timers implement this trait, but the output needs to be
// enabled explicitly.
// To support general-purpose and advanced timers, this function is added
// here defaulting to noop and overwritten for advanced timers.
//
// Enable timer outputs.
fn enable_outputs(&self) {}
}
/// General-purpose 16-bit timer with 4 channels instance.
#[allow(private_bounds)]
pub trait GeneralInstance4Channel: BasicInstance + GeneralInstance2Channel + General4ChBlankSealed {}
/// General-purpose 32-bit timer with 4 channels instance.
pub trait GeneralInstance32bit4Channel: GeneralInstance4Channel {}
/// Advanced 16-bit timer with 1 channel instance.
pub trait AdvancedInstance1Channel: BasicNoCr2Instance + GeneralInstance1Channel {
/// Communication interrupt for this timer.
type CommunicationInterrupt: interrupt::typelevel::Interrupt;
/// Break input interrupt for this timer.
type BreakInputInterrupt: interrupt::typelevel::Interrupt;
}
/// Advanced 16-bit timer with 2 channels instance.
pub trait AdvancedInstance2Channel: BasicInstance + GeneralInstance2Channel + AdvancedInstance1Channel {}
/// Advanced 16-bit timer with 4 channels instance.
pub trait AdvancedInstance4Channel: AdvancedInstance2Channel + GeneralInstance4Channel {}
pin_trait!(Channel1Pin, GeneralInstance4Channel);
pin_trait!(Channel2Pin, GeneralInstance4Channel);
pin_trait!(Channel3Pin, GeneralInstance4Channel);
pin_trait!(Channel4Pin, GeneralInstance4Channel);
pin_trait!(ExternalTriggerPin, GeneralInstance4Channel);
pin_trait!(Channel1ComplementaryPin, AdvancedInstance4Channel);
pin_trait!(Channel2ComplementaryPin, AdvancedInstance4Channel);
pin_trait!(Channel3ComplementaryPin, AdvancedInstance4Channel);
pin_trait!(Channel4ComplementaryPin, AdvancedInstance4Channel);
pin_trait!(BreakInputPin, AdvancedInstance4Channel);
pin_trait!(BreakInput2Pin, AdvancedInstance4Channel);
pin_trait!(BreakInputComparator1Pin, AdvancedInstance4Channel);
pin_trait!(BreakInputComparator2Pin, AdvancedInstance4Channel);
pin_trait!(BreakInput2Comparator1Pin, AdvancedInstance4Channel);
pin_trait!(BreakInput2Comparator2Pin, AdvancedInstance4Channel);
// Update Event trigger DMA for every timer
dma_trait!(UpDma, BasicInstance);
dma_trait!(Ch1Dma, GeneralInstance4Channel);
dma_trait!(Ch2Dma, GeneralInstance4Channel);
dma_trait!(Ch3Dma, GeneralInstance4Channel);
dma_trait!(Ch4Dma, GeneralInstance4Channel);
#[allow(unused)]
macro_rules! impl_core_timer {
($inst:ident, $bits:expr) => {
impl SealedInstance for crate::peripherals::$inst {
fn state() -> &'static State {
static STATE: State = State::new();
&STATE
}
}
impl CoreInstance for crate::peripherals::$inst {
type UpdateInterrupt = crate::_generated::peripheral_interrupts::$inst::UP;
const BITS: TimerBits = $bits;
fn regs() -> *mut () {
crate::pac::$inst.as_ptr()
}
}
};
}
#[allow(unused)]
macro_rules! impl_general_1ch {
($inst:ident) => {
impl GeneralInstance1Channel for crate::peripherals::$inst {
type CaptureCompareInterrupt = crate::_generated::peripheral_interrupts::$inst::CC;
}
};
}
#[allow(unused)]
macro_rules! impl_general_2ch {
($inst:ident) => {
impl GeneralInstance2Channel for crate::peripherals::$inst {
type TriggerInterrupt = crate::_generated::peripheral_interrupts::$inst::TRG;
}
};
}
#[allow(unused)]
macro_rules! impl_advanced_1ch {
($inst:ident) => {
impl AdvancedInstance1Channel for crate::peripherals::$inst {
type CommunicationInterrupt = crate::_generated::peripheral_interrupts::$inst::COM;
type BreakInputInterrupt = crate::_generated::peripheral_interrupts::$inst::BRK;
}
};
}
// This macro only apply to "AdvancedInstance(s)",
// not "GeneralInstance4Channel" itself.
#[allow(unused)]
macro_rules! impl_general_4ch_blank_sealed {
($inst:ident) => {
impl General4ChBlankSealed for crate::peripherals::$inst {
fn enable_outputs(&self) {
unsafe { crate::pac::timer::Tim1chCmp::from_ptr(Self::regs()) }
.bdtr()
.modify(|w| w.set_moe(true));
}
}
};
}
foreach_interrupt! {
($inst:ident, timer, TIM_BASIC, UP, $irq:ident) => {
impl_core_timer!($inst, TimerBits::Bits16);
impl BasicNoCr2Instance for crate::peripherals::$inst {}
impl BasicInstance for crate::peripherals::$inst {}
};
($inst:ident, timer, TIM_1CH, UP, $irq:ident) => {
impl_core_timer!($inst, TimerBits::Bits16);
impl BasicNoCr2Instance for crate::peripherals::$inst {}
impl BasicInstance for crate::peripherals::$inst {}
impl_general_1ch!($inst);
impl_general_2ch!($inst);
impl GeneralInstance4Channel for crate::peripherals::$inst {}
impl General4ChBlankSealed for crate::peripherals::$inst {}
};
($inst:ident, timer, TIM_2CH, UP, $irq:ident) => {
impl_core_timer!($inst, TimerBits::Bits16);
impl BasicNoCr2Instance for crate::peripherals::$inst {}
impl BasicInstance for crate::peripherals::$inst {}
impl_general_1ch!($inst);
impl_general_2ch!($inst);
impl GeneralInstance4Channel for crate::peripherals::$inst {}
impl General4ChBlankSealed for crate::peripherals::$inst {}
};
($inst:ident, timer, TIM_GP16, UP, $irq:ident) => {
impl_core_timer!($inst, TimerBits::Bits16);
impl BasicNoCr2Instance for crate::peripherals::$inst {}
impl BasicInstance for crate::peripherals::$inst {}
impl_general_1ch!($inst);
impl_general_2ch!($inst);
impl GeneralInstance4Channel for crate::peripherals::$inst {}
impl General4ChBlankSealed for crate::peripherals::$inst {}
};
($inst:ident, timer, TIM_GP32, UP, $irq:ident) => {
impl_core_timer!($inst, TimerBits::Bits32);
impl BasicNoCr2Instance for crate::peripherals::$inst {}
impl BasicInstance for crate::peripherals::$inst {}
impl_general_1ch!($inst);
impl_general_2ch!($inst);
impl GeneralInstance4Channel for crate::peripherals::$inst {}
impl GeneralInstance32bit4Channel for crate::peripherals::$inst {}
impl General4ChBlankSealed for crate::peripherals::$inst {}
};
($inst:ident, timer, TIM_1CH_CMP, UP, $irq:ident) => {
impl_core_timer!($inst, TimerBits::Bits16);
impl BasicNoCr2Instance for crate::peripherals::$inst {}
impl BasicInstance for crate::peripherals::$inst {}
impl_general_1ch!($inst);
impl_general_2ch!($inst);
impl GeneralInstance4Channel for crate::peripherals::$inst {}
impl_general_4ch_blank_sealed!($inst);
impl_advanced_1ch!($inst);
impl AdvancedInstance2Channel for crate::peripherals::$inst {}
impl AdvancedInstance4Channel for crate::peripherals::$inst {}
};
($inst:ident, timer, TIM_2CH_CMP, UP, $irq:ident) => {
impl_core_timer!($inst, TimerBits::Bits16);
impl BasicNoCr2Instance for crate::peripherals::$inst {}
impl BasicInstance for crate::peripherals::$inst {}
impl_general_1ch!($inst);
impl_general_2ch!($inst);
impl GeneralInstance4Channel for crate::peripherals::$inst {}
impl_general_4ch_blank_sealed!($inst);
impl_advanced_1ch!($inst);
impl AdvancedInstance2Channel for crate::peripherals::$inst {}
impl AdvancedInstance4Channel for crate::peripherals::$inst {}
};
($inst:ident, timer, TIM_ADV, UP, $irq:ident) => {
impl_core_timer!($inst, TimerBits::Bits16);
impl BasicNoCr2Instance for crate::peripherals::$inst {}
impl BasicInstance for crate::peripherals::$inst {}
impl_general_1ch!($inst);
impl_general_2ch!($inst);
impl GeneralInstance4Channel for crate::peripherals::$inst {}
impl_general_4ch_blank_sealed!($inst);
impl_advanced_1ch!($inst);
impl AdvancedInstance2Channel for crate::peripherals::$inst {}
impl AdvancedInstance4Channel for crate::peripherals::$inst {}
};
}
/// Update interrupt handler.
pub struct UpdateInterruptHandler<T: CoreInstance> {
_phantom: PhantomData<T>,
}
impl<T: CoreInstance> interrupt::typelevel::Handler<T::UpdateInterrupt> for UpdateInterruptHandler<T> {
unsafe fn on_interrupt() {
#[cfg(feature = "low-power")]
crate::low_power::on_wakeup_irq();
let regs = crate::pac::timer::TimCore::from_ptr(T::regs());
// Read TIM interrupt flags.
let sr = regs.sr().read();
// Mask relevant interrupts (UIE).
let bits = sr.0 & 0x00000001;
// Mask all the channels that fired.
regs.dier().modify(|w| w.0 &= !bits);
// Wake the tasks
if sr.uif() {
T::state().up_waker.wake();
}
}
}
/// Capture/Compare interrupt handler.
pub struct CaptureCompareInterruptHandler<T: GeneralInstance1Channel> {
_phantom: PhantomData<T>,
}
impl<T: GeneralInstance1Channel> interrupt::typelevel::Handler<T::CaptureCompareInterrupt>
for CaptureCompareInterruptHandler<T>
{
unsafe fn on_interrupt() {
#[cfg(feature = "low-power")]
crate::low_power::on_wakeup_irq();
let regs = crate::pac::timer::TimGp16::from_ptr(T::regs());
// Read TIM interrupt flags.
let sr = regs.sr().read();
// Mask relevant interrupts (CCIE).
let bits = sr.0 & 0x0000001E;
// Mask all the channels that fired.
regs.dier().modify(|w| w.0 &= !bits);
// Wake the tasks
for ch in 0..4 {
if sr.ccif(ch) {
T::state().cc_waker[ch].wake();
}
}
}
}