More review comments

This commit is contained in:
Jennifer Wilcox 2021-04-25 11:13:21 -05:00
parent 037fc665b2
commit b3b7677f82
3 changed files with 39 additions and 38 deletions

View file

@ -21,9 +21,10 @@ fn main() -> ! {
let mut led_pin = pins.gpio25.into_output(); let mut led_pin = pins.gpio25.into_output();
loop { loop {
led_pin.set_low().unwrap();
// TODO: I dare not use delays until we've got clocks running
led_pin.set_high().unwrap(); led_pin.set_high().unwrap();
// TODO: Other delay // TODO: Replace with proper 1s delays once we have clocks working
cortex_m::asm::delay(500_000);
led_pin.set_low().unwrap();
cortex_m::asm::delay(500_000);
} }
} }

View file

@ -21,7 +21,7 @@ fn main() -> ! {
let pins = pac.IO_BANK0.split(pac.PADS_BANK0, pac.SIO, &mut pac.RESETS); let pins = pac.IO_BANK0.split(pac.PADS_BANK0, pac.SIO, &mut pac.RESETS);
let mut led_pin = pins.gpio25.into_output(); let mut led_pin = pins.gpio25.into_output();
let button_pin = pins.gpio15.into_input().pull_high(); let button_pin = pins.gpio15.into_input().pull_up();
loop { loop {
if button_pin.is_high().unwrap() { if button_pin.is_high().unwrap() {

View file

@ -45,13 +45,13 @@ pub trait GpioExt<PADS, SIO> {
/// The amount of current that a pin can drive when used as an output /// The amount of current that a pin can drive when used as an output
pub enum OutputDriveStrength { pub enum OutputDriveStrength {
/// 2 mA /// 2 mA
Ma2, TwoMilliAmps,
/// 4 mA /// 4 mA
Ma4, FourMilliAmps,
/// 8 mA /// 8 mA
Ma8, EightMilliAmps,
/// 12 mA /// 12 mA
Ma12, TwelveMilliAmps,
} }
#[derive(Clone, Copy, Eq, PartialEq, Debug)] #[derive(Clone, Copy, Eq, PartialEq, Debug)]
@ -64,15 +64,19 @@ pub enum OutputSlewRate {
} }
// Magic numbers from the datasheet // Magic numbers from the datasheet
// const FUNCTION_SPI: u8 = 1; // Order is important! Do not rearrange these
// const FUNCTION_UART: u8 = 2; #[allow(dead_code)]
// const FUNCTION_I2C: u8 = 3; enum GpioFunction {
// const FUNCTION_PWM: u8 = 4; Spi = 1,
const FUNCTION_SIO: u8 = 5; Uart,
// const FUNCTION_PIO0: u8 = 6; I2c,
// const FUNCTION_PIO1: u8 = 7; Pwn,
// const FUNCTION_CLOCK: u8 = 8; Sio,
// const FUNCTION_USB: u8 = 9; Pio0,
Pio1,
Clock,
Usb,
}
macro_rules! gpio { macro_rules! gpio {
($GPIOX:ident, $gpiox:ident, $PADSX:ident, $padsx:ident, $gpioxs:expr, [ ($GPIOX:ident, $gpiox:ident, $PADSX:ident, $padsx:ident, $gpioxs:expr, [
@ -85,6 +89,7 @@ macro_rules! gpio {
use core::convert::Infallible; use core::convert::Infallible;
use core::marker::PhantomData; use core::marker::PhantomData;
use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin}; use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin};
use pac::generic::ResetValue;
use super::*; use super::*;
impl GpioExt<pac::$PADSX, pac::SIO> for pac::$GPIOX { impl GpioExt<pac::$PADSX, pac::SIO> for pac::$GPIOX {
@ -115,21 +120,16 @@ macro_rules! gpio {
)+ )+
} }
// Puts pad in same state as post-reset, according to datasheet fn reset_pad(pads: &pac::$padsx::RegisterBlock, index: usize) {
fn setup_pad_io(pads: &pac::$padsx::RegisterBlock, index: usize) { let reset_value: u32 = pac::$padsx::GPIO::reset_value();
pads.gpio[index].modify(|_, w| w.ie().set_bit() // This is safe, we get the value we're setting directly from the PAC
.od().clear_bit() pads.gpio[index].write(|w| unsafe { w.bits(reset_value) });
.pue().clear_bit()
.pde().set_bit()
.schmitt().set_bit()
.drive()._4m_a()
.slewfast().clear_bit());
} }
fn set_gpio_function(gpios: &pac::$gpiox::RegisterBlock, index: usize, function: u8) { fn set_gpio_function(gpios: &pac::$gpiox::RegisterBlock, index: usize, function: GpioFunction) {
gpios.gpio[index] gpios.gpio[index]
.gpio_ctrl .gpio_ctrl
.write_with_zero(|x| unsafe { x.funcsel().bits(function) }); .write_with_zero(|x| unsafe { x.funcsel().bits(function as _) });
} }
type PacDriveStrength = pac::$padsx::gpio::DRIVE_A; type PacDriveStrength = pac::$padsx::gpio::DRIVE_A;
@ -152,10 +152,10 @@ macro_rules! gpio {
self, self,
) -> $PXi<Output> { ) -> $PXi<Output> {
unsafe { unsafe {
setup_pad_io(&*pac::$PADSX::ptr(), $i); reset_pad(&*pac::$PADSX::ptr(), $i);
} }
unsafe { unsafe {
set_gpio_function(&*pac::$GPIOX::ptr(), $i, FUNCTION_SIO); set_gpio_function(&*pac::$GPIOX::ptr(), $i, GpioFunction::Sio);
} }
unsafe { unsafe {
(*pac::SIO::ptr()).gpio_oe_set.write(|x| { x.bits(1 << $i) }); (*pac::SIO::ptr()).gpio_oe_set.write(|x| { x.bits(1 << $i) });
@ -168,10 +168,10 @@ macro_rules! gpio {
self, self,
) -> $PXi<Input> { ) -> $PXi<Input> {
unsafe { unsafe {
setup_pad_io(&*pac::$PADSX::ptr(), $i); reset_pad(&*pac::$PADSX::ptr(), $i);
} }
unsafe { unsafe {
set_gpio_function(&*pac::$GPIOX::ptr(), $i, FUNCTION_SIO); set_gpio_function(&*pac::$GPIOX::ptr(), $i, GpioFunction::Sio);
} }
unsafe { unsafe {
(*pac::SIO::ptr()).gpio_oe_clr.write(|x| { x.bits(1 << $i) }); (*pac::SIO::ptr()).gpio_oe_clr.write(|x| { x.bits(1 << $i) });
@ -235,10 +235,10 @@ macro_rules! gpio {
#[doc = "Configure the drive strength for this output pin"] #[doc = "Configure the drive strength for this output pin"]
pub fn drive_strength(self, strength: OutputDriveStrength) -> Self { pub fn drive_strength(self, strength: OutputDriveStrength) -> Self {
let converted = match strength { let converted = match strength {
OutputDriveStrength::Ma2 => PacDriveStrength::_2MA, OutputDriveStrength::TwoMilliAmps => PacDriveStrength::_2MA,
OutputDriveStrength::Ma4 => PacDriveStrength::_4MA, OutputDriveStrength::FourMilliAmps => PacDriveStrength::_4MA,
OutputDriveStrength::Ma8 => PacDriveStrength::_8MA, OutputDriveStrength::EightMilliAmps => PacDriveStrength::_8MA,
OutputDriveStrength::Ma12 => PacDriveStrength::_12MA, OutputDriveStrength::TwelveMilliAmps => PacDriveStrength::_12MA,
}; };
unsafe { unsafe {
(*pac::$PADSX::ptr()).gpio[$i].modify(|_, w| w.drive().variant(converted)); (*pac::$PADSX::ptr()).gpio[$i].modify(|_, w| w.drive().variant(converted));
@ -257,7 +257,7 @@ macro_rules! gpio {
impl $PXi<Input> { impl $PXi<Input> {
#[doc = "Pull this input pin high using internal resistors"] #[doc = "Pull this input pin high using internal resistors"]
pub fn pull_high(self) -> Self { pub fn pull_up(self) -> Self {
unsafe { unsafe {
(*pac::$PADSX::ptr()).gpio[$i].modify(|_, w| w.pue().set_bit().pde().clear_bit()); (*pac::$PADSX::ptr()).gpio[$i].modify(|_, w| w.pue().set_bit().pde().clear_bit());
} }
@ -265,7 +265,7 @@ macro_rules! gpio {
} }
#[doc = "Pull this input pin low using internal resistors"] #[doc = "Pull this input pin low using internal resistors"]
pub fn pull_low(self) -> Self { pub fn pull_down(self) -> Self {
unsafe { unsafe {
(*pac::$PADSX::ptr()).gpio[$i].modify(|_, w| w.pue().clear_bit().pde().set_bit()); (*pac::$PADSX::ptr()).gpio[$i].modify(|_, w| w.pue().clear_bit().pde().set_bit());
} }