diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-10-20 01:29:10 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2023-10-20 01:29:10 +0200 |
| commit | 630443a4d64dda9d2840525307453bc358ef02d3 (patch) | |
| tree | b96acad3a57cc733086c26d17c074aa2f6263e0d /embassy-net-wiznet | |
| parent | 035800bfbd2779f713e1b2117146d0d89269bcb4 (diff) | |
net-wiznet: report link up/down on cable plug/unplug.
Diffstat (limited to 'embassy-net-wiznet')
| -rw-r--r-- | embassy-net-wiznet/src/lib.rs | 54 |
1 files changed, 28 insertions, 26 deletions
diff --git a/embassy-net-wiznet/src/lib.rs b/embassy-net-wiznet/src/lib.rs index 48d17cac2..afdb6729c 100644 --- a/embassy-net-wiznet/src/lib.rs +++ b/embassy-net-wiznet/src/lib.rs | |||
| @@ -1,14 +1,14 @@ | |||
| 1 | //! [`embassy-net`](https://crates.io/crates/embassy-net) driver for WIZnet ethernet chips. | ||
| 2 | #![no_std] | 1 | #![no_std] |
| 3 | #![feature(async_fn_in_trait)] | 2 | #![feature(async_fn_in_trait)] |
| 3 | #![doc = include_str!("../README.md")] | ||
| 4 | 4 | ||
| 5 | pub mod chip; | 5 | pub mod chip; |
| 6 | mod device; | 6 | mod device; |
| 7 | 7 | ||
| 8 | use embassy_futures::select::{select, Either}; | 8 | use embassy_futures::select::{select3, Either3}; |
| 9 | use embassy_net_driver_channel as ch; | 9 | use embassy_net_driver_channel as ch; |
| 10 | use embassy_net_driver_channel::driver::LinkState; | 10 | use embassy_net_driver_channel::driver::LinkState; |
| 11 | use embassy_time::Timer; | 11 | use embassy_time::{Duration, Ticker, Timer}; |
| 12 | use embedded_hal::digital::OutputPin; | 12 | use embedded_hal::digital::OutputPin; |
| 13 | use embedded_hal_async::digital::Wait; | 13 | use embedded_hal_async::digital::Wait; |
| 14 | use embedded_hal_async::spi::SpiDevice; | 14 | use embedded_hal_async::spi::SpiDevice; |
| @@ -49,32 +49,34 @@ pub struct Runner<'d, C: Chip, SPI: SpiDevice, INT: Wait, RST: OutputPin> { | |||
| 49 | impl<'d, C: Chip, SPI: SpiDevice, INT: Wait, RST: OutputPin> Runner<'d, C, SPI, INT, RST> { | 49 | impl<'d, C: Chip, SPI: SpiDevice, INT: Wait, RST: OutputPin> Runner<'d, C, SPI, INT, RST> { |
| 50 | pub async fn run(mut self) -> ! { | 50 | pub async fn run(mut self) -> ! { |
| 51 | let (state_chan, mut rx_chan, mut tx_chan) = self.ch.split(); | 51 | let (state_chan, mut rx_chan, mut tx_chan) = self.ch.split(); |
| 52 | let mut tick = Ticker::every(Duration::from_millis(500)); | ||
| 52 | loop { | 53 | loop { |
| 53 | if self.mac.is_link_up().await { | 54 | match select3( |
| 54 | state_chan.set_link_state(LinkState::Up); | 55 | async { |
| 55 | loop { | 56 | self.int.wait_for_low().await.ok(); |
| 56 | match select( | 57 | rx_chan.rx_buf().await |
| 57 | async { | 58 | }, |
| 58 | self.int.wait_for_low().await.ok(); | 59 | tx_chan.tx_buf(), |
| 59 | rx_chan.rx_buf().await | 60 | tick.next(), |
| 60 | }, | 61 | ) |
| 61 | tx_chan.tx_buf(), | 62 | .await |
| 62 | ) | 63 | { |
| 63 | .await | 64 | Either3::First(p) => { |
| 64 | { | 65 | if let Ok(n) = self.mac.read_frame(p).await { |
| 65 | Either::First(p) => { | 66 | rx_chan.rx_done(n); |
| 66 | if let Ok(n) = self.mac.read_frame(p).await { | 67 | } |
| 67 | rx_chan.rx_done(n); | 68 | } |
| 68 | } | 69 | Either3::Second(p) => { |
| 69 | } | 70 | self.mac.write_frame(p).await.ok(); |
| 70 | Either::Second(p) => { | 71 | tx_chan.tx_done(); |
| 71 | self.mac.write_frame(p).await.ok(); | 72 | } |
| 72 | tx_chan.tx_done(); | 73 | Either3::Third(()) => { |
| 73 | } | 74 | if self.mac.is_link_up().await { |
| 75 | state_chan.set_link_state(LinkState::Up); | ||
| 76 | } else { | ||
| 77 | state_chan.set_link_state(LinkState::Down); | ||
| 74 | } | 78 | } |
| 75 | } | 79 | } |
| 76 | } else { | ||
| 77 | state_chan.set_link_state(LinkState::Down); | ||
| 78 | } | 80 | } |
| 79 | } | 81 | } |
| 80 | } | 82 | } |
