max7800x-hal 0.7.1

A Hardware Abstraction Layer for the MAX7800X microcontroller family
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
//! General Purpose Input/Output (GPIO)
use core::marker::PhantomData;
use embedded_hal::digital::{ErrorType, InputPin, OutputPin, StatefulOutputPin};
use paste::paste;

/// Marker trait for GPIO pin modes.
pub trait PinMode: crate::Sealed {}

pub struct Input;
pub struct InputOutput;
pub struct Af1;
pub struct Af2;

impl crate::Sealed for Input {}
impl crate::Sealed for InputOutput {}
impl crate::Sealed for Af1 {}
impl crate::Sealed for Af2 {}

impl PinMode for Input {}
impl PinMode for InputOutput {}
impl PinMode for Af1 {}
impl PinMode for Af2 {}

/// Marker trait for GPIO pin power supply.
pub trait PowerSupply: crate::Sealed {}

pub struct Vddio;
pub struct Vddioh;

impl crate::Sealed for Vddio {}
impl crate::Sealed for Vddioh {}

impl PowerSupply for Vddio {}
impl PowerSupply for Vddioh {}

/// Marker trait for GPIO pin input pad modes.
pub trait PadMode: crate::Sealed {}

pub struct HighImpedance;
pub struct PullUpWeak;
pub struct PullUpStrong;
pub struct PullDownWeak;
pub struct PullDownStrong;

impl crate::Sealed for HighImpedance {}
impl crate::Sealed for PullUpWeak {}
impl crate::Sealed for PullUpStrong {}
impl crate::Sealed for PullDownWeak {}
impl crate::Sealed for PullDownStrong {}

impl PadMode for HighImpedance {}
impl PadMode for PullUpWeak {}
impl PadMode for PullUpStrong {}
impl PadMode for PullDownWeak {}
impl PadMode for PullDownStrong {}

/// Marker trait for GPIO pin output drive strengths.
pub trait DriveStrength: crate::Sealed {}

pub struct Strength0;
pub struct Strength1;
pub struct Strength2;
pub struct Strength3;

impl crate::Sealed for Strength0 {}
impl crate::Sealed for Strength1 {}
impl crate::Sealed for Strength2 {}
impl crate::Sealed for Strength3 {}

impl DriveStrength for Strength0 {}
impl DriveStrength for Strength1 {}
impl DriveStrength for Strength2 {}
impl DriveStrength for Strength3 {}

/// Zero-sized abstraction type for a GPIO pin.
///
/// Traits from [`embedded_hal::digital`] are also implemented for each pin.
///
/// - `P` is the GPIO port number (e.g. `0` for `Gpio0`, `1` for `Gpio1`, etc.)
/// - `N` is the GPIO pin number.
/// - `MODE` is one of the pin modes (e.g. `Input`, `InputOutput`, `Af1`, `Af2`).
pub struct Pin<
    const P: u8,
    const N: u8,
    MODE: PinMode = Input,
    SUPPLY: PowerSupply = Vddio,
    PAD: PadMode = HighImpedance,
    DRIVE: DriveStrength = Strength0,
> {
    _mode: PhantomData<MODE>,
    _supply: PhantomData<SUPPLY>,
    _pad: PhantomData<PAD>,
    _drive: PhantomData<DRIVE>,
}

/// Default methods that should work across all pin modes.
impl<const P: u8, const N: u8, MODE: PinMode> Pin<P, N, MODE> {
    const fn new() -> Self {
        Self {
            _mode: PhantomData,
            _supply: PhantomData,
            _pad: PhantomData,
            _drive: PhantomData,
        }
    }

    #[doc(hidden)]
    #[inline(always)]
    fn _output_enable(&mut self) {
        // Safety: Concurrent write access to the GPIO output enable atomic set register is safe
        let gpio = unsafe { &*gpiox_ptr::<P>() };
        gpio.outen_set().write(|w| unsafe { w.bits(1 << N) });
    }

    #[doc(hidden)]
    #[inline(always)]
    fn _output_disable(&mut self) {
        // Safety: Concurrent write access to the GPIO output enable atomic clear register is safe
        let gpio = unsafe { &*gpiox_ptr::<P>() };
        gpio.outen_clr().write(|w| unsafe { w.bits(1 << N) });
    }

    #[doc(hidden)]
    #[inline(always)]
    fn _into_af1(&mut self) {
        let gpio = unsafe { &*gpiox_ptr::<P>() };
        // Set EN0 to 1
        gpio.en0_set().write(|w| unsafe { w.bits(1 << N) });
        // Set EN1 to 0
        gpio.en1_clr().write(|w| unsafe { w.bits(1 << N) });
        // Set EN0 to 0
        gpio.en0_clr().write(|w| unsafe { w.bits(1 << N) });
    }

    #[doc(hidden)]
    #[inline(always)]
    fn _into_af2(&mut self) {
        let gpio = unsafe { &*gpiox_ptr::<P>() };
        // Set EN0 to 1
        gpio.en0_set().write(|w| unsafe { w.bits(1 << N) });
        // Set EN1 to 1
        gpio.en1_set().write(|w| unsafe { w.bits(1 << N) });
        // Set EN1 to 0
        gpio.en1_clr().write(|w| unsafe { w.bits(1 << N) });
    }

    #[doc(hidden)]
    #[inline(always)]
    fn _is_high(&self) -> bool {
        // Safety: Concurrent read access to the GPIO input register is safe
        let gpio = unsafe { &*gpiox_ptr::<P>() };
        gpio.in_().read().gpio_in().bits() & (1 << N) != 0
    }

    #[doc(hidden)]
    #[inline(always)]
    fn _is_low(&self) -> bool {
        // Safety: Concurrent read access to the GPIO input register is safe
        let gpio = unsafe { &*gpiox_ptr::<P>() };
        gpio.in_().read().gpio_in().bits() & (1 << N) == 0
    }

    #[doc(hidden)]
    #[inline(always)]
    fn _set_high(&mut self) {
        // Safety: Concurrent write access to the GPIO output atomic set register is safe
        let gpio = unsafe { &*gpiox_ptr::<P>() };
        gpio.out_set().write(|w| unsafe { w.bits(1 << N) });
    }

    #[doc(hidden)]
    #[inline(always)]
    fn _set_low(&mut self) {
        // Safety: Concurrent write access to the GPIO output atomic clear register is safe
        let gpio = unsafe { &*gpiox_ptr::<P>() };
        gpio.out_clr().write(|w| unsafe { w.bits(1 << N) });
    }

    #[doc(hidden)]
    #[inline(always)]
    fn _is_set_high(&self) -> bool {
        // Safety: Concurrent read access to the GPIO output register is safe
        let gpio = unsafe { &*gpiox_ptr::<P>() };
        gpio.out().read().bits() & (1 << N) != 0
    }

    #[doc(hidden)]
    #[inline(always)]
    fn _is_set_low(&self) -> bool {
        // Safety: Concurrent read access to the GPIO output register is safe
        let gpio = unsafe { &*gpiox_ptr::<P>() };
        gpio.out().read().bits() & (1 << N) == 0
    }

    /// Returns [`true`] if the pin is high, [`false`] if the pin is low
    #[inline(always)]
    pub fn is_high(&self) -> bool {
        self._is_high()
    }

    /// Returns [`true`] if the pin is low, [`false`] if the pin is high
    #[inline(always)]
    pub fn is_low(&self) -> bool {
        self._is_low()
    }
}

/// Methods for input pins.
impl<const P: u8, const N: u8> Pin<P, N, Input> {
    /// Configures the pin as an input/output pin.
    #[inline(always)]
    pub fn into_input_output(self) -> Pin<P, N, InputOutput> {
        // Enable the output for the pin
        let mut pin = Pin::<P, N, InputOutput>::new();
        pin._output_enable();
        pin
    }

    /// Configures the pin as an alternate function 1 pin.
    #[inline(always)]
    pub fn into_af1(self) -> Pin<P, N, Af1> {
        let mut pin = Pin::<P, N, Af1>::new();
        pin._into_af1();
        pin
    }

    /// Configures the pin as an alternate function 2 pin.
    #[inline(always)]
    pub fn into_af2(self) -> Pin<P, N, Af2> {
        let mut pin = Pin::<P, N, Af2>::new();
        pin._into_af2();
        pin
    }
}

/// Methods for input/output pins.
impl<const P: u8, const N: u8> Pin<P, N, InputOutput> {
    /// Configures the pin as an input pin (disables output).
    #[inline(always)]
    pub fn into_input(self) -> Pin<P, N, Input> {
        // Disable the output for the pin
        let mut pin = Pin::<P, N, Input>::new();
        pin._output_disable();
        pin
    }

    /// Sets the pin high.
    #[inline(always)]
    pub fn set_high(&mut self) {
        self._set_high();
    }

    /// Sets the pin low.
    #[inline(always)]
    pub fn set_low(&mut self) {
        self._set_low();
    }

    /// Returns [`true`] if the pin is set to high, [`false`] if the pin is set to low.
    #[inline(always)]
    pub fn is_set_high(&self) -> bool {
        self._is_set_high()
    }

    /// Returns [`true`] if the pin is set to low, [`false`] if the pin is set to high.
    #[inline(always)]
    pub fn is_set_low(&self) -> bool {
        self._is_set_low()
    }

    /// Sets the pin power supply to VDDIO.
    #[inline(always)]
    pub fn set_power_vddio(&mut self) {
        let gpio = unsafe { &*gpiox_ptr::<P>() };
        gpio.vssel()
            .modify(|r, w| unsafe { w.bits(r.bits() & !(1 << N)) });
    }

    /// Sets the pin power supply to VDDIOH.
    #[inline(always)]
    pub fn set_power_vddioh(&mut self) {
        let gpio = unsafe { &*gpiox_ptr::<P>() };
        gpio.vssel()
            .modify(|r, w| unsafe { w.bits(r.bits() | (1 << N)) });
    }
}

/// embedded-hal ErrorType trait
impl<const P: u8, const N: u8, MODE: PinMode> ErrorType for Pin<P, N, MODE> {
    type Error = core::convert::Infallible;
}

/// embedded-hal InputPin trait
impl<const P: u8, const N: u8, MODE: PinMode> InputPin for Pin<P, N, MODE> {
    #[inline(always)]
    fn is_high(&mut self) -> Result<bool, Self::Error> {
        Ok(self._is_high())
    }

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

/// embedded-hal OutputPin trait
impl<const P: u8, const N: u8> OutputPin for Pin<P, N, InputOutput> {
    #[inline(always)]
    fn set_high(&mut self) -> Result<(), Self::Error> {
        self._set_high();
        Ok(())
    }

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

/// embedded-hal StatefulOutputPin trait
impl<const P: u8, const N: u8> StatefulOutputPin for Pin<P, N, InputOutput> {
    #[inline(always)]
    fn is_set_high(&mut self) -> Result<bool, Self::Error> {
        Ok(self._is_set_high())
    }

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

/// Macro that generates a GPIO module with an interface for splitting GPIO pins.
///
/// - `$MODULE_PAC`: The peripheral access crate (PAC) module for the GPIO (e.g., `Gpio1`).
/// - `$MODULE_HAL`: The name of the module to be generated (e.g., `gpio1`).
/// - `$GCR_TYPE`: The type of the GCR register for the GPIO (e.g., `gcr`).
/// - `$PORT_NUM`: The port number (e.g., `1` for `Gpio1`).
/// - `[$($PIN_NUM:literal),*]`: A list of pin numbers to include (e.g., `[0, 1, 2, 3]`).
macro_rules! gpio {
    ($MODULE_PAC:ident, $MODULE_HAL:ident, $GCR_TYPE:ident, $PORT_NUM:expr, [$($PIN_NUM:literal),*]) => {
        paste!{
            pub mod $MODULE_HAL {
                /// Collection of GPIO pins from a single GPIO port.
                pub struct Parts {
                    $(
                        pub [<p $PORT_NUM _ $PIN_NUM>]: [<P $PORT_NUM _ $PIN_NUM>],
                    )+
                }

                /// # General Purpose Input/Output (GPIO) Peripheral
                ///
                /// This peripheral provides an interface for enabling and
                /// splitting up GPIO pins.
                ///
                /// ## Example
                /// ```
                /// // Initialize a Gcr
                /// let mut gcr = Gcr::new(peripherals.gcr, peripherals.lpgcr);
                /// // Initialize the GPIO0 peripheral
                /// let gpio0 = hal::gpio::Gpio0::new(p.gpio0, &mut gcr.reg);
                /// // Split into pins
                /// let pins0 = gpio0.split();
                /// // Set up pins for UART communication
                /// let rx_pin = pins0.p0_0.into_af1();
                /// let tx_pin = pins0.p0_1.into_af1();
                ///
                /// // Initialize the GPIO2 peripheral
                /// let gpio2 = hal::gpio::Gpio2::new(p.gpio2, &mut gcr.reg);
                /// // Split into pins
                /// let pins2 = gpio2.split();
                /// // Set up pins for LED control/output
                /// let led_red = pins2.p2_0.into_input_output();
                /// let led_green = pins2.p2_1.into_input_output();
                /// let led_blue = pins2.p2_2.into_input_output();
                ///
                /// // Acquired pins can then be passed to other peripherals in
                /// // the HAL or embedded-hal driver crates.
                /// ```
                pub struct GpioPeripheral {
                    _gpio: $crate::pac::$MODULE_PAC,
                }

                impl GpioPeripheral {
                    /// Constructs and initializes a GPIO peripheral.
                    pub fn new(gpio: $crate::pac::$MODULE_PAC, reg: &mut crate::gcr::GcrRegisters) -> Self {
                        // Enable the GPIO peripheral clock
                        use crate::gcr::ClockForPeripheral;
                        unsafe { gpio.enable_clock(&mut reg.$GCR_TYPE); };
                        Self {
                            _gpio: gpio,
                        }
                    }
                    /// Splits the GPIO peripheral into independent pins.
                    pub fn split(self) -> Parts {
                        Parts {
                            $(
                                [<p $PORT_NUM _ $PIN_NUM>]: [<P $PORT_NUM _ $PIN_NUM>]::new(),
                            )+
                        }
                    }
                }

                // #[doc="Common type for "]
                // #[doc=stringify!($GPIOX)]
                // #[doc=" related pins"]
                // pub type $PX_x<MODE> = super::PartiallyErasedPin<$port_id, MODE>;

                // Creates a zero-sized type for each pin
                $(
                    #[doc=stringify!([<P $PORT_NUM _ $PIN_NUM>])]
                    #[doc=" pin"]
                    pub type [<P $PORT_NUM _ $PIN_NUM>] = super::Pin<$PORT_NUM, $PIN_NUM>;
                )+
            }

            // Re-export the peripheral constructor for easy access
            pub use $MODULE_HAL::GpioPeripheral as $MODULE_PAC;
        }
    };
}

gpio!(
    Gpio0,
    gpio0,
    gcr,
    0,
    [
        0, 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
    ]
);
gpio!(Gpio1, gpio1, gcr, 1, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
gpio!(Gpio2, gpio2, lpgcr, 2, [0, 1, 2, 3, 4, 5, 6, 7]);

/// Zero runtime cost function to get the address of a GPIO peripheral.
#[inline(always)]
const fn gpiox_ptr<const P: u8>() -> *const crate::pac::gpio0::RegisterBlock {
    match P {
        0 => crate::pac::Gpio0::ptr(),
        1 => crate::pac::Gpio1::ptr(),
        2 => crate::pac::Gpio2::ptr(),
        _ => panic!("Invalid GPIO port number"),
    }
}