Merge pull request #1653 from xoviat/eth

stm32/eth: solve busy-loop polling
This commit is contained in:
Dario Nieuwenhuis 2023-07-15 21:18:03 +00:00 committed by GitHub
commit 25197308e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 194 additions and 34 deletions

View File

@ -1,5 +1,11 @@
//! Generic SMI Ethernet PHY //! Generic SMI Ethernet PHY
#[cfg(feature = "time")]
use embassy_time::{Duration, Timer};
use futures::task::Context;
#[cfg(feature = "time")]
use futures::FutureExt;
use super::{StationManagement, PHY}; use super::{StationManagement, PHY};
#[allow(dead_code)] #[allow(dead_code)]
@ -36,25 +42,48 @@ mod phy_consts {
use self::phy_consts::*; use self::phy_consts::*;
/// Generic SMI Ethernet PHY /// Generic SMI Ethernet PHY
pub struct GenericSMI; pub struct GenericSMI {
#[cfg(feature = "time")]
poll_interval: Duration,
}
impl GenericSMI {
#[cfg(feature = "time")]
pub fn new() -> Self {
Self {
poll_interval: Duration::from_millis(500),
}
}
#[cfg(not(feature = "time"))]
pub fn new() -> Self {
Self {}
}
}
unsafe impl PHY for GenericSMI { unsafe impl PHY for GenericSMI {
/// Reset PHY and wait for it to come out of reset. /// Reset PHY and wait for it to come out of reset.
fn phy_reset<S: StationManagement>(sm: &mut S) { fn phy_reset<S: StationManagement>(&mut self, sm: &mut S) {
sm.smi_write(PHY_REG_BCR, PHY_REG_BCR_RESET); sm.smi_write(PHY_REG_BCR, PHY_REG_BCR_RESET);
while sm.smi_read(PHY_REG_BCR) & PHY_REG_BCR_RESET == PHY_REG_BCR_RESET {} while sm.smi_read(PHY_REG_BCR) & PHY_REG_BCR_RESET == PHY_REG_BCR_RESET {}
} }
/// PHY initialisation. /// PHY initialisation.
fn phy_init<S: StationManagement>(sm: &mut S) { fn phy_init<S: StationManagement>(&mut self, sm: &mut S) {
// Clear WU CSR // Clear WU CSR
Self::smi_write_ext(sm, PHY_REG_WUCSR, 0); self.smi_write_ext(sm, PHY_REG_WUCSR, 0);
// Enable auto-negotiation // Enable auto-negotiation
sm.smi_write(PHY_REG_BCR, PHY_REG_BCR_AN | PHY_REG_BCR_ANRST | PHY_REG_BCR_100M); sm.smi_write(PHY_REG_BCR, PHY_REG_BCR_AN | PHY_REG_BCR_ANRST | PHY_REG_BCR_100M);
} }
fn poll_link<S: StationManagement>(sm: &mut S) -> bool { fn poll_link<S: StationManagement>(&mut self, sm: &mut S, cx: &mut Context) -> bool {
#[cfg(not(feature = "time"))]
cx.waker().wake_by_ref();
#[cfg(feature = "time")]
let _ = Timer::after(self.poll_interval).poll_unpin(cx);
let bsr = sm.smi_read(PHY_REG_BSR); let bsr = sm.smi_read(PHY_REG_BSR);
// No link without autonegotiate // No link without autonegotiate
@ -73,8 +102,12 @@ unsafe impl PHY for GenericSMI {
/// Public functions for the PHY /// Public functions for the PHY
impl GenericSMI { impl GenericSMI {
pub fn set_poll_interval(&mut self, poll_interval: Duration) {
self.poll_interval = poll_interval
}
// Writes a value to an extended PHY register in MMD address space // Writes a value to an extended PHY register in MMD address space
fn smi_write_ext<S: StationManagement>(sm: &mut S, reg_addr: u16, reg_data: u16) { fn smi_write_ext<S: StationManagement>(&mut self, sm: &mut S, reg_addr: u16, reg_data: u16) {
sm.smi_write(PHY_REG_CTL, 0x0003); // set address sm.smi_write(PHY_REG_CTL, 0x0003); // set address
sm.smi_write(PHY_REG_ADDAR, reg_addr); sm.smi_write(PHY_REG_ADDAR, reg_addr);
sm.smi_write(PHY_REG_CTL, 0x4003); // set data sm.smi_write(PHY_REG_CTL, 0x4003); // set data

View File

@ -81,9 +81,7 @@ impl<'d, T: Instance, P: PHY> embassy_net_driver::Driver for Ethernet<'d, T, P>
} }
fn link_state(&mut self, cx: &mut Context) -> LinkState { fn link_state(&mut self, cx: &mut Context) -> LinkState {
// TODO: wake cx.waker on link state change if self.phy.poll_link(&mut self.station_management, cx) {
cx.waker().wake_by_ref();
if P::poll_link(self) {
LinkState::Up LinkState::Up
} else { } else {
LinkState::Down LinkState::Down
@ -148,11 +146,11 @@ pub unsafe trait StationManagement {
/// The methods cannot move S /// The methods cannot move S
pub unsafe trait PHY { pub unsafe trait PHY {
/// Reset PHY and wait for it to come out of reset. /// Reset PHY and wait for it to come out of reset.
fn phy_reset<S: StationManagement>(sm: &mut S); fn phy_reset<S: StationManagement>(&mut self, sm: &mut S);
/// PHY initialisation. /// PHY initialisation.
fn phy_init<S: StationManagement>(sm: &mut S); fn phy_init<S: StationManagement>(&mut self, sm: &mut S);
/// Poll link to see if it is up and FD with 100Mbps /// Poll link to see if it is up and FD with 100Mbps
fn poll_link<S: StationManagement>(sm: &mut S) -> bool; fn poll_link<S: StationManagement>(&mut self, sm: &mut S, cx: &mut Context) -> bool;
} }
pub(crate) mod sealed { pub(crate) mod sealed {

View File

@ -3,6 +3,7 @@
mod rx_desc; mod rx_desc;
mod tx_desc; mod tx_desc;
use core::marker::PhantomData;
use core::sync::atomic::{fence, Ordering}; use core::sync::atomic::{fence, Ordering};
use embassy_hal_common::{into_ref, PeripheralRef}; use embassy_hal_common::{into_ref, PeripheralRef};
@ -48,9 +49,8 @@ pub struct Ethernet<'d, T: Instance, P: PHY> {
pub(crate) rx: RDesRing<'d>, pub(crate) rx: RDesRing<'d>,
pins: [PeripheralRef<'d, AnyPin>; 9], pins: [PeripheralRef<'d, AnyPin>; 9],
_phy: P, pub(crate) phy: P,
clock_range: Cr, pub(crate) station_management: EthernetStationManagement<T>,
phy_addr: u8,
pub(crate) mac_addr: [u8; 6], pub(crate) mac_addr: [u8; 6],
} }
@ -224,9 +224,12 @@ impl<'d, T: Instance, P: PHY> Ethernet<'d, T, P> {
let mut this = Self { let mut this = Self {
_peri: peri, _peri: peri,
pins, pins,
_phy: phy, phy: phy,
clock_range, station_management: EthernetStationManagement {
phy_addr, peri: PhantomData,
clock_range: clock_range,
phy_addr: phy_addr,
},
mac_addr, mac_addr,
tx: TDesRing::new(&mut queue.tx_desc, &mut queue.tx_buf), tx: TDesRing::new(&mut queue.tx_desc, &mut queue.tx_buf),
rx: RDesRing::new(&mut queue.rx_desc, &mut queue.rx_buf), rx: RDesRing::new(&mut queue.rx_desc, &mut queue.rx_buf),
@ -256,8 +259,8 @@ impl<'d, T: Instance, P: PHY> Ethernet<'d, T, P> {
w.set_tie(true); w.set_tie(true);
}); });
P::phy_reset(&mut this); this.phy.phy_reset(&mut this.station_management);
P::phy_init(&mut this); this.phy.phy_init(&mut this.station_management);
interrupt::ETH.unpend(); interrupt::ETH.unpend();
unsafe { interrupt::ETH.enable() }; unsafe { interrupt::ETH.enable() };
@ -266,7 +269,13 @@ impl<'d, T: Instance, P: PHY> Ethernet<'d, T, P> {
} }
} }
unsafe impl<'d, T: Instance, P: PHY> StationManagement for Ethernet<'d, T, P> { pub struct EthernetStationManagement<T: Instance> {
peri: PhantomData<T>,
clock_range: Cr,
phy_addr: u8,
}
unsafe impl<T: Instance> StationManagement for EthernetStationManagement<T> {
fn smi_read(&mut self, reg: u8) -> u16 { fn smi_read(&mut self, reg: u8) -> u16 {
let mac = ETH.ethernet_mac(); let mac = ETH.ethernet_mac();

View File

@ -1,5 +1,6 @@
mod descriptors; mod descriptors;
use core::marker::PhantomData;
use core::sync::atomic::{fence, Ordering}; use core::sync::atomic::{fence, Ordering};
use embassy_hal_common::{into_ref, PeripheralRef}; use embassy_hal_common::{into_ref, PeripheralRef};
@ -40,9 +41,8 @@ pub struct Ethernet<'d, T: Instance, P: PHY> {
pub(crate) tx: TDesRing<'d>, pub(crate) tx: TDesRing<'d>,
pub(crate) rx: RDesRing<'d>, pub(crate) rx: RDesRing<'d>,
pins: [PeripheralRef<'d, AnyPin>; 9], pins: [PeripheralRef<'d, AnyPin>; 9],
_phy: P, pub(crate) phy: P,
clock_range: u8, pub(crate) station_management: EthernetStationManagement<T>,
phy_addr: u8,
pub(crate) mac_addr: [u8; 6], pub(crate) mac_addr: [u8; 6],
} }
@ -201,9 +201,12 @@ impl<'d, T: Instance, P: PHY> Ethernet<'d, T, P> {
tx: TDesRing::new(&mut queue.tx_desc, &mut queue.tx_buf), tx: TDesRing::new(&mut queue.tx_desc, &mut queue.tx_buf),
rx: RDesRing::new(&mut queue.rx_desc, &mut queue.rx_buf), rx: RDesRing::new(&mut queue.rx_desc, &mut queue.rx_buf),
pins, pins,
_phy: phy, phy: phy,
clock_range, station_management: EthernetStationManagement {
phy_addr, peri: PhantomData,
clock_range: clock_range,
phy_addr: phy_addr,
},
mac_addr, mac_addr,
}; };
@ -229,8 +232,8 @@ impl<'d, T: Instance, P: PHY> Ethernet<'d, T, P> {
w.set_tie(true); w.set_tie(true);
}); });
P::phy_reset(&mut this); this.phy.phy_reset(&mut this.station_management);
P::phy_init(&mut this); this.phy.phy_init(&mut this.station_management);
interrupt::ETH.unpend(); interrupt::ETH.unpend();
unsafe { interrupt::ETH.enable() }; unsafe { interrupt::ETH.enable() };
@ -239,7 +242,13 @@ impl<'d, T: Instance, P: PHY> Ethernet<'d, T, P> {
} }
} }
unsafe impl<'d, T: Instance, P: PHY> StationManagement for Ethernet<'d, T, P> { pub struct EthernetStationManagement<T: Instance> {
peri: PhantomData<T>,
clock_range: u8,
phy_addr: u8,
}
unsafe impl<T: Instance> StationManagement for EthernetStationManagement<T> {
fn smi_read(&mut self, reg: u8) -> u16 { fn smi_read(&mut self, reg: u8) -> u16 {
let mac = ETH.ethernet_mac(); let mac = ETH.ethernet_mac();

View File

@ -0,0 +1,111 @@
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use defmt::*;
use embassy_executor::Spawner;
use embassy_net::tcp::TcpSocket;
use embassy_net::{Ipv4Address, Stack, StackResources};
use embassy_stm32::eth::generic_smi::GenericSMI;
use embassy_stm32::eth::{Ethernet, PacketQueue};
use embassy_stm32::peripherals::ETH;
use embassy_stm32::rng::Rng;
use embassy_stm32::time::mhz;
use embassy_stm32::{bind_interrupts, eth, Config};
use embassy_time::{Duration, Timer};
use embedded_io::asynch::Write;
use static_cell::make_static;
use {defmt_rtt as _, panic_probe as _};
bind_interrupts!(struct Irqs {
ETH => eth::InterruptHandler;
});
type Device = Ethernet<'static, ETH, GenericSMI>;
#[embassy_executor::task]
async fn net_task(stack: &'static Stack<Device>) -> ! {
stack.run().await
}
#[embassy_executor::main]
async fn main(spawner: Spawner) -> ! {
let mut config = Config::default();
config.rcc.sys_ck = Some(mhz(200));
let p = embassy_stm32::init(config);
info!("Hello World!");
// Generate random seed.
let mut rng = Rng::new(p.RNG);
let mut seed = [0; 8];
let _ = rng.async_fill_bytes(&mut seed).await;
let seed = u64::from_le_bytes(seed);
let mac_addr = [0x00, 0x00, 0xDE, 0xAD, 0xBE, 0xEF];
let device = Ethernet::new(
make_static!(PacketQueue::<16, 16>::new()),
p.ETH,
Irqs,
p.PA1,
p.PA2,
p.PC1,
p.PA7,
p.PC4,
p.PC5,
p.PG13,
p.PB13,
p.PG11,
GenericSMI::new(),
mac_addr,
0,
);
let config = embassy_net::Config::dhcpv4(Default::default());
//let config = embassy_net::Config::ipv4_static(embassy_net::StaticConfigV4 {
// address: Ipv4Cidr::new(Ipv4Address::new(10, 42, 0, 61), 24),
// dns_servers: Vec::new(),
// gateway: Some(Ipv4Address::new(10, 42, 0, 1)),
//});
// Init network stack
let stack = &*make_static!(Stack::new(
device,
config,
make_static!(StackResources::<2>::new()),
seed
));
// Launch network task
unwrap!(spawner.spawn(net_task(&stack)));
info!("Network task initialized");
// Then we can use it!
let mut rx_buffer = [0; 4096];
let mut tx_buffer = [0; 4096];
loop {
let mut socket = TcpSocket::new(&stack, &mut rx_buffer, &mut tx_buffer);
socket.set_timeout(Some(embassy_time::Duration::from_secs(10)));
let remote_endpoint = (Ipv4Address::new(10, 42, 0, 1), 8000);
info!("connecting...");
let r = socket.connect(remote_endpoint).await;
if let Err(e) = r {
info!("connect error: {:?}", e);
continue;
}
info!("connected!");
let buf = [0; 1024];
loop {
let r = socket.write_all(&buf).await;
if let Err(e) = r {
info!("write error: {:?}", e);
continue;
}
Timer::after(Duration::from_secs(1)).await;
}
}
}

View File

@ -57,7 +57,7 @@ async fn main(spawner: Spawner) -> ! {
p.PG13, p.PG13,
p.PB13, p.PB13,
p.PG11, p.PG11,
GenericSMI, GenericSMI::new(),
mac_addr, mac_addr,
0, 0,
); );

View File

@ -76,7 +76,7 @@ async fn main(spawner: Spawner) -> ! {
p.PG13, p.PG13,
p.PB15, p.PB15,
p.PG11, p.PG11,
GenericSMI, GenericSMI::new(),
mac_addr, mac_addr,
0, 0,
); );

View File

@ -58,7 +58,7 @@ async fn main(spawner: Spawner) -> ! {
p.PG13, p.PG13,
p.PB13, p.PB13,
p.PG11, p.PG11,
GenericSMI, GenericSMI::new(),
mac_addr, mac_addr,
0, 0,
); );

View File

@ -59,7 +59,7 @@ async fn main(spawner: Spawner) -> ! {
p.PG13, p.PG13,
p.PB13, p.PB13,
p.PG11, p.PG11,
GenericSMI, GenericSMI::new(),
mac_addr, mac_addr,
0, 0,
); );