msp430fr247x-hal 0.1.1

Implementation of embedded-hal for microcontrollers MSP430FR2475 and MSP430FR2476
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
//! Capture ports
//!
//! Configures the board's TimerB peripherals into capture pins. Each capture pin has a 16-bit
//! capture register where its timer value is written whenever its capture event is triggered.
//!
//! Due to hardware constraints, the configurations for all capture pins derived from a timer must
//! be decided before any of them can be used. This differs from `Pwm`, where pins are initialized
//! on an individual basis.

use crate::gpio::{
    Alternate2, Floating, Input, Pin, Pin0, Pin1, Pin2, Pin3, Pin4, Pin7,
    P4, P5
};
use crate::hw_traits::timerb::{CCRn, Ccis, Cm};
use crate::timer::{read_tbxiv, CapCmpTimer3, CapCmpTimer7, TimerVector};
use core::marker::PhantomData;
use msp430fr247x as pac;

pub use crate::timer::{
    CapCmp, TimerConfig, TimerDiv, TimerExDiv, TimerPeriph, CCR0, CCR1, CCR2, CCR3, CCR4, CCR5,
    CCR6,
};

/// Capture edge trigger
pub enum CapTrigger {
    /// Capture on rising edge
    RisingEdge,
    /// Capture on falling edge
    FallingEdge,
    /// Capture on both edges
    BothEdges,
}

impl Into<Cm> for CapTrigger {
    #[inline]
    fn into(self) -> Cm {
        match self {
            CapTrigger::RisingEdge => Cm::RisingEdge,
            CapTrigger::FallingEdge => Cm::FallingEdge,
            CapTrigger::BothEdges => Cm::BothEdges,
        }
    }
}

struct PinConfig {
    select: Ccis,
    trigger: CapTrigger,
}

impl Default for PinConfig {
    fn default() -> Self {
        Self {
            select: Ccis::Gnd,
            trigger: CapTrigger::RisingEdge,
        }
    }
}

/// Extension trait for creating capture pins from timer peripherals
pub trait CapturePeriph: TimerPeriph {
    /// GPIO pin that supplies input A for capture pin 1
    type Gpio1;
    /// GPIO pin that supplies input A for capture pin 2
    type Gpio2;
    /// GPIO pin that supplies input A for capture pin 3
    type Gpio3;
    /// GPIO pin that supplies input A for capture pin 4
    type Gpio4;
    /// GPIO pin that supplies input A for capture pin 5
    type Gpio5;
    /// GPIO pin that supplies input A for capture pin 6
    type Gpio6;
}
impl CapturePeriph for pac::TB0 {
    type Gpio1 = Pin<P4, Pin7, Alternate2<Input<Floating>>>;
    type Gpio2 = Pin<P5, Pin0, Alternate2<Input<Floating>>>;
    type Gpio3 = Pin<P5, Pin1, Alternate2<Input<Floating>>>;
    type Gpio4 = Pin<P5, Pin2, Alternate2<Input<Floating>>>;
    type Gpio5 = Pin<P5, Pin3, Alternate2<Input<Floating>>>;
    type Gpio6 = Pin<P5, Pin4, Alternate2<Input<Floating>>>;
}

// impl CapturePeriph for pac::TB1 {
//     type Gpio1 = Pin<P2, Pin0, Alternate1<Input<Floating>>>;
//     type Gpio2 = Pin<P2, Pin1, Alternate1<Input<Floating>>>;
//     type Gpio3 = ();
//     type Gpio4 = ();
//     type Gpio5 = ();
//     type Gpio6 = ();
// }

// impl CapturePeriph for pac::TB2 {
//     type Gpio1 = Pin<P5, Pin0, Alternate1<Input<Floating>>>;
//     type Gpio2 = Pin<P5, Pin1, Alternate1<Input<Floating>>>;
//     type Gpio3 = ();
//     type Gpio4 = ();
//     type Gpio5 = ();
//     type Gpio6 = ();
// }

// impl CapturePeriph for pac::TB3 {
//     type Gpio1 = Pin<P6, Pin0, Alternate1<Input<Floating>>>;
//     type Gpio2 = Pin<P6, Pin1, Alternate1<Input<Floating>>>;
//     type Gpio3 = Pin<P6, Pin2, Alternate1<Input<Floating>>>;
//     type Gpio4 = Pin<P6, Pin3, Alternate1<Input<Floating>>>;
//     type Gpio5 = Pin<P6, Pin4, Alternate1<Input<Floating>>>;
//     type Gpio6 = Pin<P6, Pin5, Alternate1<Input<Floating>>>;
// }

macro_rules! config_fn {
    (methods $config_sel_b:ident, $config_trigger:ident, $pin:ident) => {
        #[allow(non_snake_case)]
        #[inline(always)]
        /// Configure the capture input select of the capture pin as capture input B
        pub fn $config_sel_b(mut self) -> Self {
            self.$pin.select = Ccis::InputB;
            self
        }

        #[inline(always)]
        /// Configure the capture trigger event of the capture pin
        pub fn $config_trigger(mut self, trigger: CapTrigger) -> Self {
            self.$pin.trigger = trigger;
            self
        }
    };

    ($config_sel_a:ident, $config_sel_b:ident, $config_trigger:ident, $pin:ident, $gpio:ident) => {
        #[allow(non_snake_case)]
        #[inline(always)]
        /// Configure the capture input select of the capture pin as capture input A, which
        /// requires a correctly configured GPIO pin.
        pub fn $config_sel_a(mut self, _gpio: T::$gpio) -> Self {
            self.$pin.select = Ccis::InputA;
            self
        }
        config_fn!(methods $config_sel_b, $config_trigger, $pin);
    };

    ($config_sel_a:ident, $config_sel_b:ident, $config_trigger:ident, $pin:ident) => {
        #[allow(non_snake_case)]
        #[inline(always)]
        /// Configure the capture input select of the capture pin as capture input A
        pub fn $config_sel_a(mut self) -> Self {
            self.$pin.select = Ccis::InputA;
            self
        }
        config_fn!(methods $config_sel_b, $config_trigger, $pin);
    };
}

/// Builder object for configuring capture ports derived from timer peripherals with 3
/// capture-compare registers
///
/// Each pin has a input source, which determines the signal that controls the capture, and a
/// capture trigger event, which determines the input transitions that actually trigger the
/// capture. By default, all pins use GND as their input source and trigger a capture on a rising
/// edge.
pub struct CaptureConfig3<T: CapturePeriph>
where
    T: CapCmpTimer3,
{
    timer: T,
    config: TimerConfig<T>,
    cap0: PinConfig,
    cap1: PinConfig,
    cap2: PinConfig,
}

impl<T: CapturePeriph + CapCmpTimer3> CaptureParts3<T> {
    /// Create capture configuration
    pub fn config(timer: T, config: TimerConfig<T>) -> CaptureConfig3<T> {
        CaptureConfig3 {
            timer,
            config,
            cap0: PinConfig::default(),
            cap1: PinConfig::default(),
            cap2: PinConfig::default(),
        }
    }
}

impl<T: CapturePeriph + CapCmpTimer3> CaptureConfig3<T> {
    config_fn!(
        config_cap0_input_A,
        config_cap0_input_B,
        config_cap0_trigger,
        cap0
    );
    config_fn!(
        config_cap1_input_A,
        config_cap1_input_B,
        config_cap1_trigger,
        cap1,
        Gpio1
    );
    config_fn!(
        config_cap2_input_A,
        config_cap2_input_B,
        config_cap2_trigger,
        cap2,
        Gpio2
    );

    /// Writes all previously configured timer and capture settings into peripheral registers
    pub fn commit(self) -> CaptureParts3<T> {
        let timer = self.timer;
        self.config.write_regs(&timer);
        CCRn::<CCR0>::config_cap_mode(&timer, self.cap0.trigger.into(), self.cap0.select.into());
        CCRn::<CCR1>::config_cap_mode(&timer, self.cap1.trigger.into(), self.cap1.select.into());
        CCRn::<CCR2>::config_cap_mode(&timer, self.cap2.trigger.into(), self.cap2.select.into());
        timer.continuous();

        CaptureParts3 {
            cap0: Capture::new(),
            cap1: Capture::new(),
            cap2: Capture::new(),
            tbxiv: TBxIV(PhantomData),
        }
    }
}

/// Builder object for configuring capture ports derived from timer peripherals with 7
/// capture-compare registers
///
/// Each pin has a input source, which determines the signal that controls the capture, and a
/// capture trigger event, which determines the input transitions that actually trigger the
/// capture. By default, all pins use GND as their input source and trigger a capture on a rising
/// edge.
pub struct CaptureConfig7<T: CapturePeriph>
where
    T: CapCmpTimer7,
{
    timer: T,
    config: TimerConfig<T>,
    cap0: PinConfig,
    cap1: PinConfig,
    cap2: PinConfig,
    cap3: PinConfig,
    cap4: PinConfig,
    cap5: PinConfig,
    cap6: PinConfig,
}

impl<T: CapturePeriph + CapCmpTimer7> CaptureParts7<T> {
    /// Create capture configuration
    pub fn config(timer: T, config: TimerConfig<T>) -> CaptureConfig7<T> {
        CaptureConfig7 {
            timer,
            config,
            cap0: PinConfig::default(),
            cap1: PinConfig::default(),
            cap2: PinConfig::default(),
            cap3: PinConfig::default(),
            cap4: PinConfig::default(),
            cap5: PinConfig::default(),
            cap6: PinConfig::default(),
        }
    }
}

impl<T: CapturePeriph + CapCmpTimer7> CaptureConfig7<T> {
    config_fn!(
        config_cap0_input_A,
        config_cap0_input_B,
        config_cap0_trigger,
        cap0
    );
    config_fn!(
        config_cap1_input_A,
        config_cap1_input_B,
        config_cap1_trigger,
        cap1,
        Gpio1
    );
    config_fn!(
        config_cap2_input_A,
        config_cap2_input_B,
        config_cap2_trigger,
        cap2,
        Gpio2
    );
    config_fn!(
        config_cap3_input_A,
        config_cap3_input_B,
        config_cap3_trigger,
        cap3,
        Gpio3
    );
    config_fn!(
        config_cap4_input_A,
        config_cap4_input_B,
        config_cap4_trigger,
        cap4,
        Gpio4
    );
    config_fn!(
        config_cap5_input_A,
        config_cap5_input_B,
        config_cap5_trigger,
        cap5,
        Gpio5
    );
    config_fn!(
        config_cap6_input_A,
        config_cap6_input_B,
        config_cap6_trigger,
        cap6,
        Gpio6
    );

    /// Writes all previously configured timer and capture settings into peripheral registers
    pub fn commit(self) -> CaptureParts7<T> {
        let timer = self.timer;
        self.config.write_regs(&timer);
        CCRn::<CCR0>::config_cap_mode(&timer, self.cap0.trigger.into(), self.cap0.select.into());
        CCRn::<CCR1>::config_cap_mode(&timer, self.cap1.trigger.into(), self.cap1.select.into());
        CCRn::<CCR2>::config_cap_mode(&timer, self.cap2.trigger.into(), self.cap2.select.into());
        CCRn::<CCR3>::config_cap_mode(&timer, self.cap3.trigger.into(), self.cap3.select.into());
        CCRn::<CCR4>::config_cap_mode(&timer, self.cap4.trigger.into(), self.cap4.select.into());
        CCRn::<CCR5>::config_cap_mode(&timer, self.cap5.trigger.into(), self.cap5.select.into());
        CCRn::<CCR6>::config_cap_mode(&timer, self.cap6.trigger.into(), self.cap6.select.into());
        timer.continuous();

        CaptureParts7 {
            cap0: Capture::new(),
            cap1: Capture::new(),
            cap2: Capture::new(),
            cap3: Capture::new(),
            cap4: Capture::new(),
            cap5: Capture::new(),
            cap6: Capture::new(),
            tbxiv: TBxIV(PhantomData),
        }
    }
}

/// Collection of capture pins derived from timer peripheral with 3 capture-compare registers
pub struct CaptureParts3<T: CapCmpTimer3> {
    /// Capture pin 0 (derived from capture-compare register 0)
    pub cap0: Capture<T, CCR0>,
    /// Capture pin 1 (derived from capture-compare register 1)
    pub cap1: Capture<T, CCR1>,
    /// Capture pin 2 (derived from capture-compare register 2)
    pub cap2: Capture<T, CCR2>,
    /// Interrupt vector register
    pub tbxiv: TBxIV<T>,
}

/// Collection of capture pins derived from timer peripheral with 7 capture-compare registers
pub struct CaptureParts7<T: CapCmpTimer7> {
    /// Capture pin 0 (derived from capture-compare register 0)
    pub cap0: Capture<T, CCR0>,
    /// Capture pin 1 (derived from capture-compare register 1)
    pub cap1: Capture<T, CCR1>,
    /// Capture pin 2 (derived from capture-compare register 2)
    pub cap2: Capture<T, CCR2>,
    /// Capture pin 3 (derived from capture-compare register 3)
    pub cap3: Capture<T, CCR3>,
    /// Capture pin 4 (derived from capture-compare register 4)
    pub cap4: Capture<T, CCR4>,
    /// Capture pin 5 (derived from capture-compare register 5)
    pub cap5: Capture<T, CCR5>,
    /// Capture pin 6 (derived from capture-compare register 6)
    pub cap6: Capture<T, CCR6>,
    /// Interrupt vector register
    pub tbxiv: TBxIV<T>,
}

/// Single capture pin with its own capture register
pub struct Capture<T: CapCmp<C>, C>(PhantomData<T>, PhantomData<C>);

impl<T: CapCmp<C>, C> Capture<T, C> {
    fn new() -> Self {
        Self(PhantomData, PhantomData)
    }
}

// Candidate for embedded_hal inclusion
/// Single input capture pin
pub trait CapturePin {
    /// Type  of value returned by capture
    type Capture;
    /// Enumeration of `Capture` errors
    ///
    /// Possible errors:
    ///
    /// - *overcapture*, the previous capture value was overwritten because it
    ///   was not read in a timely manner
    type Error;

    /// "Waits" for a transition in the capture `channel` and returns the value
    /// of counter at that instant
    fn capture(&mut self) -> nb::Result<Self::Capture, Self::Error>;
}

impl<T: CapCmp<C>, C> CapturePin for Capture<T, C> {
    type Capture = u16;
    type Error = OverCapture;

    #[inline]
    fn capture(&mut self) -> nb::Result<Self::Capture, Self::Error> {
        let timer = unsafe { T::steal() };
        let (cov, ccifg) = timer.cov_ccifg_rd();
        if ccifg {
            let ccrn = timer.get_ccrn();
            timer.cov_ccifg_clr();
            if cov {
                Err(nb::Error::Other(OverCapture(ccrn)))
            } else {
                Ok(ccrn)
            }
        } else {
            Err(nb::Error::WouldBlock)
        }
    }
}

impl<T: CapCmp<C>, C> Capture<T, C> {
    #[inline]
    /// Enable capture interrupts
    pub fn enable_interrupts(&mut self) {
        let timer = unsafe { T::steal() };
        timer.ccie_set();
    }

    #[inline]
    /// Disable capture interrupts
    pub fn disable_interrupts(&mut self) {
        let timer = unsafe { T::steal() };
        timer.ccie_clr();
    }
}

/// Error returned when the previous capture was overwritten before being read
pub struct OverCapture(pub u16);

/// Capture TBIV interrupt vector
pub enum CaptureVector<T> {
    /// No pending interrupt
    NoInterrupt,
    /// Interrupt caused by capture register 1.
    Capture1(InterruptCapture<T, CCR1>),
    /// Interrupt caused by capture register 2.
    Capture2(InterruptCapture<T, CCR2>),
    /// Interrupt caused by capture register 3.
    Capture3(InterruptCapture<T, CCR3>),
    /// Interrupt caused by capture register 4.
    Capture4(InterruptCapture<T, CCR4>),
    /// Interrupt caused by capture register 5.
    Capture5(InterruptCapture<T, CCR5>),
    /// Interrupt caused by capture register 6.
    Capture6(InterruptCapture<T, CCR6>),
    /// Interrupt caused by main timer overflow
    MainTimer,
}

/// Token returned when reading the interrupt vector that allows a one-time read of the capture
/// register corresponding to the interrupt.
pub struct InterruptCapture<T, C>(PhantomData<T>, PhantomData<C>);

impl<T: CapCmp<C>, C> InterruptCapture<T, C> {
    /// Performs a one-time capture read without considering the interrupt flag. Always call this
    /// instead of `capture()` after reading the capture interrupt vector, since reading the vector
    /// already clears the interrupt flag that `capture()` checks for.
    #[inline]
    pub fn interrupt_capture(self, _cap: &mut Capture<T, C>) -> Result<u16, OverCapture> {
        let timer = unsafe { T::steal() };
        let (cov, _) = timer.cov_ccifg_rd();
        let ccrn = timer.get_ccrn();
        if cov {
            timer.cov_ccifg_clr();
            Err(OverCapture(ccrn))
        } else {
            Ok(ccrn)
        }
    }
}

/// Interrupt vector register for determining which capture-register caused an ISR
pub struct TBxIV<T: TimerPeriph>(PhantomData<T>);

impl<T: TimerPeriph> TBxIV<T> {
    #[inline]
    /// Read the capture interrupt vector and resets corresponding interrupt flag. If
    /// the vector corresponds to an available capture, a one-time capture read token will be
    /// returned as well.
    pub fn interrupt_vector(&mut self) -> CaptureVector<T> {
        let timer = unsafe { T::steal() };
        match read_tbxiv(&timer) {
            TimerVector::NoInterrupt => CaptureVector::NoInterrupt,
            TimerVector::SubTimer1 => {
                CaptureVector::Capture1(InterruptCapture(PhantomData, PhantomData))
            }
            TimerVector::SubTimer2 => {
                CaptureVector::Capture2(InterruptCapture(PhantomData, PhantomData))
            }
            TimerVector::SubTimer3 => {
                CaptureVector::Capture3(InterruptCapture(PhantomData, PhantomData))
            }
            TimerVector::SubTimer4 => {
                CaptureVector::Capture4(InterruptCapture(PhantomData, PhantomData))
            }
            TimerVector::SubTimer5 => {
                CaptureVector::Capture5(InterruptCapture(PhantomData, PhantomData))
            }
            TimerVector::SubTimer6 => {
                CaptureVector::Capture6(InterruptCapture(PhantomData, PhantomData))
            }
            TimerVector::MainTimer => CaptureVector::MainTimer,
        }
    }
}