Macro atsamd_hal::bsp_pins[][src]

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

The following example macro call

atsamd_hal::bsp_pins!(
    #[cfg(feature = "unproven")]
    PA24 {
        name: led_pass,
        aliases: {
            AlternateH: LedPass,
            #[cfg(feature = "usb")]
            AlternateM: UsbPin
        }
    }
    PA25 {
        name: led_fail
    }
);

would expand to something like this

pub struct Pins {
    port: Option<PORT>,
    #[cfg(feature = "unproven")]
    pub led_pass: Pin<PA24, Reset>,
    pub led_fail: Pin<PA25, Reset>,
}

impl Pins {

    pub fn new(port: PORT) -> Self {
        let pins = gpio::Pins::new(port);
        Self {
            port: Some(unsafe { pins.port() }),
            #[cfg(feature = "unproven")]
            led_pass: pins.pa24,
            led_fail: pins.pa25,
        }
    }

    #[inline]
    pub unsafe fn port(&mut self) -> PORT {
        self.port.take().unwrap()
    }
}

#[cfg(feature = "unproven")]
pub type LedPass = Pin<PA24, AlternateH>;

#[cfg(feature = "unproven")]
pub const LED_PASS_ID: DynPinId = <PA24 as PinId>::DYN;

#[cfg(feature = "unproven")]
pub const LED_PASS_MODE: DynPinMode = <AlternateH as PinMode>::DYN;

#[cfg(feature = "unproven")]
#[cfg(feature = "usb")]
pub type UsbPin = Pin<PA24, AlternateM>;

#[cfg(feature = "unproven")]
#[cfg(feature = "usb")]
pub const USB_PIN_ID: DynPinId = <PA24 as PinId>::DYN;

#[cfg(feature = "unproven")]
#[cfg(feature = "usb")]
pub const USB_PIN_MODE: DynPinMode = <AlternateM as PinMode>::DYN;