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

35 lines
1.2 KiB
Rust
Raw Normal View History

2023-05-09 01:51:08 +02:00
use embedded_hal_async::spi::{Operation, SpiDevice};
2023-05-31 01:01:30 +02:00
use crate::device::RegisterBlock;
2023-08-15 17:11:24 +02:00
pub type Address = (RegisterBlock, u16);
2023-05-09 01:51:08 +02:00
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct SpiInterface<SPI>(pub SPI);
impl<SPI: SpiDevice> SpiInterface<SPI> {
2023-08-15 17:11:24 +02:00
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];
2023-05-09 01:51:08 +02:00
let operations = &mut [
Operation::Write(&address_phase),
Operation::Write(&control_phase),
Operation::TransferInPlace(data),
];
self.0.transaction(operations).await
}
2023-08-15 17:11:24 +02:00
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];
2023-05-09 01:51:08 +02:00
let data_phase = data;
2023-07-04 19:53:06 +02:00
let operations = &mut [
Operation::Write(&address_phase[..]),
Operation::Write(&control_phase),
Operation::Write(&data_phase),
];
self.0.transaction(operations).await
2023-05-09 01:51:08 +02:00
}
}