embassy/embassy-net-w5500/src/socket.rs

79 lines
2.7 KiB
Rust
Raw Normal View History

2023-05-31 01:01:30 +02:00
use embedded_hal_async::spi::SpiDevice;
2023-05-09 01:51:08 +02:00
use crate::device::RegisterBlock;
2023-08-15 17:11:24 +02:00
use crate::spi::{Address, SpiInterface};
2023-05-09 01:51:08 +02:00
2023-08-15 17:11:24 +02:00
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);
2023-05-09 01:51:08 +02:00
#[repr(u8)]
pub enum Command {
Open = 0x01,
Send = 0x20,
Receive = 0x40,
}
#[repr(u8)]
pub enum Interrupt {
Receive = 0b00100_u8,
}
2023-05-31 01:01:30 +02:00
pub async fn reset_interrupt<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>, code: Interrupt) -> Result<(), SPI::Error> {
2023-05-09 01:51:08 +02:00
let data = [code as u8];
2023-08-15 17:11:24 +02:00
bus.write_frame(INTR, &data).await
2023-05-09 01:51:08 +02:00
}
2023-05-31 01:01:30 +02:00
pub async fn get_tx_write_ptr<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result<u16, SPI::Error> {
2023-05-09 01:51:08 +02:00
let mut data = [0u8; 2];
2023-08-15 17:11:24 +02:00
bus.read_frame(TX_DATA_WRITE_PTR, &mut data).await?;
2023-05-09 01:51:08 +02:00
Ok(u16::from_be_bytes(data))
}
2023-05-31 01:01:30 +02:00
pub async fn set_tx_write_ptr<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>, ptr: u16) -> Result<(), SPI::Error> {
2023-05-09 01:51:08 +02:00
let data = ptr.to_be_bytes();
2023-08-15 17:11:24 +02:00
bus.write_frame(TX_DATA_WRITE_PTR, &data).await
2023-05-09 01:51:08 +02:00
}
2023-05-31 01:01:30 +02:00
pub async fn get_rx_read_ptr<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result<u16, SPI::Error> {
2023-05-09 01:51:08 +02:00
let mut data = [0u8; 2];
2023-08-15 17:11:24 +02:00
bus.read_frame(RX_DATA_READ_PTR, &mut data).await?;
2023-05-09 01:51:08 +02:00
Ok(u16::from_be_bytes(data))
}
2023-05-31 01:01:30 +02:00
pub async fn set_rx_read_ptr<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>, ptr: u16) -> Result<(), SPI::Error> {
2023-05-09 01:51:08 +02:00
let data = ptr.to_be_bytes();
2023-08-15 17:11:24 +02:00
bus.write_frame(RX_DATA_READ_PTR, &data).await
2023-05-09 01:51:08 +02:00
}
2023-05-31 01:01:30 +02:00
pub async fn command<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>, command: Command) -> Result<(), SPI::Error> {
2023-05-09 01:51:08 +02:00
let data = [command as u8];
2023-08-15 17:11:24 +02:00
bus.write_frame(COMMAND, &data).await
2023-05-09 01:51:08 +02:00
}
pub async fn get_rx_size<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result<u16, SPI::Error> {
loop {
// Wait until two sequential reads are equal
let mut res0 = [0u8; 2];
2023-08-15 17:11:24 +02:00
bus.read_frame(RECVD_SIZE, &mut res0).await?;
2023-05-09 01:51:08 +02:00
let mut res1 = [0u8; 2];
2023-08-15 17:11:24 +02:00
bus.read_frame(RECVD_SIZE, &mut res1).await?;
2023-05-09 01:51:08 +02:00
if res0 == res1 {
break Ok(u16::from_be_bytes(res0));
}
}
}
2023-05-31 01:01:30 +02:00
pub async fn get_tx_free_size<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result<u16, SPI::Error> {
2023-05-09 01:51:08 +02:00
let mut data = [0; 2];
2023-08-15 17:11:24 +02:00
bus.read_frame(TX_FREE_SIZE, &mut data).await?;
2023-05-09 01:51:08 +02:00
Ok(u16::from_be_bytes(data))
}