diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-08-15 17:11:24 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2023-08-15 22:52:37 +0200 |
| commit | c367b84ee53b9d7a31f442f962c939f822aadb0d (patch) | |
| tree | ba9bb9de172db5f69b7f84b27e519b03573dcf02 | |
| parent | 46f671ae420f27b527edf03756d1da76d86216c7 (diff) | |
net-w5500: add Address type.
| -rw-r--r-- | embassy-net-w5500/src/device.rs | 41 | ||||
| -rw-r--r-- | embassy-net-w5500/src/socket.rs | 42 | ||||
| -rw-r--r-- | 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 4933625e2..1793baaa3 100644 --- a/embassy-net-w5500/src/device.rs +++ b/embassy-net-w5500/src/device.rs | |||
| @@ -1,12 +1,12 @@ | |||
| 1 | use embedded_hal_async::spi::SpiDevice; | 1 | use embedded_hal_async::spi::SpiDevice; |
| 2 | 2 | ||
| 3 | use crate::socket; | 3 | use crate::socket; |
| 4 | use crate::spi::SpiInterface; | 4 | use crate::spi::{Address, SpiInterface}; |
| 5 | 5 | ||
| 6 | pub const MODE: u16 = 0x00; | 6 | pub const MODE: Address = (RegisterBlock::Common, 0x00); |
| 7 | pub const MAC: u16 = 0x09; | 7 | pub const MAC: Address = (RegisterBlock::Common, 0x09); |
| 8 | pub const SOCKET_INTR: u16 = 0x18; | 8 | pub const SOCKET_INTR: Address = (RegisterBlock::Common, 0x18); |
| 9 | pub const PHY_CFG: u16 = 0x2E; | 9 | pub const PHY_CFG: Address = (RegisterBlock::Common, 0x2E); |
| 10 | 10 | ||
| 11 | #[repr(u8)] | 11 | #[repr(u8)] |
| 12 | pub enum RegisterBlock { | 12 | pub enum RegisterBlock { |
| @@ -28,30 +28,24 @@ impl<SPI: SpiDevice> W5500<SPI> { | |||
| 28 | pub async fn new(spi: SPI, mac_addr: [u8; 6]) -> Result<W5500<SPI>, SPI::Error> { | 28 | pub async fn new(spi: SPI, mac_addr: [u8; 6]) -> Result<W5500<SPI>, SPI::Error> { |
| 29 | let mut bus = SpiInterface(spi); | 29 | let mut bus = SpiInterface(spi); |
| 30 | // Reset device | 30 | // Reset device |
| 31 | bus.write_frame(RegisterBlock::Common, MODE, &[0x80]).await?; | 31 | bus.write_frame(MODE, &[0x80]).await?; |
| 32 | 32 | ||
| 33 | // Enable interrupt pin | 33 | // Enable interrupt pin |
| 34 | bus.write_frame(RegisterBlock::Common, SOCKET_INTR, &[0x01]).await?; | 34 | bus.write_frame(SOCKET_INTR, &[0x01]).await?; |
| 35 | // Enable receive interrupt | 35 | // Enable receive interrupt |
| 36 | bus.write_frame( | 36 | bus.write_frame(socket::SOCKET_INTR_MASK, &[socket::Interrupt::Receive as u8]) |
| 37 | RegisterBlock::Socket0, | 37 | .await?; |
| 38 | socket::SOCKET_INTR_MASK, | ||
| 39 | &[socket::Interrupt::Receive as u8], | ||
| 40 | ) | ||
| 41 | .await?; | ||
| 42 | 38 | ||
| 43 | // Set MAC address | 39 | // Set MAC address |
| 44 | bus.write_frame(RegisterBlock::Common, MAC, &mac_addr).await?; | 40 | bus.write_frame(MAC, &mac_addr).await?; |
| 45 | 41 | ||
| 46 | // Set the raw socket RX/TX buffer sizes to 16KB | 42 | // Set the raw socket RX/TX buffer sizes to 16KB |
| 47 | bus.write_frame(RegisterBlock::Socket0, socket::TXBUF_SIZE, &[16]) | 43 | bus.write_frame(socket::TXBUF_SIZE, &[16]).await?; |
| 48 | .await?; | 44 | bus.write_frame(socket::RXBUF_SIZE, &[16]).await?; |
| 49 | bus.write_frame(RegisterBlock::Socket0, socket::RXBUF_SIZE, &[16]) | ||
| 50 | .await?; | ||
| 51 | 45 | ||
| 52 | // MACRAW mode with MAC filtering. | 46 | // MACRAW mode with MAC filtering. |
| 53 | let mode: u8 = (1 << 2) | (1 << 7); | 47 | let mode: u8 = (1 << 2) | (1 << 7); |
| 54 | bus.write_frame(RegisterBlock::Socket0, socket::MODE, &[mode]).await?; | 48 | bus.write_frame(socket::MODE, &[mode]).await?; |
| 55 | socket::command(&mut bus, socket::Command::Open).await?; | 49 | socket::command(&mut bus, socket::Command::Open).await?; |
| 56 | 50 | ||
| 57 | Ok(Self { bus }) | 51 | Ok(Self { bus }) |
| @@ -59,7 +53,7 @@ impl<SPI: SpiDevice> W5500<SPI> { | |||
| 59 | 53 | ||
| 60 | /// Read bytes from the RX buffer. Returns the number of bytes read. | 54 | /// 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> { | 55 | 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?; | 56 | self.bus.read_frame((RegisterBlock::RxBuf, *read_ptr), buffer).await?; |
| 63 | *read_ptr = (*read_ptr).wrapping_add(buffer.len() as u16); | 57 | *read_ptr = (*read_ptr).wrapping_add(buffer.len() as u16); |
| 64 | 58 | ||
| 65 | Ok(()) | 59 | Ok(()) |
| @@ -98,7 +92,7 @@ impl<SPI: SpiDevice> W5500<SPI> { | |||
| 98 | pub async fn write_frame(&mut self, frame: &[u8]) -> Result<usize, SPI::Error> { | 92 | 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 {} | 93 | 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?; | 94 | let write_ptr = socket::get_tx_write_ptr(&mut self.bus).await?; |
| 101 | self.bus.write_frame(RegisterBlock::TxBuf, write_ptr, frame).await?; | 95 | 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?; | 96 | 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?; | 97 | socket::command(&mut self.bus, socket::Command::Send).await?; |
| 104 | Ok(frame.len()) | 98 | Ok(frame.len()) |
| @@ -106,10 +100,7 @@ impl<SPI: SpiDevice> W5500<SPI> { | |||
| 106 | 100 | ||
| 107 | pub async fn is_link_up(&mut self) -> bool { | 101 | pub async fn is_link_up(&mut self) -> bool { |
| 108 | let mut link = [0]; | 102 | let mut link = [0]; |
| 109 | self.bus | 103 | self.bus.read_frame(PHY_CFG, &mut link).await.ok(); |
| 110 | .read_frame(RegisterBlock::Common, PHY_CFG, &mut link) | ||
| 111 | .await | ||
| 112 | .ok(); | ||
| 113 | link[0] & 1 == 1 | 104 | link[0] & 1 == 1 |
| 114 | } | 105 | } |
| 115 | } | 106 | } |
diff --git a/embassy-net-w5500/src/socket.rs b/embassy-net-w5500/src/socket.rs index 3d65583c1..1c96cca1f 100644 --- a/embassy-net-w5500/src/socket.rs +++ b/embassy-net-w5500/src/socket.rs | |||
| @@ -1,17 +1,18 @@ | |||
| 1 | use embedded_hal_async::spi::SpiDevice; | 1 | use embedded_hal_async::spi::SpiDevice; |
| 2 | 2 | ||
| 3 | use crate::device::RegisterBlock; | 3 | use crate::device::RegisterBlock; |
| 4 | use crate::spi::SpiInterface; | 4 | use crate::spi::{Address, SpiInterface}; |
| 5 | 5 | ||
| 6 | pub const MODE: u16 = 0x00; | 6 | pub const MODE: Address = (RegisterBlock::Socket0, 0x00); |
| 7 | pub const COMMAND: u16 = 0x01; | 7 | pub const COMMAND: Address = (RegisterBlock::Socket0, 0x01); |
| 8 | pub const RXBUF_SIZE: u16 = 0x1E; | 8 | pub const RXBUF_SIZE: Address = (RegisterBlock::Socket0, 0x1E); |
| 9 | pub const TXBUF_SIZE: u16 = 0x1F; | 9 | pub const TXBUF_SIZE: Address = (RegisterBlock::Socket0, 0x1F); |
| 10 | pub const TX_FREE_SIZE: u16 = 0x20; | 10 | pub const TX_FREE_SIZE: Address = (RegisterBlock::Socket0, 0x20); |
| 11 | pub const TX_DATA_WRITE_PTR: u16 = 0x24; | 11 | pub const TX_DATA_WRITE_PTR: Address = (RegisterBlock::Socket0, 0x24); |
| 12 | pub const RECVD_SIZE: u16 = 0x26; | 12 | pub const RECVD_SIZE: Address = (RegisterBlock::Socket0, 0x26); |
| 13 | pub const RX_DATA_READ_PTR: u16 = 0x28; | 13 | pub const RX_DATA_READ_PTR: Address = (RegisterBlock::Socket0, 0x28); |
| 14 | pub const SOCKET_INTR_MASK: u16 = 0x2C; | 14 | pub const SOCKET_INTR_MASK: Address = (RegisterBlock::Socket0, 0x2C); |
| 15 | pub const INTR: Address = (RegisterBlock::Socket0, 0x02); | ||
| 15 | 16 | ||
| 16 | #[repr(u8)] | 17 | #[repr(u8)] |
| 17 | pub enum Command { | 18 | pub enum Command { |
| @@ -20,7 +21,6 @@ pub enum Command { | |||
| 20 | Receive = 0x40, | 21 | Receive = 0x40, |
| 21 | } | 22 | } |
| 22 | 23 | ||
| 23 | pub const INTR: u16 = 0x02; | ||
| 24 | #[repr(u8)] | 24 | #[repr(u8)] |
| 25 | pub enum Interrupt { | 25 | pub enum Interrupt { |
| 26 | Receive = 0b00100_u8, | 26 | Receive = 0b00100_u8, |
| @@ -28,45 +28,43 @@ pub enum Interrupt { | |||
| 28 | 28 | ||
| 29 | pub async fn reset_interrupt<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>, code: Interrupt) -> Result<(), SPI::Error> { | 29 | pub async fn reset_interrupt<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>, code: Interrupt) -> Result<(), SPI::Error> { |
| 30 | let data = [code as u8]; | 30 | let data = [code as u8]; |
| 31 | bus.write_frame(RegisterBlock::Socket0, INTR, &data).await | 31 | bus.write_frame(INTR, &data).await |
| 32 | } | 32 | } |
| 33 | 33 | ||
| 34 | pub async fn get_tx_write_ptr<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result<u16, SPI::Error> { | 34 | pub async fn get_tx_write_ptr<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result<u16, SPI::Error> { |
| 35 | let mut data = [0u8; 2]; | 35 | let mut data = [0u8; 2]; |
| 36 | bus.read_frame(RegisterBlock::Socket0, TX_DATA_WRITE_PTR, &mut data) | 36 | bus.read_frame(TX_DATA_WRITE_PTR, &mut data).await?; |
| 37 | .await?; | ||
| 38 | Ok(u16::from_be_bytes(data)) | 37 | Ok(u16::from_be_bytes(data)) |
| 39 | } | 38 | } |
| 40 | 39 | ||
| 41 | pub async fn set_tx_write_ptr<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>, ptr: u16) -> Result<(), SPI::Error> { | 40 | 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(); | 41 | let data = ptr.to_be_bytes(); |
| 43 | bus.write_frame(RegisterBlock::Socket0, TX_DATA_WRITE_PTR, &data).await | 42 | bus.write_frame(TX_DATA_WRITE_PTR, &data).await |
| 44 | } | 43 | } |
| 45 | 44 | ||
| 46 | pub async fn get_rx_read_ptr<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result<u16, SPI::Error> { | 45 | pub async fn get_rx_read_ptr<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result<u16, SPI::Error> { |
| 47 | let mut data = [0u8; 2]; | 46 | let mut data = [0u8; 2]; |
| 48 | bus.read_frame(RegisterBlock::Socket0, RX_DATA_READ_PTR, &mut data) | 47 | bus.read_frame(RX_DATA_READ_PTR, &mut data).await?; |
| 49 | .await?; | ||
| 50 | Ok(u16::from_be_bytes(data)) | 48 | Ok(u16::from_be_bytes(data)) |
| 51 | } | 49 | } |
| 52 | 50 | ||
| 53 | pub async fn set_rx_read_ptr<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>, ptr: u16) -> Result<(), SPI::Error> { | 51 | 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(); | 52 | let data = ptr.to_be_bytes(); |
| 55 | bus.write_frame(RegisterBlock::Socket0, RX_DATA_READ_PTR, &data).await | 53 | bus.write_frame(RX_DATA_READ_PTR, &data).await |
| 56 | } | 54 | } |
| 57 | 55 | ||
| 58 | pub async fn command<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>, command: Command) -> Result<(), SPI::Error> { | 56 | pub async fn command<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>, command: Command) -> Result<(), SPI::Error> { |
| 59 | let data = [command as u8]; | 57 | let data = [command as u8]; |
| 60 | bus.write_frame(RegisterBlock::Socket0, COMMAND, &data).await | 58 | bus.write_frame(COMMAND, &data).await |
| 61 | } | 59 | } |
| 62 | 60 | ||
| 63 | pub async fn get_rx_size<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result<u16, SPI::Error> { | 61 | pub async fn get_rx_size<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result<u16, SPI::Error> { |
| 64 | loop { | 62 | loop { |
| 65 | // Wait until two sequential reads are equal | 63 | // Wait until two sequential reads are equal |
| 66 | let mut res0 = [0u8; 2]; | 64 | let mut res0 = [0u8; 2]; |
| 67 | bus.read_frame(RegisterBlock::Socket0, RECVD_SIZE, &mut res0).await?; | 65 | bus.read_frame(RECVD_SIZE, &mut res0).await?; |
| 68 | let mut res1 = [0u8; 2]; | 66 | let mut res1 = [0u8; 2]; |
| 69 | bus.read_frame(RegisterBlock::Socket0, RECVD_SIZE, &mut res1).await?; | 67 | bus.read_frame(RECVD_SIZE, &mut res1).await?; |
| 70 | if res0 == res1 { | 68 | if res0 == res1 { |
| 71 | break Ok(u16::from_be_bytes(res0)); | 69 | break Ok(u16::from_be_bytes(res0)); |
| 72 | } | 70 | } |
| @@ -75,6 +73,6 @@ pub async fn get_rx_size<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result< | |||
| 75 | 73 | ||
| 76 | pub async fn get_tx_free_size<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result<u16, SPI::Error> { | 74 | pub async fn get_tx_free_size<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result<u16, SPI::Error> { |
| 77 | let mut data = [0; 2]; | 75 | let mut data = [0; 2]; |
| 78 | bus.read_frame(RegisterBlock::Socket0, TX_FREE_SIZE, &mut data).await?; | 76 | bus.read_frame(TX_FREE_SIZE, &mut data).await?; |
| 79 | Ok(u16::from_be_bytes(data)) | 77 | Ok(u16::from_be_bytes(data)) |
| 80 | } | 78 | } |
diff --git a/embassy-net-w5500/src/spi.rs b/embassy-net-w5500/src/spi.rs index 07749d6be..316c6521e 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}; | |||
| 2 | 2 | ||
| 3 | use crate::device::RegisterBlock; | 3 | use crate::device::RegisterBlock; |
| 4 | 4 | ||
| 5 | pub type Address = (RegisterBlock, u16); | ||
| 6 | |||
| 5 | #[derive(Debug)] | 7 | #[derive(Debug)] |
| 6 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 8 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 7 | pub struct SpiInterface<SPI>(pub SPI); | 9 | pub struct SpiInterface<SPI>(pub SPI); |
| 8 | 10 | ||
| 9 | impl<SPI: SpiDevice> SpiInterface<SPI> { | 11 | impl<SPI: SpiDevice> SpiInterface<SPI> { |
| 10 | pub async fn read_frame(&mut self, block: RegisterBlock, address: u16, data: &mut [u8]) -> Result<(), SPI::Error> { | 12 | pub async fn read_frame(&mut self, address: Address, data: &mut [u8]) -> Result<(), SPI::Error> { |
| 11 | let address_phase = address.to_be_bytes(); | 13 | let address_phase = address.1.to_be_bytes(); |
| 12 | let control_phase = [(block as u8) << 3]; | 14 | let control_phase = [(address.0 as u8) << 3]; |
| 13 | let operations = &mut [ | 15 | let operations = &mut [ |
| 14 | Operation::Write(&address_phase), | 16 | Operation::Write(&address_phase), |
| 15 | Operation::Write(&control_phase), | 17 | Operation::Write(&control_phase), |
| @@ -18,9 +20,9 @@ impl<SPI: SpiDevice> SpiInterface<SPI> { | |||
| 18 | self.0.transaction(operations).await | 20 | self.0.transaction(operations).await |
| 19 | } | 21 | } |
| 20 | 22 | ||
| 21 | pub async fn write_frame(&mut self, block: RegisterBlock, address: u16, data: &[u8]) -> Result<(), SPI::Error> { | 23 | pub async fn write_frame(&mut self, address: Address, data: &[u8]) -> Result<(), SPI::Error> { |
| 22 | let address_phase = address.to_be_bytes(); | 24 | let address_phase = address.1.to_be_bytes(); |
| 23 | let control_phase = [(block as u8) << 3 | 0b0000_0100]; | 25 | let control_phase = [(address.0 as u8) << 3 | 0b0000_0100]; |
| 24 | let data_phase = data; | 26 | let data_phase = data; |
| 25 | let operations = &mut [ | 27 | let operations = &mut [ |
| 26 | Operation::Write(&address_phase[..]), | 28 | Operation::Write(&address_phase[..]), |
