stm32-hrtim 0.2.0

STM32 HRTIM peripheral driver
Documentation
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#[cfg(feature = "hrtim_v2")]
use crate::adc_trigger;
#[cfg(feature = "hrtim_v2")]
use crate::fault::FltMonitor6;
use crate::fault::{
    FltMonitor1, FltMonitor2, FltMonitor3, FltMonitor4, FltMonitor5, FltMonitorSys,
};

use crate::timer::{self, HrTimer};
use crate::{pac, pac::HRTIM_COMMON};

use super::{external_event::EevInputs, fault::FaultInputs};

impl HrTimOngoingCalibration {
    /// Look in the hal for an corresponding extension trait for `HRTIM_COMMON`.
    ///
    /// ..unless you are the one implementing the hal
    ///
    /// # Safety
    /// The user is expected to have setup and enabled rcc clock to the peripheral
    pub unsafe fn hr_control() -> HrTimOngoingCalibration {
        #[allow(unused_variables)]
        let common = unsafe { &*HRTIM_COMMON::ptr() };

        // Start calibration procedure
        #[cfg(not(feature = "stm32h7"))]
        common
            .dllcr()
            .write(|w| w.cal().set_bit().calen().clear_bit());

        HrTimOngoingCalibration {
            #[cfg(feature = "stm32g4")]
            adc_trigger1_postscaler: AdcTriggerPostscaler::None,
            #[cfg(feature = "stm32g4")]
            adc_trigger2_postscaler: AdcTriggerPostscaler::None,
            #[cfg(feature = "stm32g4")]
            adc_trigger3_postscaler: AdcTriggerPostscaler::None,
            #[cfg(feature = "stm32g4")]
            adc_trigger4_postscaler: AdcTriggerPostscaler::None,

            #[cfg(feature = "stm32g4")]
            adc_trigger5_postscaler: AdcTriggerPostscaler::None,
            #[cfg(feature = "stm32g4")]
            adc_trigger6_postscaler: AdcTriggerPostscaler::None,
            #[cfg(feature = "stm32g4")]
            adc_trigger7_postscaler: AdcTriggerPostscaler::None,
            #[cfg(feature = "stm32g4")]
            adc_trigger8_postscaler: AdcTriggerPostscaler::None,
            #[cfg(feature = "stm32g4")]
            adc_trigger9_postscaler: AdcTriggerPostscaler::None,
            #[cfg(feature = "stm32g4")]
            adc_trigger10_postscaler: AdcTriggerPostscaler::None,

            flt_divider: SamplingClkDiv::None,
            eev_divider: SamplingClkDiv::None,
        }
    }
}

pub struct HrTimOngoingCalibration {
    #[cfg(feature = "stm32g4")]
    adc_trigger1_postscaler: AdcTriggerPostscaler,
    #[cfg(feature = "stm32g4")]
    adc_trigger2_postscaler: AdcTriggerPostscaler,
    #[cfg(feature = "stm32g4")]
    adc_trigger3_postscaler: AdcTriggerPostscaler,
    #[cfg(feature = "stm32g4")]
    adc_trigger4_postscaler: AdcTriggerPostscaler,

    #[cfg(feature = "stm32g4")]
    adc_trigger5_postscaler: AdcTriggerPostscaler,
    #[cfg(feature = "stm32g4")]
    adc_trigger6_postscaler: AdcTriggerPostscaler,
    #[cfg(feature = "stm32g4")]
    adc_trigger7_postscaler: AdcTriggerPostscaler,
    #[cfg(feature = "stm32g4")]
    adc_trigger8_postscaler: AdcTriggerPostscaler,
    #[cfg(feature = "stm32g4")]
    adc_trigger9_postscaler: AdcTriggerPostscaler,
    #[cfg(feature = "stm32g4")]
    adc_trigger10_postscaler: AdcTriggerPostscaler,

    flt_divider: SamplingClkDiv,
    eev_divider: SamplingClkDiv,
}

impl HrTimOngoingCalibration {
    /// SAFETY: Calibration needs to be done before calling this
    unsafe fn init(self) {
        let common = unsafe { &*HRTIM_COMMON::ptr() };

        let Self {
            #[cfg(feature = "stm32g4")]
            adc_trigger1_postscaler,
            #[cfg(feature = "stm32g4")]
            adc_trigger2_postscaler,
            #[cfg(feature = "stm32g4")]
            adc_trigger3_postscaler,
            #[cfg(feature = "stm32g4")]
            adc_trigger4_postscaler,

            #[cfg(feature = "stm32g4")]
            adc_trigger5_postscaler,
            #[cfg(feature = "stm32g4")]
            adc_trigger6_postscaler,
            #[cfg(feature = "stm32g4")]
            adc_trigger7_postscaler,
            #[cfg(feature = "stm32g4")]
            adc_trigger8_postscaler,
            #[cfg(feature = "stm32g4")]
            adc_trigger9_postscaler,
            #[cfg(feature = "stm32g4")]
            adc_trigger10_postscaler,

            flt_divider,
            eev_divider,
        } = self;

        unsafe {
            // Enable periodic calibration
            // with f_hrtim at 170MHz, these settings leads to
            // a period of about 6.2ms
            #[cfg(not(feature = "stm32h7"))]
            common
                .dllcr()
                .modify(|_r, w| w.calrte().bits(0b00).cal().set_bit().calen().clear_bit());
            common
                .fltinr2()
                .write(|w| w.fltsd().bits(flt_divider as u8));

            common.eecr3().write(|w| w.eevsd().bits(eev_divider as u8));

            #[cfg(feature = "stm32g4")]
            common.adcps1().write(|w| {
                w.adc1psc()
                    .bits(adc_trigger1_postscaler as u8)
                    .adc2psc()
                    .bits(adc_trigger2_postscaler as u8)
                    .adc3psc()
                    .bits(adc_trigger3_postscaler as u8)
                    .adc4psc()
                    .bits(adc_trigger4_postscaler as u8)
                    .adc5psc()
                    .bits(adc_trigger5_postscaler as u8)
            });

            #[cfg(feature = "stm32g4")]
            common.adcps2().write(|w| {
                w.adc6psc()
                    .bits(adc_trigger6_postscaler as u8)
                    .adc7psc()
                    .bits(adc_trigger7_postscaler as u8)
                    .adc8psc()
                    .bits(adc_trigger8_postscaler as u8)
                    .adc9psc()
                    .bits(adc_trigger9_postscaler as u8)
                    .adc10psc()
                    .bits(adc_trigger10_postscaler as u8)
            });

            // TODO: Adc trigger 5-10
        }
    }

    pub fn wait_for_calibration(self) -> (HrTimCalibrated, FaultInputs, EevInputs) {
        #[cfg(not(feature = "stm32h7"))]
        {
            let common = unsafe { &*HRTIM_COMMON::ptr() };
            while common.isr().read().dllrdy().bit_is_clear() {
                // Wait until ready
            }
        }

        // Calibration is now done, it is safe to continue
        unsafe { self.init() };

        (HrTimCalibrated, unsafe { FaultInputs::new() }, unsafe {
            EevInputs::new()
        })
    }

    #[cfg(feature = "stm32g4")]
    pub fn set_adc1_trigger_psc(mut self, post_scaler: AdcTriggerPostscaler) -> Self {
        self.adc_trigger1_postscaler = post_scaler;
        self
    }

    #[cfg(feature = "stm32g4")]
    pub fn set_adc2_trigger_psc(mut self, post_scaler: AdcTriggerPostscaler) -> Self {
        self.adc_trigger2_postscaler = post_scaler;
        self
    }

    #[cfg(feature = "stm32g4")]
    pub fn set_adc3_trigger_psc(mut self, post_scaler: AdcTriggerPostscaler) -> Self {
        self.adc_trigger3_postscaler = post_scaler;
        self
    }

    #[cfg(feature = "stm32g4")]
    pub fn set_adc4_trigger_psc(mut self, post_scaler: AdcTriggerPostscaler) -> Self {
        self.adc_trigger4_postscaler = post_scaler;
        self
    }

    pub fn set_fault_sampling_division(mut self, divider: SamplingClkDiv) -> Self {
        self.flt_divider = divider;
        self
    }

    pub fn set_eev_sampling_division(mut self, divider: SamplingClkDiv) -> Self {
        self.eev_divider = divider;
        self
    }
}

/// This object may be used for things that needs to be done before any timers have been started but after the calibration has been completed. Its existence is proof that no timers have started.
///
/// Once done with setup, use the `constrain` to get a `HrPwmControl` which can be used to start the timers.
#[non_exhaustive]
pub struct HrTimCalibrated;

impl HrTimCalibrated {
    pub fn constrain(self) -> HrPwmControl {
        HrPwmControl {
            control: HrPwmCtrl,
            fault_sys: FltMonitorSys,
            fault_1: FltMonitor1,
            fault_2: FltMonitor2,
            fault_3: FltMonitor3,
            fault_4: FltMonitor4,
            fault_5: FltMonitor5,
            #[cfg(feature = "hrtim_v2")]
            fault_6: FltMonitor6,

            #[cfg(feature = "stm32g4")]
            adc_trigger1: adc_trigger::AdcTrigger1,
            #[cfg(feature = "stm32g4")]
            adc_trigger2: adc_trigger::AdcTrigger2,
            #[cfg(feature = "stm32g4")]
            adc_trigger3: adc_trigger::AdcTrigger3,
            #[cfg(feature = "stm32g4")]
            adc_trigger4: adc_trigger::AdcTrigger4,
            #[cfg(feature = "stm32g4")]
            adc_trigger5: adc_trigger::AdcTrigger5,
            #[cfg(feature = "stm32g4")]
            adc_trigger6: adc_trigger::AdcTrigger6,
            #[cfg(feature = "stm32g4")]
            adc_trigger7: adc_trigger::AdcTrigger7,
            #[cfg(feature = "stm32g4")]
            adc_trigger8: adc_trigger::AdcTrigger8,
            #[cfg(feature = "stm32g4")]
            adc_trigger9: adc_trigger::AdcTrigger9,
            #[cfg(feature = "stm32g4")]
            adc_trigger10: adc_trigger::AdcTrigger10,
        }
    }
}

impl<'a> From<&'a mut HrPwmControl> for &'a mut HrPwmCtrl {
    fn from(val: &'a mut HrPwmControl) -> Self {
        &mut val.control
    }
}

/// Used as a token to guarantee unique access to resources common to multiple timers
///
/// An instance of this object can be obtained from [`HrPwmControl`].control
#[non_exhaustive]
pub struct HrPwmCtrl;

pub struct W<'a>(&'a mut pac::hrtim_master::cr::W);

impl<'a> W<'a> {
    pub fn start<T: HrTimer>(self, _t: &mut T) -> Self {
        use crate::timer::Instance;

        let w = self.0;
        W(match T::Timer::TIMX {
            timer::Timer::Master => w.mcen().set_bit(),
            timer::Timer::Tim(v) => w.tcen(v as _).set_bit(),
        })
    }
    pub fn stop<T: HrTimer>(self, _t: &mut T) -> Self {
        use crate::timer::Instance;

        let w = self.0;
        W(match T::Timer::TIMX {
            timer::Timer::Master => w.mcen().clear_bit(),
            timer::Timer::Tim(v) => w.tcen(v as _).clear_bit(),
        })
    }
}

impl HrPwmCtrl {
    /// Start/stop multiple timers at the exact same time
    ///
    /// ```
    /// let mut timer_a = ...;
    /// let mut timer_b = ...;
    /// let mut timer_c = ...;
    /// hr_control.start_stop_timers(|w| w
    ///     .start(&mut timer_a)
    ///     .start(&mut timer_b)
    ///     .stop(&mut timer_c)
    /// );
    /// ```
    pub fn start_stop_timers(&mut self, p: impl FnOnce(W) -> W) {
        let master = unsafe { pac::HRTIM_MASTER::steal() };
        master.cr().modify(|_, w| p(W(w)).0);
    }
}

/// Used as a token to guarantee unique access to resources common to multiple timers
#[non_exhaustive]
pub struct HrPwmControl {
    pub control: HrPwmCtrl,

    pub fault_sys: FltMonitorSys,
    pub fault_1: FltMonitor1,
    pub fault_2: FltMonitor2,
    pub fault_3: FltMonitor3,
    pub fault_4: FltMonitor4,
    pub fault_5: FltMonitor5,
    #[cfg(feature = "stm32g4")]
    pub fault_6: FltMonitor6,

    #[cfg(feature = "stm32g4")]
    pub adc_trigger1: adc_trigger::AdcTrigger1,
    #[cfg(feature = "stm32g4")]
    pub adc_trigger2: adc_trigger::AdcTrigger2,
    #[cfg(feature = "stm32g4")]
    pub adc_trigger3: adc_trigger::AdcTrigger3,
    #[cfg(feature = "stm32g4")]
    pub adc_trigger4: adc_trigger::AdcTrigger4,

    #[cfg(feature = "stm32g4")]
    pub adc_trigger5: adc_trigger::AdcTrigger5,
    #[cfg(feature = "stm32g4")]
    pub adc_trigger6: adc_trigger::AdcTrigger6,
    #[cfg(feature = "stm32g4")]
    pub adc_trigger7: adc_trigger::AdcTrigger7,
    #[cfg(feature = "stm32g4")]
    pub adc_trigger8: adc_trigger::AdcTrigger8,
    #[cfg(feature = "stm32g4")]
    pub adc_trigger9: adc_trigger::AdcTrigger9,
    #[cfg(feature = "stm32g4")]
    pub adc_trigger10: adc_trigger::AdcTrigger10,
}

#[cfg(feature = "stm32g4")]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum AdcTriggerPostscaler {
    None = 0,
    Div2 = 1,
    Div3 = 2,
    Div4 = 3,
    Div5 = 4,
    Div6 = 5,
    Div7 = 6,
    Div8 = 7,
    Div9 = 8,
    Div10 = 9,
    Div11 = 10,
    Div12 = 11,
    Div13 = 12,
    Div14 = 13,
    Div15 = 14,
    Div16 = 15,
    Div17 = 16,
    Div18 = 17,
    Div19 = 18,
    Div20 = 19,
    Div21 = 20,
    Div22 = 21,
    Div23 = 22,
    Div24 = 23,
    Div25 = 24,
    Div26 = 25,
    Div27 = 26,
    Div28 = 27,
    Div29 = 28,
    Div30 = 29,
    Div31 = 30,
    Div32 = 31,
}

/// The divsion ratio between f_hrtim and the fault signal sampling clock for digital filters
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Debug, Clone, Copy)]
pub enum SamplingClkDiv {
    /// No division
    ///
    /// fault signal sampling clock f_flts = f_hrtim
    None = 0b00,

    /// 1/2
    ///
    /// fault signal sampling clock f_flts = f_hrtim / 2
    Two = 0b01,

    /// 1/4
    ///
    /// fault signal sampling clock f_flts = f_hrtim / 4
    Four = 0b10,

    /// 1/8
    ///
    /// fault signal sampling clock f_flts = f_hrtim / 8
    Eight = 0b11,
}