diff --git a/embassy-stm32/src/eth/v2/mod.rs b/embassy-stm32/src/eth/v2/mod.rs index 7debd5a1..8a17ac00 100644 --- a/embassy-stm32/src/eth/v2/mod.rs +++ b/embassy-stm32/src/eth/v2/mod.rs @@ -11,7 +11,6 @@ use embassy_net::{Device, DeviceCapabilities, LinkState, PacketBuf, MTU}; use crate::gpio::sealed::Pin as __GpioPin; use crate::gpio::AnyPin; use crate::gpio::Pin as GpioPin; -use crate::interrupt::Interrupt; use crate::pac::gpio::vals::Ospeedr; use crate::pac::ETH; use crate::peripherals; @@ -21,8 +20,8 @@ mod descriptors; use super::{StationManagement, PHY}; use descriptors::DescriptorRing; -pub struct Ethernet<'d, T: Instance, P: PHY, const TX: usize, const RX: usize> { - state: PeripheralMutex>, +pub struct Ethernet<'d, P: PHY, const TX: usize, const RX: usize> { + state: PeripheralMutex>, pins: [AnyPin; 9], _phy: P, clock_range: u8, @@ -30,19 +29,19 @@ pub struct Ethernet<'d, T: Instance, P: PHY, const TX: usize, const RX: usize> { mac_addr: [u8; 6], } -impl<'d, T: Instance, P: PHY, const TX: usize, const RX: usize> Ethernet<'d, T, P, TX, RX> { +impl<'d, P: PHY, const TX: usize, const RX: usize> Ethernet<'d, P, TX, RX> { pub fn new( - peri: impl Unborrow + 'd, - interrupt: impl Unborrow + 'd, - ref_clk: impl Unborrow> + 'd, - mdio: impl Unborrow> + 'd, - mdc: impl Unborrow> + 'd, - crs: impl Unborrow> + 'd, - rx_d0: impl Unborrow> + 'd, - rx_d1: impl Unborrow> + 'd, - tx_d0: impl Unborrow> + 'd, - tx_d1: impl Unborrow> + 'd, - tx_en: impl Unborrow> + 'd, + peri: impl Unborrow + 'd, + interrupt: impl Unborrow + 'd, + ref_clk: impl Unborrow + 'd, + mdio: impl Unborrow + 'd, + mdc: impl Unborrow + 'd, + crs: impl Unborrow + 'd, + rx_d0: impl Unborrow + 'd, + rx_d1: impl Unborrow + 'd, + tx_d0: impl Unborrow + 'd, + tx_d1: impl Unborrow + 'd, + tx_en: impl Unborrow + 'd, phy: P, mac_addr: [u8; 6], hclk: Hertz, @@ -183,8 +182,8 @@ impl<'d, T: Instance, P: PHY, const TX: usize, const RX: usize> Ethernet<'d, T, } } -unsafe impl<'d, T: Instance, P: PHY, const TX: usize, const RX: usize> StationManagement - for Ethernet<'d, T, P, TX, RX> +unsafe impl<'d, P: PHY, const TX: usize, const RX: usize> StationManagement + for Ethernet<'d, P, TX, RX> { fn smi_read(&mut self, reg: u8) -> u16 { // NOTE(unsafe) These registers aren't used in the interrupt and we have `&mut self` @@ -221,9 +220,7 @@ unsafe impl<'d, T: Instance, P: PHY, const TX: usize, const RX: usize> StationMa } } -impl<'d, T: Instance, P: PHY, const TX: usize, const RX: usize> Device - for Pin<&mut Ethernet<'d, T, P, TX, RX>> -{ +impl<'d, P: PHY, const TX: usize, const RX: usize> Device for Pin<&mut Ethernet<'d, P, TX, RX>> { fn is_transmit_ready(&mut self) -> bool { // NOTE(unsafe) We won't move out of self let this = unsafe { self.as_mut().get_unchecked_mut() }; @@ -249,7 +246,7 @@ impl<'d, T: Instance, P: PHY, const TX: usize, const RX: usize> Device } fn register_waker(&mut self, waker: &Waker) { - T::state().register(waker); + WAKER.register(waker); } fn capabilities(&mut self) -> DeviceCapabilities { @@ -278,9 +275,7 @@ impl<'d, T: Instance, P: PHY, const TX: usize, const RX: usize> Device } } -impl<'d, T: Instance, P: PHY, const TX: usize, const RX: usize> Drop - for Ethernet<'d, T, P, TX, RX> -{ +impl<'d, P: PHY, const TX: usize, const RX: usize> Drop for Ethernet<'d, P, TX, RX> { fn drop(&mut self) { // NOTE(unsafe) We have `&mut self` and the interrupt doesn't use this registers unsafe { @@ -323,13 +318,13 @@ impl<'d, T: Instance, P: PHY, const TX: usize, const RX: usize> Drop //---------------------------------------------------------------------- -struct Inner<'d, T: Instance, const TX: usize, const RX: usize> { - _peri: PhantomData<&'d mut T>, +struct Inner<'d, const TX: usize, const RX: usize> { + _peri: PhantomData<&'d mut peripherals::ETH>, desc_ring: DescriptorRing, } -impl<'d, T: Instance, const TX: usize, const RX: usize> Inner<'d, T, TX, RX> { - pub fn new(_peri: impl Unborrow + 'd) -> Self { +impl<'d, const TX: usize, const RX: usize> Inner<'d, TX, RX> { + pub fn new(_peri: impl Unborrow + 'd) -> Self { Self { _peri: PhantomData, desc_ring: DescriptorRing::new(), @@ -337,14 +332,14 @@ impl<'d, T: Instance, const TX: usize, const RX: usize> Inner<'d, T, TX, RX> { } } -impl<'d, T: Instance, const TX: usize, const RX: usize> PeripheralState for Inner<'d, T, TX, RX> { - type Interrupt = T::Interrupt; +impl<'d, const TX: usize, const RX: usize> PeripheralState for Inner<'d, TX, RX> { + type Interrupt = crate::interrupt::ETH; fn on_interrupt(&mut self) { unwrap!(self.desc_ring.tx.on_interrupt()); self.desc_ring.rx.on_interrupt(); - T::state().wake(); + WAKER.wake(); // TODO: Check and clear more flags unsafe { @@ -364,87 +359,66 @@ impl<'d, T: Instance, const TX: usize, const RX: usize> PeripheralState for Inne mod sealed { use super::*; - pub trait Instance { - type Interrupt: Interrupt; - - fn state() -> &'static AtomicWaker; - } - - pub trait RefClkPin: GpioPin { + pub trait RefClkPin: GpioPin { fn configure(&mut self); } - pub trait MDIOPin: GpioPin { + pub trait MDIOPin: GpioPin { fn configure(&mut self); } - pub trait MDCPin: GpioPin { + pub trait MDCPin: GpioPin { fn configure(&mut self); } - pub trait CRSPin: GpioPin { + pub trait CRSPin: GpioPin { fn configure(&mut self); } - pub trait RXD0Pin: GpioPin { + pub trait RXD0Pin: GpioPin { fn configure(&mut self); } - pub trait RXD1Pin: GpioPin { + pub trait RXD1Pin: GpioPin { fn configure(&mut self); } - pub trait TXD0Pin: GpioPin { + pub trait TXD0Pin: GpioPin { fn configure(&mut self); } - pub trait TXD1Pin: GpioPin { + pub trait TXD1Pin: GpioPin { fn configure(&mut self); } - pub trait TXEnPin: GpioPin { + pub trait TXEnPin: GpioPin { fn configure(&mut self); } } -pub trait Instance: sealed::Instance + 'static {} +pub trait RefClkPin: sealed::RefClkPin + 'static {} -pub trait RefClkPin: sealed::RefClkPin + 'static {} +pub trait MDIOPin: sealed::MDIOPin + 'static {} -pub trait MDIOPin: sealed::MDIOPin + 'static {} +pub trait MDCPin: sealed::MDCPin + 'static {} -pub trait MDCPin: sealed::MDCPin + 'static {} +pub trait CRSPin: sealed::CRSPin + 'static {} -pub trait CRSPin: sealed::CRSPin + 'static {} +pub trait RXD0Pin: sealed::RXD0Pin + 'static {} -pub trait RXD0Pin: sealed::RXD0Pin + 'static {} +pub trait RXD1Pin: sealed::RXD1Pin + 'static {} -pub trait RXD1Pin: sealed::RXD1Pin + 'static {} +pub trait TXD0Pin: sealed::TXD0Pin + 'static {} -pub trait TXD0Pin: sealed::TXD0Pin + 'static {} +pub trait TXD1Pin: sealed::TXD1Pin + 'static {} -pub trait TXD1Pin: sealed::TXD1Pin + 'static {} +pub trait TXEnPin: sealed::TXEnPin + 'static {} -pub trait TXEnPin: sealed::TXEnPin + 'static {} - -crate::pac::peripherals!( - (eth, $inst:ident) => { - impl sealed::Instance for peripherals::$inst { - type Interrupt = crate::interrupt::$inst; - - fn state() -> &'static AtomicWaker { - static WAKER: AtomicWaker = AtomicWaker::new(); - &WAKER - } - } - - impl Instance for peripherals::$inst {} - }; -); +static WAKER: AtomicWaker = AtomicWaker::new(); macro_rules! impl_pin { - ($inst:ident, $pin:ident, $signal:ident, $af:expr) => { - impl sealed::$signal for peripherals::$pin { + ($pin:ident, $signal:ident, $af:expr) => { + impl sealed::$signal for peripherals::$pin { fn configure(&mut self) { // NOTE(unsafe) Exclusive access to the registers critical_section::with(|_| unsafe { @@ -456,36 +430,36 @@ macro_rules! impl_pin { } } - impl $signal for peripherals::$pin {} + impl $signal for peripherals::$pin {} }; } crate::pac::peripheral_pins!( ($inst:ident, eth, ETH, $pin:ident, REF_CLK, $af:expr) => { - impl_pin!($inst, $pin, RefClkPin, $af); + impl_pin!($pin, RefClkPin, $af); }; ($inst:ident, eth, ETH, $pin:ident, MDIO, $af:expr) => { - impl_pin!($inst, $pin, MDIOPin, $af); + impl_pin!($pin, MDIOPin, $af); }; ($inst:ident, eth, ETH, $pin:ident, MDC, $af:expr) => { - impl_pin!($inst, $pin, MDCPin, $af); + impl_pin!($pin, MDCPin, $af); }; ($inst:ident, eth, ETH, $pin:ident, CRS_DV, $af:expr) => { - impl_pin!($inst, $pin, CRSPin, $af); + impl_pin!($pin, CRSPin, $af); }; ($inst:ident, eth, ETH, $pin:ident, RXD0, $af:expr) => { - impl_pin!($inst, $pin, RXD0Pin, $af); + impl_pin!($pin, RXD0Pin, $af); }; ($inst:ident, eth, ETH, $pin:ident, RXD1, $af:expr) => { - impl_pin!($inst, $pin, RXD1Pin, $af); + impl_pin!($pin, RXD1Pin, $af); }; ($inst:ident, eth, ETH, $pin:ident, TXD0, $af:expr) => { - impl_pin!($inst, $pin, TXD0Pin, $af); + impl_pin!($pin, TXD0Pin, $af); }; ($inst:ident, eth, ETH, $pin:ident, TXD1, $af:expr) => { - impl_pin!($inst, $pin, TXD1Pin, $af); + impl_pin!($pin, TXD1Pin, $af); }; ($inst:ident, eth, ETH, $pin:ident, TX_EN, $af:expr) => { - impl_pin!($inst, $pin, TXEnPin, $af); + impl_pin!($pin, TXEnPin, $af); }; );