Macro rp2040_hal::bsp_pins

source ·
macro_rules! bsp_pins {
    (
        $(
            $( #[$id_cfg:meta] )*
            $Id:ident {
                $( #[$name_doc:meta] )*
                name: $name:ident $(,)?
                $(
                    aliases: {
                        $(
                            $( #[$alias_cfg:meta] )*
                            $Mode:ident: $Alias:ident
                        ),+
                    }
                )?
            } $(,)?
        )+
    ) => { ... };
    ( @aliases, $( $( $( #[$attr:meta] )* $Id:ident $Mode:ident $Alias:ident )+ )? ) => { ... };
}
Expand description

Helper macro to give meaningful names to GPIO pins

The normal Pins struct names each Pin according to its PinId. However, BSP authors would prefer to name each Pin according to its function. This macro defines a new Pins struct with custom field names for each Pin, and it defines type aliases and constants to make it easier to work with the Pins and DynPins.

When specifying pin aliases, be sure to use a PinMode. See here for a list of the available PinMode type aliases.

Example

Calling the macro like this:

use rp2040_hal::bsp_pins;
bsp_pins! {
    #[cfg(feature = "gpio")]
    Gpio0 {
         /// Doc gpio0
         name: gpio0,
         aliases: { FunctionPio0: PioPin }
    },
    Gpio1 {
         name: led,
         aliases: { FunctionPwm: LedPwm }
     },
}

Is roughly equivalent to the following source code (excluding the docs strings below):

use ::rp2040_hal as hal;
use hal::gpio;
pub struct Pins {
    #[cfg(feature = "gpio")]
    /// Doc gpio0
    pub gpio0: gpio::Pin<gpio::bank0::Gpio0, <gpio::bank0::Gpio0 as gpio::PinId>::Reset>,
    pub led: gpio::Pin<gpio::bank0::Gpio1, <gpio::bank0::Gpio1 as gpio::PinId>::Reset>,
}
impl Pins {
    #[inline]
    pub fn new(
        io: hal::pac::IO_BANK0,
        pads: hal::pac::PADS_BANK0,
        sio: hal::sio::SioGpioBank0,
        reset: &mut hal::pac::RESETS,
    ) -> Self {
        let mut pins = gpio::Pins::new(io, pads, sio, reset);
        Self {
            #[cfg(feature = "gpio")]
            gpio0: pins.gpio0,
            led: pins.gpio1,
        }
    }
}
pub type PioPin = gpio::Pin<gpio::bank0::Gpio0, gpio::FunctionPio0>;
pub const PIO_PIN_ID: gpio::DynPinId = <gpio::bank0::Gpio0 as gpio::PinId>::DYN;
pub const PIO_PIN_MODE: gpio::DynPinMode = <gpio::FunctionPio0 as gpio::PinMode>::DYN;
pub type LedPwm = gpio::Pin<gpio::bank0::Gpio1, gpio::FunctionPwm>;
pub const LED_PWM_ID: gpio::DynPinId = <gpio::bank0::Gpio1 as gpio::PinId>::DYN;
pub const LED_PWM_MODE: gpio::DynPinMode = <gpio::FunctionPwm as gpio::PinMode>::DYN;