# gpio.rs - source
Source of the Rust file `/home/runner/.cargo/git/checkouts/k210-hal-1069b16e3180bc3a/795ac3f/src/gpio.rs`.
Keywords: rust, rustlang, rust-lang

    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

    //! General Purpose Input/Output (GPIO)

    use core::marker::PhantomData;
    use crate::pac;
    use crate::sysctl::{self, APB0};
    use crate::fpioa::{IoPin, Pull, Mode};
    use crate::bit_utils::{u32_set_bit, u32_toggle_bit, u32_bit_is_set, u32_bit_is_clear};
    use embedded_hal::digital::{OutputPin, StatefulOutputPin, InputPin, ToggleableOutputPin};

    /// Extension trait to split a GPIO peripheral into independent pins
    pub trait GpioExt {
        /// Split the GPIO peripheral into parts
        fn split(self, apb0: &mut APB0) -> Parts;
    }

    macro_rules! def_gpio_pins {
        ($($GPIOX: ident: ($num: expr, $gpiox: ident, $func: ident);)+) => {

    impl GpioExt for pac::GPIO {
        fn split(self, apb0: &mut APB0) -> Parts {
            // enable APB0 bus
            apb0.enable();
            // enable sysctl peripheral
            sysctl::clk_en_peri().modify(|_r, w| w.gpio_clk_en().set_bit());
            // return ownership
            Parts {
                $( $gpiox: $GPIOX { _ownership: () }, )+
            }
        }
    }

    /// GPIO peripheral parts
    pub struct Parts {
        $(
            /// GPIO pin
            pub $gpiox: $GPIOX,
        )+
    }

    pub use gpio_pins::*;

    /// All GPIO pins
    pub mod gpio_pins {
        use super::GpioIndex;
    $(
        /// GPIO pin
        pub struct $GPIOX {
            pub(crate) _ownership: ()
        }

        impl GpioIndex for $GPIOX {
            type FUNC = crate::fpioa::functions::$func;
            const INDEX: u8 = $num;
        }
    )+
    }
        };
    }

    def_gpio_pins! {
        GPIO0: (0, gpio0, GPIO0);
        GPIO1: (1, gpio1, GPIO1);
        GPIO2: (2, gpio2, GPIO2);
        GPIO3: (3, gpio3, GPIO3);
        GPIO4: (4, gpio4, GPIO4);
        GPIO5: (5, gpio5, GPIO5);
        GPIO6: (6, gpio6, GPIO6);
        GPIO7: (7, gpio7, GPIO7);
    }

    /// GPIO Index
    pub trait GpioIndex {
        type FUNC;
        const INDEX: u8;
    }

    /// Unknown mode (type state)
    pub struct Unknown;

    /// Input mode (type state)
    pub struct Input<MODE>(MODE);

    /// Floating input (type state)
    pub struct Floating;

    /// Pull down input (type state)
    pub struct PullDown;

    /// Pull up input (type state)
    pub struct PullUp;

    /// Output mode (type state)
    pub struct Output;

    /// Marker trait for active states
    pub trait Active {}

    impl Active for Unknown {}

    impl Active for Input<Floating> {}

    impl Active for Input<PullUp> {}

    impl Active for Input<PullDown> {}

    impl Active for Output {}

    /// GPIO wrapper struct
    pub struct Gpio<GPIO, PIN, MODE> {
        gpio: GPIO,
        pin: PIN,
        _mode: PhantomData<MODE>,
    }

    impl<GPIO: GpioIndex, PIN: Mode<GPIO::FUNC>> Gpio<GPIO, PIN, Unknown> {
        pub fn new(gpio: GPIO, pin: PIN) -> Gpio<GPIO, PIN, Unknown> {
            Gpio { gpio, pin, _mode: PhantomData }
        }
    }

    impl<GPIO, PIN, MODE> Gpio<GPIO, PIN, MODE> {
        pub fn free(self) -> (GPIO, PIN) {
            (self.gpio, self.pin)
        }
    }

    impl<GPIO: GpioIndex, PIN: IoPin, MODE: Active> Gpio<GPIO, PIN, MODE> {
        pub fn into_floating_input(mut self) -> Gpio<GPIO, PIN, Input<Floating>> {
            self.pin.set_io_pull(Pull::None);
            self.direction_in();
            Gpio { gpio: self.gpio, pin: self.pin, _mode: PhantomData }
        }

        pub fn into_pull_up_input(mut self) -> Gpio<GPIO, PIN, Input<PullUp>> {
            self.pin.set_io_pull(Pull::Up);
            self.direction_in();
            Gpio { gpio: self.gpio, pin: self.pin, _mode: PhantomData }
        }

        pub fn into_pull_down_input(mut self) -> Gpio<GPIO, PIN, Input<PullDown>> {
            self.pin.set_io_pull(Pull::Down);
            self.direction_in();
            Gpio { gpio: self.gpio, pin: self.pin, _mode: PhantomData }
        }

        pub fn into_push_pull_output(mut self) -> Gpio<GPIO, PIN, Output> {
            self.pin.set_io_pull(Pull::Down);
            self.direction_out();
            Gpio { gpio: self.gpio, pin: self.pin, _mode: PhantomData }
        }

        #[inline]
        fn direction_in(&mut self) {
            unsafe {
                let p = &(*pac::GPIO::ptr()).direction as *const _ as *mut _;
                u32_set_bit(p, false, GPIO::INDEX as usize);
            }
        }

        #[inline]
        fn direction_out(&mut self) {
            unsafe {
                let p = &(*pac::GPIO::ptr()).direction as *const _ as *mut _;
                u32_set_bit(p, true, GPIO::INDEX as usize);
            }
        }
    }

    impl<GPIO: GpioIndex, PIN, MODE> InputPin for Gpio<GPIO, PIN, Input<MODE>> {
        type Error = core::convert::Infallible;

        fn try_is_high(&self) -> Result<bool, Self::Error> {
            Ok(unsafe {
                let p = &(*pac::GPIO::ptr()).data_input as *const _ as *const _;
                u32_bit_is_set(p, GPIO::INDEX as usize)
            })
        }

        fn try_is_low(&self) -> Result<bool, Self::Error> {
            Ok(unsafe {
                let p = &(*pac::GPIO::ptr()).data_input as *const _ as *const _;
                u32_bit_is_clear(p, GPIO::INDEX as usize)
            })
        }
    }

    impl<GPIO: GpioIndex, PIN> OutputPin for Gpio<GPIO, PIN, Output> {
        type Error = core::convert::Infallible;

        fn try_set_high(&mut self) -> Result<(), Self::Error> {
            unsafe {
                let p = &(*pac::GPIO::ptr()).data_output as *const _ as *mut _;
                u32_set_bit(p, true, GPIO::INDEX as usize);
            }
            Ok(())
        }

        fn try_set_low(&mut self) -> Result<(), Self::Error> {
            unsafe {
                let p = &(*pac::GPIO::ptr()).data_output as *const _ as *mut _;
                u32_set_bit(p, false, GPIO::INDEX as usize);
            }
            Ok(())
        }
    }

    impl<GPIO: GpioIndex, PIN> StatefulOutputPin for Gpio<GPIO, PIN, Output> {
        fn try_is_set_high(&self) -> Result<bool, Self::Error> {
            Ok(unsafe {
                let p = &(*pac::GPIO::ptr()).data_output as *const _ as *const _;
                u32_bit_is_set(p, GPIO::INDEX as usize)
            })
        }

        fn try_is_set_low(&self) -> Result<bool, Self::Error> {
            Ok(unsafe {
                let p = &(*pac::GPIO::ptr()).data_output as *const _ as *const _;
                u32_bit_is_clear(p, GPIO::INDEX as usize)
            })
        }
    }

    impl<GPIO: GpioIndex, PIN> ToggleableOutputPin for Gpio<GPIO, PIN, Output> {
        type Error = core::convert::Infallible;

        fn try_toggle(&mut self) -> Result<(), Self::Error> {
            unsafe {
                let p = &(*pac::GPIO::ptr()).data_output as *const _ as *mut _;
                u32_toggle_bit(p, GPIO::INDEX as usize);
            }
            Ok(())
        }
    }