Readding reference docs
This commit is contained in:
parent
20981a7963
commit
953ed3c7b1
4 changed files with 67 additions and 43 deletions
51
README.md
51
README.md
|
|
@ -30,7 +30,7 @@ of the SPI implementation. It must be set up to work as the W5500 chip requires
|
|||
* Clock speed: 33MHz maximum
|
||||
|
||||
```rust,no_run
|
||||
use embedded_nal::{IpAddr, Ipv4Addr, SocketAddr, UdpClientStack};
|
||||
use embedded_nal::{IpAddr, Ipv4Addr, SocketAddr};
|
||||
#
|
||||
# struct Mock;
|
||||
#
|
||||
|
|
@ -75,30 +75,53 @@ use embedded_nal::{IpAddr, Ipv4Addr, SocketAddr, UdpClientStack};
|
|||
# }
|
||||
# }
|
||||
|
||||
let mut spi = Mock;
|
||||
let mut cs = Mock;
|
||||
{
|
||||
use embedded_nal::UdpClientStack;
|
||||
|
||||
let mut device = w5500::UninitializedDevice::new(w5500::bus::FourWire::new(spi, cs))
|
||||
let mut spi = Mock;
|
||||
let mut cs = Mock;
|
||||
|
||||
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();
|
||||
|
||||
// Allocate a UDP socket to send data with
|
||||
let mut socket = device.socket().unwrap();
|
||||
// Allocate a UDP socket to send data with
|
||||
let mut socket = device.socket().unwrap();
|
||||
|
||||
// Connect the socket to the IP address and port we want to send to.
|
||||
device.connect(&mut socket,
|
||||
// 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();
|
||||
).unwrap();
|
||||
|
||||
// Send the data
|
||||
nb::block!(device.send(&mut socket, &[104, 101, 108, 108, 111, 10]));
|
||||
// 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();
|
||||
// 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
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#![no_std]
|
||||
#![allow(unused)]
|
||||
#![deny(rustdoc::broken_intra_doc_links)]
|
||||
#[doc = include_str!("../README.md")]
|
||||
|
||||
pub mod bus;
|
||||
mod device;
|
||||
|
|
|
|||
28
src/tcp.rs
28
src/tcp.rs
|
|
@ -65,13 +65,13 @@ impl TcpSocket {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn _close<B: Bus>(&self, bus: &mut B) -> Result<(), TcpSocketError<B::Error>> {
|
||||
fn socket_close<B: Bus>(&self, bus: &mut B) -> Result<(), TcpSocketError<B::Error>> {
|
||||
self.socket.set_mode(bus, socketn::Protocol::Closed)?;
|
||||
self.socket.command(bus, socketn::Command::Close)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn _connect<B: Bus>(
|
||||
fn socket_connect<B: Bus>(
|
||||
&mut self,
|
||||
bus: &mut B,
|
||||
remote: SocketAddrV4,
|
||||
|
|
@ -89,7 +89,7 @@ impl TcpSocket {
|
|||
// All other cases are transient TCP states. For these, we need to reset the TCP
|
||||
// machinery to return to the INIT state.
|
||||
Ok(_) => {
|
||||
self._close(bus)?;
|
||||
self.socket_close(bus)?;
|
||||
self.reopen(bus)?;
|
||||
}
|
||||
}
|
||||
|
|
@ -115,7 +115,7 @@ impl TcpSocket {
|
|||
Ok(socketn::Status::Closed) => {
|
||||
// For now, always return an open socket so that the user can re-connect with
|
||||
// it in the future.
|
||||
self._close(bus)?;
|
||||
self.socket_close(bus)?;
|
||||
return self.reopen(bus);
|
||||
}
|
||||
|
||||
|
|
@ -126,16 +126,16 @@ impl TcpSocket {
|
|||
}
|
||||
}
|
||||
|
||||
fn _is_connected<B: Bus>(&self, bus: &mut B) -> Result<bool, TcpSocketError<B::Error>> {
|
||||
fn socket_is_connected<B: Bus>(&self, bus: &mut B) -> Result<bool, TcpSocketError<B::Error>> {
|
||||
Ok(self.socket.get_status(bus)? == socketn::Status::Established as u8)
|
||||
}
|
||||
|
||||
fn _send<B: Bus>(
|
||||
fn socket_send<B: Bus>(
|
||||
&mut self,
|
||||
bus: &mut B,
|
||||
data: &[u8],
|
||||
) -> Result<usize, TcpSocketError<B::Error>> {
|
||||
if !self._is_connected(bus)? {
|
||||
if !self.socket_is_connected(bus)? {
|
||||
return Err(TcpSocketError::NotReady);
|
||||
}
|
||||
|
||||
|
|
@ -166,12 +166,12 @@ impl TcpSocket {
|
|||
Ok(write_data.len())
|
||||
}
|
||||
|
||||
fn _receive<B: Bus>(
|
||||
fn socket_receive<B: Bus>(
|
||||
&mut self,
|
||||
bus: &mut B,
|
||||
data: &mut [u8],
|
||||
) -> Result<usize, TcpSocketError<B::Error>> {
|
||||
if !self._is_connected(bus)? {
|
||||
if !self.socket_is_connected(bus)? {
|
||||
return Err(TcpSocketError::NotReady);
|
||||
}
|
||||
|
||||
|
|
@ -227,13 +227,13 @@ impl<SpiBus: Bus, HostImpl: Host> TcpClientStack for DeviceRefMut<'_, SpiBus, Ho
|
|||
};
|
||||
// TODO dynamically select a random port
|
||||
socket.open(&mut self.bus, 49849 + u16::from(socket.socket.index))?; // chosen by fair dice roll.
|
||||
socket._connect(&mut self.bus, remote)?;
|
||||
socket.socket_connect(&mut self.bus, remote)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn is_connected(&mut self, socket: &Self::TcpSocket) -> Result<bool, Self::Error> {
|
||||
socket._is_connected(&mut self.bus)
|
||||
socket.socket_is_connected(&mut self.bus)
|
||||
}
|
||||
|
||||
fn send(
|
||||
|
|
@ -241,7 +241,7 @@ impl<SpiBus: Bus, HostImpl: Host> TcpClientStack for DeviceRefMut<'_, SpiBus, Ho
|
|||
socket: &mut Self::TcpSocket,
|
||||
buffer: &[u8],
|
||||
) -> nb::Result<usize, Self::Error> {
|
||||
let len = socket._send(&mut self.bus, buffer)?;
|
||||
let len = socket.socket_send(&mut self.bus, buffer)?;
|
||||
Ok(len)
|
||||
}
|
||||
|
||||
|
|
@ -250,11 +250,11 @@ impl<SpiBus: Bus, HostImpl: Host> TcpClientStack for DeviceRefMut<'_, SpiBus, Ho
|
|||
socket: &mut Self::TcpSocket,
|
||||
buffer: &mut [u8],
|
||||
) -> nb::Result<usize, Self::Error> {
|
||||
Ok(socket._receive(&mut self.bus, buffer)?)
|
||||
Ok(socket.socket_receive(&mut self.bus, buffer)?)
|
||||
}
|
||||
|
||||
fn close(&mut self, socket: Self::TcpSocket) -> Result<(), Self::Error> {
|
||||
socket._close(&mut self.bus)?;
|
||||
socket.socket_close(&mut self.bus)?;
|
||||
self.release_socket(socket.socket);
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
18
src/udp.rs
18
src/udp.rs
|
|
@ -44,7 +44,7 @@ impl UdpSocket {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn _send<SpiBus: Bus>(
|
||||
fn socket_send<SpiBus: Bus>(
|
||||
&self,
|
||||
bus: &mut SpiBus,
|
||||
send_buffer: &[u8],
|
||||
|
|
@ -79,20 +79,20 @@ impl UdpSocket {
|
|||
}
|
||||
|
||||
/// Sets a new destination before performing the send operation.
|
||||
fn _send_to<SpiBus: Bus>(
|
||||
fn socket_send_to<SpiBus: Bus>(
|
||||
&mut self,
|
||||
bus: &mut SpiBus,
|
||||
remote: SocketAddrV4,
|
||||
send_buffer: &[u8],
|
||||
) -> NbResult<(), UdpSocketError<SpiBus::Error>> {
|
||||
self.set_destination(bus, remote)?;
|
||||
self._send(bus, send_buffer)
|
||||
self.socket_send(bus, send_buffer)
|
||||
}
|
||||
|
||||
/// Receive data and mutate the `receive_buffer`.
|
||||
///
|
||||
/// If [`Interrupt::Receive`] is not set, it will always return [`NbError::WouldBlock`].
|
||||
fn _receive<SpiBus: Bus>(
|
||||
fn socket_receive<SpiBus: Bus>(
|
||||
&mut self,
|
||||
bus: &mut SpiBus,
|
||||
receive_buffer: &mut [u8],
|
||||
|
|
@ -138,7 +138,7 @@ impl UdpSocket {
|
|||
Ok((packet_size, remote))
|
||||
}
|
||||
|
||||
fn _close<SpiBus: Bus>(&self, bus: &mut SpiBus) -> Result<(), UdpSocketError<SpiBus::Error>> {
|
||||
fn socket_close<SpiBus: Bus>(&self, bus: &mut SpiBus) -> Result<(), UdpSocketError<SpiBus::Error>> {
|
||||
self.socket.set_mode(bus, socketn::Protocol::Closed)?;
|
||||
self.socket.command(bus, socketn::Command::Close)?;
|
||||
Ok(())
|
||||
|
|
@ -267,7 +267,7 @@ where
|
|||
}
|
||||
|
||||
fn send(&mut self, socket: &mut Self::UdpSocket, buffer: &[u8]) -> nb::Result<(), Self::Error> {
|
||||
socket._send(&mut self.bus, buffer)?;
|
||||
socket.socket_send(&mut self.bus, buffer)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
@ -276,11 +276,11 @@ where
|
|||
socket: &mut Self::UdpSocket,
|
||||
buffer: &mut [u8],
|
||||
) -> nb::Result<(usize, SocketAddr), Self::Error> {
|
||||
Ok(socket._receive(&mut self.bus, buffer)?)
|
||||
Ok(socket.socket_receive(&mut self.bus, buffer)?)
|
||||
}
|
||||
|
||||
fn close(&mut self, socket: Self::UdpSocket) -> Result<(), Self::Error> {
|
||||
socket._close(&mut self.bus)?;
|
||||
socket.socket_close(&mut self.bus)?;
|
||||
self.release_socket(socket.socket);
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -327,7 +327,7 @@ where
|
|||
return Err(nb::Error::Other(Self::Error::UnsupportedAddress))
|
||||
};
|
||||
|
||||
socket._send_to(&mut self.bus, remote, buffer)?;
|
||||
socket.socket_send_to(&mut self.bus, remote, buffer)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue