diff options
| author | pennae <[email protected]> | 2023-05-03 08:46:08 +0200 |
|---|---|---|
| committer | pennae <[email protected]> | 2023-05-03 11:25:53 +0200 |
| commit | 6ad58f428a42fd73377b6b8ec81315da06357049 (patch) | |
| tree | a40696082ab64325d155024b1f1ad2426064be02 | |
| parent | 4ccb2bc95aab6202d6f53882a59265427cdd5655 (diff) | |
rp/pio: wrap PioPins from ref, like everything else
also store peripheral refs instead of a raw pin/bank number, like
everything else.
| -rw-r--r-- | embassy-rp/src/pio.rs | 31 |
1 files changed, 13 insertions, 18 deletions
diff --git a/embassy-rp/src/pio.rs b/embassy-rp/src/pio.rs index 21223eafc..bb9bdb8ac 100644 --- a/embassy-rp/src/pio.rs +++ b/embassy-rp/src/pio.rs | |||
| @@ -6,13 +6,13 @@ use core::task::{Context, Poll}; | |||
| 6 | 6 | ||
| 7 | use atomic_polyfill::{AtomicU32, AtomicU8}; | 7 | use atomic_polyfill::{AtomicU32, AtomicU8}; |
| 8 | use embassy_cortex_m::interrupt::{Interrupt, InterruptExt}; | 8 | use embassy_cortex_m::interrupt::{Interrupt, InterruptExt}; |
| 9 | use embassy_hal_common::{Peripheral, PeripheralRef}; | 9 | use embassy_hal_common::{into_ref, Peripheral, PeripheralRef}; |
| 10 | use embassy_sync::waitqueue::AtomicWaker; | 10 | use embassy_sync::waitqueue::AtomicWaker; |
| 11 | use pac::io::vals::Gpio0ctrlFuncsel; | 11 | use pac::io::vals::Gpio0ctrlFuncsel; |
| 12 | 12 | ||
| 13 | use crate::dma::{Channel, Transfer, Word}; | 13 | use crate::dma::{Channel, Transfer, Word}; |
| 14 | use crate::gpio::sealed::Pin as SealedPin; | 14 | use crate::gpio::sealed::Pin as SealedPin; |
| 15 | use crate::gpio::{self, Drive, Pull, SlewRate}; | 15 | use crate::gpio::{self, AnyPin, Drive, Pull, SlewRate}; |
| 16 | use crate::pac::dma::vals::TreqSel; | 16 | use crate::pac::dma::vals::TreqSel; |
| 17 | use crate::pio::sealed::PioInstance as _; | 17 | use crate::pio::sealed::PioInstance as _; |
| 18 | use crate::{interrupt, pac, peripherals, RegExt}; | 18 | use crate::{interrupt, pac, peripherals, RegExt}; |
| @@ -244,17 +244,17 @@ impl<'d, PIO: PioInstance> Drop for IrqFuture<PIO> { | |||
| 244 | } | 244 | } |
| 245 | } | 245 | } |
| 246 | 246 | ||
| 247 | pub struct Pin<PIO: PioInstance> { | 247 | pub struct Pin<'l, PIO: PioInstance> { |
| 248 | pin_bank: u8, | 248 | pin: PeripheralRef<'l, AnyPin>, |
| 249 | pio: PhantomData<PIO>, | 249 | pio: PhantomData<PIO>, |
| 250 | } | 250 | } |
| 251 | 251 | ||
| 252 | impl<PIO: PioInstance> Pin<PIO> { | 252 | impl<'l, PIO: PioInstance> Pin<'l, PIO> { |
| 253 | /// Set the pin's drive strength. | 253 | /// Set the pin's drive strength. |
| 254 | #[inline] | 254 | #[inline] |
| 255 | pub fn set_drive_strength(&mut self, strength: Drive) { | 255 | pub fn set_drive_strength(&mut self, strength: Drive) { |
| 256 | unsafe { | 256 | unsafe { |
| 257 | self.pad_ctrl().modify(|w| { | 257 | self.pin.pad_ctrl().modify(|w| { |
| 258 | w.set_drive(match strength { | 258 | w.set_drive(match strength { |
| 259 | Drive::_2mA => pac::pads::vals::Drive::_2MA, | 259 | Drive::_2mA => pac::pads::vals::Drive::_2MA, |
| 260 | Drive::_4mA => pac::pads::vals::Drive::_4MA, | 260 | Drive::_4mA => pac::pads::vals::Drive::_4MA, |
| @@ -269,7 +269,7 @@ impl<PIO: PioInstance> Pin<PIO> { | |||
| 269 | #[inline] | 269 | #[inline] |
| 270 | pub fn set_slew_rate(&mut self, slew_rate: SlewRate) { | 270 | pub fn set_slew_rate(&mut self, slew_rate: SlewRate) { |
| 271 | unsafe { | 271 | unsafe { |
| 272 | self.pad_ctrl().modify(|w| { | 272 | self.pin.pad_ctrl().modify(|w| { |
| 273 | w.set_slewfast(slew_rate == SlewRate::Fast); | 273 | w.set_slewfast(slew_rate == SlewRate::Fast); |
| 274 | }); | 274 | }); |
| 275 | } | 275 | } |
| @@ -279,7 +279,7 @@ impl<PIO: PioInstance> Pin<PIO> { | |||
| 279 | #[inline] | 279 | #[inline] |
| 280 | pub fn set_pull(&mut self, pull: Pull) { | 280 | pub fn set_pull(&mut self, pull: Pull) { |
| 281 | unsafe { | 281 | unsafe { |
| 282 | self.pad_ctrl().modify(|w| { | 282 | self.pin.pad_ctrl().modify(|w| { |
| 283 | w.set_pue(pull == Pull::Up); | 283 | w.set_pue(pull == Pull::Up); |
| 284 | w.set_pde(pull == Pull::Down); | 284 | w.set_pde(pull == Pull::Down); |
| 285 | }); | 285 | }); |
| @@ -290,7 +290,7 @@ impl<PIO: PioInstance> Pin<PIO> { | |||
| 290 | #[inline] | 290 | #[inline] |
| 291 | pub fn set_schmitt(&mut self, enable: bool) { | 291 | pub fn set_schmitt(&mut self, enable: bool) { |
| 292 | unsafe { | 292 | unsafe { |
| 293 | self.pad_ctrl().modify(|w| { | 293 | self.pin.pad_ctrl().modify(|w| { |
| 294 | w.set_schmitt(enable); | 294 | w.set_schmitt(enable); |
| 295 | }); | 295 | }); |
| 296 | } | 296 | } |
| @@ -308,13 +308,7 @@ impl<PIO: PioInstance> Pin<PIO> { | |||
| 308 | } | 308 | } |
| 309 | 309 | ||
| 310 | pub fn pin(&self) -> u8 { | 310 | pub fn pin(&self) -> u8 { |
| 311 | self._pin() | 311 | self.pin._pin() |
| 312 | } | ||
| 313 | } | ||
| 314 | |||
| 315 | impl<PIO: PioInstance> SealedPin for Pin<PIO> { | ||
| 316 | fn pin_bank(&self) -> u8 { | ||
| 317 | self.pin_bank | ||
| 318 | } | 312 | } |
| 319 | } | 313 | } |
| 320 | 314 | ||
| @@ -890,14 +884,15 @@ impl<'d, PIO: PioInstance> PioCommon<'d, PIO> { | |||
| 890 | /// Register a pin for PIO usage. Pins will be released from the PIO block | 884 | /// Register a pin for PIO usage. Pins will be released from the PIO block |
| 891 | /// (i.e., have their `FUNCSEL` reset to `NULL`) when the [`PioCommon`] *and* | 885 | /// (i.e., have their `FUNCSEL` reset to `NULL`) when the [`PioCommon`] *and* |
| 892 | /// all [`PioStateMachine`]s for this block have been dropped. | 886 | /// all [`PioStateMachine`]s for this block have been dropped. |
| 893 | pub fn make_pio_pin(&mut self, pin: impl PioPin) -> Pin<PIO> { | 887 | pub fn make_pio_pin(&mut self, pin: impl Peripheral<P = impl PioPin + 'd> + 'd) -> Pin<'d, PIO> { |
| 888 | into_ref!(pin); | ||
| 894 | unsafe { | 889 | unsafe { |
| 895 | pin.io().ctrl().write(|w| w.set_funcsel(PIO::FUNCSEL.0)); | 890 | pin.io().ctrl().write(|w| w.set_funcsel(PIO::FUNCSEL.0)); |
| 896 | } | 891 | } |
| 897 | // we can be relaxed about this because we're &mut here and nothing is cached | 892 | // we can be relaxed about this because we're &mut here and nothing is cached |
| 898 | PIO::state().used_pins.fetch_or(1 << pin.pin_bank(), Ordering::Relaxed); | 893 | PIO::state().used_pins.fetch_or(1 << pin.pin_bank(), Ordering::Relaxed); |
| 899 | Pin { | 894 | Pin { |
| 900 | pin_bank: pin.pin_bank(), | 895 | pin: pin.into_ref().map_into(), |
| 901 | pio: PhantomData::default(), | 896 | pio: PhantomData::default(), |
| 902 | } | 897 | } |
| 903 | } | 898 | } |
