From 46f671ae420f27b527edf03756d1da76d86216c7 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Tue, 15 Aug 2023 22:47:03 +0200 Subject: [PATCH 1/6] rp: fix async spi read sometimes hanging. --- embassy-rp/src/dma.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/embassy-rp/src/dma.rs b/embassy-rp/src/dma.rs index c8f74180..45ca21a7 100644 --- a/embassy-rp/src/dma.rs +++ b/embassy-rp/src/dma.rs @@ -76,7 +76,8 @@ pub unsafe fn write<'a, C: Channel, W: Word>( ) } -static DUMMY: u32 = 0; +// static mut so that this is allocated in RAM. +static mut DUMMY: u32 = 0; pub unsafe fn write_repeated<'a, C: Channel, W: Word>( ch: impl Peripheral

+ 'a, @@ -86,7 +87,7 @@ pub unsafe fn write_repeated<'a, C: Channel, W: Word>( ) -> Transfer<'a, C> { copy_inner( ch, - &DUMMY as *const u32, + &mut DUMMY as *const u32, to as *mut u32, len, W::size(), From c367b84ee53b9d7a31f442f962c939f822aadb0d Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Tue, 15 Aug 2023 17:11:24 +0200 Subject: [PATCH 2/6] net-w5500: add Address type. --- embassy-net-w5500/src/device.rs | 41 +++++++++++++------------------- embassy-net-w5500/src/socket.rs | 42 ++++++++++++++++----------------- embassy-net-w5500/src/spi.rs | 14 ++++++----- 3 files changed, 44 insertions(+), 53 deletions(-) diff --git a/embassy-net-w5500/src/device.rs b/embassy-net-w5500/src/device.rs index 4933625e..1793baaa 100644 --- a/embassy-net-w5500/src/device.rs +++ b/embassy-net-w5500/src/device.rs @@ -1,12 +1,12 @@ use embedded_hal_async::spi::SpiDevice; use crate::socket; -use crate::spi::SpiInterface; +use crate::spi::{Address, SpiInterface}; -pub const MODE: u16 = 0x00; -pub const MAC: u16 = 0x09; -pub const SOCKET_INTR: u16 = 0x18; -pub const PHY_CFG: u16 = 0x2E; +pub const MODE: Address = (RegisterBlock::Common, 0x00); +pub const MAC: Address = (RegisterBlock::Common, 0x09); +pub const SOCKET_INTR: Address = (RegisterBlock::Common, 0x18); +pub const PHY_CFG: Address = (RegisterBlock::Common, 0x2E); #[repr(u8)] pub enum RegisterBlock { @@ -28,30 +28,24 @@ impl W5500 { pub async fn new(spi: SPI, mac_addr: [u8; 6]) -> Result, SPI::Error> { let mut bus = SpiInterface(spi); // Reset device - bus.write_frame(RegisterBlock::Common, MODE, &[0x80]).await?; + bus.write_frame(MODE, &[0x80]).await?; // Enable interrupt pin - bus.write_frame(RegisterBlock::Common, SOCKET_INTR, &[0x01]).await?; + bus.write_frame(SOCKET_INTR, &[0x01]).await?; // Enable receive interrupt - bus.write_frame( - RegisterBlock::Socket0, - socket::SOCKET_INTR_MASK, - &[socket::Interrupt::Receive as u8], - ) - .await?; + bus.write_frame(socket::SOCKET_INTR_MASK, &[socket::Interrupt::Receive as u8]) + .await?; // Set MAC address - bus.write_frame(RegisterBlock::Common, MAC, &mac_addr).await?; + bus.write_frame(MAC, &mac_addr).await?; // Set the raw socket RX/TX buffer sizes to 16KB - bus.write_frame(RegisterBlock::Socket0, socket::TXBUF_SIZE, &[16]) - .await?; - bus.write_frame(RegisterBlock::Socket0, socket::RXBUF_SIZE, &[16]) - .await?; + bus.write_frame(socket::TXBUF_SIZE, &[16]).await?; + bus.write_frame(socket::RXBUF_SIZE, &[16]).await?; // MACRAW mode with MAC filtering. let mode: u8 = (1 << 2) | (1 << 7); - bus.write_frame(RegisterBlock::Socket0, socket::MODE, &[mode]).await?; + bus.write_frame(socket::MODE, &[mode]).await?; socket::command(&mut bus, socket::Command::Open).await?; Ok(Self { bus }) @@ -59,7 +53,7 @@ impl W5500 { /// Read bytes from the RX buffer. Returns the number of bytes read. async fn read_bytes(&mut self, read_ptr: &mut u16, buffer: &mut [u8]) -> Result<(), SPI::Error> { - self.bus.read_frame(RegisterBlock::RxBuf, *read_ptr, buffer).await?; + self.bus.read_frame((RegisterBlock::RxBuf, *read_ptr), buffer).await?; *read_ptr = (*read_ptr).wrapping_add(buffer.len() as u16); Ok(()) @@ -98,7 +92,7 @@ impl W5500 { pub async fn write_frame(&mut self, frame: &[u8]) -> Result { while socket::get_tx_free_size(&mut self.bus).await? < frame.len() as u16 {} let write_ptr = socket::get_tx_write_ptr(&mut self.bus).await?; - self.bus.write_frame(RegisterBlock::TxBuf, write_ptr, frame).await?; + self.bus.write_frame((RegisterBlock::TxBuf, write_ptr), frame).await?; socket::set_tx_write_ptr(&mut self.bus, write_ptr.wrapping_add(frame.len() as u16)).await?; socket::command(&mut self.bus, socket::Command::Send).await?; Ok(frame.len()) @@ -106,10 +100,7 @@ impl W5500 { pub async fn is_link_up(&mut self) -> bool { let mut link = [0]; - self.bus - .read_frame(RegisterBlock::Common, PHY_CFG, &mut link) - .await - .ok(); + self.bus.read_frame(PHY_CFG, &mut link).await.ok(); link[0] & 1 == 1 } } diff --git a/embassy-net-w5500/src/socket.rs b/embassy-net-w5500/src/socket.rs index 3d65583c..1c96cca1 100644 --- a/embassy-net-w5500/src/socket.rs +++ b/embassy-net-w5500/src/socket.rs @@ -1,17 +1,18 @@ use embedded_hal_async::spi::SpiDevice; use crate::device::RegisterBlock; -use crate::spi::SpiInterface; +use crate::spi::{Address, SpiInterface}; -pub const MODE: u16 = 0x00; -pub const COMMAND: u16 = 0x01; -pub const RXBUF_SIZE: u16 = 0x1E; -pub const TXBUF_SIZE: u16 = 0x1F; -pub const TX_FREE_SIZE: u16 = 0x20; -pub const TX_DATA_WRITE_PTR: u16 = 0x24; -pub const RECVD_SIZE: u16 = 0x26; -pub const RX_DATA_READ_PTR: u16 = 0x28; -pub const SOCKET_INTR_MASK: u16 = 0x2C; +pub const MODE: Address = (RegisterBlock::Socket0, 0x00); +pub const COMMAND: Address = (RegisterBlock::Socket0, 0x01); +pub const RXBUF_SIZE: Address = (RegisterBlock::Socket0, 0x1E); +pub const TXBUF_SIZE: Address = (RegisterBlock::Socket0, 0x1F); +pub const TX_FREE_SIZE: Address = (RegisterBlock::Socket0, 0x20); +pub const TX_DATA_WRITE_PTR: Address = (RegisterBlock::Socket0, 0x24); +pub const RECVD_SIZE: Address = (RegisterBlock::Socket0, 0x26); +pub const RX_DATA_READ_PTR: Address = (RegisterBlock::Socket0, 0x28); +pub const SOCKET_INTR_MASK: Address = (RegisterBlock::Socket0, 0x2C); +pub const INTR: Address = (RegisterBlock::Socket0, 0x02); #[repr(u8)] pub enum Command { @@ -20,7 +21,6 @@ pub enum Command { Receive = 0x40, } -pub const INTR: u16 = 0x02; #[repr(u8)] pub enum Interrupt { Receive = 0b00100_u8, @@ -28,45 +28,43 @@ pub enum Interrupt { pub async fn reset_interrupt(bus: &mut SpiInterface, code: Interrupt) -> Result<(), SPI::Error> { let data = [code as u8]; - bus.write_frame(RegisterBlock::Socket0, INTR, &data).await + bus.write_frame(INTR, &data).await } pub async fn get_tx_write_ptr(bus: &mut SpiInterface) -> Result { let mut data = [0u8; 2]; - bus.read_frame(RegisterBlock::Socket0, TX_DATA_WRITE_PTR, &mut data) - .await?; + bus.read_frame(TX_DATA_WRITE_PTR, &mut data).await?; Ok(u16::from_be_bytes(data)) } pub async fn set_tx_write_ptr(bus: &mut SpiInterface, ptr: u16) -> Result<(), SPI::Error> { let data = ptr.to_be_bytes(); - bus.write_frame(RegisterBlock::Socket0, TX_DATA_WRITE_PTR, &data).await + bus.write_frame(TX_DATA_WRITE_PTR, &data).await } pub async fn get_rx_read_ptr(bus: &mut SpiInterface) -> Result { let mut data = [0u8; 2]; - bus.read_frame(RegisterBlock::Socket0, RX_DATA_READ_PTR, &mut data) - .await?; + bus.read_frame(RX_DATA_READ_PTR, &mut data).await?; Ok(u16::from_be_bytes(data)) } pub async fn set_rx_read_ptr(bus: &mut SpiInterface, ptr: u16) -> Result<(), SPI::Error> { let data = ptr.to_be_bytes(); - bus.write_frame(RegisterBlock::Socket0, RX_DATA_READ_PTR, &data).await + bus.write_frame(RX_DATA_READ_PTR, &data).await } pub async fn command(bus: &mut SpiInterface, command: Command) -> Result<(), SPI::Error> { let data = [command as u8]; - bus.write_frame(RegisterBlock::Socket0, COMMAND, &data).await + bus.write_frame(COMMAND, &data).await } pub async fn get_rx_size(bus: &mut SpiInterface) -> Result { loop { // Wait until two sequential reads are equal let mut res0 = [0u8; 2]; - bus.read_frame(RegisterBlock::Socket0, RECVD_SIZE, &mut res0).await?; + bus.read_frame(RECVD_SIZE, &mut res0).await?; let mut res1 = [0u8; 2]; - bus.read_frame(RegisterBlock::Socket0, RECVD_SIZE, &mut res1).await?; + bus.read_frame(RECVD_SIZE, &mut res1).await?; if res0 == res1 { break Ok(u16::from_be_bytes(res0)); } @@ -75,6 +73,6 @@ pub async fn get_rx_size(bus: &mut SpiInterface) -> Result< pub async fn get_tx_free_size(bus: &mut SpiInterface) -> Result { let mut data = [0; 2]; - bus.read_frame(RegisterBlock::Socket0, TX_FREE_SIZE, &mut data).await?; + bus.read_frame(TX_FREE_SIZE, &mut data).await?; Ok(u16::from_be_bytes(data)) } diff --git a/embassy-net-w5500/src/spi.rs b/embassy-net-w5500/src/spi.rs index 07749d6b..316c6521 100644 --- a/embassy-net-w5500/src/spi.rs +++ b/embassy-net-w5500/src/spi.rs @@ -2,14 +2,16 @@ use embedded_hal_async::spi::{Operation, SpiDevice}; use crate::device::RegisterBlock; +pub type Address = (RegisterBlock, u16); + #[derive(Debug)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct SpiInterface(pub SPI); impl SpiInterface { - pub async fn read_frame(&mut self, block: RegisterBlock, address: u16, data: &mut [u8]) -> Result<(), SPI::Error> { - let address_phase = address.to_be_bytes(); - let control_phase = [(block as u8) << 3]; + pub async fn read_frame(&mut self, address: Address, data: &mut [u8]) -> Result<(), SPI::Error> { + let address_phase = address.1.to_be_bytes(); + let control_phase = [(address.0 as u8) << 3]; let operations = &mut [ Operation::Write(&address_phase), Operation::Write(&control_phase), @@ -18,9 +20,9 @@ impl SpiInterface { self.0.transaction(operations).await } - pub async fn write_frame(&mut self, block: RegisterBlock, address: u16, data: &[u8]) -> Result<(), SPI::Error> { - let address_phase = address.to_be_bytes(); - let control_phase = [(block as u8) << 3 | 0b0000_0100]; + pub async fn write_frame(&mut self, address: Address, data: &[u8]) -> Result<(), SPI::Error> { + let address_phase = address.1.to_be_bytes(); + let control_phase = [(address.0 as u8) << 3 | 0b0000_0100]; let data_phase = data; let operations = &mut [ Operation::Write(&address_phase[..]), From a436bd068f6059fa073a9f1f246a77e1ce38dd31 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Tue, 15 Aug 2023 17:20:41 +0200 Subject: [PATCH 3/6] net-w5500: inline socket into device. --- embassy-net-w5500/src/device.rs | 124 +++++++++++++++++++++++++------- embassy-net-w5500/src/lib.rs | 1 - embassy-net-w5500/src/socket.rs | 78 -------------------- 3 files changed, 99 insertions(+), 104 deletions(-) delete mode 100644 embassy-net-w5500/src/socket.rs diff --git a/embassy-net-w5500/src/device.rs b/embassy-net-w5500/src/device.rs index 1793baaa..cf451b33 100644 --- a/embassy-net-w5500/src/device.rs +++ b/embassy-net-w5500/src/device.rs @@ -1,12 +1,34 @@ use embedded_hal_async::spi::SpiDevice; -use crate::socket; use crate::spi::{Address, SpiInterface}; -pub const MODE: Address = (RegisterBlock::Common, 0x00); -pub const MAC: Address = (RegisterBlock::Common, 0x09); -pub const SOCKET_INTR: Address = (RegisterBlock::Common, 0x18); -pub const PHY_CFG: Address = (RegisterBlock::Common, 0x2E); +pub const COMMON_MODE: Address = (RegisterBlock::Common, 0x00); +pub const COMMON_MAC: Address = (RegisterBlock::Common, 0x09); +pub const COMMON_SOCKET_INTR: Address = (RegisterBlock::Common, 0x18); +pub const COMMON_PHY_CFG: Address = (RegisterBlock::Common, 0x2E); + +pub const SOCKET_MODE: Address = (RegisterBlock::Socket0, 0x00); +pub const SOCKET_COMMAND: Address = (RegisterBlock::Socket0, 0x01); +pub const SOCKET_RXBUF_SIZE: Address = (RegisterBlock::Socket0, 0x1E); +pub const SOCKET_TXBUF_SIZE: Address = (RegisterBlock::Socket0, 0x1F); +pub const SOCKET_TX_FREE_SIZE: Address = (RegisterBlock::Socket0, 0x20); +pub const SOCKET_TX_DATA_WRITE_PTR: Address = (RegisterBlock::Socket0, 0x24); +pub const SOCKET_RECVD_SIZE: Address = (RegisterBlock::Socket0, 0x26); +pub const SOCKET_RX_DATA_READ_PTR: Address = (RegisterBlock::Socket0, 0x28); +pub const SOCKET_INTR_MASK: Address = (RegisterBlock::Socket0, 0x2C); +pub const SOCKET_INTR: Address = (RegisterBlock::Socket0, 0x02); + +#[repr(u8)] +pub enum Command { + Open = 0x01, + Send = 0x20, + Receive = 0x40, +} + +#[repr(u8)] +pub enum Interrupt { + Receive = 0b00100_u8, +} #[repr(u8)] pub enum RegisterBlock { @@ -28,27 +50,78 @@ impl W5500 { pub async fn new(spi: SPI, mac_addr: [u8; 6]) -> Result, SPI::Error> { let mut bus = SpiInterface(spi); // Reset device - bus.write_frame(MODE, &[0x80]).await?; + bus.write_frame(COMMON_MODE, &[0x80]).await?; // Enable interrupt pin - bus.write_frame(SOCKET_INTR, &[0x01]).await?; + bus.write_frame(COMMON_SOCKET_INTR, &[0x01]).await?; // Enable receive interrupt - bus.write_frame(socket::SOCKET_INTR_MASK, &[socket::Interrupt::Receive as u8]) - .await?; + bus.write_frame(SOCKET_INTR_MASK, &[Interrupt::Receive as u8]).await?; // Set MAC address - bus.write_frame(MAC, &mac_addr).await?; + bus.write_frame(COMMON_MAC, &mac_addr).await?; // Set the raw socket RX/TX buffer sizes to 16KB - bus.write_frame(socket::TXBUF_SIZE, &[16]).await?; - bus.write_frame(socket::RXBUF_SIZE, &[16]).await?; + bus.write_frame(SOCKET_TXBUF_SIZE, &[16]).await?; + bus.write_frame(SOCKET_RXBUF_SIZE, &[16]).await?; // MACRAW mode with MAC filtering. let mode: u8 = (1 << 2) | (1 << 7); - bus.write_frame(socket::MODE, &[mode]).await?; - socket::command(&mut bus, socket::Command::Open).await?; + bus.write_frame(SOCKET_MODE, &[mode]).await?; + let mut this = Self { bus }; + this.command(Command::Open).await?; - Ok(Self { bus }) + Ok(this) + } + + async fn reset_interrupt(&mut self, code: Interrupt) -> Result<(), SPI::Error> { + let data = [code as u8]; + self.bus.write_frame(SOCKET_INTR, &data).await + } + + async fn get_tx_write_ptr(&mut self) -> Result { + let mut data = [0u8; 2]; + self.bus.read_frame(SOCKET_TX_DATA_WRITE_PTR, &mut data).await?; + Ok(u16::from_be_bytes(data)) + } + + async fn set_tx_write_ptr(&mut self, ptr: u16) -> Result<(), SPI::Error> { + let data = ptr.to_be_bytes(); + self.bus.write_frame(SOCKET_TX_DATA_WRITE_PTR, &data).await + } + + async fn get_rx_read_ptr(&mut self) -> Result { + let mut data = [0u8; 2]; + self.bus.read_frame(SOCKET_RX_DATA_READ_PTR, &mut data).await?; + Ok(u16::from_be_bytes(data)) + } + + async fn set_rx_read_ptr(&mut self, ptr: u16) -> Result<(), SPI::Error> { + let data = ptr.to_be_bytes(); + self.bus.write_frame(SOCKET_RX_DATA_READ_PTR, &data).await + } + + async fn command(&mut self, command: Command) -> Result<(), SPI::Error> { + let data = [command as u8]; + self.bus.write_frame(SOCKET_COMMAND, &data).await + } + + async fn get_rx_size(&mut self) -> Result { + loop { + // Wait until two sequential reads are equal + let mut res0 = [0u8; 2]; + self.bus.read_frame(SOCKET_RECVD_SIZE, &mut res0).await?; + let mut res1 = [0u8; 2]; + self.bus.read_frame(SOCKET_RECVD_SIZE, &mut res1).await?; + if res0 == res1 { + break Ok(u16::from_be_bytes(res0)); + } + } + } + + async fn get_tx_free_size(&mut self) -> Result { + let mut data = [0; 2]; + self.bus.read_frame(SOCKET_TX_FREE_SIZE, &mut data).await?; + Ok(u16::from_be_bytes(data)) } /// Read bytes from the RX buffer. Returns the number of bytes read. @@ -61,14 +134,14 @@ impl W5500 { /// Read an ethernet frame from the device. Returns the number of bytes read. pub async fn read_frame(&mut self, frame: &mut [u8]) -> Result { - let rx_size = socket::get_rx_size(&mut self.bus).await? as usize; + let rx_size = self.get_rx_size().await? as usize; if rx_size == 0 { return Ok(0); } - socket::reset_interrupt(&mut self.bus, socket::Interrupt::Receive).await?; + self.reset_interrupt(Interrupt::Receive).await?; - let mut read_ptr = socket::get_rx_read_ptr(&mut self.bus).await?; + let mut read_ptr = self.get_rx_read_ptr().await?; // First two bytes gives the size of the received ethernet frame let expected_frame_size: usize = { @@ -82,25 +155,26 @@ impl W5500 { .await?; // Register RX as completed - socket::set_rx_read_ptr(&mut self.bus, read_ptr).await?; - socket::command(&mut self.bus, socket::Command::Receive).await?; + self.set_rx_read_ptr(read_ptr).await?; + self.command(Command::Receive).await?; Ok(expected_frame_size) } /// Write an ethernet frame to the device. Returns number of bytes written pub async fn write_frame(&mut self, frame: &[u8]) -> Result { - while socket::get_tx_free_size(&mut self.bus).await? < frame.len() as u16 {} - let write_ptr = socket::get_tx_write_ptr(&mut self.bus).await?; + while self.get_tx_free_size().await? < frame.len() as u16 {} + let write_ptr = self.get_tx_write_ptr().await?; self.bus.write_frame((RegisterBlock::TxBuf, write_ptr), frame).await?; - socket::set_tx_write_ptr(&mut self.bus, write_ptr.wrapping_add(frame.len() as u16)).await?; - socket::command(&mut self.bus, socket::Command::Send).await?; + self.set_tx_write_ptr(write_ptr.wrapping_add(frame.len() as u16)) + .await?; + self.command(Command::Send).await?; Ok(frame.len()) } pub async fn is_link_up(&mut self) -> bool { let mut link = [0]; - self.bus.read_frame(PHY_CFG, &mut link).await.ok(); + self.bus.read_frame(COMMON_PHY_CFG, &mut link).await.ok(); link[0] & 1 == 1 } } diff --git a/embassy-net-w5500/src/lib.rs b/embassy-net-w5500/src/lib.rs index 52494b44..3c54777d 100644 --- a/embassy-net-w5500/src/lib.rs +++ b/embassy-net-w5500/src/lib.rs @@ -2,7 +2,6 @@ #![no_std] mod device; -mod socket; mod spi; use embassy_futures::select::{select, Either}; diff --git a/embassy-net-w5500/src/socket.rs b/embassy-net-w5500/src/socket.rs deleted file mode 100644 index 1c96cca1..00000000 --- a/embassy-net-w5500/src/socket.rs +++ /dev/null @@ -1,78 +0,0 @@ -use embedded_hal_async::spi::SpiDevice; - -use crate::device::RegisterBlock; -use crate::spi::{Address, SpiInterface}; - -pub const MODE: Address = (RegisterBlock::Socket0, 0x00); -pub const COMMAND: Address = (RegisterBlock::Socket0, 0x01); -pub const RXBUF_SIZE: Address = (RegisterBlock::Socket0, 0x1E); -pub const TXBUF_SIZE: Address = (RegisterBlock::Socket0, 0x1F); -pub const TX_FREE_SIZE: Address = (RegisterBlock::Socket0, 0x20); -pub const TX_DATA_WRITE_PTR: Address = (RegisterBlock::Socket0, 0x24); -pub const RECVD_SIZE: Address = (RegisterBlock::Socket0, 0x26); -pub const RX_DATA_READ_PTR: Address = (RegisterBlock::Socket0, 0x28); -pub const SOCKET_INTR_MASK: Address = (RegisterBlock::Socket0, 0x2C); -pub const INTR: Address = (RegisterBlock::Socket0, 0x02); - -#[repr(u8)] -pub enum Command { - Open = 0x01, - Send = 0x20, - Receive = 0x40, -} - -#[repr(u8)] -pub enum Interrupt { - Receive = 0b00100_u8, -} - -pub async fn reset_interrupt(bus: &mut SpiInterface, code: Interrupt) -> Result<(), SPI::Error> { - let data = [code as u8]; - bus.write_frame(INTR, &data).await -} - -pub async fn get_tx_write_ptr(bus: &mut SpiInterface) -> Result { - let mut data = [0u8; 2]; - bus.read_frame(TX_DATA_WRITE_PTR, &mut data).await?; - Ok(u16::from_be_bytes(data)) -} - -pub async fn set_tx_write_ptr(bus: &mut SpiInterface, ptr: u16) -> Result<(), SPI::Error> { - let data = ptr.to_be_bytes(); - bus.write_frame(TX_DATA_WRITE_PTR, &data).await -} - -pub async fn get_rx_read_ptr(bus: &mut SpiInterface) -> Result { - let mut data = [0u8; 2]; - bus.read_frame(RX_DATA_READ_PTR, &mut data).await?; - Ok(u16::from_be_bytes(data)) -} - -pub async fn set_rx_read_ptr(bus: &mut SpiInterface, ptr: u16) -> Result<(), SPI::Error> { - let data = ptr.to_be_bytes(); - bus.write_frame(RX_DATA_READ_PTR, &data).await -} - -pub async fn command(bus: &mut SpiInterface, command: Command) -> Result<(), SPI::Error> { - let data = [command as u8]; - bus.write_frame(COMMAND, &data).await -} - -pub async fn get_rx_size(bus: &mut SpiInterface) -> Result { - loop { - // Wait until two sequential reads are equal - let mut res0 = [0u8; 2]; - bus.read_frame(RECVD_SIZE, &mut res0).await?; - let mut res1 = [0u8; 2]; - bus.read_frame(RECVD_SIZE, &mut res1).await?; - if res0 == res1 { - break Ok(u16::from_be_bytes(res0)); - } - } -} - -pub async fn get_tx_free_size(bus: &mut SpiInterface) -> Result { - let mut data = [0; 2]; - bus.read_frame(TX_FREE_SIZE, &mut data).await?; - Ok(u16::from_be_bytes(data)) -} From 76276c326aaa4fd64a73253a480e2ea22f5ff740 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Tue, 15 Aug 2023 17:50:56 +0200 Subject: [PATCH 4/6] net-w5500: extract chip-specific stuff to a trait. --- embassy-net-w5500/src/chip/mod.rs | 41 ++++++++ embassy-net-w5500/src/chip/w5500.rs | 67 +++++++++++++ embassy-net-w5500/src/device.rs | 94 +++++++++---------- embassy-net-w5500/src/lib.rs | 19 ++-- embassy-net-w5500/src/spi.rs | 34 ------- .../rp/src/bin/ethernet_w5500_multisocket.rs | 2 + .../rp/src/bin/ethernet_w5500_tcp_client.rs | 2 + .../rp/src/bin/ethernet_w5500_tcp_server.rs | 2 + examples/rp/src/bin/ethernet_w5500_udp.rs | 2 + 9 files changed, 169 insertions(+), 94 deletions(-) create mode 100644 embassy-net-w5500/src/chip/mod.rs create mode 100644 embassy-net-w5500/src/chip/w5500.rs delete mode 100644 embassy-net-w5500/src/spi.rs diff --git a/embassy-net-w5500/src/chip/mod.rs b/embassy-net-w5500/src/chip/mod.rs new file mode 100644 index 00000000..b7d550e2 --- /dev/null +++ b/embassy-net-w5500/src/chip/mod.rs @@ -0,0 +1,41 @@ +mod w5500; +pub use w5500::W5500; + +pub(crate) mod sealed { + use embedded_hal_async::spi::SpiDevice; + + pub trait Chip { + type Address; + + const COMMON_MODE: Self::Address; + const COMMON_MAC: Self::Address; + const COMMON_SOCKET_INTR: Self::Address; + const COMMON_PHY_CFG: Self::Address; + const SOCKET_MODE: Self::Address; + const SOCKET_COMMAND: Self::Address; + const SOCKET_RXBUF_SIZE: Self::Address; + const SOCKET_TXBUF_SIZE: Self::Address; + const SOCKET_TX_FREE_SIZE: Self::Address; + const SOCKET_TX_DATA_WRITE_PTR: Self::Address; + const SOCKET_RECVD_SIZE: Self::Address; + const SOCKET_RX_DATA_READ_PTR: Self::Address; + const SOCKET_INTR_MASK: Self::Address; + const SOCKET_INTR: Self::Address; + + fn rx_addr(addr: u16) -> Self::Address; + fn tx_addr(addr: u16) -> Self::Address; + + async fn bus_read( + spi: &mut SPI, + address: Self::Address, + data: &mut [u8], + ) -> Result<(), SPI::Error>; + async fn bus_write( + spi: &mut SPI, + address: Self::Address, + data: &[u8], + ) -> Result<(), SPI::Error>; + } +} + +pub trait Chip: sealed::Chip {} diff --git a/embassy-net-w5500/src/chip/w5500.rs b/embassy-net-w5500/src/chip/w5500.rs new file mode 100644 index 00000000..f514e12a --- /dev/null +++ b/embassy-net-w5500/src/chip/w5500.rs @@ -0,0 +1,67 @@ +use embedded_hal_async::spi::{Operation, SpiDevice}; + +#[repr(u8)] +pub enum RegisterBlock { + Common = 0x00, + Socket0 = 0x01, + TxBuf = 0x02, + RxBuf = 0x03, +} + +pub enum W5500 {} + +impl super::Chip for W5500 {} +impl super::sealed::Chip for W5500 { + type Address = (RegisterBlock, u16); + + const COMMON_MODE: Self::Address = (RegisterBlock::Common, 0x00); + const COMMON_MAC: Self::Address = (RegisterBlock::Common, 0x09); + const COMMON_SOCKET_INTR: Self::Address = (RegisterBlock::Common, 0x18); + const COMMON_PHY_CFG: Self::Address = (RegisterBlock::Common, 0x2E); + + const SOCKET_MODE: Self::Address = (RegisterBlock::Socket0, 0x00); + const SOCKET_COMMAND: Self::Address = (RegisterBlock::Socket0, 0x01); + const SOCKET_RXBUF_SIZE: Self::Address = (RegisterBlock::Socket0, 0x1E); + const SOCKET_TXBUF_SIZE: Self::Address = (RegisterBlock::Socket0, 0x1F); + const SOCKET_TX_FREE_SIZE: Self::Address = (RegisterBlock::Socket0, 0x20); + const SOCKET_TX_DATA_WRITE_PTR: Self::Address = (RegisterBlock::Socket0, 0x24); + const SOCKET_RECVD_SIZE: Self::Address = (RegisterBlock::Socket0, 0x26); + const SOCKET_RX_DATA_READ_PTR: Self::Address = (RegisterBlock::Socket0, 0x28); + const SOCKET_INTR_MASK: Self::Address = (RegisterBlock::Socket0, 0x2C); + const SOCKET_INTR: Self::Address = (RegisterBlock::Socket0, 0x02); + + fn rx_addr(addr: u16) -> Self::Address { + (RegisterBlock::RxBuf, addr) + } + + fn tx_addr(addr: u16) -> Self::Address { + (RegisterBlock::TxBuf, addr) + } + + async fn bus_read( + spi: &mut SPI, + address: Self::Address, + data: &mut [u8], + ) -> Result<(), SPI::Error> { + let address_phase = address.1.to_be_bytes(); + let control_phase = [(address.0 as u8) << 3]; + let operations = &mut [ + Operation::Write(&address_phase), + Operation::Write(&control_phase), + Operation::TransferInPlace(data), + ]; + spi.transaction(operations).await + } + + async fn bus_write(spi: &mut SPI, address: Self::Address, data: &[u8]) -> Result<(), SPI::Error> { + let address_phase = address.1.to_be_bytes(); + let control_phase = [(address.0 as u8) << 3 | 0b0000_0100]; + let data_phase = data; + let operations = &mut [ + Operation::Write(&address_phase[..]), + Operation::Write(&control_phase), + Operation::Write(&data_phase), + ]; + spi.transaction(operations).await + } +} diff --git a/embassy-net-w5500/src/device.rs b/embassy-net-w5500/src/device.rs index cf451b33..a6ee8f8f 100644 --- a/embassy-net-w5500/src/device.rs +++ b/embassy-net-w5500/src/device.rs @@ -1,117 +1,107 @@ +use core::marker::PhantomData; + use embedded_hal_async::spi::SpiDevice; -use crate::spi::{Address, SpiInterface}; - -pub const COMMON_MODE: Address = (RegisterBlock::Common, 0x00); -pub const COMMON_MAC: Address = (RegisterBlock::Common, 0x09); -pub const COMMON_SOCKET_INTR: Address = (RegisterBlock::Common, 0x18); -pub const COMMON_PHY_CFG: Address = (RegisterBlock::Common, 0x2E); - -pub const SOCKET_MODE: Address = (RegisterBlock::Socket0, 0x00); -pub const SOCKET_COMMAND: Address = (RegisterBlock::Socket0, 0x01); -pub const SOCKET_RXBUF_SIZE: Address = (RegisterBlock::Socket0, 0x1E); -pub const SOCKET_TXBUF_SIZE: Address = (RegisterBlock::Socket0, 0x1F); -pub const SOCKET_TX_FREE_SIZE: Address = (RegisterBlock::Socket0, 0x20); -pub const SOCKET_TX_DATA_WRITE_PTR: Address = (RegisterBlock::Socket0, 0x24); -pub const SOCKET_RECVD_SIZE: Address = (RegisterBlock::Socket0, 0x26); -pub const SOCKET_RX_DATA_READ_PTR: Address = (RegisterBlock::Socket0, 0x28); -pub const SOCKET_INTR_MASK: Address = (RegisterBlock::Socket0, 0x2C); -pub const SOCKET_INTR: Address = (RegisterBlock::Socket0, 0x02); +use crate::chip::Chip; #[repr(u8)] -pub enum Command { +enum Command { Open = 0x01, Send = 0x20, Receive = 0x40, } #[repr(u8)] -pub enum Interrupt { +enum Interrupt { Receive = 0b00100_u8, } -#[repr(u8)] -pub enum RegisterBlock { - Common = 0x00, - Socket0 = 0x01, - TxBuf = 0x02, - RxBuf = 0x03, -} - /// W5500 in MACRAW mode #[derive(Debug)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] -pub struct W5500 { - bus: SpiInterface, +pub(crate) struct WiznetDevice { + spi: SPI, + _phantom: PhantomData, } -impl W5500 { +impl WiznetDevice { /// Create and initialize the W5500 driver - pub async fn new(spi: SPI, mac_addr: [u8; 6]) -> Result, SPI::Error> { - let mut bus = SpiInterface(spi); + pub async fn new(spi: SPI, mac_addr: [u8; 6]) -> Result { + let mut this = Self { + spi, + _phantom: PhantomData, + }; + // Reset device - bus.write_frame(COMMON_MODE, &[0x80]).await?; + this.bus_write(C::COMMON_MODE, &[0x80]).await?; // Enable interrupt pin - bus.write_frame(COMMON_SOCKET_INTR, &[0x01]).await?; + this.bus_write(C::COMMON_SOCKET_INTR, &[0x01]).await?; // Enable receive interrupt - bus.write_frame(SOCKET_INTR_MASK, &[Interrupt::Receive as u8]).await?; + this.bus_write(C::SOCKET_INTR_MASK, &[Interrupt::Receive as u8]).await?; // Set MAC address - bus.write_frame(COMMON_MAC, &mac_addr).await?; + this.bus_write(C::COMMON_MAC, &mac_addr).await?; // Set the raw socket RX/TX buffer sizes to 16KB - bus.write_frame(SOCKET_TXBUF_SIZE, &[16]).await?; - bus.write_frame(SOCKET_RXBUF_SIZE, &[16]).await?; + this.bus_write(C::SOCKET_TXBUF_SIZE, &[16]).await?; + this.bus_write(C::SOCKET_RXBUF_SIZE, &[16]).await?; // MACRAW mode with MAC filtering. let mode: u8 = (1 << 2) | (1 << 7); - bus.write_frame(SOCKET_MODE, &[mode]).await?; - let mut this = Self { bus }; + this.bus_write(C::SOCKET_MODE, &[mode]).await?; this.command(Command::Open).await?; Ok(this) } + async fn bus_read(&mut self, address: C::Address, data: &mut [u8]) -> Result<(), SPI::Error> { + C::bus_read(&mut self.spi, address, data).await + } + + async fn bus_write(&mut self, address: C::Address, data: &[u8]) -> Result<(), SPI::Error> { + C::bus_write(&mut self.spi, address, data).await + } + async fn reset_interrupt(&mut self, code: Interrupt) -> Result<(), SPI::Error> { let data = [code as u8]; - self.bus.write_frame(SOCKET_INTR, &data).await + self.bus_write(C::SOCKET_INTR, &data).await } async fn get_tx_write_ptr(&mut self) -> Result { let mut data = [0u8; 2]; - self.bus.read_frame(SOCKET_TX_DATA_WRITE_PTR, &mut data).await?; + self.bus_read(C::SOCKET_TX_DATA_WRITE_PTR, &mut data).await?; Ok(u16::from_be_bytes(data)) } async fn set_tx_write_ptr(&mut self, ptr: u16) -> Result<(), SPI::Error> { let data = ptr.to_be_bytes(); - self.bus.write_frame(SOCKET_TX_DATA_WRITE_PTR, &data).await + self.bus_write(C::SOCKET_TX_DATA_WRITE_PTR, &data).await } async fn get_rx_read_ptr(&mut self) -> Result { let mut data = [0u8; 2]; - self.bus.read_frame(SOCKET_RX_DATA_READ_PTR, &mut data).await?; + self.bus_read(C::SOCKET_RX_DATA_READ_PTR, &mut data).await?; Ok(u16::from_be_bytes(data)) } async fn set_rx_read_ptr(&mut self, ptr: u16) -> Result<(), SPI::Error> { let data = ptr.to_be_bytes(); - self.bus.write_frame(SOCKET_RX_DATA_READ_PTR, &data).await + self.bus_write(C::SOCKET_RX_DATA_READ_PTR, &data).await } async fn command(&mut self, command: Command) -> Result<(), SPI::Error> { let data = [command as u8]; - self.bus.write_frame(SOCKET_COMMAND, &data).await + self.bus_write(C::SOCKET_COMMAND, &data).await } async fn get_rx_size(&mut self) -> Result { loop { // Wait until two sequential reads are equal let mut res0 = [0u8; 2]; - self.bus.read_frame(SOCKET_RECVD_SIZE, &mut res0).await?; + self.bus_read(C::SOCKET_RECVD_SIZE, &mut res0).await?; let mut res1 = [0u8; 2]; - self.bus.read_frame(SOCKET_RECVD_SIZE, &mut res1).await?; + self.bus_read(C::SOCKET_RECVD_SIZE, &mut res1).await?; if res0 == res1 { break Ok(u16::from_be_bytes(res0)); } @@ -120,13 +110,13 @@ impl W5500 { async fn get_tx_free_size(&mut self) -> Result { let mut data = [0; 2]; - self.bus.read_frame(SOCKET_TX_FREE_SIZE, &mut data).await?; + self.bus_read(C::SOCKET_TX_FREE_SIZE, &mut data).await?; Ok(u16::from_be_bytes(data)) } /// Read bytes from the RX buffer. Returns the number of bytes read. async fn read_bytes(&mut self, read_ptr: &mut u16, buffer: &mut [u8]) -> Result<(), SPI::Error> { - self.bus.read_frame((RegisterBlock::RxBuf, *read_ptr), buffer).await?; + self.bus_read(C::rx_addr(*read_ptr), buffer).await?; *read_ptr = (*read_ptr).wrapping_add(buffer.len() as u16); Ok(()) @@ -165,7 +155,7 @@ impl W5500 { pub async fn write_frame(&mut self, frame: &[u8]) -> Result { while self.get_tx_free_size().await? < frame.len() as u16 {} let write_ptr = self.get_tx_write_ptr().await?; - self.bus.write_frame((RegisterBlock::TxBuf, write_ptr), frame).await?; + self.bus_write(C::tx_addr(write_ptr), frame).await?; self.set_tx_write_ptr(write_ptr.wrapping_add(frame.len() as u16)) .await?; self.command(Command::Send).await?; @@ -174,7 +164,7 @@ impl W5500 { pub async fn is_link_up(&mut self) -> bool { let mut link = [0]; - self.bus.read_frame(COMMON_PHY_CFG, &mut link).await.ok(); + self.bus_read(C::COMMON_PHY_CFG, &mut link).await.ok(); link[0] & 1 == 1 } } diff --git a/embassy-net-w5500/src/lib.rs b/embassy-net-w5500/src/lib.rs index 3c54777d..9b53e961 100644 --- a/embassy-net-w5500/src/lib.rs +++ b/embassy-net-w5500/src/lib.rs @@ -1,8 +1,9 @@ //! [`embassy-net`](https://crates.io/crates/embassy-net) driver for the WIZnet W5500 ethernet chip. #![no_std] +#![feature(async_fn_in_trait)] +pub mod chip; mod device; -mod spi; use embassy_futures::select::{select, Either}; use embassy_net_driver_channel as ch; @@ -12,7 +13,9 @@ use embedded_hal::digital::OutputPin; use embedded_hal_async::digital::Wait; use embedded_hal_async::spi::SpiDevice; -use crate::device::W5500; +use crate::chip::Chip; +use crate::device::WiznetDevice; + const MTU: usize = 1514; /// Type alias for the embassy-net driver for W5500 @@ -35,15 +38,15 @@ impl State { /// Background runner for the W5500. /// /// You must call `.run()` in a background task for the W5500 to operate. -pub struct Runner<'d, SPI: SpiDevice, INT: Wait, RST: OutputPin> { - mac: W5500, +pub struct Runner<'d, C: Chip, SPI: SpiDevice, INT: Wait, RST: OutputPin> { + mac: WiznetDevice, ch: ch::Runner<'d, MTU>, int: INT, _reset: RST, } /// You must call this in a background task for the W5500 to operate. -impl<'d, SPI: SpiDevice, INT: Wait, RST: OutputPin> Runner<'d, 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) -> ! { let (state_chan, mut rx_chan, mut tx_chan) = self.ch.split(); loop { @@ -78,13 +81,13 @@ impl<'d, SPI: SpiDevice, INT: Wait, RST: OutputPin> Runner<'d, SPI, INT, RST> { } /// Obtain a driver for using the W5500 with [`embassy-net`](https://crates.io/crates/embassy-net). -pub async fn new<'a, const N_RX: usize, const N_TX: usize, SPI: SpiDevice, INT: Wait, RST: OutputPin>( +pub async fn new<'a, const N_RX: usize, const N_TX: usize, C: Chip, SPI: SpiDevice, INT: Wait, RST: OutputPin>( mac_addr: [u8; 6], state: &'a mut State, spi_dev: SPI, int: INT, mut reset: RST, -) -> (Device<'a>, Runner<'a, SPI, INT, RST>) { +) -> (Device<'a>, Runner<'a, C, SPI, INT, RST>) { // Reset the W5500. reset.set_low().ok(); // Ensure the reset is registered. @@ -93,7 +96,7 @@ pub async fn new<'a, const N_RX: usize, const N_TX: usize, SPI: SpiDevice, INT: // Wait for the W5500 to achieve PLL lock. Timer::after(Duration::from_millis(2)).await; - let mac = W5500::new(spi_dev, mac_addr).await.unwrap(); + let mac = WiznetDevice::new(spi_dev, mac_addr).await.unwrap(); let (runner, device) = ch::new(&mut state.ch_state, ch::driver::HardwareAddress::Ethernet(mac_addr)); ( diff --git a/embassy-net-w5500/src/spi.rs b/embassy-net-w5500/src/spi.rs deleted file mode 100644 index 316c6521..00000000 --- a/embassy-net-w5500/src/spi.rs +++ /dev/null @@ -1,34 +0,0 @@ -use embedded_hal_async::spi::{Operation, SpiDevice}; - -use crate::device::RegisterBlock; - -pub type Address = (RegisterBlock, u16); - -#[derive(Debug)] -#[cfg_attr(feature = "defmt", derive(defmt::Format))] -pub struct SpiInterface(pub SPI); - -impl SpiInterface { - pub async fn read_frame(&mut self, address: Address, data: &mut [u8]) -> Result<(), SPI::Error> { - let address_phase = address.1.to_be_bytes(); - let control_phase = [(address.0 as u8) << 3]; - let operations = &mut [ - Operation::Write(&address_phase), - Operation::Write(&control_phase), - Operation::TransferInPlace(data), - ]; - self.0.transaction(operations).await - } - - pub async fn write_frame(&mut self, address: Address, data: &[u8]) -> Result<(), SPI::Error> { - let address_phase = address.1.to_be_bytes(); - let control_phase = [(address.0 as u8) << 3 | 0b0000_0100]; - let data_phase = data; - let operations = &mut [ - Operation::Write(&address_phase[..]), - Operation::Write(&control_phase), - Operation::Write(&data_phase), - ]; - self.0.transaction(operations).await - } -} diff --git a/examples/rp/src/bin/ethernet_w5500_multisocket.rs b/examples/rp/src/bin/ethernet_w5500_multisocket.rs index 9f800d0d..3677f3cd 100644 --- a/examples/rp/src/bin/ethernet_w5500_multisocket.rs +++ b/examples/rp/src/bin/ethernet_w5500_multisocket.rs @@ -10,6 +10,7 @@ use defmt::*; use embassy_executor::Spawner; use embassy_futures::yield_now; use embassy_net::{Stack, StackResources}; +use embassy_net_w5500::chip::W5500; use embassy_net_w5500::*; use embassy_rp::clocks::RoscRng; use embassy_rp::gpio::{Input, Level, Output, Pull}; @@ -26,6 +27,7 @@ use {defmt_rtt as _, panic_probe as _}; async fn ethernet_task( runner: Runner< 'static, + W5500, ExclusiveDevice, Output<'static, PIN_17>, Delay>, Input<'static, PIN_21>, Output<'static, PIN_20>, diff --git a/examples/rp/src/bin/ethernet_w5500_tcp_client.rs b/examples/rp/src/bin/ethernet_w5500_tcp_client.rs index fee84b61..b78a0977 100644 --- a/examples/rp/src/bin/ethernet_w5500_tcp_client.rs +++ b/examples/rp/src/bin/ethernet_w5500_tcp_client.rs @@ -12,6 +12,7 @@ use defmt::*; use embassy_executor::Spawner; use embassy_futures::yield_now; use embassy_net::{Stack, StackResources}; +use embassy_net_w5500::chip::W5500; use embassy_net_w5500::*; use embassy_rp::clocks::RoscRng; use embassy_rp::gpio::{Input, Level, Output, Pull}; @@ -28,6 +29,7 @@ use {defmt_rtt as _, panic_probe as _}; async fn ethernet_task( runner: Runner< 'static, + W5500, ExclusiveDevice, Output<'static, PIN_17>, Delay>, Input<'static, PIN_21>, Output<'static, PIN_20>, diff --git a/examples/rp/src/bin/ethernet_w5500_tcp_server.rs b/examples/rp/src/bin/ethernet_w5500_tcp_server.rs index 02457426..34f054d9 100644 --- a/examples/rp/src/bin/ethernet_w5500_tcp_server.rs +++ b/examples/rp/src/bin/ethernet_w5500_tcp_server.rs @@ -11,6 +11,7 @@ use defmt::*; use embassy_executor::Spawner; use embassy_futures::yield_now; use embassy_net::{Stack, StackResources}; +use embassy_net_w5500::chip::W5500; use embassy_net_w5500::*; use embassy_rp::clocks::RoscRng; use embassy_rp::gpio::{Input, Level, Output, Pull}; @@ -26,6 +27,7 @@ use {defmt_rtt as _, panic_probe as _}; async fn ethernet_task( runner: Runner< 'static, + W5500, ExclusiveDevice, Output<'static, PIN_17>, Delay>, Input<'static, PIN_21>, Output<'static, PIN_20>, diff --git a/examples/rp/src/bin/ethernet_w5500_udp.rs b/examples/rp/src/bin/ethernet_w5500_udp.rs index 038432b1..8f38e453 100644 --- a/examples/rp/src/bin/ethernet_w5500_udp.rs +++ b/examples/rp/src/bin/ethernet_w5500_udp.rs @@ -11,6 +11,7 @@ use embassy_executor::Spawner; use embassy_futures::yield_now; use embassy_net::udp::{PacketMetadata, UdpSocket}; use embassy_net::{Stack, StackResources}; +use embassy_net_w5500::chip::W5500; use embassy_net_w5500::*; use embassy_rp::clocks::RoscRng; use embassy_rp::gpio::{Input, Level, Output, Pull}; @@ -25,6 +26,7 @@ use {defmt_rtt as _, panic_probe as _}; async fn ethernet_task( runner: Runner< 'static, + W5500, ExclusiveDevice, Output<'static, PIN_17>, Delay>, Input<'static, PIN_21>, Output<'static, PIN_20>, From 1d4b941d52a7e257b0305935034e999facb537bd Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Tue, 15 Aug 2023 18:19:03 +0200 Subject: [PATCH 5/6] net-w5500: add w5100s support. --- embassy-net-w5500/Cargo.toml | 3 +- embassy-net-w5500/src/chip/mod.rs | 7 ++++ embassy-net-w5500/src/chip/w5100s.rs | 61 ++++++++++++++++++++++++++++ embassy-net-w5500/src/chip/w5500.rs | 5 +++ embassy-net-w5500/src/device.rs | 41 +++++++++++++++---- embassy-net-w5500/src/lib.rs | 24 +++++++---- 6 files changed, 123 insertions(+), 18 deletions(-) create mode 100644 embassy-net-w5500/src/chip/w5100s.rs diff --git a/embassy-net-w5500/Cargo.toml b/embassy-net-w5500/Cargo.toml index 8972b814..c4559868 100644 --- a/embassy-net-w5500/Cargo.toml +++ b/embassy-net-w5500/Cargo.toml @@ -18,4 +18,5 @@ defmt = { version = "0.3", optional = true } [package.metadata.embassy_docs] src_base = "https://github.com/embassy-rs/embassy/blob/embassy-net-w5500-v$VERSION/embassy-net-w5500/src/" src_base_git = "https://github.com/embassy-rs/embassy/blob/$COMMIT/embassy-net-w5500/src/" -target = "thumbv7em-none-eabi" \ No newline at end of file +target = "thumbv7em-none-eabi" +features = ["defmt"] \ No newline at end of file diff --git a/embassy-net-w5500/src/chip/mod.rs b/embassy-net-w5500/src/chip/mod.rs index b7d550e2..562db515 100644 --- a/embassy-net-w5500/src/chip/mod.rs +++ b/embassy-net-w5500/src/chip/mod.rs @@ -1,5 +1,7 @@ mod w5500; pub use w5500::W5500; +mod w5100s; +pub use w5100s::W5100S; pub(crate) mod sealed { use embedded_hal_async::spi::SpiDevice; @@ -22,6 +24,11 @@ pub(crate) mod sealed { const SOCKET_INTR_MASK: Self::Address; const SOCKET_INTR: Self::Address; + const SOCKET_MODE_VALUE: u8; + + const BUF_SIZE: u16; + const AUTO_WRAP: bool; + fn rx_addr(addr: u16) -> Self::Address; fn tx_addr(addr: u16) -> Self::Address; diff --git a/embassy-net-w5500/src/chip/w5100s.rs b/embassy-net-w5500/src/chip/w5100s.rs new file mode 100644 index 00000000..07a84037 --- /dev/null +++ b/embassy-net-w5500/src/chip/w5100s.rs @@ -0,0 +1,61 @@ +use embedded_hal_async::spi::{Operation, SpiDevice}; + +const SOCKET_BASE: u16 = 0x400; +const TX_BASE: u16 = 0x4000; +const RX_BASE: u16 = 0x6000; + +pub enum W5100S {} + +impl super::Chip for W5100S {} +impl super::sealed::Chip for W5100S { + type Address = u16; + + const COMMON_MODE: Self::Address = 0x00; + const COMMON_MAC: Self::Address = 0x09; + const COMMON_SOCKET_INTR: Self::Address = 0x16; + const COMMON_PHY_CFG: Self::Address = 0x3c; + + const SOCKET_MODE: Self::Address = SOCKET_BASE + 0x00; + const SOCKET_COMMAND: Self::Address = SOCKET_BASE + 0x01; + const SOCKET_RXBUF_SIZE: Self::Address = SOCKET_BASE + 0x1E; + const SOCKET_TXBUF_SIZE: Self::Address = SOCKET_BASE + 0x1F; + const SOCKET_TX_FREE_SIZE: Self::Address = SOCKET_BASE + 0x20; + const SOCKET_TX_DATA_WRITE_PTR: Self::Address = SOCKET_BASE + 0x24; + const SOCKET_RECVD_SIZE: Self::Address = SOCKET_BASE + 0x26; + const SOCKET_RX_DATA_READ_PTR: Self::Address = SOCKET_BASE + 0x28; + const SOCKET_INTR_MASK: Self::Address = SOCKET_BASE + 0x2C; + const SOCKET_INTR: Self::Address = SOCKET_BASE + 0x02; + + const SOCKET_MODE_VALUE: u8 = (1 << 2) | (1 << 6); + + const BUF_SIZE: u16 = 0x2000; + const AUTO_WRAP: bool = false; + + fn rx_addr(addr: u16) -> Self::Address { + RX_BASE + addr + } + + fn tx_addr(addr: u16) -> Self::Address { + TX_BASE + addr + } + + async fn bus_read( + spi: &mut SPI, + address: Self::Address, + data: &mut [u8], + ) -> Result<(), SPI::Error> { + spi.transaction(&mut [ + Operation::Write(&[0x0F, (address >> 8) as u8, address as u8]), + Operation::Read(data), + ]) + .await + } + + async fn bus_write(spi: &mut SPI, address: Self::Address, data: &[u8]) -> Result<(), SPI::Error> { + spi.transaction(&mut [ + Operation::Write(&[0xF0, (address >> 8) as u8, address as u8]), + Operation::Write(data), + ]) + .await + } +} diff --git a/embassy-net-w5500/src/chip/w5500.rs b/embassy-net-w5500/src/chip/w5500.rs index f514e12a..61e51294 100644 --- a/embassy-net-w5500/src/chip/w5500.rs +++ b/embassy-net-w5500/src/chip/w5500.rs @@ -30,6 +30,11 @@ impl super::sealed::Chip for W5500 { const SOCKET_INTR_MASK: Self::Address = (RegisterBlock::Socket0, 0x2C); const SOCKET_INTR: Self::Address = (RegisterBlock::Socket0, 0x02); + const SOCKET_MODE_VALUE: u8 = (1 << 2) | (1 << 7); + + const BUF_SIZE: u16 = 0x4000; + const AUTO_WRAP: bool = true; + fn rx_addr(addr: u16) -> Self::Address { (RegisterBlock::RxBuf, addr) } diff --git a/embassy-net-w5500/src/device.rs b/embassy-net-w5500/src/device.rs index a6ee8f8f..f367bc3e 100644 --- a/embassy-net-w5500/src/device.rs +++ b/embassy-net-w5500/src/device.rs @@ -43,13 +43,13 @@ impl WiznetDevice { // Set MAC address this.bus_write(C::COMMON_MAC, &mac_addr).await?; - // Set the raw socket RX/TX buffer sizes to 16KB - this.bus_write(C::SOCKET_TXBUF_SIZE, &[16]).await?; - this.bus_write(C::SOCKET_RXBUF_SIZE, &[16]).await?; + // Set the raw socket RX/TX buffer sizes. + let buf_kbs = (C::BUF_SIZE / 1024) as u8; + this.bus_write(C::SOCKET_TXBUF_SIZE, &[buf_kbs]).await?; + this.bus_write(C::SOCKET_RXBUF_SIZE, &[buf_kbs]).await?; // MACRAW mode with MAC filtering. - let mode: u8 = (1 << 2) | (1 << 7); - this.bus_write(C::SOCKET_MODE, &[mode]).await?; + this.bus_write(C::SOCKET_MODE, &[C::SOCKET_MODE_VALUE]).await?; this.command(Command::Open).await?; Ok(this) @@ -114,9 +114,21 @@ impl WiznetDevice { Ok(u16::from_be_bytes(data)) } - /// Read bytes from the RX buffer. Returns the number of bytes read. + /// Read bytes from the RX buffer. async fn read_bytes(&mut self, read_ptr: &mut u16, buffer: &mut [u8]) -> Result<(), SPI::Error> { - self.bus_read(C::rx_addr(*read_ptr), buffer).await?; + if C::AUTO_WRAP { + self.bus_read(C::rx_addr(*read_ptr), buffer).await?; + } else { + let addr = *read_ptr % C::BUF_SIZE; + if addr as usize + buffer.len() <= C::BUF_SIZE as usize { + self.bus_read(C::rx_addr(addr), buffer).await?; + } else { + let n = C::BUF_SIZE - addr; + self.bus_read(C::rx_addr(addr), &mut buffer[..n as usize]).await?; + self.bus_read(C::rx_addr(0), &mut buffer[n as usize..]).await?; + } + } + *read_ptr = (*read_ptr).wrapping_add(buffer.len() as u16); Ok(()) @@ -155,7 +167,20 @@ impl WiznetDevice { pub async fn write_frame(&mut self, frame: &[u8]) -> Result { while self.get_tx_free_size().await? < frame.len() as u16 {} let write_ptr = self.get_tx_write_ptr().await?; - self.bus_write(C::tx_addr(write_ptr), frame).await?; + + if C::AUTO_WRAP { + self.bus_write(C::tx_addr(write_ptr), frame).await?; + } else { + let addr = write_ptr % C::BUF_SIZE; + if addr as usize + frame.len() <= C::BUF_SIZE as usize { + self.bus_write(C::tx_addr(addr), frame).await?; + } else { + let n = C::BUF_SIZE - addr; + self.bus_write(C::tx_addr(addr), &frame[..n as usize]).await?; + self.bus_write(C::tx_addr(0), &frame[n as usize..]).await?; + } + } + self.set_tx_write_ptr(write_ptr.wrapping_add(frame.len() as u16)) .await?; self.command(Command::Send).await?; diff --git a/embassy-net-w5500/src/lib.rs b/embassy-net-w5500/src/lib.rs index 9b53e961..3030dfb9 100644 --- a/embassy-net-w5500/src/lib.rs +++ b/embassy-net-w5500/src/lib.rs @@ -1,4 +1,4 @@ -//! [`embassy-net`](https://crates.io/crates/embassy-net) driver for the WIZnet W5500 ethernet chip. +//! [`embassy-net`](https://crates.io/crates/embassy-net) driver for WIZnet ethernet chips. #![no_std] #![feature(async_fn_in_trait)] @@ -18,7 +18,7 @@ use crate::device::WiznetDevice; const MTU: usize = 1514; -/// Type alias for the embassy-net driver for W5500 +/// Type alias for the embassy-net driver. pub type Device<'d> = embassy_net_driver_channel::Device<'d, MTU>; /// Internal state for the embassy-net integration. @@ -35,9 +35,9 @@ impl State { } } -/// Background runner for the W5500. +/// Background runner for the driver. /// -/// You must call `.run()` in a background task for the W5500 to operate. +/// You must call `.run()` in a background task for the driver to operate. pub struct Runner<'d, C: Chip, SPI: SpiDevice, INT: Wait, RST: OutputPin> { mac: WiznetDevice, ch: ch::Runner<'d, MTU>, @@ -45,7 +45,7 @@ pub struct Runner<'d, C: Chip, SPI: SpiDevice, INT: Wait, RST: OutputPin> { _reset: RST, } -/// You must call this in a background task for the W5500 to operate. +/// You must call this in a background task for the driver to operate. impl<'d, C: Chip, SPI: SpiDevice, INT: Wait, RST: OutputPin> Runner<'d, C, SPI, INT, RST> { pub async fn run(mut self) -> ! { let (state_chan, mut rx_chan, mut tx_chan) = self.ch.split(); @@ -80,7 +80,11 @@ impl<'d, C: Chip, SPI: SpiDevice, INT: Wait, RST: OutputPin> Runner<'d, C, SPI, } } -/// Obtain a driver for using the W5500 with [`embassy-net`](https://crates.io/crates/embassy-net). +/// Create a Wiznet ethernet chip driver for [`embassy-net`](https://crates.io/crates/embassy-net). +/// +/// This returns two structs: +/// - a `Device` that you must pass to the `embassy-net` stack. +/// - a `Runner`. You must call `.run()` on it in a background task. pub async fn new<'a, const N_RX: usize, const N_TX: usize, C: Chip, SPI: SpiDevice, INT: Wait, RST: OutputPin>( mac_addr: [u8; 6], state: &'a mut State, @@ -88,13 +92,15 @@ pub async fn new<'a, const N_RX: usize, const N_TX: usize, C: Chip, SPI: SpiDevi int: INT, mut reset: RST, ) -> (Device<'a>, Runner<'a, C, SPI, INT, RST>) { - // Reset the W5500. + // Reset the chip. reset.set_low().ok(); // Ensure the reset is registered. Timer::after(Duration::from_millis(1)).await; reset.set_high().ok(); - // Wait for the W5500 to achieve PLL lock. - Timer::after(Duration::from_millis(2)).await; + + // Wait for PLL lock. Some chips are slower than others. + // Slowest is w5100s which is 100ms, so let's just wait that. + Timer::after(Duration::from_millis(100)).await; let mac = WiznetDevice::new(spi_dev, mac_addr).await.unwrap(); From 11b66a73b4fc85469eeb8f718531492aed128e26 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Tue, 15 Aug 2023 23:02:53 +0200 Subject: [PATCH 6/6] net-wiznet: rename from embassy-net-w5500. --- .github/ci/doc.sh | 2 +- embassy-net-driver-channel/README.md | 2 +- embassy-net-w5500/README.md | 7 ----- .../Cargo.toml | 10 +++---- embassy-net-wiznet/README.md | 27 +++++++++++++++++++ .../src/chip/mod.rs | 0 .../src/chip/w5100s.rs | 0 .../src/chip/w5500.rs | 0 .../src/device.rs | 4 +-- .../src/lib.rs | 0 embassy-net/README.md | 2 +- examples/rp/Cargo.toml | 2 +- .../rp/src/bin/ethernet_w5500_multisocket.rs | 6 ++--- .../rp/src/bin/ethernet_w5500_tcp_client.rs | 6 ++--- .../rp/src/bin/ethernet_w5500_tcp_server.rs | 7 ++--- examples/rp/src/bin/ethernet_w5500_udp.rs | 7 ++--- 16 files changed, 52 insertions(+), 30 deletions(-) delete mode 100644 embassy-net-w5500/README.md rename {embassy-net-w5500 => embassy-net-wiznet}/Cargo.toml (74%) create mode 100644 embassy-net-wiznet/README.md rename {embassy-net-w5500 => embassy-net-wiznet}/src/chip/mod.rs (100%) rename {embassy-net-w5500 => embassy-net-wiznet}/src/chip/w5100s.rs (100%) rename {embassy-net-w5500 => embassy-net-wiznet}/src/chip/w5500.rs (100%) rename {embassy-net-w5500 => embassy-net-wiznet}/src/device.rs (98%) rename {embassy-net-w5500 => embassy-net-wiznet}/src/lib.rs (100%) diff --git a/.github/ci/doc.sh b/.github/ci/doc.sh index f829582a..57184dc1 100755 --- a/.github/ci/doc.sh +++ b/.github/ci/doc.sh @@ -35,7 +35,7 @@ docserver-builder -i ./embassy-usb-driver -o webroot/crates/embassy-usb-driver/g docserver-builder -i ./embassy-usb-logger -o webroot/crates/embassy-usb-logger/git.zup docserver-builder -i ./cyw43 -o webroot/crates/cyw43/git.zup docserver-builder -i ./cyw43-pio -o webroot/crates/cyw43-pio/git.zup -docserver-builder -i ./embassy-net-w5500 -o webroot/crates/embassy-net-w5500/git.zup +docserver-builder -i ./embassy-net-wiznet -o webroot/crates/embassy-net-wiznet/git.zup docserver-builder -i ./embassy-net-enc28j60 -o webroot/crates/embassy-net-enc28j60/git.zup docserver-builder -i ./embassy-net-esp-hosted -o webroot/crates/embassy-net-esp-hosted/git.zup docserver-builder -i ./embassy-stm32-wpan -o webroot/crates/embassy-stm32-wpan/git.zup --output-static webroot/static diff --git a/embassy-net-driver-channel/README.md b/embassy-net-driver-channel/README.md index dd90e7ad..8f904ce9 100644 --- a/embassy-net-driver-channel/README.md +++ b/embassy-net-driver-channel/README.md @@ -76,7 +76,7 @@ These `embassy-net` drivers are implemented using this crate. You can look at th - [`cyw43`](https://github.com/embassy-rs/embassy/tree/main/cyw43) for WiFi on CYW43xx chips, used in the Raspberry Pi Pico W - [`embassy-usb`](https://github.com/embassy-rs/embassy/tree/main/embassy-usb) for Ethernet-over-USB (CDC NCM) support. -- [`embassy-net-w5500`](https://github.com/embassy-rs/embassy/tree/main/embassy-net-w5500) for Wiznet W5500 SPI Ethernet MAC+PHY chip. +- [`embassy-net-wiznet`](https://github.com/embassy-rs/embassy/tree/main/embassy-net-wiznet) for Wiznet SPI Ethernet MAC+PHY chips. - [`embassy-net-esp-hosted`](https://github.com/embassy-rs/embassy/tree/main/embassy-net-esp-hosted) for using ESP32 chips with the [`esp-hosted`](https://github.com/espressif/esp-hosted) firmware as WiFi adapters for another non-ESP32 MCU. diff --git a/embassy-net-w5500/README.md b/embassy-net-w5500/README.md deleted file mode 100644 index 9eaf4b70..00000000 --- a/embassy-net-w5500/README.md +++ /dev/null @@ -1,7 +0,0 @@ -# WIZnet W5500 `embassy-net` integration - -[`embassy-net`](https://crates.io/crates/embassy-net) integration for the WIZnet W5500 SPI ethernet chip, operating in MACRAW mode. - -Supports any SPI driver implementing [`embedded-hal-async`](https://crates.io/crates/embedded-hal-async) - -See [`examples`](https://github.com/kalkyl/embassy-net-w5500/tree/main/examples) directory for usage examples with the rp2040 [`WIZnet W5500-EVB-Pico`](https://www.wiznet.io/product-item/w5500-evb-pico/) module. \ No newline at end of file diff --git a/embassy-net-w5500/Cargo.toml b/embassy-net-wiznet/Cargo.toml similarity index 74% rename from embassy-net-w5500/Cargo.toml rename to embassy-net-wiznet/Cargo.toml index c4559868..dff03ac8 100644 --- a/embassy-net-w5500/Cargo.toml +++ b/embassy-net-wiznet/Cargo.toml @@ -1,8 +1,8 @@ [package] -name = "embassy-net-w5500" +name = "embassy-net-wiznet" version = "0.1.0" -description = "embassy-net driver for the W5500 ethernet chip" -keywords = ["embedded", "w5500", "embassy-net", "embedded-hal-async", "ethernet", "async"] +description = "embassy-net driver for WIZnet SPI Ethernet chips" +keywords = ["embedded", "wiznet", "embassy-net", "embedded-hal-async", "ethernet", "async"] categories = ["embedded", "hardware-support", "no-std", "network-programming", "async"] license = "MIT OR Apache-2.0" edition = "2021" @@ -16,7 +16,7 @@ embassy-futures = { version = "0.1.0", path = "../embassy-futures" } defmt = { version = "0.3", optional = true } [package.metadata.embassy_docs] -src_base = "https://github.com/embassy-rs/embassy/blob/embassy-net-w5500-v$VERSION/embassy-net-w5500/src/" -src_base_git = "https://github.com/embassy-rs/embassy/blob/$COMMIT/embassy-net-w5500/src/" +src_base = "https://github.com/embassy-rs/embassy/blob/embassy-net-wiznet-v$VERSION/embassy-net-wiznet/src/" +src_base_git = "https://github.com/embassy-rs/embassy/blob/$COMMIT/embassy-net-wiznet/src/" target = "thumbv7em-none-eabi" features = ["defmt"] \ No newline at end of file diff --git a/embassy-net-wiznet/README.md b/embassy-net-wiznet/README.md new file mode 100644 index 00000000..b8e4bdc8 --- /dev/null +++ b/embassy-net-wiznet/README.md @@ -0,0 +1,27 @@ +# WIZnet `embassy-net` integration + +[`embassy-net`](https://crates.io/crates/embassy-net) integration for the WIZnet SPI ethernet chips, operating in MACRAW mode. + +See [`examples`](https://github.com/embassy-rs/embassy/tree/main/examples/rp) directory for usage examples with the rp2040 [`WIZnet W5500-EVB-Pico`](https://www.wiznet.io/product-item/w5500-evb-pico/) module. + +## Supported chips + +- W5500 +- W5100S + +## Interoperability + +This crate can run on any executor. + +It supports any SPI driver implementing [`embedded-hal-async`](https://crates.io/crates/embedded-hal-async). + + +## License + +This work is licensed under either of + +- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or + http://www.apache.org/licenses/LICENSE-2.0) +- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) + +at your option. diff --git a/embassy-net-w5500/src/chip/mod.rs b/embassy-net-wiznet/src/chip/mod.rs similarity index 100% rename from embassy-net-w5500/src/chip/mod.rs rename to embassy-net-wiznet/src/chip/mod.rs diff --git a/embassy-net-w5500/src/chip/w5100s.rs b/embassy-net-wiznet/src/chip/w5100s.rs similarity index 100% rename from embassy-net-w5500/src/chip/w5100s.rs rename to embassy-net-wiznet/src/chip/w5100s.rs diff --git a/embassy-net-w5500/src/chip/w5500.rs b/embassy-net-wiznet/src/chip/w5500.rs similarity index 100% rename from embassy-net-w5500/src/chip/w5500.rs rename to embassy-net-wiznet/src/chip/w5500.rs diff --git a/embassy-net-w5500/src/device.rs b/embassy-net-wiznet/src/device.rs similarity index 98% rename from embassy-net-w5500/src/device.rs rename to embassy-net-wiznet/src/device.rs index f367bc3e..43f9512a 100644 --- a/embassy-net-w5500/src/device.rs +++ b/embassy-net-wiznet/src/device.rs @@ -16,7 +16,7 @@ enum Interrupt { Receive = 0b00100_u8, } -/// W5500 in MACRAW mode +/// Wiznet chip in MACRAW mode #[derive(Debug)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub(crate) struct WiznetDevice { @@ -25,7 +25,7 @@ pub(crate) struct WiznetDevice { } impl WiznetDevice { - /// Create and initialize the W5500 driver + /// Create and initialize the driver pub async fn new(spi: SPI, mac_addr: [u8; 6]) -> Result { let mut this = Self { spi, diff --git a/embassy-net-w5500/src/lib.rs b/embassy-net-wiznet/src/lib.rs similarity index 100% rename from embassy-net-w5500/src/lib.rs rename to embassy-net-wiznet/src/lib.rs diff --git a/embassy-net/README.md b/embassy-net/README.md index 811321ca..7bb2283c 100644 --- a/embassy-net/README.md +++ b/embassy-net/README.md @@ -22,7 +22,7 @@ unimplemented features of the network protocols. - [`cyw43`](https://github.com/embassy-rs/embassy/tree/main/cyw43) for WiFi on CYW43xx chips, used in the Raspberry Pi Pico W - [`embassy-usb`](https://github.com/embassy-rs/embassy/tree/main/embassy-usb) for Ethernet-over-USB (CDC NCM) support. - [`embassy-stm32`](https://github.com/embassy-rs/embassy/tree/main/embassy-stm32) for the builtin Ethernet MAC in all STM32 chips (STM32F1, STM32F2, STM32F4, STM32F7, STM32H7, STM32H5). -- [`embassy-net-w5500`](https://github.com/embassy-rs/embassy/tree/main/embassy-net-w5500) for Wiznet W5500 SPI Ethernet MAC+PHY chip. +- [`embassy-net-wiznet`](https://github.com/embassy-rs/embassy/tree/main/embassy-net-wiznet) for Wiznet SPI Ethernet MAC+PHY chips (W5100S, W5500) - [`embassy-net-esp-hosted`](https://github.com/embassy-rs/embassy/tree/main/embassy-net-esp-hosted) for using ESP32 chips with the [`esp-hosted`](https://github.com/espressif/esp-hosted) firmware as WiFi adapters for another non-ESP32 MCU. ## Examples diff --git a/examples/rp/Cargo.toml b/examples/rp/Cargo.toml index d83e370d..6742ce7e 100644 --- a/examples/rp/Cargo.toml +++ b/examples/rp/Cargo.toml @@ -13,7 +13,7 @@ embassy-time = { version = "0.1.2", path = "../../embassy-time", features = ["ni embassy-rp = { version = "0.1.0", path = "../../embassy-rp", features = ["defmt", "unstable-traits", "nightly", "unstable-pac", "time-driver", "critical-section-impl"] } embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "udp", "dhcpv4", "medium-ethernet"] } -embassy-net-w5500 = { version = "0.1.0", path = "../../embassy-net-w5500", features = ["defmt"] } +embassy-net-wiznet = { version = "0.1.0", path = "../../embassy-net-wiznet", features = ["defmt"] } embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } embassy-usb-logger = { version = "0.1.0", path = "../../embassy-usb-logger" } embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["time", "defmt"] } diff --git a/examples/rp/src/bin/ethernet_w5500_multisocket.rs b/examples/rp/src/bin/ethernet_w5500_multisocket.rs index 3677f3cd..d4af8764 100644 --- a/examples/rp/src/bin/ethernet_w5500_multisocket.rs +++ b/examples/rp/src/bin/ethernet_w5500_multisocket.rs @@ -10,8 +10,8 @@ use defmt::*; use embassy_executor::Spawner; use embassy_futures::yield_now; use embassy_net::{Stack, StackResources}; -use embassy_net_w5500::chip::W5500; -use embassy_net_w5500::*; +use embassy_net_wiznet::chip::W5500; +use embassy_net_wiznet::*; use embassy_rp::clocks::RoscRng; use embassy_rp::gpio::{Input, Level, Output, Pull}; use embassy_rp::peripherals::{PIN_17, PIN_20, PIN_21, SPI0}; @@ -56,7 +56,7 @@ async fn main(spawner: Spawner) { let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00]; let state = make_static!(State::<8, 8>::new()); - let (device, runner) = embassy_net_w5500::new( + let (device, runner) = embassy_net_wiznet::new( mac_addr, state, ExclusiveDevice::new(spi, cs, Delay), diff --git a/examples/rp/src/bin/ethernet_w5500_tcp_client.rs b/examples/rp/src/bin/ethernet_w5500_tcp_client.rs index b78a0977..78340492 100644 --- a/examples/rp/src/bin/ethernet_w5500_tcp_client.rs +++ b/examples/rp/src/bin/ethernet_w5500_tcp_client.rs @@ -12,8 +12,8 @@ use defmt::*; use embassy_executor::Spawner; use embassy_futures::yield_now; use embassy_net::{Stack, StackResources}; -use embassy_net_w5500::chip::W5500; -use embassy_net_w5500::*; +use embassy_net_wiznet::chip::W5500; +use embassy_net_wiznet::*; use embassy_rp::clocks::RoscRng; use embassy_rp::gpio::{Input, Level, Output, Pull}; use embassy_rp::peripherals::{PIN_17, PIN_20, PIN_21, SPI0}; @@ -59,7 +59,7 @@ async fn main(spawner: Spawner) { let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00]; let state = make_static!(State::<8, 8>::new()); - let (device, runner) = embassy_net_w5500::new( + let (device, runner) = embassy_net_wiznet::new( mac_addr, state, ExclusiveDevice::new(spi, cs, Delay), diff --git a/examples/rp/src/bin/ethernet_w5500_tcp_server.rs b/examples/rp/src/bin/ethernet_w5500_tcp_server.rs index 34f054d9..e6d13274 100644 --- a/examples/rp/src/bin/ethernet_w5500_tcp_server.rs +++ b/examples/rp/src/bin/ethernet_w5500_tcp_server.rs @@ -11,8 +11,8 @@ use defmt::*; use embassy_executor::Spawner; use embassy_futures::yield_now; use embassy_net::{Stack, StackResources}; -use embassy_net_w5500::chip::W5500; -use embassy_net_w5500::*; +use embassy_net_wiznet::chip::W5500; +use embassy_net_wiznet::*; use embassy_rp::clocks::RoscRng; use embassy_rp::gpio::{Input, Level, Output, Pull}; use embassy_rp::peripherals::{PIN_17, PIN_20, PIN_21, SPI0}; @@ -23,6 +23,7 @@ use embedded_io_async::Write; use rand::RngCore; use static_cell::make_static; use {defmt_rtt as _, panic_probe as _}; + #[embassy_executor::task] async fn ethernet_task( runner: Runner< @@ -57,7 +58,7 @@ async fn main(spawner: Spawner) { let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00]; let state = make_static!(State::<8, 8>::new()); - let (device, runner) = embassy_net_w5500::new( + let (device, runner) = embassy_net_wiznet::new( mac_addr, state, ExclusiveDevice::new(spi, cs, Delay), diff --git a/examples/rp/src/bin/ethernet_w5500_udp.rs b/examples/rp/src/bin/ethernet_w5500_udp.rs index 8f38e453..b546714d 100644 --- a/examples/rp/src/bin/ethernet_w5500_udp.rs +++ b/examples/rp/src/bin/ethernet_w5500_udp.rs @@ -11,8 +11,8 @@ use embassy_executor::Spawner; use embassy_futures::yield_now; use embassy_net::udp::{PacketMetadata, UdpSocket}; use embassy_net::{Stack, StackResources}; -use embassy_net_w5500::chip::W5500; -use embassy_net_w5500::*; +use embassy_net_wiznet::chip::W5500; +use embassy_net_wiznet::*; use embassy_rp::clocks::RoscRng; use embassy_rp::gpio::{Input, Level, Output, Pull}; use embassy_rp::peripherals::{PIN_17, PIN_20, PIN_21, SPI0}; @@ -22,6 +22,7 @@ use embedded_hal_async::spi::ExclusiveDevice; use rand::RngCore; use static_cell::make_static; use {defmt_rtt as _, panic_probe as _}; + #[embassy_executor::task] async fn ethernet_task( runner: Runner< @@ -55,7 +56,7 @@ async fn main(spawner: Spawner) { let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00]; let state = make_static!(State::<8, 8>::new()); - let (device, runner) = embassy_net_w5500::new( + let (device, runner) = embassy_net_wiznet::new( mac_addr, state, ExclusiveDevice::new(spi, cs, Delay),