Fixing docs and tests

This commit is contained in:
Ryan Summers 2024-07-01 18:07:48 +02:00 committed by kellerkindt
commit d2dea7a42c
3 changed files with 43 additions and 118 deletions

View file

@ -23,4 +23,4 @@ nb = "1.0.0"
defmt = { version = "0.3", optional = true } defmt = { version = "0.3", optional = true }
[dev-dependencies] [dev-dependencies]
embedded-hal-mock = "0.9" embedded-hal-mock = { version = "0.11", features = ["eh1"] }

View file

@ -34,94 +34,39 @@ use embedded_nal::{IpAddr, Ipv4Addr, SocketAddr};
# #
# struct Mock; # struct Mock;
# #
# impl embedded_hal::blocking::spi::Transfer<u8> for Mock { # impl embedded_hal::spi::ErrorType for Mock {
# type Error = (); # type Error = core::convert::Infallible;
#
# fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> {
# words[0] = 0x04;
# Ok(words)
# }
# } # }
# #
# impl embedded_hal::spi::FullDuplex<u8> for Mock { # impl embedded_hal::spi::SpiDevice for Mock {
# type Error = (); # fn transaction(&mut self, operations: &mut [embedded_hal::spi::Operation<'_, u8>]) -> Result<(), Self::Error> {
#
# fn read(&mut self) -> nb::Result<u8, Self::Error> {
# Ok(0)
# }
#
# fn send(&mut self, word: u8) -> nb::Result<(), Self::Error> {
# Ok(())
# }
# }
#
# impl embedded_hal::blocking::spi::Write<u8> for Mock {
# type Error = ();
#
# fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> {
# Ok(())
# }
# }
#
# impl embedded_hal::digital::v2::OutputPin for Mock {
# type Error = ();
#
# fn set_low(&mut self) -> Result<(), Self::Error> {
# Ok(())
# }
#
# fn set_high(&mut self) -> Result<(), Self::Error> {
# Ok(()) # Ok(())
# } # }
# } # }
use embedded_nal::UdpClientStack;
{ let mut spi = Mock;
use embedded_nal::UdpClientStack;
let mut spi = Mock; let mut device = w5500::UninitializedDevice::new(w5500::bus::FourWire::new(spi))
let mut cs = Mock; .initialize_manual(
w5500::MacAddress::new(0, 1, 2, 3, 4, 5),
Ipv4Addr::new(192, 168, 86, 79),
w5500::Mode::default()
).unwrap();
let mut device = w5500::UninitializedDevice::new(w5500::bus::FourWire::new(spi, cs)) // Allocate a UDP socket to send data with
.initialize_manual( let mut socket = device.socket().unwrap();
w5500::MacAddress::new(0, 1, 2, 3, 4, 5),
Ipv4Addr::new(192, 168, 86, 79),
w5500::Mode::default()
).unwrap();
// Allocate a UDP socket to send data with // Connect the socket to the IP address and port we want to send to.
let mut socket = device.socket().unwrap(); device.connect(&mut socket,
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 86, 38)), 8000),
).unwrap();
// Connect the socket to the IP address and port we want to send to. // Send the data
device.connect(&mut socket, nb::block!(device.send(&mut socket, &[104, 101, 108, 108, 111, 10]));
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 86, 38)), 8000),
).unwrap();
// Send the data // Optionally close the socket
nb::block!(device.send(&mut socket, &[104, 101, 108, 108, 111, 10])); device.close(socket);
// Optionally close the socket and deactivate the device
device.close(socket);
let (spi_bus, inactive_device) = device.deactivate();
}
// Alternatively, you can use the W5500 where a SPI bus is shared between drivers:
{
use embedded_nal::TcpClientStack;
let mut spi = Mock;
let mut cs = Mock;
let mut device: Option<w5500::InactiveDevice<w5500::Manual>> = None; // maybe: previously initialized device
let mut socket: Option<w5500::tcp::TcpSocket> = None; // maybe: previously opened socket
if let (Some(socket), Some(device)) = (socket.as_mut(), device.as_mut()) {
let mut buffer = [0u8; 1024];
let mut active = device.activate_ref(w5500::bus::FourWireRef::new(&mut spi, &mut cs));
// Read from the device.
active.receive(socket, &mut buffer).unwrap();
}
}
``` ```
## Todo ## Todo

View file

@ -34,7 +34,7 @@ impl<SPI: SpiDevice> Bus for FourWire<SPI> {
self.spi.transaction(&mut [ self.spi.transaction(&mut [
Operation::Write(&address_phase), Operation::Write(&address_phase),
Operation::Write(&[control_phase]), Operation::Write(&[control_phase]),
Operation::Write(data), Operation::TransferInPlace(data),
])?; ])?;
Ok(()) Ok(())
@ -56,11 +56,7 @@ impl<SPI: SpiDevice> Bus for FourWire<SPI> {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use embedded_hal::digital::v2::OutputPin; use embedded_hal_mock::eh1::spi::{Mock as SpiMock, Transaction as SpiTransaction};
use embedded_hal_mock::{
pin::{Mock as PinMock, State as PinState, Transaction as PinTransaction},
spi::{Mock as SpiMock, Transaction as SpiTransaction},
};
use crate::{ use crate::{
bus::{four_wire::WRITE_MODE_MASK, Bus}, bus::{four_wire::WRITE_MODE_MASK, Bus},
@ -71,30 +67,20 @@ mod test {
#[test] #[test]
fn test_read_frame() { fn test_read_frame() {
let mut cs_pin = PinMock::new(&[
// we begin with pin HIGH
PinTransaction::set(PinState::High),
// When reading
PinTransaction::set(PinState::Low),
// When finished reading
PinTransaction::set(PinState::High),
]);
// initiate the pin to high.
cs_pin.set_high().expect("Should set pin to high");
let mut actual_version = [0_u8; 1]; let mut actual_version = [0_u8; 1];
let mut expected_version = 5; let mut expected_version = 5;
let expectations = [ let expectations = [
SpiTransaction::write(register::common::VERSION.to_be_bytes().to_vec()), SpiTransaction::transaction_start(),
SpiTransaction::write(vec![register::COMMON << 3]), SpiTransaction::write_vec(register::common::VERSION.to_be_bytes().to_vec()),
SpiTransaction::transfer(actual_version.to_vec(), vec![expected_version]), SpiTransaction::write(register::COMMON << 3),
SpiTransaction::transfer_in_place(actual_version.to_vec(), vec![expected_version]),
SpiTransaction::transaction_end(),
]; ];
let mock_spi = SpiMock::new(&expectations); let mock_spi = SpiMock::new(&expectations);
let mut four_wire = FourWire::new(mock_spi, cs_pin); let mut four_wire = FourWire::new(mock_spi);
four_wire.read_frame( four_wire.read_frame(
register::COMMON, register::COMMON,
@ -102,41 +88,35 @@ mod test {
&mut actual_version, &mut actual_version,
); );
four_wire.release().done();
assert_eq!(expected_version, actual_version[0]); assert_eq!(expected_version, actual_version[0]);
} }
#[test] #[test]
fn test_write_frame() { fn test_write_frame() {
let mut cs_pin = PinMock::new(&[
// we begin with pin HIGH
PinTransaction::set(PinState::High),
// When reading
PinTransaction::set(PinState::Low),
// When finished reading
PinTransaction::set(PinState::High),
]);
// initiate the pin to high.
cs_pin.set_high().expect("Should set pin to high");
let socket_0_reg = 0x01_u8; let socket_0_reg = 0x01_u8;
let socket_1_reg = 0x05_u8; let socket_1_reg = 0x05_u8;
let source_port = 49849_u16; let source_port = 49849_u16;
let expectations = [ let expectations = [
SpiTransaction::write(register::socketn::SOURCE_PORT.to_be_bytes().to_vec()), SpiTransaction::transaction_start(),
SpiTransaction::write(vec![socket_1_reg << 3 | WRITE_MODE_MASK]), SpiTransaction::write_vec(register::socketn::SOURCE_PORT.to_be_bytes().to_vec()),
SpiTransaction::write(source_port.to_be_bytes().to_vec()), SpiTransaction::write(socket_1_reg << 3 | WRITE_MODE_MASK),
SpiTransaction::write_vec(source_port.to_be_bytes().to_vec()),
SpiTransaction::transaction_end(),
]; ];
let mock_spi = SpiMock::new(&expectations); let mock_spi = SpiMock::new(&expectations);
let mut four_wire = FourWire::new(mock_spi, cs_pin); let mut four_wire = FourWire::new(mock_spi);
four_wire.write_frame( four_wire.write_frame(
socket_1_reg, socket_1_reg,
register::socketn::SOURCE_PORT, register::socketn::SOURCE_PORT,
&source_port.to_be_bytes(), &source_port.to_be_bytes(),
); );
four_wire.release().done();
} }
} }