diff --git a/README.md b/README.md index 7618a64..eb59ddf 100644 --- a/README.md +++ b/README.md @@ -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 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 for Mock { +# type Error = (); +# +# fn read(&mut self) -> nb::Result { +# Ok(0) +# } +# +# fn send(&mut self, word: u8) -> nb::Result<(), Self::Error> { +# Ok(()) +# } +# } +# +# impl embedded_hal::blocking::spi::Write 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> = ...; // maybe: previously initialized device - let mut socket: Option = ...; // 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 diff --git a/src/tcp.rs b/src/tcp.rs index 14e7c57..986305e 100644 --- a/src/tcp.rs +++ b/src/tcp.rs @@ -65,13 +65,13 @@ impl TcpSocket { Ok(()) } - fn close(&self, bus: &mut B) -> Result<(), TcpSocketError> { + fn _close(&self, bus: &mut B) -> Result<(), TcpSocketError> { self.socket.set_mode(bus, socketn::Protocol::Closed)?; self.socket.command(bus, socketn::Command::Close)?; Ok(()) } - fn connect( + fn _connect( &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._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._close(bus)?; return self.reopen(bus); } @@ -126,16 +126,16 @@ impl TcpSocket { } } - pub fn is_connected(&self, bus: &mut B) -> Result> { + fn _is_connected(&self, bus: &mut B) -> Result> { Ok(self.socket.get_status(bus)? == socketn::Status::Established as u8) } - fn send( + fn _send( &mut self, bus: &mut B, data: &[u8], ) -> Result> { - if !self.is_connected(bus)? { + if !self._is_connected(bus)? { return Err(TcpSocketError::NotReady); } @@ -166,12 +166,12 @@ impl TcpSocket { Ok(write_data.len()) } - fn receive( + fn _receive( &mut self, bus: &mut B, data: &mut [u8], ) -> Result> { - if !self.is_connected(bus)? { + if !self._is_connected(bus)? { return Err(TcpSocketError::NotReady); } @@ -222,19 +222,18 @@ impl TcpClientStack for DeviceRefMut<'_, SpiBus, Ho socket: &mut Self::TcpSocket, remote: SocketAddr, ) -> nb::Result<(), Self::Error> { - if let SocketAddr::V4(remote) = remote { - // 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)?; + let SocketAddr::V4(remote) = remote else { + return Err(nb::Error::Other(Self::Error::UnsupportedAddress)) + }; + // 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)?; - Ok(()) - } else { - Err(nb::Error::Other(Self::Error::UnsupportedAddress)) - } + Ok(()) } fn is_connected(&mut self, socket: &Self::TcpSocket) -> Result { - socket.is_connected(&mut self.bus) + socket._is_connected(&mut self.bus) } fn send( @@ -242,7 +241,7 @@ impl TcpClientStack for DeviceRefMut<'_, SpiBus, Ho socket: &mut Self::TcpSocket, buffer: &[u8], ) -> nb::Result { - let len = socket.send(&mut self.bus, buffer)?; + let len = socket._send(&mut self.bus, buffer)?; Ok(len) } @@ -251,11 +250,11 @@ impl TcpClientStack for DeviceRefMut<'_, SpiBus, Ho socket: &mut Self::TcpSocket, buffer: &mut [u8], ) -> nb::Result { - Ok(socket.receive(&mut self.bus, buffer)?) + Ok(socket._receive(&mut self.bus, buffer)?) } fn close(&mut self, socket: Self::TcpSocket) -> Result<(), Self::Error> { - socket.close(&mut self.bus)?; + socket._close(&mut self.bus)?; self.release_socket(socket.socket); Ok(()) } diff --git a/src/udp.rs b/src/udp.rs index 7566f16..a2f2427 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -44,7 +44,7 @@ impl UdpSocket { Ok(()) } - fn send( + fn _send( &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( + fn _send_to( &mut self, bus: &mut SpiBus, remote: SocketAddrV4, send_buffer: &[u8], ) -> NbResult<(), UdpSocketError> { self.set_destination(bus, remote)?; - self.send(bus, send_buffer) + self._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( + fn _receive( &mut self, bus: &mut SpiBus, receive_buffer: &mut [u8], @@ -138,7 +138,7 @@ impl UdpSocket { Ok((packet_size, remote)) } - fn close(&self, bus: &mut SpiBus) -> Result<(), UdpSocketError> { + fn _close(&self, bus: &mut SpiBus) -> Result<(), UdpSocketError> { self.socket.set_mode(bus, socketn::Protocol::Closed)?; self.socket.command(bus, socketn::Command::Close)?; Ok(()) @@ -256,19 +256,18 @@ where socket: &mut Self::UdpSocket, remote: SocketAddr, ) -> Result<(), Self::Error> { - if let SocketAddr::V4(remote) = remote { - // TODO dynamically select a random port - socket.open(&mut self.bus, 49849 + u16::from(socket.socket.index))?; // chosen by fair dice roll. - // guaranteed to be random. - socket.set_destination(&mut self.bus, remote)?; - Ok(()) - } else { - Err(Self::Error::UnsupportedAddress) - } + let SocketAddr::V4(remote) = remote else { + return Err(Self::Error::UnsupportedAddress) + }; + // TODO dynamically select a random port + socket.open(&mut self.bus, 49849 + u16::from(socket.socket.index))?; // chosen by fair dice roll. + // guaranteed to be random. + socket.set_destination(&mut self.bus, remote)?; + Ok(()) } fn send(&mut self, socket: &mut Self::UdpSocket, buffer: &[u8]) -> nb::Result<(), Self::Error> { - socket.send(&mut self.bus, buffer)?; + socket._send(&mut self.bus, buffer)?; Ok(()) } @@ -277,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._receive(&mut self.bus, buffer)?) } fn close(&mut self, socket: Self::UdpSocket) -> Result<(), Self::Error> { - socket.close(&mut self.bus)?; + socket._close(&mut self.bus)?; self.release_socket(socket.socket); Ok(()) } @@ -324,11 +323,11 @@ where remote: SocketAddr, buffer: &[u8], ) -> nb::Result<(), Self::Error> { - if let SocketAddr::V4(remote) = remote { - socket.send_to(&mut self.bus, remote, buffer)?; - Ok(()) - } else { - Err(nb::Error::Other(Self::Error::UnsupportedAddress)) - } + let SocketAddr::V4(remote) = remote else { + return Err(nb::Error::Other(Self::Error::UnsupportedAddress)) + }; + + socket._send_to(&mut self.bus, remote, buffer)?; + Ok(()) } }