From d41eeeae79388f219bf6a84e2f7bde9f6b532516 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Wed, 26 Mar 2025 16:01:37 +0100 Subject: Remove Peripheral trait, rename PeripheralRef->Peri. --- embassy-nxp/src/gpio.rs | 35 ++++++++++++++--------------------- embassy-nxp/src/lib.rs | 2 +- embassy-nxp/src/pint.rs | 18 +++++++++--------- 3 files changed, 24 insertions(+), 31 deletions(-) (limited to 'embassy-nxp/src') diff --git a/embassy-nxp/src/gpio.rs b/embassy-nxp/src/gpio.rs index d5d04ee69..c7c78ce61 100644 --- a/embassy-nxp/src/gpio.rs +++ b/embassy-nxp/src/gpio.rs @@ -1,7 +1,7 @@ -use embassy_hal_internal::impl_peripheral; +use embassy_hal_internal::{impl_peripheral, PeripheralType}; use crate::pac_utils::*; -use crate::{peripherals, Peripheral, PeripheralRef}; +use crate::{peripherals, Peri}; pub(crate) fn init() { // Enable clocks for GPIO, PINT, and IOCON @@ -45,7 +45,7 @@ pub struct Output<'d> { impl<'d> Output<'d> { /// Create GPIO output driver for a [Pin] with the provided [initial output](Level). #[inline] - pub fn new(pin: impl Peripheral

+ 'd, initial_output: Level) -> Self { + pub fn new(pin: Peri<'d, impl Pin>, initial_output: Level) -> Self { let mut pin = Flex::new(pin); pin.set_as_output(); let mut result = Self { pin }; @@ -90,7 +90,7 @@ pub struct Input<'d> { impl<'d> Input<'d> { /// Create GPIO output driver for a [Pin] with the provided [Pull]. #[inline] - pub fn new(pin: impl Peripheral

+ 'd, pull: Pull) -> Self { + pub fn new(pin: Peri<'d, impl Pin>, pull: Pull) -> Self { let mut pin = Flex::new(pin); pin.set_as_input(); let mut result = Self { pin }; @@ -124,7 +124,7 @@ impl<'d> Input<'d> { /// A flexible GPIO (digital mode) pin whose mode is not yet determined. Under the hood, this is a /// reference to a type-erased pin called ["AnyPin"](AnyPin). pub struct Flex<'d> { - pub(crate) pin: PeripheralRef<'d, AnyPin>, + pub(crate) pin: Peri<'d, AnyPin>, } impl<'d> Flex<'d> { @@ -132,10 +132,8 @@ impl<'d> Flex<'d> { /// /// Note: you cannot assume that the pin will be in Digital mode after this call. #[inline] - pub fn new(pin: impl Peripheral

+ 'd) -> Self { - Self { - pin: pin.into_ref().map_into(), - } + pub fn new(pin: Peri<'d, impl Pin>) -> Self { + Self { pin: pin.into() } } /// Get the bank of this pin. See also [Bank]. @@ -218,15 +216,7 @@ pub(crate) trait SealedPin: Sized { /// [AnyPin]. By default, this trait is sealed and cannot be implemented outside of the /// `embassy-nxp` crate due to the [SealedPin] trait. #[allow(private_bounds)] -pub trait Pin: Peripheral

+ Into + SealedPin + Sized + 'static { - /// Degrade to a generic pin struct - fn degrade(self) -> AnyPin { - AnyPin { - pin_bank: self.pin_bank(), - pin_number: self.pin_number(), - } - } - +pub trait Pin: PeripheralType + Into + SealedPin + Sized + 'static { /// Returns the pin number within a bank #[inline] fn pin(&self) -> u8 { @@ -252,8 +242,8 @@ impl AnyPin { /// # Safety /// /// You must ensure that you’re only using one instance of this type at a time. - pub unsafe fn steal(pin_bank: Bank, pin_number: u8) -> Self { - Self { pin_bank, pin_number } + pub unsafe fn steal(pin_bank: Bank, pin_number: u8) -> Peri<'static, Self> { + Peri::new_unchecked(Self { pin_bank, pin_number }) } } @@ -289,7 +279,10 @@ macro_rules! impl_pin { impl From for crate::gpio::AnyPin { fn from(val: peripherals::$name) -> Self { - crate::gpio::Pin::degrade(val) + Self { + pin_bank: val.pin_bank(), + pin_number: val.pin_number(), + } } } }; diff --git a/embassy-nxp/src/lib.rs b/embassy-nxp/src/lib.rs index 80fdecb2e..ad2056c06 100644 --- a/embassy-nxp/src/lib.rs +++ b/embassy-nxp/src/lib.rs @@ -4,7 +4,7 @@ pub mod gpio; mod pac_utils; pub mod pint; -pub use embassy_hal_internal::{into_ref, Peripheral, PeripheralRef}; +pub use embassy_hal_internal::Peri; pub use lpc55_pac as pac; /// Initialize the `embassy-nxp` HAL with the provided configuration. diff --git a/embassy-nxp/src/pint.rs b/embassy-nxp/src/pint.rs index 809be4bff..8d6dc1277 100644 --- a/embassy-nxp/src/pint.rs +++ b/embassy-nxp/src/pint.rs @@ -5,12 +5,12 @@ use core::pin::Pin as FuturePin; use core::task::{Context, Poll}; use critical_section::Mutex; -use embassy_hal_internal::{Peripheral, PeripheralRef}; use embassy_sync::waitqueue::AtomicWaker; use crate::gpio::{self, AnyPin, Level, SealedPin}; use crate::pac::interrupt; use crate::pac_utils::*; +use crate::Peri; struct PinInterrupt { assigned: bool, @@ -107,14 +107,14 @@ pub(crate) fn init() { #[must_use = "futures do nothing unless you `.await` or poll them"] struct InputFuture<'d> { #[allow(dead_code)] - pin: PeripheralRef<'d, AnyPin>, + pin: Peri<'d, AnyPin>, interrupt_number: usize, } impl<'d> InputFuture<'d> { /// Create a new input future. Returns None if all interrupts are in use. - fn new(pin: impl Peripheral

+ 'd, interrupt_on: InterruptOn) -> Option { - let pin = pin.into_ref().map_into(); + fn new(pin: Peri<'d, impl gpio::Pin>, interrupt_on: InterruptOn) -> Option { + let pin = pin.into(); let interrupt_number = next_available_interrupt()?; // Clear interrupt, just in case @@ -344,35 +344,35 @@ impl gpio::Flex<'_> { /// Wait for a falling or rising edge on the pin. You can have at most 8 pins waiting. If you /// try to wait for more than 8 pins, this function will return `None`. pub async fn wait_for_any_edge(&mut self) -> Option<()> { - InputFuture::new(&mut self.pin, InterruptOn::Edge(Edge::Both))?.await; + InputFuture::new(self.pin.reborrow(), InterruptOn::Edge(Edge::Both))?.await; Some(()) } /// Wait for a falling edge on the pin. You can have at most 8 pins waiting. If you try to wait /// for more than 8 pins, this function will return `None`. pub async fn wait_for_falling_edge(&mut self) -> Option<()> { - InputFuture::new(&mut self.pin, InterruptOn::Edge(Edge::Falling))?.await; + InputFuture::new(self.pin.reborrow(), InterruptOn::Edge(Edge::Falling))?.await; Some(()) } /// Wait for a rising edge on the pin. You can have at most 8 pins waiting. If you try to wait /// for more than 8 pins, this function will return `None`. pub async fn wait_for_rising_edge(&mut self) -> Option<()> { - InputFuture::new(&mut self.pin, InterruptOn::Edge(Edge::Rising))?.await; + InputFuture::new(self.pin.reborrow(), InterruptOn::Edge(Edge::Rising))?.await; Some(()) } /// Wait for a low level on the pin. You can have at most 8 pins waiting. If you try to wait for /// more than 8 pins, this function will return `None`. pub async fn wait_for_low(&mut self) -> Option<()> { - InputFuture::new(&mut self.pin, InterruptOn::Level(Level::Low))?.await; + InputFuture::new(self.pin.reborrow(), InterruptOn::Level(Level::Low))?.await; Some(()) } /// Wait for a high level on the pin. You can have at most 8 pins waiting. If you try to wait for /// more than 8 pins, this function will return `None`. pub async fn wait_for_high(&mut self) -> Option<()> { - InputFuture::new(&mut self.pin, InterruptOn::Level(Level::High))?.await; + InputFuture::new(self.pin.reborrow(), InterruptOn::Level(Level::High))?.await; Some(()) } } -- cgit