diff options
| -rwxr-xr-x | .github/ci/doc.sh | 2 | ||||
| -rw-r--r-- | embassy-net-driver-channel/README.md | 2 | ||||
| -rw-r--r-- | embassy-net-w5500/README.md | 7 | ||||
| -rw-r--r-- | embassy-net-w5500/src/device.rs | 115 | ||||
| -rw-r--r-- | embassy-net-w5500/src/socket.rs | 80 | ||||
| -rw-r--r-- | embassy-net-w5500/src/spi.rs | 32 | ||||
| -rw-r--r-- | embassy-net-wiznet/Cargo.toml (renamed from embassy-net-w5500/Cargo.toml) | 13 | ||||
| -rw-r--r-- | embassy-net-wiznet/README.md | 27 | ||||
| -rw-r--r-- | embassy-net-wiznet/src/chip/mod.rs | 48 | ||||
| -rw-r--r-- | embassy-net-wiznet/src/chip/w5100s.rs | 61 | ||||
| -rw-r--r-- | embassy-net-wiznet/src/chip/w5500.rs | 72 | ||||
| -rw-r--r-- | embassy-net-wiznet/src/device.rs | 195 | ||||
| -rw-r--r-- | embassy-net-wiznet/src/lib.rs (renamed from embassy-net-w5500/src/lib.rs) | 44 | ||||
| -rw-r--r-- | embassy-net/README.md | 2 | ||||
| -rw-r--r-- | embassy-rp/src/dma.rs | 5 | ||||
| -rw-r--r-- | examples/rp/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/rp/src/bin/ethernet_w5500_multisocket.rs | 6 | ||||
| -rw-r--r-- | examples/rp/src/bin/ethernet_w5500_tcp_client.rs | 6 | ||||
| -rw-r--r-- | examples/rp/src/bin/ethernet_w5500_tcp_server.rs | 7 | ||||
| -rw-r--r-- | examples/rp/src/bin/ethernet_w5500_udp.rs | 7 |
20 files changed, 461 insertions, 272 deletions
diff --git a/.github/ci/doc.sh b/.github/ci/doc.sh index f829582ab..57184dc1d 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 | |||
| 35 | docserver-builder -i ./embassy-usb-logger -o webroot/crates/embassy-usb-logger/git.zup | 35 | docserver-builder -i ./embassy-usb-logger -o webroot/crates/embassy-usb-logger/git.zup |
| 36 | docserver-builder -i ./cyw43 -o webroot/crates/cyw43/git.zup | 36 | docserver-builder -i ./cyw43 -o webroot/crates/cyw43/git.zup |
| 37 | docserver-builder -i ./cyw43-pio -o webroot/crates/cyw43-pio/git.zup | 37 | docserver-builder -i ./cyw43-pio -o webroot/crates/cyw43-pio/git.zup |
| 38 | docserver-builder -i ./embassy-net-w5500 -o webroot/crates/embassy-net-w5500/git.zup | 38 | docserver-builder -i ./embassy-net-wiznet -o webroot/crates/embassy-net-wiznet/git.zup |
| 39 | docserver-builder -i ./embassy-net-enc28j60 -o webroot/crates/embassy-net-enc28j60/git.zup | 39 | docserver-builder -i ./embassy-net-enc28j60 -o webroot/crates/embassy-net-enc28j60/git.zup |
| 40 | docserver-builder -i ./embassy-net-esp-hosted -o webroot/crates/embassy-net-esp-hosted/git.zup | 40 | docserver-builder -i ./embassy-net-esp-hosted -o webroot/crates/embassy-net-esp-hosted/git.zup |
| 41 | docserver-builder -i ./embassy-stm32-wpan -o webroot/crates/embassy-stm32-wpan/git.zup --output-static webroot/static | 41 | 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 dd90e7ad2..8f904ce95 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 | |||
| 76 | 76 | ||
| 77 | - [`cyw43`](https://github.com/embassy-rs/embassy/tree/main/cyw43) for WiFi on CYW43xx chips, used in the Raspberry Pi Pico W | 77 | - [`cyw43`](https://github.com/embassy-rs/embassy/tree/main/cyw43) for WiFi on CYW43xx chips, used in the Raspberry Pi Pico W |
| 78 | - [`embassy-usb`](https://github.com/embassy-rs/embassy/tree/main/embassy-usb) for Ethernet-over-USB (CDC NCM) support. | 78 | - [`embassy-usb`](https://github.com/embassy-rs/embassy/tree/main/embassy-usb) for Ethernet-over-USB (CDC NCM) support. |
| 79 | - [`embassy-net-w5500`](https://github.com/embassy-rs/embassy/tree/main/embassy-net-w5500) for Wiznet W5500 SPI Ethernet MAC+PHY chip. | 79 | - [`embassy-net-wiznet`](https://github.com/embassy-rs/embassy/tree/main/embassy-net-wiznet) for Wiznet SPI Ethernet MAC+PHY chips. |
| 80 | - [`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. | 80 | - [`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. |
| 81 | 81 | ||
| 82 | 82 | ||
diff --git a/embassy-net-w5500/README.md b/embassy-net-w5500/README.md deleted file mode 100644 index 9eaf4b700..000000000 --- a/embassy-net-w5500/README.md +++ /dev/null | |||
| @@ -1,7 +0,0 @@ | |||
| 1 | # WIZnet W5500 `embassy-net` integration | ||
| 2 | |||
| 3 | [`embassy-net`](https://crates.io/crates/embassy-net) integration for the WIZnet W5500 SPI ethernet chip, operating in MACRAW mode. | ||
| 4 | |||
| 5 | Supports any SPI driver implementing [`embedded-hal-async`](https://crates.io/crates/embedded-hal-async) | ||
| 6 | |||
| 7 | 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/src/device.rs b/embassy-net-w5500/src/device.rs deleted file mode 100644 index 4933625e2..000000000 --- a/embassy-net-w5500/src/device.rs +++ /dev/null | |||
| @@ -1,115 +0,0 @@ | |||
| 1 | use embedded_hal_async::spi::SpiDevice; | ||
| 2 | |||
| 3 | use crate::socket; | ||
| 4 | use crate::spi::SpiInterface; | ||
| 5 | |||
| 6 | pub const MODE: u16 = 0x00; | ||
| 7 | pub const MAC: u16 = 0x09; | ||
| 8 | pub const SOCKET_INTR: u16 = 0x18; | ||
| 9 | pub const PHY_CFG: u16 = 0x2E; | ||
| 10 | |||
| 11 | #[repr(u8)] | ||
| 12 | pub enum RegisterBlock { | ||
| 13 | Common = 0x00, | ||
| 14 | Socket0 = 0x01, | ||
| 15 | TxBuf = 0x02, | ||
| 16 | RxBuf = 0x03, | ||
| 17 | } | ||
| 18 | |||
| 19 | /// W5500 in MACRAW mode | ||
| 20 | #[derive(Debug)] | ||
| 21 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 22 | pub struct W5500<SPI> { | ||
| 23 | bus: SpiInterface<SPI>, | ||
| 24 | } | ||
| 25 | |||
| 26 | impl<SPI: SpiDevice> W5500<SPI> { | ||
| 27 | /// Create and initialize the W5500 driver | ||
| 28 | pub async fn new(spi: SPI, mac_addr: [u8; 6]) -> Result<W5500<SPI>, SPI::Error> { | ||
| 29 | let mut bus = SpiInterface(spi); | ||
| 30 | // Reset device | ||
| 31 | bus.write_frame(RegisterBlock::Common, MODE, &[0x80]).await?; | ||
| 32 | |||
| 33 | // Enable interrupt pin | ||
| 34 | bus.write_frame(RegisterBlock::Common, SOCKET_INTR, &[0x01]).await?; | ||
| 35 | // Enable receive interrupt | ||
| 36 | bus.write_frame( | ||
| 37 | RegisterBlock::Socket0, | ||
| 38 | socket::SOCKET_INTR_MASK, | ||
| 39 | &[socket::Interrupt::Receive as u8], | ||
| 40 | ) | ||
| 41 | .await?; | ||
| 42 | |||
| 43 | // Set MAC address | ||
| 44 | bus.write_frame(RegisterBlock::Common, MAC, &mac_addr).await?; | ||
| 45 | |||
| 46 | // Set the raw socket RX/TX buffer sizes to 16KB | ||
| 47 | bus.write_frame(RegisterBlock::Socket0, socket::TXBUF_SIZE, &[16]) | ||
| 48 | .await?; | ||
| 49 | bus.write_frame(RegisterBlock::Socket0, socket::RXBUF_SIZE, &[16]) | ||
| 50 | .await?; | ||
| 51 | |||
| 52 | // MACRAW mode with MAC filtering. | ||
| 53 | let mode: u8 = (1 << 2) | (1 << 7); | ||
| 54 | bus.write_frame(RegisterBlock::Socket0, socket::MODE, &[mode]).await?; | ||
| 55 | socket::command(&mut bus, socket::Command::Open).await?; | ||
| 56 | |||
| 57 | Ok(Self { bus }) | ||
| 58 | } | ||
| 59 | |||
| 60 | /// Read bytes from the RX buffer. Returns the number of bytes read. | ||
| 61 | async fn read_bytes(&mut self, read_ptr: &mut u16, buffer: &mut [u8]) -> Result<(), SPI::Error> { | ||
| 62 | self.bus.read_frame(RegisterBlock::RxBuf, *read_ptr, buffer).await?; | ||
| 63 | *read_ptr = (*read_ptr).wrapping_add(buffer.len() as u16); | ||
| 64 | |||
| 65 | Ok(()) | ||
| 66 | } | ||
| 67 | |||
| 68 | /// Read an ethernet frame from the device. Returns the number of bytes read. | ||
| 69 | pub async fn read_frame(&mut self, frame: &mut [u8]) -> Result<usize, SPI::Error> { | ||
| 70 | let rx_size = socket::get_rx_size(&mut self.bus).await? as usize; | ||
| 71 | if rx_size == 0 { | ||
| 72 | return Ok(0); | ||
| 73 | } | ||
| 74 | |||
| 75 | socket::reset_interrupt(&mut self.bus, socket::Interrupt::Receive).await?; | ||
| 76 | |||
| 77 | let mut read_ptr = socket::get_rx_read_ptr(&mut self.bus).await?; | ||
| 78 | |||
| 79 | // First two bytes gives the size of the received ethernet frame | ||
| 80 | let expected_frame_size: usize = { | ||
| 81 | let mut frame_bytes = [0u8; 2]; | ||
| 82 | self.read_bytes(&mut read_ptr, &mut frame_bytes).await?; | ||
| 83 | u16::from_be_bytes(frame_bytes) as usize - 2 | ||
| 84 | }; | ||
| 85 | |||
| 86 | // Read the ethernet frame | ||
| 87 | self.read_bytes(&mut read_ptr, &mut frame[..expected_frame_size]) | ||
| 88 | .await?; | ||
| 89 | |||
| 90 | // Register RX as completed | ||
| 91 | socket::set_rx_read_ptr(&mut self.bus, read_ptr).await?; | ||
| 92 | socket::command(&mut self.bus, socket::Command::Receive).await?; | ||
| 93 | |||
| 94 | Ok(expected_frame_size) | ||
| 95 | } | ||
| 96 | |||
| 97 | /// Write an ethernet frame to the device. Returns number of bytes written | ||
| 98 | pub async fn write_frame(&mut self, frame: &[u8]) -> Result<usize, SPI::Error> { | ||
| 99 | while socket::get_tx_free_size(&mut self.bus).await? < frame.len() as u16 {} | ||
| 100 | let write_ptr = socket::get_tx_write_ptr(&mut self.bus).await?; | ||
| 101 | self.bus.write_frame(RegisterBlock::TxBuf, write_ptr, frame).await?; | ||
| 102 | socket::set_tx_write_ptr(&mut self.bus, write_ptr.wrapping_add(frame.len() as u16)).await?; | ||
| 103 | socket::command(&mut self.bus, socket::Command::Send).await?; | ||
| 104 | Ok(frame.len()) | ||
| 105 | } | ||
| 106 | |||
| 107 | pub async fn is_link_up(&mut self) -> bool { | ||
| 108 | let mut link = [0]; | ||
| 109 | self.bus | ||
| 110 | .read_frame(RegisterBlock::Common, PHY_CFG, &mut link) | ||
| 111 | .await | ||
| 112 | .ok(); | ||
| 113 | link[0] & 1 == 1 | ||
| 114 | } | ||
| 115 | } | ||
diff --git a/embassy-net-w5500/src/socket.rs b/embassy-net-w5500/src/socket.rs deleted file mode 100644 index 3d65583c1..000000000 --- a/embassy-net-w5500/src/socket.rs +++ /dev/null | |||
| @@ -1,80 +0,0 @@ | |||
| 1 | use embedded_hal_async::spi::SpiDevice; | ||
| 2 | |||
| 3 | use crate::device::RegisterBlock; | ||
| 4 | use crate::spi::SpiInterface; | ||
| 5 | |||
| 6 | pub const MODE: u16 = 0x00; | ||
| 7 | pub const COMMAND: u16 = 0x01; | ||
| 8 | pub const RXBUF_SIZE: u16 = 0x1E; | ||
| 9 | pub const TXBUF_SIZE: u16 = 0x1F; | ||
| 10 | pub const TX_FREE_SIZE: u16 = 0x20; | ||
| 11 | pub const TX_DATA_WRITE_PTR: u16 = 0x24; | ||
| 12 | pub const RECVD_SIZE: u16 = 0x26; | ||
| 13 | pub const RX_DATA_READ_PTR: u16 = 0x28; | ||
| 14 | pub const SOCKET_INTR_MASK: u16 = 0x2C; | ||
| 15 | |||
| 16 | #[repr(u8)] | ||
| 17 | pub enum Command { | ||
| 18 | Open = 0x01, | ||
| 19 | Send = 0x20, | ||
| 20 | Receive = 0x40, | ||
| 21 | } | ||
| 22 | |||
| 23 | pub const INTR: u16 = 0x02; | ||
| 24 | #[repr(u8)] | ||
| 25 | pub enum Interrupt { | ||
| 26 | Receive = 0b00100_u8, | ||
| 27 | } | ||
| 28 | |||
| 29 | pub async fn reset_interrupt<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>, code: Interrupt) -> Result<(), SPI::Error> { | ||
| 30 | let data = [code as u8]; | ||
| 31 | bus.write_frame(RegisterBlock::Socket0, INTR, &data).await | ||
| 32 | } | ||
| 33 | |||
| 34 | pub async fn get_tx_write_ptr<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result<u16, SPI::Error> { | ||
| 35 | let mut data = [0u8; 2]; | ||
| 36 | bus.read_frame(RegisterBlock::Socket0, TX_DATA_WRITE_PTR, &mut data) | ||
| 37 | .await?; | ||
| 38 | Ok(u16::from_be_bytes(data)) | ||
| 39 | } | ||
| 40 | |||
| 41 | pub async fn set_tx_write_ptr<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>, ptr: u16) -> Result<(), SPI::Error> { | ||
| 42 | let data = ptr.to_be_bytes(); | ||
| 43 | bus.write_frame(RegisterBlock::Socket0, TX_DATA_WRITE_PTR, &data).await | ||
| 44 | } | ||
| 45 | |||
| 46 | pub async fn get_rx_read_ptr<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result<u16, SPI::Error> { | ||
| 47 | let mut data = [0u8; 2]; | ||
| 48 | bus.read_frame(RegisterBlock::Socket0, RX_DATA_READ_PTR, &mut data) | ||
| 49 | .await?; | ||
| 50 | Ok(u16::from_be_bytes(data)) | ||
| 51 | } | ||
| 52 | |||
| 53 | pub async fn set_rx_read_ptr<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>, ptr: u16) -> Result<(), SPI::Error> { | ||
| 54 | let data = ptr.to_be_bytes(); | ||
| 55 | bus.write_frame(RegisterBlock::Socket0, RX_DATA_READ_PTR, &data).await | ||
| 56 | } | ||
| 57 | |||
| 58 | pub async fn command<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>, command: Command) -> Result<(), SPI::Error> { | ||
| 59 | let data = [command as u8]; | ||
| 60 | bus.write_frame(RegisterBlock::Socket0, COMMAND, &data).await | ||
| 61 | } | ||
| 62 | |||
| 63 | pub async fn get_rx_size<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result<u16, SPI::Error> { | ||
| 64 | loop { | ||
| 65 | // Wait until two sequential reads are equal | ||
| 66 | let mut res0 = [0u8; 2]; | ||
| 67 | bus.read_frame(RegisterBlock::Socket0, RECVD_SIZE, &mut res0).await?; | ||
| 68 | let mut res1 = [0u8; 2]; | ||
| 69 | bus.read_frame(RegisterBlock::Socket0, RECVD_SIZE, &mut res1).await?; | ||
| 70 | if res0 == res1 { | ||
| 71 | break Ok(u16::from_be_bytes(res0)); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | pub async fn get_tx_free_size<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result<u16, SPI::Error> { | ||
| 77 | let mut data = [0; 2]; | ||
| 78 | bus.read_frame(RegisterBlock::Socket0, TX_FREE_SIZE, &mut data).await?; | ||
| 79 | Ok(u16::from_be_bytes(data)) | ||
| 80 | } | ||
diff --git a/embassy-net-w5500/src/spi.rs b/embassy-net-w5500/src/spi.rs deleted file mode 100644 index 07749d6be..000000000 --- a/embassy-net-w5500/src/spi.rs +++ /dev/null | |||
| @@ -1,32 +0,0 @@ | |||
| 1 | use embedded_hal_async::spi::{Operation, SpiDevice}; | ||
| 2 | |||
| 3 | use crate::device::RegisterBlock; | ||
| 4 | |||
| 5 | #[derive(Debug)] | ||
| 6 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 7 | pub struct SpiInterface<SPI>(pub SPI); | ||
| 8 | |||
| 9 | impl<SPI: SpiDevice> SpiInterface<SPI> { | ||
| 10 | pub async fn read_frame(&mut self, block: RegisterBlock, address: u16, data: &mut [u8]) -> Result<(), SPI::Error> { | ||
| 11 | let address_phase = address.to_be_bytes(); | ||
| 12 | let control_phase = [(block as u8) << 3]; | ||
| 13 | let operations = &mut [ | ||
| 14 | Operation::Write(&address_phase), | ||
| 15 | Operation::Write(&control_phase), | ||
| 16 | Operation::TransferInPlace(data), | ||
| 17 | ]; | ||
| 18 | self.0.transaction(operations).await | ||
| 19 | } | ||
| 20 | |||
| 21 | pub async fn write_frame(&mut self, block: RegisterBlock, address: u16, data: &[u8]) -> Result<(), SPI::Error> { | ||
| 22 | let address_phase = address.to_be_bytes(); | ||
| 23 | let control_phase = [(block as u8) << 3 | 0b0000_0100]; | ||
| 24 | let data_phase = data; | ||
| 25 | let operations = &mut [ | ||
| 26 | Operation::Write(&address_phase[..]), | ||
| 27 | Operation::Write(&control_phase), | ||
| 28 | Operation::Write(&data_phase), | ||
| 29 | ]; | ||
| 30 | self.0.transaction(operations).await | ||
| 31 | } | ||
| 32 | } | ||
diff --git a/embassy-net-w5500/Cargo.toml b/embassy-net-wiznet/Cargo.toml index 8972b814a..dff03ac83 100644 --- a/embassy-net-w5500/Cargo.toml +++ b/embassy-net-wiznet/Cargo.toml | |||
| @@ -1,8 +1,8 @@ | |||
| 1 | [package] | 1 | [package] |
| 2 | name = "embassy-net-w5500" | 2 | name = "embassy-net-wiznet" |
| 3 | version = "0.1.0" | 3 | version = "0.1.0" |
| 4 | description = "embassy-net driver for the W5500 ethernet chip" | 4 | description = "embassy-net driver for WIZnet SPI Ethernet chips" |
| 5 | keywords = ["embedded", "w5500", "embassy-net", "embedded-hal-async", "ethernet", "async"] | 5 | keywords = ["embedded", "wiznet", "embassy-net", "embedded-hal-async", "ethernet", "async"] |
| 6 | categories = ["embedded", "hardware-support", "no-std", "network-programming", "async"] | 6 | categories = ["embedded", "hardware-support", "no-std", "network-programming", "async"] |
| 7 | license = "MIT OR Apache-2.0" | 7 | license = "MIT OR Apache-2.0" |
| 8 | edition = "2021" | 8 | edition = "2021" |
| @@ -16,6 +16,7 @@ embassy-futures = { version = "0.1.0", path = "../embassy-futures" } | |||
| 16 | defmt = { version = "0.3", optional = true } | 16 | defmt = { version = "0.3", optional = true } |
| 17 | 17 | ||
| 18 | [package.metadata.embassy_docs] | 18 | [package.metadata.embassy_docs] |
| 19 | src_base = "https://github.com/embassy-rs/embassy/blob/embassy-net-w5500-v$VERSION/embassy-net-w5500/src/" | 19 | src_base = "https://github.com/embassy-rs/embassy/blob/embassy-net-wiznet-v$VERSION/embassy-net-wiznet/src/" |
| 20 | src_base_git = "https://github.com/embassy-rs/embassy/blob/$COMMIT/embassy-net-w5500/src/" | 20 | src_base_git = "https://github.com/embassy-rs/embassy/blob/$COMMIT/embassy-net-wiznet/src/" |
| 21 | target = "thumbv7em-none-eabi" \ No newline at end of file | 21 | target = "thumbv7em-none-eabi" |
| 22 | 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 000000000..b8e4bdc8e --- /dev/null +++ b/embassy-net-wiznet/README.md | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | # WIZnet `embassy-net` integration | ||
| 2 | |||
| 3 | [`embassy-net`](https://crates.io/crates/embassy-net) integration for the WIZnet SPI ethernet chips, operating in MACRAW mode. | ||
| 4 | |||
| 5 | 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. | ||
| 6 | |||
| 7 | ## Supported chips | ||
| 8 | |||
| 9 | - W5500 | ||
| 10 | - W5100S | ||
| 11 | |||
| 12 | ## Interoperability | ||
| 13 | |||
| 14 | This crate can run on any executor. | ||
| 15 | |||
| 16 | It supports any SPI driver implementing [`embedded-hal-async`](https://crates.io/crates/embedded-hal-async). | ||
| 17 | |||
| 18 | |||
| 19 | ## License | ||
| 20 | |||
| 21 | This work is licensed under either of | ||
| 22 | |||
| 23 | - Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or | ||
| 24 | http://www.apache.org/licenses/LICENSE-2.0) | ||
| 25 | - MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) | ||
| 26 | |||
| 27 | at your option. | ||
diff --git a/embassy-net-wiznet/src/chip/mod.rs b/embassy-net-wiznet/src/chip/mod.rs new file mode 100644 index 000000000..562db515a --- /dev/null +++ b/embassy-net-wiznet/src/chip/mod.rs | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | mod w5500; | ||
| 2 | pub use w5500::W5500; | ||
| 3 | mod w5100s; | ||
| 4 | pub use w5100s::W5100S; | ||
| 5 | |||
| 6 | pub(crate) mod sealed { | ||
| 7 | use embedded_hal_async::spi::SpiDevice; | ||
| 8 | |||
| 9 | pub trait Chip { | ||
| 10 | type Address; | ||
| 11 | |||
| 12 | const COMMON_MODE: Self::Address; | ||
| 13 | const COMMON_MAC: Self::Address; | ||
| 14 | const COMMON_SOCKET_INTR: Self::Address; | ||
| 15 | const COMMON_PHY_CFG: Self::Address; | ||
| 16 | const SOCKET_MODE: Self::Address; | ||
| 17 | const SOCKET_COMMAND: Self::Address; | ||
| 18 | const SOCKET_RXBUF_SIZE: Self::Address; | ||
| 19 | const SOCKET_TXBUF_SIZE: Self::Address; | ||
| 20 | const SOCKET_TX_FREE_SIZE: Self::Address; | ||
| 21 | const SOCKET_TX_DATA_WRITE_PTR: Self::Address; | ||
| 22 | const SOCKET_RECVD_SIZE: Self::Address; | ||
| 23 | const SOCKET_RX_DATA_READ_PTR: Self::Address; | ||
| 24 | const SOCKET_INTR_MASK: Self::Address; | ||
| 25 | const SOCKET_INTR: Self::Address; | ||
| 26 | |||
| 27 | const SOCKET_MODE_VALUE: u8; | ||
| 28 | |||
| 29 | const BUF_SIZE: u16; | ||
| 30 | const AUTO_WRAP: bool; | ||
| 31 | |||
| 32 | fn rx_addr(addr: u16) -> Self::Address; | ||
| 33 | fn tx_addr(addr: u16) -> Self::Address; | ||
| 34 | |||
| 35 | async fn bus_read<SPI: SpiDevice>( | ||
| 36 | spi: &mut SPI, | ||
| 37 | address: Self::Address, | ||
| 38 | data: &mut [u8], | ||
| 39 | ) -> Result<(), SPI::Error>; | ||
| 40 | async fn bus_write<SPI: SpiDevice>( | ||
| 41 | spi: &mut SPI, | ||
| 42 | address: Self::Address, | ||
| 43 | data: &[u8], | ||
| 44 | ) -> Result<(), SPI::Error>; | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | pub trait Chip: sealed::Chip {} | ||
diff --git a/embassy-net-wiznet/src/chip/w5100s.rs b/embassy-net-wiznet/src/chip/w5100s.rs new file mode 100644 index 000000000..07a840370 --- /dev/null +++ b/embassy-net-wiznet/src/chip/w5100s.rs | |||
| @@ -0,0 +1,61 @@ | |||
| 1 | use embedded_hal_async::spi::{Operation, SpiDevice}; | ||
| 2 | |||
| 3 | const SOCKET_BASE: u16 = 0x400; | ||
| 4 | const TX_BASE: u16 = 0x4000; | ||
| 5 | const RX_BASE: u16 = 0x6000; | ||
| 6 | |||
| 7 | pub enum W5100S {} | ||
| 8 | |||
| 9 | impl super::Chip for W5100S {} | ||
| 10 | impl super::sealed::Chip for W5100S { | ||
| 11 | type Address = u16; | ||
| 12 | |||
| 13 | const COMMON_MODE: Self::Address = 0x00; | ||
| 14 | const COMMON_MAC: Self::Address = 0x09; | ||
| 15 | const COMMON_SOCKET_INTR: Self::Address = 0x16; | ||
| 16 | const COMMON_PHY_CFG: Self::Address = 0x3c; | ||
| 17 | |||
| 18 | const SOCKET_MODE: Self::Address = SOCKET_BASE + 0x00; | ||
| 19 | const SOCKET_COMMAND: Self::Address = SOCKET_BASE + 0x01; | ||
| 20 | const SOCKET_RXBUF_SIZE: Self::Address = SOCKET_BASE + 0x1E; | ||
| 21 | const SOCKET_TXBUF_SIZE: Self::Address = SOCKET_BASE + 0x1F; | ||
| 22 | const SOCKET_TX_FREE_SIZE: Self::Address = SOCKET_BASE + 0x20; | ||
| 23 | const SOCKET_TX_DATA_WRITE_PTR: Self::Address = SOCKET_BASE + 0x24; | ||
| 24 | const SOCKET_RECVD_SIZE: Self::Address = SOCKET_BASE + 0x26; | ||
| 25 | const SOCKET_RX_DATA_READ_PTR: Self::Address = SOCKET_BASE + 0x28; | ||
| 26 | const SOCKET_INTR_MASK: Self::Address = SOCKET_BASE + 0x2C; | ||
| 27 | const SOCKET_INTR: Self::Address = SOCKET_BASE + 0x02; | ||
| 28 | |||
| 29 | const SOCKET_MODE_VALUE: u8 = (1 << 2) | (1 << 6); | ||
| 30 | |||
| 31 | const BUF_SIZE: u16 = 0x2000; | ||
| 32 | const AUTO_WRAP: bool = false; | ||
| 33 | |||
| 34 | fn rx_addr(addr: u16) -> Self::Address { | ||
| 35 | RX_BASE + addr | ||
| 36 | } | ||
| 37 | |||
| 38 | fn tx_addr(addr: u16) -> Self::Address { | ||
| 39 | TX_BASE + addr | ||
| 40 | } | ||
| 41 | |||
| 42 | async fn bus_read<SPI: SpiDevice>( | ||
| 43 | spi: &mut SPI, | ||
| 44 | address: Self::Address, | ||
| 45 | data: &mut [u8], | ||
| 46 | ) -> Result<(), SPI::Error> { | ||
| 47 | spi.transaction(&mut [ | ||
| 48 | Operation::Write(&[0x0F, (address >> 8) as u8, address as u8]), | ||
| 49 | Operation::Read(data), | ||
| 50 | ]) | ||
| 51 | .await | ||
| 52 | } | ||
| 53 | |||
| 54 | async fn bus_write<SPI: SpiDevice>(spi: &mut SPI, address: Self::Address, data: &[u8]) -> Result<(), SPI::Error> { | ||
| 55 | spi.transaction(&mut [ | ||
| 56 | Operation::Write(&[0xF0, (address >> 8) as u8, address as u8]), | ||
| 57 | Operation::Write(data), | ||
| 58 | ]) | ||
| 59 | .await | ||
| 60 | } | ||
| 61 | } | ||
diff --git a/embassy-net-wiznet/src/chip/w5500.rs b/embassy-net-wiznet/src/chip/w5500.rs new file mode 100644 index 000000000..61e512946 --- /dev/null +++ b/embassy-net-wiznet/src/chip/w5500.rs | |||
| @@ -0,0 +1,72 @@ | |||
| 1 | use embedded_hal_async::spi::{Operation, SpiDevice}; | ||
| 2 | |||
| 3 | #[repr(u8)] | ||
| 4 | pub enum RegisterBlock { | ||
| 5 | Common = 0x00, | ||
| 6 | Socket0 = 0x01, | ||
| 7 | TxBuf = 0x02, | ||
| 8 | RxBuf = 0x03, | ||
| 9 | } | ||
| 10 | |||
| 11 | pub enum W5500 {} | ||
| 12 | |||
| 13 | impl super::Chip for W5500 {} | ||
| 14 | impl super::sealed::Chip for W5500 { | ||
| 15 | type Address = (RegisterBlock, u16); | ||
| 16 | |||
| 17 | const COMMON_MODE: Self::Address = (RegisterBlock::Common, 0x00); | ||
| 18 | const COMMON_MAC: Self::Address = (RegisterBlock::Common, 0x09); | ||
| 19 | const COMMON_SOCKET_INTR: Self::Address = (RegisterBlock::Common, 0x18); | ||
| 20 | const COMMON_PHY_CFG: Self::Address = (RegisterBlock::Common, 0x2E); | ||
| 21 | |||
| 22 | const SOCKET_MODE: Self::Address = (RegisterBlock::Socket0, 0x00); | ||
| 23 | const SOCKET_COMMAND: Self::Address = (RegisterBlock::Socket0, 0x01); | ||
| 24 | const SOCKET_RXBUF_SIZE: Self::Address = (RegisterBlock::Socket0, 0x1E); | ||
| 25 | const SOCKET_TXBUF_SIZE: Self::Address = (RegisterBlock::Socket0, 0x1F); | ||
| 26 | const SOCKET_TX_FREE_SIZE: Self::Address = (RegisterBlock::Socket0, 0x20); | ||
| 27 | const SOCKET_TX_DATA_WRITE_PTR: Self::Address = (RegisterBlock::Socket0, 0x24); | ||
| 28 | const SOCKET_RECVD_SIZE: Self::Address = (RegisterBlock::Socket0, 0x26); | ||
| 29 | const SOCKET_RX_DATA_READ_PTR: Self::Address = (RegisterBlock::Socket0, 0x28); | ||
| 30 | const SOCKET_INTR_MASK: Self::Address = (RegisterBlock::Socket0, 0x2C); | ||
| 31 | const SOCKET_INTR: Self::Address = (RegisterBlock::Socket0, 0x02); | ||
| 32 | |||
| 33 | const SOCKET_MODE_VALUE: u8 = (1 << 2) | (1 << 7); | ||
| 34 | |||
| 35 | const BUF_SIZE: u16 = 0x4000; | ||
| 36 | const AUTO_WRAP: bool = true; | ||
| 37 | |||
| 38 | fn rx_addr(addr: u16) -> Self::Address { | ||
| 39 | (RegisterBlock::RxBuf, addr) | ||
| 40 | } | ||
| 41 | |||
| 42 | fn tx_addr(addr: u16) -> Self::Address { | ||
| 43 | (RegisterBlock::TxBuf, addr) | ||
| 44 | } | ||
| 45 | |||
| 46 | async fn bus_read<SPI: SpiDevice>( | ||
| 47 | spi: &mut SPI, | ||
| 48 | address: Self::Address, | ||
| 49 | data: &mut [u8], | ||
| 50 | ) -> Result<(), SPI::Error> { | ||
| 51 | let address_phase = address.1.to_be_bytes(); | ||
| 52 | let control_phase = [(address.0 as u8) << 3]; | ||
| 53 | let operations = &mut [ | ||
| 54 | Operation::Write(&address_phase), | ||
| 55 | Operation::Write(&control_phase), | ||
| 56 | Operation::TransferInPlace(data), | ||
| 57 | ]; | ||
| 58 | spi.transaction(operations).await | ||
| 59 | } | ||
| 60 | |||
| 61 | async fn bus_write<SPI: SpiDevice>(spi: &mut SPI, address: Self::Address, data: &[u8]) -> Result<(), SPI::Error> { | ||
| 62 | let address_phase = address.1.to_be_bytes(); | ||
| 63 | let control_phase = [(address.0 as u8) << 3 | 0b0000_0100]; | ||
| 64 | let data_phase = data; | ||
| 65 | let operations = &mut [ | ||
| 66 | Operation::Write(&address_phase[..]), | ||
| 67 | Operation::Write(&control_phase), | ||
| 68 | Operation::Write(&data_phase), | ||
| 69 | ]; | ||
| 70 | spi.transaction(operations).await | ||
| 71 | } | ||
| 72 | } | ||
diff --git a/embassy-net-wiznet/src/device.rs b/embassy-net-wiznet/src/device.rs new file mode 100644 index 000000000..43f9512a3 --- /dev/null +++ b/embassy-net-wiznet/src/device.rs | |||
| @@ -0,0 +1,195 @@ | |||
| 1 | use core::marker::PhantomData; | ||
| 2 | |||
| 3 | use embedded_hal_async::spi::SpiDevice; | ||
| 4 | |||
| 5 | use crate::chip::Chip; | ||
| 6 | |||
| 7 | #[repr(u8)] | ||
| 8 | enum Command { | ||
| 9 | Open = 0x01, | ||
| 10 | Send = 0x20, | ||
| 11 | Receive = 0x40, | ||
| 12 | } | ||
| 13 | |||
| 14 | #[repr(u8)] | ||
| 15 | enum Interrupt { | ||
| 16 | Receive = 0b00100_u8, | ||
| 17 | } | ||
| 18 | |||
| 19 | /// Wiznet chip in MACRAW mode | ||
| 20 | #[derive(Debug)] | ||
| 21 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 22 | pub(crate) struct WiznetDevice<C, SPI> { | ||
| 23 | spi: SPI, | ||
| 24 | _phantom: PhantomData<C>, | ||
| 25 | } | ||
| 26 | |||
| 27 | impl<C: Chip, SPI: SpiDevice> WiznetDevice<C, SPI> { | ||
| 28 | /// Create and initialize the driver | ||
| 29 | pub async fn new(spi: SPI, mac_addr: [u8; 6]) -> Result<Self, SPI::Error> { | ||
| 30 | let mut this = Self { | ||
| 31 | spi, | ||
| 32 | _phantom: PhantomData, | ||
| 33 | }; | ||
| 34 | |||
| 35 | // Reset device | ||
| 36 | this.bus_write(C::COMMON_MODE, &[0x80]).await?; | ||
| 37 | |||
| 38 | // Enable interrupt pin | ||
| 39 | this.bus_write(C::COMMON_SOCKET_INTR, &[0x01]).await?; | ||
| 40 | // Enable receive interrupt | ||
| 41 | this.bus_write(C::SOCKET_INTR_MASK, &[Interrupt::Receive as u8]).await?; | ||
| 42 | |||
| 43 | // Set MAC address | ||
| 44 | this.bus_write(C::COMMON_MAC, &mac_addr).await?; | ||
| 45 | |||
| 46 | // Set the raw socket RX/TX buffer sizes. | ||
| 47 | let buf_kbs = (C::BUF_SIZE / 1024) as u8; | ||
| 48 | this.bus_write(C::SOCKET_TXBUF_SIZE, &[buf_kbs]).await?; | ||
| 49 | this.bus_write(C::SOCKET_RXBUF_SIZE, &[buf_kbs]).await?; | ||
| 50 | |||
| 51 | // MACRAW mode with MAC filtering. | ||
| 52 | this.bus_write(C::SOCKET_MODE, &[C::SOCKET_MODE_VALUE]).await?; | ||
| 53 | this.command(Command::Open).await?; | ||
| 54 | |||
| 55 | Ok(this) | ||
| 56 | } | ||
| 57 | |||
| 58 | async fn bus_read(&mut self, address: C::Address, data: &mut [u8]) -> Result<(), SPI::Error> { | ||
| 59 | C::bus_read(&mut self.spi, address, data).await | ||
| 60 | } | ||
| 61 | |||
| 62 | async fn bus_write(&mut self, address: C::Address, data: &[u8]) -> Result<(), SPI::Error> { | ||
| 63 | C::bus_write(&mut self.spi, address, data).await | ||
| 64 | } | ||
| 65 | |||
| 66 | async fn reset_interrupt(&mut self, code: Interrupt) -> Result<(), SPI::Error> { | ||
| 67 | let data = [code as u8]; | ||
| 68 | self.bus_write(C::SOCKET_INTR, &data).await | ||
| 69 | } | ||
| 70 | |||
| 71 | async fn get_tx_write_ptr(&mut self) -> Result<u16, SPI::Error> { | ||
| 72 | let mut data = [0u8; 2]; | ||
| 73 | self.bus_read(C::SOCKET_TX_DATA_WRITE_PTR, &mut data).await?; | ||
| 74 | Ok(u16::from_be_bytes(data)) | ||
| 75 | } | ||
| 76 | |||
| 77 | async fn set_tx_write_ptr(&mut self, ptr: u16) -> Result<(), SPI::Error> { | ||
| 78 | let data = ptr.to_be_bytes(); | ||
| 79 | self.bus_write(C::SOCKET_TX_DATA_WRITE_PTR, &data).await | ||
| 80 | } | ||
| 81 | |||
| 82 | async fn get_rx_read_ptr(&mut self) -> Result<u16, SPI::Error> { | ||
| 83 | let mut data = [0u8; 2]; | ||
| 84 | self.bus_read(C::SOCKET_RX_DATA_READ_PTR, &mut data).await?; | ||
| 85 | Ok(u16::from_be_bytes(data)) | ||
| 86 | } | ||
| 87 | |||
| 88 | async fn set_rx_read_ptr(&mut self, ptr: u16) -> Result<(), SPI::Error> { | ||
| 89 | let data = ptr.to_be_bytes(); | ||
| 90 | self.bus_write(C::SOCKET_RX_DATA_READ_PTR, &data).await | ||
| 91 | } | ||
| 92 | |||
| 93 | async fn command(&mut self, command: Command) -> Result<(), SPI::Error> { | ||
| 94 | let data = [command as u8]; | ||
| 95 | self.bus_write(C::SOCKET_COMMAND, &data).await | ||
| 96 | } | ||
| 97 | |||
| 98 | async fn get_rx_size(&mut self) -> Result<u16, SPI::Error> { | ||
| 99 | loop { | ||
| 100 | // Wait until two sequential reads are equal | ||
| 101 | let mut res0 = [0u8; 2]; | ||
| 102 | self.bus_read(C::SOCKET_RECVD_SIZE, &mut res0).await?; | ||
| 103 | let mut res1 = [0u8; 2]; | ||
| 104 | self.bus_read(C::SOCKET_RECVD_SIZE, &mut res1).await?; | ||
| 105 | if res0 == res1 { | ||
| 106 | break Ok(u16::from_be_bytes(res0)); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | async fn get_tx_free_size(&mut self) -> Result<u16, SPI::Error> { | ||
| 112 | let mut data = [0; 2]; | ||
| 113 | self.bus_read(C::SOCKET_TX_FREE_SIZE, &mut data).await?; | ||
| 114 | Ok(u16::from_be_bytes(data)) | ||
| 115 | } | ||
| 116 | |||
| 117 | /// Read bytes from the RX buffer. | ||
| 118 | async fn read_bytes(&mut self, read_ptr: &mut u16, buffer: &mut [u8]) -> Result<(), SPI::Error> { | ||
| 119 | if C::AUTO_WRAP { | ||
| 120 | self.bus_read(C::rx_addr(*read_ptr), buffer).await?; | ||
| 121 | } else { | ||
| 122 | let addr = *read_ptr % C::BUF_SIZE; | ||
| 123 | if addr as usize + buffer.len() <= C::BUF_SIZE as usize { | ||
| 124 | self.bus_read(C::rx_addr(addr), buffer).await?; | ||
| 125 | } else { | ||
| 126 | let n = C::BUF_SIZE - addr; | ||
| 127 | self.bus_read(C::rx_addr(addr), &mut buffer[..n as usize]).await?; | ||
| 128 | self.bus_read(C::rx_addr(0), &mut buffer[n as usize..]).await?; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | *read_ptr = (*read_ptr).wrapping_add(buffer.len() as u16); | ||
| 133 | |||
| 134 | Ok(()) | ||
| 135 | } | ||
| 136 | |||
| 137 | /// Read an ethernet frame from the device. Returns the number of bytes read. | ||
| 138 | pub async fn read_frame(&mut self, frame: &mut [u8]) -> Result<usize, SPI::Error> { | ||
| 139 | let rx_size = self.get_rx_size().await? as usize; | ||
| 140 | if rx_size == 0 { | ||
| 141 | return Ok(0); | ||
| 142 | } | ||
| 143 | |||
| 144 | self.reset_interrupt(Interrupt::Receive).await?; | ||
| 145 | |||
| 146 | let mut read_ptr = self.get_rx_read_ptr().await?; | ||
| 147 | |||
| 148 | // First two bytes gives the size of the received ethernet frame | ||
| 149 | let expected_frame_size: usize = { | ||
| 150 | let mut frame_bytes = [0u8; 2]; | ||
| 151 | self.read_bytes(&mut read_ptr, &mut frame_bytes).await?; | ||
| 152 | u16::from_be_bytes(frame_bytes) as usize - 2 | ||
| 153 | }; | ||
| 154 | |||
| 155 | // Read the ethernet frame | ||
| 156 | self.read_bytes(&mut read_ptr, &mut frame[..expected_frame_size]) | ||
| 157 | .await?; | ||
| 158 | |||
| 159 | // Register RX as completed | ||
| 160 | self.set_rx_read_ptr(read_ptr).await?; | ||
| 161 | self.command(Command::Receive).await?; | ||
| 162 | |||
| 163 | Ok(expected_frame_size) | ||
| 164 | } | ||
| 165 | |||
| 166 | /// Write an ethernet frame to the device. Returns number of bytes written | ||
| 167 | pub async fn write_frame(&mut self, frame: &[u8]) -> Result<usize, SPI::Error> { | ||
| 168 | while self.get_tx_free_size().await? < frame.len() as u16 {} | ||
| 169 | let write_ptr = self.get_tx_write_ptr().await?; | ||
| 170 | |||
| 171 | if C::AUTO_WRAP { | ||
| 172 | self.bus_write(C::tx_addr(write_ptr), frame).await?; | ||
| 173 | } else { | ||
| 174 | let addr = write_ptr % C::BUF_SIZE; | ||
| 175 | if addr as usize + frame.len() <= C::BUF_SIZE as usize { | ||
| 176 | self.bus_write(C::tx_addr(addr), frame).await?; | ||
| 177 | } else { | ||
| 178 | let n = C::BUF_SIZE - addr; | ||
| 179 | self.bus_write(C::tx_addr(addr), &frame[..n as usize]).await?; | ||
| 180 | self.bus_write(C::tx_addr(0), &frame[n as usize..]).await?; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | self.set_tx_write_ptr(write_ptr.wrapping_add(frame.len() as u16)) | ||
| 185 | .await?; | ||
| 186 | self.command(Command::Send).await?; | ||
| 187 | Ok(frame.len()) | ||
| 188 | } | ||
| 189 | |||
| 190 | pub async fn is_link_up(&mut self) -> bool { | ||
| 191 | let mut link = [0]; | ||
| 192 | self.bus_read(C::COMMON_PHY_CFG, &mut link).await.ok(); | ||
| 193 | link[0] & 1 == 1 | ||
| 194 | } | ||
| 195 | } | ||
diff --git a/embassy-net-w5500/src/lib.rs b/embassy-net-wiznet/src/lib.rs index 52494b443..3030dfb90 100644 --- a/embassy-net-w5500/src/lib.rs +++ b/embassy-net-wiznet/src/lib.rs | |||
| @@ -1,9 +1,9 @@ | |||
| 1 | //! [`embassy-net`](https://crates.io/crates/embassy-net) driver for the WIZnet W5500 ethernet chip. | 1 | //! [`embassy-net`](https://crates.io/crates/embassy-net) driver for WIZnet ethernet chips. |
| 2 | #![no_std] | 2 | #![no_std] |
| 3 | #![feature(async_fn_in_trait)] | ||
| 3 | 4 | ||
| 5 | pub mod chip; | ||
| 4 | mod device; | 6 | mod device; |
| 5 | mod socket; | ||
| 6 | mod spi; | ||
| 7 | 7 | ||
| 8 | use embassy_futures::select::{select, Either}; | 8 | use embassy_futures::select::{select, Either}; |
| 9 | use embassy_net_driver_channel as ch; | 9 | use embassy_net_driver_channel as ch; |
| @@ -13,10 +13,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; |
| 15 | 15 | ||
| 16 | use crate::device::W5500; | 16 | use crate::chip::Chip; |
| 17 | use crate::device::WiznetDevice; | ||
| 18 | |||
| 17 | const MTU: usize = 1514; | 19 | const MTU: usize = 1514; |
| 18 | 20 | ||
| 19 | /// Type alias for the embassy-net driver for W5500 | 21 | /// Type alias for the embassy-net driver. |
| 20 | pub type Device<'d> = embassy_net_driver_channel::Device<'d, MTU>; | 22 | pub type Device<'d> = embassy_net_driver_channel::Device<'d, MTU>; |
| 21 | 23 | ||
| 22 | /// Internal state for the embassy-net integration. | 24 | /// Internal state for the embassy-net integration. |
| @@ -33,18 +35,18 @@ impl<const N_RX: usize, const N_TX: usize> State<N_RX, N_TX> { | |||
| 33 | } | 35 | } |
| 34 | } | 36 | } |
| 35 | 37 | ||
| 36 | /// Background runner for the W5500. | 38 | /// Background runner for the driver. |
| 37 | /// | 39 | /// |
| 38 | /// You must call `.run()` in a background task for the W5500 to operate. | 40 | /// You must call `.run()` in a background task for the driver to operate. |
| 39 | pub struct Runner<'d, SPI: SpiDevice, INT: Wait, RST: OutputPin> { | 41 | pub struct Runner<'d, C: Chip, SPI: SpiDevice, INT: Wait, RST: OutputPin> { |
| 40 | mac: W5500<SPI>, | 42 | mac: WiznetDevice<C, SPI>, |
| 41 | ch: ch::Runner<'d, MTU>, | 43 | ch: ch::Runner<'d, MTU>, |
| 42 | int: INT, | 44 | int: INT, |
| 43 | _reset: RST, | 45 | _reset: RST, |
| 44 | } | 46 | } |
| 45 | 47 | ||
| 46 | /// You must call this in a background task for the W5500 to operate. | 48 | /// You must call this in a background task for the driver to operate. |
| 47 | impl<'d, SPI: SpiDevice, INT: Wait, RST: OutputPin> Runner<'d, SPI, INT, RST> { | 49 | impl<'d, C: Chip, SPI: SpiDevice, INT: Wait, RST: OutputPin> Runner<'d, C, SPI, INT, RST> { |
| 48 | pub async fn run(mut self) -> ! { | 50 | pub async fn run(mut self) -> ! { |
| 49 | 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(); |
| 50 | loop { | 52 | loop { |
| @@ -78,23 +80,29 @@ impl<'d, SPI: SpiDevice, INT: Wait, RST: OutputPin> Runner<'d, SPI, INT, RST> { | |||
| 78 | } | 80 | } |
| 79 | } | 81 | } |
| 80 | 82 | ||
| 81 | /// Obtain a driver for using the W5500 with [`embassy-net`](https://crates.io/crates/embassy-net). | 83 | /// Create a Wiznet ethernet chip driver for [`embassy-net`](https://crates.io/crates/embassy-net). |
| 82 | pub async fn new<'a, const N_RX: usize, const N_TX: usize, SPI: SpiDevice, INT: Wait, RST: OutputPin>( | 84 | /// |
| 85 | /// This returns two structs: | ||
| 86 | /// - a `Device` that you must pass to the `embassy-net` stack. | ||
| 87 | /// - a `Runner`. You must call `.run()` on it in a background task. | ||
| 88 | pub async fn new<'a, const N_RX: usize, const N_TX: usize, C: Chip, SPI: SpiDevice, INT: Wait, RST: OutputPin>( | ||
| 83 | mac_addr: [u8; 6], | 89 | mac_addr: [u8; 6], |
| 84 | state: &'a mut State<N_RX, N_TX>, | 90 | state: &'a mut State<N_RX, N_TX>, |
| 85 | spi_dev: SPI, | 91 | spi_dev: SPI, |
| 86 | int: INT, | 92 | int: INT, |
| 87 | mut reset: RST, | 93 | mut reset: RST, |
| 88 | ) -> (Device<'a>, Runner<'a, SPI, INT, RST>) { | 94 | ) -> (Device<'a>, Runner<'a, C, SPI, INT, RST>) { |
| 89 | // Reset the W5500. | 95 | // Reset the chip. |
| 90 | reset.set_low().ok(); | 96 | reset.set_low().ok(); |
| 91 | // Ensure the reset is registered. | 97 | // Ensure the reset is registered. |
| 92 | Timer::after(Duration::from_millis(1)).await; | 98 | Timer::after(Duration::from_millis(1)).await; |
| 93 | reset.set_high().ok(); | 99 | reset.set_high().ok(); |
| 94 | // Wait for the W5500 to achieve PLL lock. | ||
| 95 | Timer::after(Duration::from_millis(2)).await; | ||
| 96 | 100 | ||
| 97 | let mac = W5500::new(spi_dev, mac_addr).await.unwrap(); | 101 | // Wait for PLL lock. Some chips are slower than others. |
| 102 | // Slowest is w5100s which is 100ms, so let's just wait that. | ||
| 103 | Timer::after(Duration::from_millis(100)).await; | ||
| 104 | |||
| 105 | let mac = WiznetDevice::new(spi_dev, mac_addr).await.unwrap(); | ||
| 98 | 106 | ||
| 99 | let (runner, device) = ch::new(&mut state.ch_state, ch::driver::HardwareAddress::Ethernet(mac_addr)); | 107 | let (runner, device) = ch::new(&mut state.ch_state, ch::driver::HardwareAddress::Ethernet(mac_addr)); |
| 100 | ( | 108 | ( |
diff --git a/embassy-net/README.md b/embassy-net/README.md index 811321ca4..7bb2283c4 100644 --- a/embassy-net/README.md +++ b/embassy-net/README.md | |||
| @@ -22,7 +22,7 @@ unimplemented features of the network protocols. | |||
| 22 | - [`cyw43`](https://github.com/embassy-rs/embassy/tree/main/cyw43) for WiFi on CYW43xx chips, used in the Raspberry Pi Pico W | 22 | - [`cyw43`](https://github.com/embassy-rs/embassy/tree/main/cyw43) for WiFi on CYW43xx chips, used in the Raspberry Pi Pico W |
| 23 | - [`embassy-usb`](https://github.com/embassy-rs/embassy/tree/main/embassy-usb) for Ethernet-over-USB (CDC NCM) support. | 23 | - [`embassy-usb`](https://github.com/embassy-rs/embassy/tree/main/embassy-usb) for Ethernet-over-USB (CDC NCM) support. |
| 24 | - [`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). | 24 | - [`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). |
| 25 | - [`embassy-net-w5500`](https://github.com/embassy-rs/embassy/tree/main/embassy-net-w5500) for Wiznet W5500 SPI Ethernet MAC+PHY chip. | 25 | - [`embassy-net-wiznet`](https://github.com/embassy-rs/embassy/tree/main/embassy-net-wiznet) for Wiznet SPI Ethernet MAC+PHY chips (W5100S, W5500) |
| 26 | - [`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. | 26 | - [`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. |
| 27 | 27 | ||
| 28 | ## Examples | 28 | ## Examples |
diff --git a/embassy-rp/src/dma.rs b/embassy-rp/src/dma.rs index c8f741804..45ca21a75 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>( | |||
| 76 | ) | 76 | ) |
| 77 | } | 77 | } |
| 78 | 78 | ||
| 79 | static DUMMY: u32 = 0; | 79 | // static mut so that this is allocated in RAM. |
| 80 | static mut DUMMY: u32 = 0; | ||
| 80 | 81 | ||
| 81 | pub unsafe fn write_repeated<'a, C: Channel, W: Word>( | 82 | pub unsafe fn write_repeated<'a, C: Channel, W: Word>( |
| 82 | ch: impl Peripheral<P = C> + 'a, | 83 | ch: impl Peripheral<P = C> + 'a, |
| @@ -86,7 +87,7 @@ pub unsafe fn write_repeated<'a, C: Channel, W: Word>( | |||
| 86 | ) -> Transfer<'a, C> { | 87 | ) -> Transfer<'a, C> { |
| 87 | copy_inner( | 88 | copy_inner( |
| 88 | ch, | 89 | ch, |
| 89 | &DUMMY as *const u32, | 90 | &mut DUMMY as *const u32, |
| 90 | to as *mut u32, | 91 | to as *mut u32, |
| 91 | len, | 92 | len, |
| 92 | W::size(), | 93 | W::size(), |
diff --git a/examples/rp/Cargo.toml b/examples/rp/Cargo.toml index d83e370df..6742ce7ef 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 | |||
| 13 | embassy-rp = { version = "0.1.0", path = "../../embassy-rp", features = ["defmt", "unstable-traits", "nightly", "unstable-pac", "time-driver", "critical-section-impl"] } | 13 | embassy-rp = { version = "0.1.0", path = "../../embassy-rp", features = ["defmt", "unstable-traits", "nightly", "unstable-pac", "time-driver", "critical-section-impl"] } |
| 14 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } | 14 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } |
| 15 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "udp", "dhcpv4", "medium-ethernet"] } | 15 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "udp", "dhcpv4", "medium-ethernet"] } |
| 16 | embassy-net-w5500 = { version = "0.1.0", path = "../../embassy-net-w5500", features = ["defmt"] } | 16 | embassy-net-wiznet = { version = "0.1.0", path = "../../embassy-net-wiznet", features = ["defmt"] } |
| 17 | embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } | 17 | embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } |
| 18 | embassy-usb-logger = { version = "0.1.0", path = "../../embassy-usb-logger" } | 18 | embassy-usb-logger = { version = "0.1.0", path = "../../embassy-usb-logger" } |
| 19 | embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["time", "defmt"] } | 19 | 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 9f800d0d9..d4af87642 100644 --- a/examples/rp/src/bin/ethernet_w5500_multisocket.rs +++ b/examples/rp/src/bin/ethernet_w5500_multisocket.rs | |||
| @@ -10,7 +10,8 @@ use defmt::*; | |||
| 10 | use embassy_executor::Spawner; | 10 | use embassy_executor::Spawner; |
| 11 | use embassy_futures::yield_now; | 11 | use embassy_futures::yield_now; |
| 12 | use embassy_net::{Stack, StackResources}; | 12 | use embassy_net::{Stack, StackResources}; |
| 13 | use embassy_net_w5500::*; | 13 | use embassy_net_wiznet::chip::W5500; |
| 14 | use embassy_net_wiznet::*; | ||
| 14 | use embassy_rp::clocks::RoscRng; | 15 | use embassy_rp::clocks::RoscRng; |
| 15 | use embassy_rp::gpio::{Input, Level, Output, Pull}; | 16 | use embassy_rp::gpio::{Input, Level, Output, Pull}; |
| 16 | use embassy_rp::peripherals::{PIN_17, PIN_20, PIN_21, SPI0}; | 17 | use embassy_rp::peripherals::{PIN_17, PIN_20, PIN_21, SPI0}; |
| @@ -26,6 +27,7 @@ use {defmt_rtt as _, panic_probe as _}; | |||
| 26 | async fn ethernet_task( | 27 | async fn ethernet_task( |
| 27 | runner: Runner< | 28 | runner: Runner< |
| 28 | 'static, | 29 | 'static, |
| 30 | W5500, | ||
| 29 | ExclusiveDevice<Spi<'static, SPI0, Async>, Output<'static, PIN_17>, Delay>, | 31 | ExclusiveDevice<Spi<'static, SPI0, Async>, Output<'static, PIN_17>, Delay>, |
| 30 | Input<'static, PIN_21>, | 32 | Input<'static, PIN_21>, |
| 31 | Output<'static, PIN_20>, | 33 | Output<'static, PIN_20>, |
| @@ -54,7 +56,7 @@ async fn main(spawner: Spawner) { | |||
| 54 | 56 | ||
| 55 | let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00]; | 57 | let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00]; |
| 56 | let state = make_static!(State::<8, 8>::new()); | 58 | let state = make_static!(State::<8, 8>::new()); |
| 57 | let (device, runner) = embassy_net_w5500::new( | 59 | let (device, runner) = embassy_net_wiznet::new( |
| 58 | mac_addr, | 60 | mac_addr, |
| 59 | state, | 61 | state, |
| 60 | ExclusiveDevice::new(spi, cs, Delay), | 62 | 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 fee84b613..78340492e 100644 --- a/examples/rp/src/bin/ethernet_w5500_tcp_client.rs +++ b/examples/rp/src/bin/ethernet_w5500_tcp_client.rs | |||
| @@ -12,7 +12,8 @@ use defmt::*; | |||
| 12 | use embassy_executor::Spawner; | 12 | use embassy_executor::Spawner; |
| 13 | use embassy_futures::yield_now; | 13 | use embassy_futures::yield_now; |
| 14 | use embassy_net::{Stack, StackResources}; | 14 | use embassy_net::{Stack, StackResources}; |
| 15 | use embassy_net_w5500::*; | 15 | use embassy_net_wiznet::chip::W5500; |
| 16 | use embassy_net_wiznet::*; | ||
| 16 | use embassy_rp::clocks::RoscRng; | 17 | use embassy_rp::clocks::RoscRng; |
| 17 | use embassy_rp::gpio::{Input, Level, Output, Pull}; | 18 | use embassy_rp::gpio::{Input, Level, Output, Pull}; |
| 18 | use embassy_rp::peripherals::{PIN_17, PIN_20, PIN_21, SPI0}; | 19 | use embassy_rp::peripherals::{PIN_17, PIN_20, PIN_21, SPI0}; |
| @@ -28,6 +29,7 @@ use {defmt_rtt as _, panic_probe as _}; | |||
| 28 | async fn ethernet_task( | 29 | async fn ethernet_task( |
| 29 | runner: Runner< | 30 | runner: Runner< |
| 30 | 'static, | 31 | 'static, |
| 32 | W5500, | ||
| 31 | ExclusiveDevice<Spi<'static, SPI0, Async>, Output<'static, PIN_17>, Delay>, | 33 | ExclusiveDevice<Spi<'static, SPI0, Async>, Output<'static, PIN_17>, Delay>, |
| 32 | Input<'static, PIN_21>, | 34 | Input<'static, PIN_21>, |
| 33 | Output<'static, PIN_20>, | 35 | Output<'static, PIN_20>, |
| @@ -57,7 +59,7 @@ async fn main(spawner: Spawner) { | |||
| 57 | 59 | ||
| 58 | let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00]; | 60 | let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00]; |
| 59 | let state = make_static!(State::<8, 8>::new()); | 61 | let state = make_static!(State::<8, 8>::new()); |
| 60 | let (device, runner) = embassy_net_w5500::new( | 62 | let (device, runner) = embassy_net_wiznet::new( |
| 61 | mac_addr, | 63 | mac_addr, |
| 62 | state, | 64 | state, |
| 63 | ExclusiveDevice::new(spi, cs, Delay), | 65 | 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 024574267..e6d132740 100644 --- a/examples/rp/src/bin/ethernet_w5500_tcp_server.rs +++ b/examples/rp/src/bin/ethernet_w5500_tcp_server.rs | |||
| @@ -11,7 +11,8 @@ use defmt::*; | |||
| 11 | use embassy_executor::Spawner; | 11 | use embassy_executor::Spawner; |
| 12 | use embassy_futures::yield_now; | 12 | use embassy_futures::yield_now; |
| 13 | use embassy_net::{Stack, StackResources}; | 13 | use embassy_net::{Stack, StackResources}; |
| 14 | use embassy_net_w5500::*; | 14 | use embassy_net_wiznet::chip::W5500; |
| 15 | use embassy_net_wiznet::*; | ||
| 15 | use embassy_rp::clocks::RoscRng; | 16 | use embassy_rp::clocks::RoscRng; |
| 16 | use embassy_rp::gpio::{Input, Level, Output, Pull}; | 17 | use embassy_rp::gpio::{Input, Level, Output, Pull}; |
| 17 | use embassy_rp::peripherals::{PIN_17, PIN_20, PIN_21, SPI0}; | 18 | use embassy_rp::peripherals::{PIN_17, PIN_20, PIN_21, SPI0}; |
| @@ -22,10 +23,12 @@ use embedded_io_async::Write; | |||
| 22 | use rand::RngCore; | 23 | use rand::RngCore; |
| 23 | use static_cell::make_static; | 24 | use static_cell::make_static; |
| 24 | use {defmt_rtt as _, panic_probe as _}; | 25 | use {defmt_rtt as _, panic_probe as _}; |
| 26 | |||
| 25 | #[embassy_executor::task] | 27 | #[embassy_executor::task] |
| 26 | async fn ethernet_task( | 28 | async fn ethernet_task( |
| 27 | runner: Runner< | 29 | runner: Runner< |
| 28 | 'static, | 30 | 'static, |
| 31 | W5500, | ||
| 29 | ExclusiveDevice<Spi<'static, SPI0, Async>, Output<'static, PIN_17>, Delay>, | 32 | ExclusiveDevice<Spi<'static, SPI0, Async>, Output<'static, PIN_17>, Delay>, |
| 30 | Input<'static, PIN_21>, | 33 | Input<'static, PIN_21>, |
| 31 | Output<'static, PIN_20>, | 34 | Output<'static, PIN_20>, |
| @@ -55,7 +58,7 @@ async fn main(spawner: Spawner) { | |||
| 55 | 58 | ||
| 56 | let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00]; | 59 | let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00]; |
| 57 | let state = make_static!(State::<8, 8>::new()); | 60 | let state = make_static!(State::<8, 8>::new()); |
| 58 | let (device, runner) = embassy_net_w5500::new( | 61 | let (device, runner) = embassy_net_wiznet::new( |
| 59 | mac_addr, | 62 | mac_addr, |
| 60 | state, | 63 | state, |
| 61 | ExclusiveDevice::new(spi, cs, Delay), | 64 | 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 038432b17..b546714df 100644 --- a/examples/rp/src/bin/ethernet_w5500_udp.rs +++ b/examples/rp/src/bin/ethernet_w5500_udp.rs | |||
| @@ -11,7 +11,8 @@ use embassy_executor::Spawner; | |||
| 11 | use embassy_futures::yield_now; | 11 | use embassy_futures::yield_now; |
| 12 | use embassy_net::udp::{PacketMetadata, UdpSocket}; | 12 | use embassy_net::udp::{PacketMetadata, UdpSocket}; |
| 13 | use embassy_net::{Stack, StackResources}; | 13 | use embassy_net::{Stack, StackResources}; |
| 14 | use embassy_net_w5500::*; | 14 | use embassy_net_wiznet::chip::W5500; |
| 15 | use embassy_net_wiznet::*; | ||
| 15 | use embassy_rp::clocks::RoscRng; | 16 | use embassy_rp::clocks::RoscRng; |
| 16 | use embassy_rp::gpio::{Input, Level, Output, Pull}; | 17 | use embassy_rp::gpio::{Input, Level, Output, Pull}; |
| 17 | use embassy_rp::peripherals::{PIN_17, PIN_20, PIN_21, SPI0}; | 18 | use embassy_rp::peripherals::{PIN_17, PIN_20, PIN_21, SPI0}; |
| @@ -21,10 +22,12 @@ use embedded_hal_async::spi::ExclusiveDevice; | |||
| 21 | use rand::RngCore; | 22 | use rand::RngCore; |
| 22 | use static_cell::make_static; | 23 | use static_cell::make_static; |
| 23 | use {defmt_rtt as _, panic_probe as _}; | 24 | use {defmt_rtt as _, panic_probe as _}; |
| 25 | |||
| 24 | #[embassy_executor::task] | 26 | #[embassy_executor::task] |
| 25 | async fn ethernet_task( | 27 | async fn ethernet_task( |
| 26 | runner: Runner< | 28 | runner: Runner< |
| 27 | 'static, | 29 | 'static, |
| 30 | W5500, | ||
| 28 | ExclusiveDevice<Spi<'static, SPI0, Async>, Output<'static, PIN_17>, Delay>, | 31 | ExclusiveDevice<Spi<'static, SPI0, Async>, Output<'static, PIN_17>, Delay>, |
| 29 | Input<'static, PIN_21>, | 32 | Input<'static, PIN_21>, |
| 30 | Output<'static, PIN_20>, | 33 | Output<'static, PIN_20>, |
| @@ -53,7 +56,7 @@ async fn main(spawner: Spawner) { | |||
| 53 | 56 | ||
| 54 | let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00]; | 57 | let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00]; |
| 55 | let state = make_static!(State::<8, 8>::new()); | 58 | let state = make_static!(State::<8, 8>::new()); |
| 56 | let (device, runner) = embassy_net_w5500::new( | 59 | let (device, runner) = embassy_net_wiznet::new( |
| 57 | mac_addr, | 60 | mac_addr, |
| 58 | state, | 61 | state, |
| 59 | ExclusiveDevice::new(spi, cs, Delay), | 62 | ExclusiveDevice::new(spi, cs, Delay), |
