Renaming trait function names, cleaning up docs

This commit is contained in:
Ryan Summers 2023-03-30 12:54:08 +02:00
commit 0d47ea0982
3 changed files with 107 additions and 82 deletions

103
README.md
View file

@ -29,49 +29,76 @@ of the SPI implementation. It must be set up to work as the W5500 chip requires
* Clock Phase: Sample leading edge
* Clock speed: 33MHz maximum
Initialization and usage of owned `Device`:
```rust
let mut spi = ...; // SPI interface to use
let mut cs : OutputPin = ...; // chip select
```rust,no_run
use embedded_nal::{IpAddr, Ipv4Addr, SocketAddr, UdpClientStack};
#
# struct Mock;
#
# impl embedded_hal::blocking::spi::Transfer<u8> for Mock {
# type Error = ();
#
# 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 {
# type 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(())
# }
# }
// alternative FourWireRef::new(&mut spi, &mut cs)
let device = UninitializedDevice::new(FourWire::new(spi, cs))
.initialize_manual(
MacAddress::new(0, 1, 2, 3, 4, 5),
Ipv4Addr::new(192, 168, 86, 79),
Mode::default()
).unwrap();
let mut spi = Mock;
let mut cs = Mock;
let socket = device.socket();
socket.connect(
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 86, 38)), 8000),
).unwrap();
block!(interface.send(&mut socket, &[104, 101, 108, 108, 111, 10]));
device.close(socket);
let mut device = w5500::UninitializedDevice::new(w5500::bus::FourWire::new(spi, cs))
.initialize_manual(
w5500::MacAddress::new(0, 1, 2, 3, 4, 5),
Ipv4Addr::new(192, 168, 86, 79),
w5500::Mode::default()
).unwrap();
// optional
let (spi_bus, inactive_device) = device.deactivate();
```
// Allocate a UDP socket to send data with
let mut socket = device.socket().unwrap();
Usage of borrowed SPI-Bus and previously initialized `Device`:
```rust
let mut spi = ...; // SPI interface to use
let mut cs: OutputPin = ...; // chip select
// Connect the socket to the IP address and port we want to send to.
device.connect(&mut socket,
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 86, 38)), 8000),
).unwrap();
let mut device: Option<InactiveDevice<..>> = ...; // maybe: previously initialized device
let mut socket: Option<Socket> = ...; // maybe: previously opened socket
if let (Some(socket), Some(device)) = (socket.as_mut(), device.as_mut()) {
let mut buffer = [0u8; 1024];
match device
// scoped activation & usage of the SPI bus without move
.activate_ref(FourWireRef::new(&mut spi, &mut cs))
.receive(socket, &mut buffer)
{
Ok(..) => todo!(),
Err(..) => todo!(),
}
}
// Send the data
nb::block!(device.send(&mut socket, &[104, 101, 108, 108, 111, 10]));
// Optionally close the socket and deactivate the device
device.close(socket);
let (spi_bus, inactive_device) = device.deactivate();
```
## Todo