Merge pull request #2093 from embassy-rs/net-wiznet-linkupdwon

net-wiznet: report link up/down on cable plug/unplug.
This commit is contained in:
Dario Nieuwenhuis 2023-10-19 23:38:44 +00:00 committed by GitHub
commit d622181205
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,14 +1,14 @@
//! [`embassy-net`](https://crates.io/crates/embassy-net) driver for WIZnet ethernet chips.
#![no_std] #![no_std]
#![feature(async_fn_in_trait)] #![feature(async_fn_in_trait)]
#![doc = include_str!("../README.md")]
pub mod chip; pub mod chip;
mod device; mod device;
use embassy_futures::select::{select, Either}; use embassy_futures::select::{select3, Either3};
use embassy_net_driver_channel as ch; use embassy_net_driver_channel as ch;
use embassy_net_driver_channel::driver::LinkState; use embassy_net_driver_channel::driver::LinkState;
use embassy_time::Timer; use embassy_time::{Duration, Ticker, Timer};
use embedded_hal::digital::OutputPin; use embedded_hal::digital::OutputPin;
use embedded_hal_async::digital::Wait; use embedded_hal_async::digital::Wait;
use embedded_hal_async::spi::SpiDevice; use embedded_hal_async::spi::SpiDevice;
@ -49,35 +49,37 @@ pub struct Runner<'d, C: Chip, SPI: SpiDevice, INT: Wait, RST: OutputPin> {
impl<'d, C: Chip, SPI: SpiDevice, INT: Wait, RST: OutputPin> Runner<'d, C, SPI, INT, RST> { impl<'d, C: Chip, SPI: SpiDevice, INT: Wait, RST: OutputPin> Runner<'d, C, SPI, INT, RST> {
pub async fn run(mut self) -> ! { pub async fn run(mut self) -> ! {
let (state_chan, mut rx_chan, mut tx_chan) = self.ch.split(); let (state_chan, mut rx_chan, mut tx_chan) = self.ch.split();
let mut tick = Ticker::every(Duration::from_millis(500));
loop { loop {
if self.mac.is_link_up().await { match select3(
state_chan.set_link_state(LinkState::Up);
loop {
match select(
async { async {
self.int.wait_for_low().await.ok(); self.int.wait_for_low().await.ok();
rx_chan.rx_buf().await rx_chan.rx_buf().await
}, },
tx_chan.tx_buf(), tx_chan.tx_buf(),
tick.next(),
) )
.await .await
{ {
Either::First(p) => { Either3::First(p) => {
if let Ok(n) = self.mac.read_frame(p).await { if let Ok(n) = self.mac.read_frame(p).await {
rx_chan.rx_done(n); rx_chan.rx_done(n);
} }
} }
Either::Second(p) => { Either3::Second(p) => {
self.mac.write_frame(p).await.ok(); self.mac.write_frame(p).await.ok();
tx_chan.tx_done(); tx_chan.tx_done();
} }
} Either3::Third(()) => {
} if self.mac.is_link_up().await {
state_chan.set_link_state(LinkState::Up);
} else { } else {
state_chan.set_link_state(LinkState::Down); state_chan.set_link_state(LinkState::Down);
} }
} }
} }
}
}
} }
/// Create a Wiznet ethernet chip driver for [`embassy-net`](https://crates.io/crates/embassy-net). /// Create a Wiznet ethernet chip driver for [`embassy-net`](https://crates.io/crates/embassy-net).