eth-v2: Remove Instance trait

This commit is contained in:
Thales Fragoso 2021-06-10 23:33:31 -03:00 committed by Dario Nieuwenhuis
parent 05a239faf6
commit e039c7c42c

View File

@ -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<Inner<'d, T, TX, RX>>,
pub struct Ethernet<'d, P: PHY, const TX: usize, const RX: usize> {
state: PeripheralMutex<Inner<'d, TX, RX>>,
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<Target = T> + 'd,
interrupt: impl Unborrow<Target = T::Interrupt> + 'd,
ref_clk: impl Unborrow<Target = impl RefClkPin<T>> + 'd,
mdio: impl Unborrow<Target = impl MDIOPin<T>> + 'd,
mdc: impl Unborrow<Target = impl MDCPin<T>> + 'd,
crs: impl Unborrow<Target = impl CRSPin<T>> + 'd,
rx_d0: impl Unborrow<Target = impl RXD0Pin<T>> + 'd,
rx_d1: impl Unborrow<Target = impl RXD1Pin<T>> + 'd,
tx_d0: impl Unborrow<Target = impl TXD0Pin<T>> + 'd,
tx_d1: impl Unborrow<Target = impl TXD1Pin<T>> + 'd,
tx_en: impl Unborrow<Target = impl TXEnPin<T>> + 'd,
peri: impl Unborrow<Target = peripherals::ETH> + 'd,
interrupt: impl Unborrow<Target = crate::interrupt::ETH> + 'd,
ref_clk: impl Unborrow<Target = impl RefClkPin> + 'd,
mdio: impl Unborrow<Target = impl MDIOPin> + 'd,
mdc: impl Unborrow<Target = impl MDCPin> + 'd,
crs: impl Unborrow<Target = impl CRSPin> + 'd,
rx_d0: impl Unborrow<Target = impl RXD0Pin> + 'd,
rx_d1: impl Unborrow<Target = impl RXD1Pin> + 'd,
tx_d0: impl Unborrow<Target = impl TXD0Pin> + 'd,
tx_d1: impl Unborrow<Target = impl TXD1Pin> + 'd,
tx_en: impl Unborrow<Target = impl TXEnPin> + '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<TX, RX>,
}
impl<'d, T: Instance, const TX: usize, const RX: usize> Inner<'d, T, TX, RX> {
pub fn new(_peri: impl Unborrow<Target = T> + 'd) -> Self {
impl<'d, const TX: usize, const RX: usize> Inner<'d, TX, RX> {
pub fn new(_peri: impl Unborrow<Target = peripherals::ETH> + '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<T: Instance>: GpioPin {
pub trait RefClkPin: GpioPin {
fn configure(&mut self);
}
pub trait MDIOPin<T: Instance>: GpioPin {
pub trait MDIOPin: GpioPin {
fn configure(&mut self);
}
pub trait MDCPin<T: Instance>: GpioPin {
pub trait MDCPin: GpioPin {
fn configure(&mut self);
}
pub trait CRSPin<T: Instance>: GpioPin {
pub trait CRSPin: GpioPin {
fn configure(&mut self);
}
pub trait RXD0Pin<T: Instance>: GpioPin {
pub trait RXD0Pin: GpioPin {
fn configure(&mut self);
}
pub trait RXD1Pin<T: Instance>: GpioPin {
pub trait RXD1Pin: GpioPin {
fn configure(&mut self);
}
pub trait TXD0Pin<T: Instance>: GpioPin {
pub trait TXD0Pin: GpioPin {
fn configure(&mut self);
}
pub trait TXD1Pin<T: Instance>: GpioPin {
pub trait TXD1Pin: GpioPin {
fn configure(&mut self);
}
pub trait TXEnPin<T: Instance>: GpioPin {
pub trait TXEnPin: GpioPin {
fn configure(&mut self);
}
}
pub trait Instance: sealed::Instance + 'static {}
pub trait RefClkPin: sealed::RefClkPin + 'static {}
pub trait RefClkPin<T: Instance>: sealed::RefClkPin<T> + 'static {}
pub trait MDIOPin: sealed::MDIOPin + 'static {}
pub trait MDIOPin<T: Instance>: sealed::MDIOPin<T> + 'static {}
pub trait MDCPin: sealed::MDCPin + 'static {}
pub trait MDCPin<T: Instance>: sealed::MDCPin<T> + 'static {}
pub trait CRSPin: sealed::CRSPin + 'static {}
pub trait CRSPin<T: Instance>: sealed::CRSPin<T> + 'static {}
pub trait RXD0Pin: sealed::RXD0Pin + 'static {}
pub trait RXD0Pin<T: Instance>: sealed::RXD0Pin<T> + 'static {}
pub trait RXD1Pin: sealed::RXD1Pin + 'static {}
pub trait RXD1Pin<T: Instance>: sealed::RXD1Pin<T> + 'static {}
pub trait TXD0Pin: sealed::TXD0Pin + 'static {}
pub trait TXD0Pin<T: Instance>: sealed::TXD0Pin<T> + 'static {}
pub trait TXD1Pin: sealed::TXD1Pin + 'static {}
pub trait TXD1Pin<T: Instance>: sealed::TXD1Pin<T> + 'static {}
pub trait TXEnPin: sealed::TXEnPin + 'static {}
pub trait TXEnPin<T: Instance>: sealed::TXEnPin<T> + '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<peripherals::$inst> 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<peripherals::$inst> 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);
};
);