atsamx7x-hal 0.4.2

HAL and peripheral access API for ATSAME70, ATSAMS70, ATSAMV70, and ATSAMV71 microcontrollers
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
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
//! [`Pin`]-level configurations

use core::convert::Infallible;
use core::marker::PhantomData;

use crate::ehal::digital::v2::OutputPin;
#[cfg(feature = "unproven")]
use crate::ehal::digital::v2::{InputPin, StatefulOutputPin, ToggleableOutputPin};

use paste::paste;

use super::reg::RegisterInterface;
use super::*;

//================================================================================
// Registers
//================================================================================

pub(in crate::pio) struct Registers<I: PinId> {
    id: PhantomData<I>,
}

impl<I: PinId> RegisterInterface for Registers<I> {
    #[inline]
    fn id(&self) -> DynPinId {
        I::DYN
    }
}

impl<I: PinId> Registers<I> {
    #[inline]
    unsafe fn new() -> Self {
        Registers { id: PhantomData }
    }

    #[inline]
    pub(in crate::pio) fn change_mode<M: PinMode>(&mut self) {
        RegisterInterface::change_mode(self, M::DYN);
    }
}

//==============================================================================
//  Pin configurations
//==============================================================================

/// Type indicating that the [`Pin`] is in reset
pub enum Reset {}
impl generics::Sealed for Reset {}

/// Type indicating that the [`Pin`] is an input
pub enum Input {}
impl generics::Sealed for Input {}

/// Type indicating that the [`Pin`] is an output
pub enum Output {}
impl generics::Sealed for Output {}

//================================================================================
// Peripheral configurations
//================================================================================

/// Type-level enum for alternate peripheral function configurations
pub trait PeripheralConfig: generics::Sealed {
    /// Corresponding [`DynPeripheral`](super::DynPeripheral)
    const DYN: DynPeripheral;
}

macro_rules! peripheral {
    (
        $(
            $Letter:ident
        ),+
    ) => {
        paste! {
            $(
                impl PeripheralConfig for $Letter {
                    const DYN: DynPeripheral = DynPeripheral::$Letter;
                }
                #[
                    doc = "Type-level variant of [`PinMode`] for alternate \
                           peripheral function [`" $Letter "`]"
                ]
                pub type [<Peripheral $Letter>] = Peripheral<$Letter>;
            )+
        }
    }
}

peripheral!(A, B, C, D);

/// Type-level variant of [`PinMode`] for [`Peripheral`] functions
pub struct Peripheral<C: PeripheralConfig> {
    cfg: PhantomData<C>,
}

impl<C: PeripheralConfig> generics::Sealed for Peripheral<C> {}

//================================================================================
// Pin modes
//================================================================================

/// Type-level enum representing [`Pin`] modes
#[allow(missing_docs)]
pub trait PinMode: generics::Sealed {
    const DYN: DynPinMode;
}

impl PinMode for Reset {
    const DYN: DynPinMode = DynPinMode::Reset;
}

impl<C: PeripheralConfig> PinMode for Peripheral<C> {
    const DYN: DynPinMode = DynPinMode::Peripheral(C::DYN);
}

impl PinMode for Output {
    const DYN: DynPinMode = DynPinMode::Output;
}

impl PinMode for Input {
    const DYN: DynPinMode = DynPinMode::Input;
}

//================================================================================
// Pin definition
//================================================================================

#[allow(missing_docs)]
pub trait PinId: generics::Sealed {
    const DYN: DynPinId;
}

/// A type-level GPIO pin, parameterized by [`PinId`] and [`PinMode`] types
pub struct Pin<I, M>
where
    I: PinId,
    M: PinMode,
{
    pub(in crate::pio) regs: Registers<I>,
    mode: PhantomData<M>,
}
impl<I, M> generics::Sealed for Pin<I, M>
where
    I: PinId,
    M: PinMode,
{
}

impl<I, M> Pin<I, M>
where
    I: PinId,
    M: PinMode,
{
    #[inline]
    pub(in crate::pio) unsafe fn new() -> Pin<I, M> {
        Pin {
            regs: Registers::new(),
            mode: PhantomData,
        }
    }

    /// Convert the pin to the requested [`PinMode`].
    #[inline]
    pub fn into_mode<N: PinMode>(mut self) -> Pin<I, N> {
        // On reset, PB4/5/6/7/12 have special functions after reset,
        // and the bus matrix must be modified to use these pins are
        // regular (and functional) PIO pins.
        //
        // Refer to §19.4.7.
        #[cfg(feature = "reconfigurable-system-pins")]
        if I::DYN.bank == DynBank::B {
            if let pin @ (4..=7 | 12) = I::DYN.num {
                use crate::pac::{matrix::RegisterBlock, MATRIX};
                const MATRIX: *const RegisterBlock = MATRIX::ptr();

                unsafe {
                    let matrix = &*MATRIX;
                    let mask = 1 << pin;
                    matrix.ccfg_sysio.modify(|_, w| w.bits(mask));
                }
            }
        }

        if N::DYN != M::DYN {
            self.regs.change_mode::<N>();
        }

        // Safe: existing pin is dropped.
        unsafe { Pin::new() }
    }

    /// Configure the pin to operate as the corresponding [`Peripheral`] function.
    ///
    /// The type `C` indicates the desired [`Peripheral`] function.
    #[inline]
    pub fn into_peripheral<C: PeripheralConfig>(self) -> Pin<I, Peripheral<C>> {
        self.into_mode()
    }

    /// Configures this pin to operate as an [`Output`] [`Pin`]. If
    /// `bit` is `true`, the pin output is initially set high;
    /// otherwise low.
    #[inline]
    pub fn into_output(self, bit: bool) -> Pin<I, Output> {
        let mut pin: Pin<I, Output> = self.into_mode();
        pin.regs.write_pin(bit);
        pin
    }

    /// Configures this [`Pin`] to operate as an [`Input`] [`Pin`].
    #[inline]
    pub fn into_input(self, cfg: PullDir) -> Pin<I, Input> {
        let mut pin: Pin<I, Input> = self.into_mode();
        pin.set_pull_dir(cfg);
        pin
    }

    /// Configures the pull direction for this [`Pin`], or configures
    /// it for floating.
    #[inline]
    pub fn set_pull_dir(&mut self, cfg: PullDir) {
        self.regs.set_pull_dir(cfg);
    }

    /// Configures the interrupt configuration for this [`Pin`].
    ///
    /// A `cfg` of `None` disables interrupts for this [`Pin`].
    #[inline]
    pub fn set_interrupt(&mut self, cfg: Option<InterruptType>) {
        self.regs.set_interrupt(cfg);
    }

    /// Configures the input filter for this [`Pin`].
    ///
    /// A `cfg` of `None` disables the input filter for this [`Pin`].
    #[inline]
    pub fn set_filter(&mut self, cfg: Option<InputFilter>) {
        self.regs.set_filter(cfg);
    }

    /// Enables or disables the Schmitt trigger for tis [`Pin`].
    ///
    /// The Schmitt trigger is enabled by default.
    #[inline]
    pub fn set_schmitt(&mut self, cfg: bool) {
        self.regs.set_schmitt(cfg);
    }

    #[inline]
    pub(in crate::pio) fn _set_high(&mut self) {
        self.regs.write_pin(true);
    }

    #[inline]
    pub(in crate::pio) fn _set_low(&mut self) {
        self.regs.write_pin(false);
    }

    #[inline]
    #[allow(clippy::bool_comparison)]
    pub(in crate::pio) fn _is_written_high(&self) -> bool {
        self.regs.read_out_pin() == true
    }

    #[inline]
    #[allow(clippy::bool_comparison)]
    pub(in crate::pio) fn _is_written_low(&self) -> bool {
        self.regs.read_out_pin() == false
    }

    #[inline]
    #[allow(clippy::bool_comparison)]
    pub(in crate::pio) fn _is_read_high(&self) -> bool {
        self.regs.read_in_pin() == true
    }

    #[inline]
    #[allow(clippy::bool_comparison)]
    pub(in crate::pio) fn _is_read_low(&self) -> bool {
        self.regs.read_in_pin() == false
    }

    #[inline]
    pub(in crate::pio) fn _toggle(&mut self) {
        self.regs.toggle_out_pin();
    }
}

//================================================================================
// PinMode conversions
//================================================================================

/// Use a recursive macro to implement [`From`](core::convert::From) for each
/// pair of [`PinMode`]s. A macro is necessary to avoid conflicting with the
/// reflexive implementation in [`core::convert`], i.e. `impl<T> From<T> for T`.
macro_rules! impl_nxn_convert_from {
    (
        $( #[$cfg1:meta] )?
        $Mode1:ident,
    ) => {};
    (
        #[$cfg1:meta]
        $Mode1:ident,
        $(
            $( #[$cfg2:meta] )?
            $Mode2:ident,
        )*
    ) => {
        #[$cfg1]
        impl_nxn_convert_from!(
            $Mode1,
            $(
                $( #[$cfg2] )?
                $Mode2,
            )*
        );
    };
    (
        $Mode1:ident,
        $(
            $( #[$cfg2:meta] )?
            $Mode2:ident,
        )*
    ) => {
        paste! {
            $(
                $( #[$cfg2] )?
                impl<I> From<Pin<I, $Mode1>> for Pin<I, $Mode2>
                where
                    I: PinId,
                {
                    #[doc = "Convert from [`" $Mode1 "`] to [`" $Mode2 "`]"]
                    #[inline]
                    fn from(pin: Pin<I, $Mode1>) -> Self {
                        pin.into_mode()
                    }
                }
                $( #[$cfg2] )?
                impl<I> From<Pin<I, $Mode2>> for Pin<I, $Mode1>
                where
                    I: PinId,
                {
                    #[doc = "Convert from [`" $Mode2 "`] to [`" $Mode1 "`]"]
                    #[inline]
                    fn from(pin: Pin<I, $Mode2>) -> Self {
                        pin.into_mode()
                    }
                }
            )*
            impl_nxn_convert_from!(
                $(
                    $( #[$cfg2] )?
                    $Mode2,
                )*
            );
        }
    };
}

macro_rules! impl_reset_convert_from {
    (
        $(
            $Mode:ident,
        )+
    ) => {
        paste! {
            $(
                impl<I> From<Pin<I, Reset>> for Pin<I, $Mode>
                where
                    I: PinId,
                {
                    #[doc = "Convert from [`Reset`] to [`" $Mode "`]"]
                    #[inline]
                    fn from(pin: Pin<I, Reset>) -> Self {
                        pin.into_mode()
                    }
                }
            )+
        }
    };
}

// Implements bidirectional From for all permutations
impl_nxn_convert_from!(
    Output,
    Input,
    PeripheralA,
    PeripheralB,
    PeripheralC,
    PeripheralD,
);

// Implements unidirectional From<Reset> for all permutations
impl_reset_convert_from!(
    Output,
    Input,
    PeripheralA,
    PeripheralB,
    PeripheralC,
    PeripheralD,
);

//================================================================================
// Embedded HAL traits
//================================================================================

impl<I> OutputPin for Pin<I, Output>
where
    I: PinId,
{
    type Error = Infallible;

    #[inline]
    fn set_high(&mut self) -> Result<(), Self::Error> {
        self._set_high();
        Ok(())
    }

    #[inline]
    fn set_low(&mut self) -> Result<(), Self::Error> {
        self._set_low();
        Ok(())
    }
}

#[cfg(feature = "unproven")]
impl<I> ToggleableOutputPin for Pin<I, Output>
where
    I: PinId,
{
    type Error = Infallible;

    #[inline]
    fn toggle(&mut self) -> Result<(), Self::Error> {
        self._toggle();
        Ok(())
    }
}

#[cfg(feature = "unproven")]
impl<I> StatefulOutputPin for Pin<I, Output>
where
    I: PinId,
{
    #[inline]
    fn is_set_high(&self) -> Result<bool, Self::Error> {
        Ok(self._is_written_high())
    }

    #[inline]
    fn is_set_low(&self) -> Result<bool, Self::Error> {
        Ok(self._is_written_low())
    }
}

#[cfg(feature = "unproven")]
impl<I, M> InputPin for Pin<I, M>
where
    I: PinId,
    M: PinMode,
{
    type Error = Infallible;

    #[inline]
    fn is_high(&self) -> Result<bool, Self::Error> {
        Ok(self._is_read_high())
    }

    #[inline]
    fn is_low(&self) -> Result<bool, Self::Error> {
        Ok(self._is_read_low())
    }
}