Add documentation

This commit is contained in:
Jennifer Wilcox 2021-04-24 18:41:12 -05:00
parent 94f67f7eca
commit 422a45fbc5

View file

@ -1,38 +1,65 @@
//! General Purpose Input and Output (GPIO)
//!
//! TODO: Docs for this whole module
//!
//! For example, to turn on the LED on a Pico board:
//! To access the GPIO pins you must call the `split` method on the IO bank. This will return a
//! `Parts` struct with access to the individual pins:
//!
//! ```rust
//! use rp2040_hal::prelude::*;
//! let mut pac = rp2040_pac::Peripherals::take().unwrap();
//!
//! let pins = pac.IO_BANK0.split(pac.PADS_BANK0, pac.SIO, &mut pac.RESETS);
//! ```
//!
//! Once you have the GPIO pins struct, you can take individual pins and configure them:
//!
//! ```rust
//! let mut led_pin = pins.gpio25.into_output();
//! led_pin.set_high().unwrap();
//! ```
//!
//! Input pins support the following options:
//! - Pull high, pull low, or floating
//! - Schmitt trigger
//!
//! Output pins support the following options:
//! - Slew rate (fast or slow)
//! - Drive strength (2, 4, 8 or 12 mA)
/// Mode marker for an input pin
pub struct Input;
/// Mode marker for an output pin
pub struct Output;
/// Mode marker for a pin in an unknown state (generally happens at startup)
pub struct Unknown;
/// This trait adds a method to extract pins from an IO bank and convert them into HAL objects
pub trait GpioExt<PADS, SIO> {
/// The type of struct that will hold the pins once they're converted to HAL objects
type Parts;
/// Convert the IO bank into a struct of HAL pin objects
// TODO: Do we need a marker to check that clocks are up?
fn split(self, pads: PADS, sio: SIO, reset: &mut rp2040_pac::RESETS) -> Self::Parts;
}
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
/// The amount of current that a pin can drive when used as an output
pub enum OutputDriveStrength {
/// 2 mA
Ma2,
/// 4 mA
Ma4,
/// 8 mA
Ma8,
/// 12 mA
Ma12,
}
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
/// The slew rate of a pin when used as an output
pub enum OutputSlewRate {
/// Slew slow
Slow,
/// Slew fast
Fast,
}
@ -48,10 +75,13 @@ const FUNCTION_SIO: u8 = 5;
// const FUNCTION_USB: u8 = 9;
macro_rules! gpio {
($GPIOX:ident, $gpiox:ident, $PADSX:ident, $padsx:ident, [
$($PXi:ident: ($pxi:ident, $i:expr),)+
($GPIOX:ident, $gpiox:ident, $PADSX:ident, $padsx:ident, $gpioxs:expr, [
$($PXi:ident: ($pxi:ident, $i:expr, $is:expr),)+
]) => {
mod $gpiox {
#[doc = "HAL objects for the "]
#[doc = $gpioxs]
#[doc = " bank of GPIO pins"]
pub mod $gpiox {
use core::convert::Infallible;
use core::marker::PhantomData;
use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin};
@ -72,10 +102,15 @@ macro_rules! gpio {
}
}
#[doc = "Struct containing HAL objects for all the "]
#[doc = $gpioxs]
#[doc = " pins"]
pub struct Parts {
_pads: pac::$PADSX,
_sio: pac::SIO,
$(
#[doc = "GPIO pin "]
#[doc = $is]
pub $pxi: $PXi<Unknown>,
)+
}
@ -100,6 +135,8 @@ macro_rules! gpio {
type PacDriveStrength = pac::$padsx::gpio::DRIVE_A;
$(
#[doc = "HAL object for GPIO pin "]
#[doc = $is]
pub struct $PXi<MODE> {
_mode: PhantomData<MODE>,
}
@ -110,6 +147,7 @@ macro_rules! gpio {
// as-needed
impl<MODE> $PXi<MODE> {
#[doc = "Configure this pin as an output"]
pub fn into_output(
self,
) -> $PXi<Output> {
@ -125,6 +163,7 @@ macro_rules! gpio {
$PXi { _mode: PhantomData }
}
#[doc = "Configure this pin as an input"]
pub fn into_input(
self,
) -> $PXi<Input> {
@ -193,6 +232,7 @@ macro_rules! gpio {
impl_input_for!(Output);
impl $PXi<Output> {
#[doc = "Configure the drive strength for this output pin"]
pub fn drive_strength(self, strength: OutputDriveStrength) -> Self {
let converted = match strength {
OutputDriveStrength::Ma2 => PacDriveStrength::_2MA,
@ -206,6 +246,7 @@ macro_rules! gpio {
self
}
#[doc = "Configure the slew rate for this output pin"]
pub fn slew_rate(self, slew_rate: OutputSlewRate) -> Self {
unsafe {
(*pac::$PADSX::ptr()).gpio[$i].modify(|_, w| w.slewfast().bit(slew_rate == OutputSlewRate::Fast));
@ -215,6 +256,7 @@ macro_rules! gpio {
}
impl $PXi<Input> {
#[doc = "Pull this input pin high using internal resistors"]
pub fn pull_high(self) -> Self {
unsafe {
(*pac::$PADSX::ptr()).gpio[$i].modify(|_, w| w.pue().set_bit().pde().clear_bit());
@ -222,6 +264,7 @@ macro_rules! gpio {
self
}
#[doc = "Pull this input pin low using internal resistors"]
pub fn pull_low(self) -> Self {
unsafe {
(*pac::$PADSX::ptr()).gpio[$i].modify(|_, w| w.pue().clear_bit().pde().set_bit());
@ -229,6 +272,7 @@ macro_rules! gpio {
self
}
#[doc = "Allow this input pin to float (i.e. don't pull it high or low)"]
pub fn float(self) -> Self {
unsafe {
(*pac::$PADSX::ptr()).gpio[$i].modify(|_, w| w.pue().clear_bit().pde().clear_bit());
@ -236,6 +280,7 @@ macro_rules! gpio {
self
}
#[doc = "Enable the schmitt trigger for this input pin"]
pub fn enable_schmitt_trigger(self) -> Self {
unsafe {
(*pac::$PADSX::ptr()).gpio[$i].modify(|_, w| w.schmitt().set_bit());
@ -243,6 +288,7 @@ macro_rules! gpio {
self
}
#[doc = "Disable the schmitt trigger for this input pin"]
pub fn disable_schmitt_trigger(self) -> Self {
unsafe {
(*pac::$PADSX::ptr()).gpio[$i].modify(|_, w| w.schmitt().clear_bit());
@ -256,36 +302,36 @@ macro_rules! gpio {
}
gpio!(
IO_BANK0, io_bank0, PADS_BANK0, pads_bank0, [
Gpio0: (gpio0, 0),
Gpio1: (gpio1, 1),
Gpio2: (gpio2, 2),
Gpio3: (gpio3, 3),
Gpio4: (gpio4, 4),
Gpio5: (gpio5, 5),
Gpio6: (gpio6, 6),
Gpio7: (gpio7, 7),
Gpio8: (gpio8, 8),
Gpio9: (gpio9, 9),
Gpio10: (gpio10, 10),
Gpio11: (gpio11, 11),
Gpio12: (gpio12, 12),
Gpio13: (gpio13, 13),
Gpio14: (gpio14, 14),
Gpio15: (gpio15, 15),
Gpio16: (gpio16, 16),
Gpio17: (gpio17, 17),
Gpio18: (gpio18, 18),
Gpio19: (gpio19, 19),
Gpio20: (gpio20, 20),
Gpio21: (gpio21, 21),
Gpio22: (gpio22, 22),
Gpio23: (gpio23, 23),
Gpio24: (gpio24, 24),
Gpio25: (gpio25, 25),
Gpio26: (gpio26, 26),
Gpio27: (gpio27, 27),
Gpio28: (gpio28, 28),
Gpio29: (gpio29, 29),
IO_BANK0, io_bank0, PADS_BANK0, pads_bank0, "IO_BANK0", [
Gpio0: (gpio0, 0, "0"),
Gpio1: (gpio1, 1, "1"),
Gpio2: (gpio2, 2, "2"),
Gpio3: (gpio3, 3, "3"),
Gpio4: (gpio4, 4, "4"),
Gpio5: (gpio5, 5, "5"),
Gpio6: (gpio6, 6, "6"),
Gpio7: (gpio7, 7, "7"),
Gpio8: (gpio8, 8, "8"),
Gpio9: (gpio9, 9, "9"),
Gpio10: (gpio10, 10, "10"),
Gpio11: (gpio11, 11, "11"),
Gpio12: (gpio12, 12, "12"),
Gpio13: (gpio13, 13, "13"),
Gpio14: (gpio14, 14, "14"),
Gpio15: (gpio15, 15, "15"),
Gpio16: (gpio16, 16, "16"),
Gpio17: (gpio17, 17, "17"),
Gpio18: (gpio18, 18, "18"),
Gpio19: (gpio19, 19, "19"),
Gpio20: (gpio20, 20, "20"),
Gpio21: (gpio21, 21, "21"),
Gpio22: (gpio22, 22, "22"),
Gpio23: (gpio23, 23, "23"),
Gpio24: (gpio24, 24, "24"),
Gpio25: (gpio25, 25, "25"),
Gpio26: (gpio26, 26, "26"),
Gpio27: (gpio27, 27, "27"),
Gpio28: (gpio28, 28, "28"),
Gpio29: (gpio29, 29, "29"),
]
);