diff options
| author | kalkyl <[email protected]> | 2023-05-09 01:51:08 +0200 |
|---|---|---|
| committer | kalkyl <[email protected]> | 2023-05-09 01:51:08 +0200 |
| commit | 72b0379125b87bcd274bdb81127dd5f0ab29d661 (patch) | |
| tree | b620d67bc9ea930051668d63190dd6de62485b0a /src/socket.rs | |
:rainbow:
Diffstat (limited to 'src/socket.rs')
| -rw-r--r-- | src/socket.rs | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/src/socket.rs b/src/socket.rs new file mode 100644 index 000000000..0d3d1aeb2 --- /dev/null +++ b/src/socket.rs | |||
| @@ -0,0 +1,114 @@ | |||
| 1 | use crate::device::RegisterBlock; | ||
| 2 | use crate::spi::SpiInterface; | ||
| 3 | use embedded_hal_async::spi::SpiDevice; | ||
| 4 | |||
| 5 | pub const MODE: u16 = 0x00; | ||
| 6 | pub const COMMAND: u16 = 0x01; | ||
| 7 | pub const RXBUF_SIZE: u16 = 0x1E; | ||
| 8 | pub const TXBUF_SIZE: u16 = 0x1F; | ||
| 9 | pub const TX_FREE_SIZE: u16 = 0x20; | ||
| 10 | pub const TX_DATA_WRITE_PTR: u16 = 0x24; | ||
| 11 | pub const RECVD_SIZE: u16 = 0x26; | ||
| 12 | pub const RX_DATA_READ_PTR: u16 = 0x28; | ||
| 13 | pub const SOCKET_INTR_MASK: u16 = 0x2C; | ||
| 14 | |||
| 15 | #[repr(u8)] | ||
| 16 | pub enum Command { | ||
| 17 | Open = 0x01, | ||
| 18 | Send = 0x20, | ||
| 19 | Receive = 0x40, | ||
| 20 | } | ||
| 21 | |||
| 22 | pub const INTR: u16 = 0x02; | ||
| 23 | #[repr(u8)] | ||
| 24 | pub enum Interrupt { | ||
| 25 | SendOk = 0b010000_u8, | ||
| 26 | Receive = 0b00100_u8, | ||
| 27 | } | ||
| 28 | |||
| 29 | pub async fn reset_interrupt<SPI: SpiDevice>( | ||
| 30 | bus: &mut SpiInterface<SPI>, | ||
| 31 | code: Interrupt, | ||
| 32 | ) -> Result<(), SPI::Error> { | ||
| 33 | let data = [code as u8]; | ||
| 34 | bus.write_frame(RegisterBlock::Socket0, INTR, &data).await | ||
| 35 | } | ||
| 36 | |||
| 37 | pub async fn is_interrupt<SPI: SpiDevice>( | ||
| 38 | bus: &mut SpiInterface<SPI>, | ||
| 39 | code: Interrupt, | ||
| 40 | ) -> Result<bool, SPI::Error> { | ||
| 41 | let mut data = [0u8]; | ||
| 42 | bus.read_frame(RegisterBlock::Socket0, INTR, &mut data) | ||
| 43 | .await?; | ||
| 44 | Ok(data[0] & code as u8 != 0) | ||
| 45 | } | ||
| 46 | |||
| 47 | pub async fn get_tx_write_ptr<SPI: SpiDevice>( | ||
| 48 | bus: &mut SpiInterface<SPI>, | ||
| 49 | ) -> Result<u16, SPI::Error> { | ||
| 50 | let mut data = [0u8; 2]; | ||
| 51 | bus.read_frame(RegisterBlock::Socket0, TX_DATA_WRITE_PTR, &mut data) | ||
| 52 | .await?; | ||
| 53 | Ok(u16::from_be_bytes(data)) | ||
| 54 | } | ||
| 55 | |||
| 56 | pub async fn set_tx_write_ptr<SPI: SpiDevice>( | ||
| 57 | bus: &mut SpiInterface<SPI>, | ||
| 58 | ptr: u16, | ||
| 59 | ) -> Result<(), SPI::Error> { | ||
| 60 | let data = ptr.to_be_bytes(); | ||
| 61 | bus.write_frame(RegisterBlock::Socket0, TX_DATA_WRITE_PTR, &data) | ||
| 62 | .await | ||
| 63 | } | ||
| 64 | |||
| 65 | pub async fn get_rx_read_ptr<SPI: SpiDevice>( | ||
| 66 | bus: &mut SpiInterface<SPI>, | ||
| 67 | ) -> Result<u16, SPI::Error> { | ||
| 68 | let mut data = [0u8; 2]; | ||
| 69 | bus.read_frame(RegisterBlock::Socket0, RX_DATA_READ_PTR, &mut data) | ||
| 70 | .await?; | ||
| 71 | Ok(u16::from_be_bytes(data)) | ||
| 72 | } | ||
| 73 | |||
| 74 | pub async fn set_rx_read_ptr<SPI: SpiDevice>( | ||
| 75 | bus: &mut SpiInterface<SPI>, | ||
| 76 | ptr: u16, | ||
| 77 | ) -> Result<(), SPI::Error> { | ||
| 78 | let data = ptr.to_be_bytes(); | ||
| 79 | bus.write_frame(RegisterBlock::Socket0, RX_DATA_READ_PTR, &data) | ||
| 80 | .await | ||
| 81 | } | ||
| 82 | |||
| 83 | pub async fn command<SPI: SpiDevice>( | ||
| 84 | bus: &mut SpiInterface<SPI>, | ||
| 85 | command: Command, | ||
| 86 | ) -> Result<(), SPI::Error> { | ||
| 87 | let data = [command as u8]; | ||
| 88 | bus.write_frame(RegisterBlock::Socket0, COMMAND, &data) | ||
| 89 | .await | ||
| 90 | } | ||
| 91 | |||
| 92 | pub async fn get_rx_size<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result<u16, SPI::Error> { | ||
| 93 | loop { | ||
| 94 | // Wait until two sequential reads are equal | ||
| 95 | let mut res0 = [0u8; 2]; | ||
| 96 | bus.read_frame(RegisterBlock::Socket0, RECVD_SIZE, &mut res0) | ||
| 97 | .await?; | ||
| 98 | let mut res1 = [0u8; 2]; | ||
| 99 | bus.read_frame(RegisterBlock::Socket0, RECVD_SIZE, &mut res1) | ||
| 100 | .await?; | ||
| 101 | if res0 == res1 { | ||
| 102 | break Ok(u16::from_be_bytes(res0)); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | pub async fn get_tx_free_size<SPI: SpiDevice>( | ||
| 108 | bus: &mut SpiInterface<SPI>, | ||
| 109 | ) -> Result<u16, SPI::Error> { | ||
| 110 | let mut data = [0; 2]; | ||
| 111 | bus.read_frame(RegisterBlock::Socket0, TX_FREE_SIZE, &mut data) | ||
| 112 | .await?; | ||
| 113 | Ok(u16::from_be_bytes(data)) | ||
| 114 | } | ||
