From 2ef1343c053d2aefa53dfc5c67a67b159a9405c2 Mon Sep 17 00:00:00 2001 From: Andrea Nall Date: Sun, 9 May 2021 22:33:36 -0500 Subject: [PATCH 1/2] add module to manage ownership of parts of SIO --- rp2040-hal/examples/blinky.rs | 3 ++- rp2040-hal/examples/gpio_in_out.rs | 3 ++- rp2040-hal/src/gpio.rs | 11 ++++---- rp2040-hal/src/lib.rs | 1 + rp2040-hal/src/prelude.rs | 1 + rp2040-hal/src/sio.rs | 41 ++++++++++++++++++++++++++++++ 6 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 rp2040-hal/src/sio.rs diff --git a/rp2040-hal/examples/blinky.rs b/rp2040-hal/examples/blinky.rs index 2bae343..cf54150 100644 --- a/rp2040-hal/examples/blinky.rs +++ b/rp2040-hal/examples/blinky.rs @@ -17,7 +17,8 @@ pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER; fn main() -> ! { let mut pac = rp2040_pac::Peripherals::take().unwrap(); - let pins = pac.IO_BANK0.split(pac.PADS_BANK0, pac.SIO, &mut pac.RESETS); + let sio = Sio::new(pac.SIO); + let pins = pac.IO_BANK0.split(pac.PADS_BANK0, sio.gpio_bank0, &mut pac.RESETS); let mut led_pin = pins.gpio25.into_output(); loop { diff --git a/rp2040-hal/examples/gpio_in_out.rs b/rp2040-hal/examples/gpio_in_out.rs index 963b150..f405135 100644 --- a/rp2040-hal/examples/gpio_in_out.rs +++ b/rp2040-hal/examples/gpio_in_out.rs @@ -19,7 +19,8 @@ pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER; fn main() -> ! { let mut pac = rp2040_pac::Peripherals::take().unwrap(); - let pins = pac.IO_BANK0.split(pac.PADS_BANK0, pac.SIO, &mut pac.RESETS); + let sio = Sio::new(pac.SIO); + let pins = pac.IO_BANK0.split(pac.PADS_BANK0, sio.gpio_bank0, &mut pac.RESETS); let mut led_pin = pins.gpio25.into_output(); let button_pin = pins.gpio15.into_input().pull_up(); diff --git a/rp2040-hal/src/gpio.rs b/rp2040-hal/src/gpio.rs index e954257..5b2e6bc 100644 --- a/rp2040-hal/src/gpio.rs +++ b/rp2040-hal/src/gpio.rs @@ -23,6 +23,7 @@ //! Output pins support the following options: //! - Slew rate (fast or slow) //! - Drive strength (2, 4, 8 or 12 mA) +use crate::sio; /// Mode marker for an input pin pub struct Input; @@ -64,7 +65,7 @@ pub enum OutputSlewRate { } macro_rules! gpio { - ($GPIOX:ident, $gpiox:ident, $PADSX:ident, $padsx:ident, $gpioxs:expr, [ + ($GPIOX:ident, $gpiox:ident, $siotoken : ident, $PADSX:ident, $padsx:ident, $gpioxs:expr, [ $($PXi:ident: ($pxi:ident, $i:expr, $is:expr),)+ ]) => { #[doc = "HAL objects for the "] @@ -76,10 +77,10 @@ macro_rules! gpio { use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin}; use super::*; - impl GpioExt for pac::$GPIOX { + impl GpioExt for pac::$GPIOX { type Parts = Parts; - fn split(self, pads: pac::$PADSX, sio: pac::SIO, resets: &mut pac::RESETS) -> Parts { + fn split(self, pads: pac::$PADSX, sio: sio::$siotoken, resets: &mut pac::RESETS) -> Parts { resets.reset.modify(|_, w| w.$gpiox().clear_bit().$padsx().clear_bit()); // TODO: Implement Resets in the HAL while resets.reset_done.read().$gpiox().bit_is_clear() { @@ -103,7 +104,7 @@ macro_rules! gpio { #[doc = " pins"] pub struct Parts { _pads: pac::$PADSX, - _sio: pac::SIO, + _sio: sio::$siotoken, $( #[doc = "GPIO pin "] #[doc = $is] @@ -265,7 +266,7 @@ macro_rules! gpio { } gpio!( - IO_BANK0, io_bank0, PADS_BANK0, pads_bank0, "IO_BANK0", [ + IO_BANK0, io_bank0, SioGpioBank0, PADS_BANK0, pads_bank0, "IO_BANK0", [ Gpio0: (gpio0, 0, "0"), Gpio1: (gpio1, 1, "1"), Gpio2: (gpio2, 2, "2"), diff --git a/rp2040-hal/src/lib.rs b/rp2040-hal/src/lib.rs index aa23534..500dc5a 100644 --- a/rp2040-hal/src/lib.rs +++ b/rp2040-hal/src/lib.rs @@ -28,3 +28,4 @@ pub mod uart; pub mod usb; pub mod watchdog; pub mod xosc; +pub mod sio; diff --git a/rp2040-hal/src/prelude.rs b/rp2040-hal/src/prelude.rs index 57edf4f..011f347 100644 --- a/rp2040-hal/src/prelude.rs +++ b/rp2040-hal/src/prelude.rs @@ -1,2 +1,3 @@ //! Prelude pub use crate::gpio::GpioExt; +pub use crate::sio::Sio; diff --git a/rp2040-hal/src/sio.rs b/rp2040-hal/src/sio.rs new file mode 100644 index 0000000..37be161 --- /dev/null +++ b/rp2040-hal/src/sio.rs @@ -0,0 +1,41 @@ +//! Single Cycle Input and Output (CIO) +//! +//! To be able to partition parts of the SIO block to other modules: +//! +//! ```rust +//! let sio = Sio::new(pac.SIO); +//! ``` +//! +//! And then for example +//! +//! ```rust +//! let pins = pac.IO_BANK0.split(pac.PADS_BANK0, sio.gpio_bank0, &mut pac.RESETS); +//! ``` +use super::*; +use core::marker::PhantomData; + +/// Marker struct for ownership of SIO gpio bank0 +pub struct SioGpioBank0 { _private: PhantomData } + +/// Struct containing ownership markers for managing ownership of the SIO registers. +pub struct Sio { + _sio : pac::SIO, + + /// GPIO Bank 0 registers + pub gpio_bank0 : SioGpioBank0, + // we can hand out other things here, for example: + // gpio_qspi + // divider + // interp0 + // interp1 +} +impl Sio { + /// Create `Sio` from the PAC. + pub fn new(sio : pac::SIO) -> Self { + Self { + _sio: sio, + + gpio_bank0: SioGpioBank0 { _private: PhantomData } + } + } +} \ No newline at end of file From 35464a1c4b2c35d6a8e9b26c0ac529a3260bacb7 Mon Sep 17 00:00:00 2001 From: Andrea Nall Date: Mon, 10 May 2021 08:29:59 -0500 Subject: [PATCH 2/2] typo fix, rustfmt --- rp2040-hal/examples/blinky.rs | 4 +++- rp2040-hal/examples/gpio_in_out.rs | 4 +++- rp2040-hal/src/lib.rs | 2 +- rp2040-hal/src/sio.rs | 18 +++++++++++------- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/rp2040-hal/examples/blinky.rs b/rp2040-hal/examples/blinky.rs index cf54150..e567df8 100644 --- a/rp2040-hal/examples/blinky.rs +++ b/rp2040-hal/examples/blinky.rs @@ -18,7 +18,9 @@ fn main() -> ! { let mut pac = rp2040_pac::Peripherals::take().unwrap(); let sio = Sio::new(pac.SIO); - let pins = pac.IO_BANK0.split(pac.PADS_BANK0, sio.gpio_bank0, &mut pac.RESETS); + let pins = pac + .IO_BANK0 + .split(pac.PADS_BANK0, sio.gpio_bank0, &mut pac.RESETS); let mut led_pin = pins.gpio25.into_output(); loop { diff --git a/rp2040-hal/examples/gpio_in_out.rs b/rp2040-hal/examples/gpio_in_out.rs index f405135..21947bf 100644 --- a/rp2040-hal/examples/gpio_in_out.rs +++ b/rp2040-hal/examples/gpio_in_out.rs @@ -20,7 +20,9 @@ fn main() -> ! { let mut pac = rp2040_pac::Peripherals::take().unwrap(); let sio = Sio::new(pac.SIO); - let pins = pac.IO_BANK0.split(pac.PADS_BANK0, sio.gpio_bank0, &mut pac.RESETS); + let pins = pac + .IO_BANK0 + .split(pac.PADS_BANK0, sio.gpio_bank0, &mut pac.RESETS); let mut led_pin = pins.gpio25.into_output(); let button_pin = pins.gpio15.into_input().pull_up(); diff --git a/rp2040-hal/src/lib.rs b/rp2040-hal/src/lib.rs index 500dc5a..9c0cb0d 100644 --- a/rp2040-hal/src/lib.rs +++ b/rp2040-hal/src/lib.rs @@ -21,6 +21,7 @@ pub mod prelude; pub mod pwm; pub mod rom_data; pub mod rtc; +pub mod sio; pub mod spi; pub mod ssi; pub mod timer; @@ -28,4 +29,3 @@ pub mod uart; pub mod usb; pub mod watchdog; pub mod xosc; -pub mod sio; diff --git a/rp2040-hal/src/sio.rs b/rp2040-hal/src/sio.rs index 37be161..111376b 100644 --- a/rp2040-hal/src/sio.rs +++ b/rp2040-hal/src/sio.rs @@ -1,4 +1,4 @@ -//! Single Cycle Input and Output (CIO) +//! Single Cycle Input and Output (SIO) //! //! To be able to partition parts of the SIO block to other modules: //! @@ -15,14 +15,16 @@ use super::*; use core::marker::PhantomData; /// Marker struct for ownership of SIO gpio bank0 -pub struct SioGpioBank0 { _private: PhantomData } +pub struct SioGpioBank0 { + _private: PhantomData, +} /// Struct containing ownership markers for managing ownership of the SIO registers. pub struct Sio { - _sio : pac::SIO, + _sio: pac::SIO, /// GPIO Bank 0 registers - pub gpio_bank0 : SioGpioBank0, + pub gpio_bank0: SioGpioBank0, // we can hand out other things here, for example: // gpio_qspi // divider @@ -31,11 +33,13 @@ pub struct Sio { } impl Sio { /// Create `Sio` from the PAC. - pub fn new(sio : pac::SIO) -> Self { + pub fn new(sio: pac::SIO) -> Self { Self { _sio: sio, - gpio_bank0: SioGpioBank0 { _private: PhantomData } + gpio_bank0: SioGpioBank0 { + _private: PhantomData, + }, } } -} \ No newline at end of file +}