From 747fe0cca4ff68620b4a803be94a3c733b64345b Mon Sep 17 00:00:00 2001 From: Lachezar Lechev Date: Fri, 10 Feb 2023 14:50:08 +0200 Subject: [PATCH 1/8] feat: export Host Signed-off-by: Lachezar Lechev --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index a81bf7e..8f9dc5e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,7 @@ pub mod udp; mod uninitialized_device; pub use device::{Device, DeviceRefMut, InactiveDevice}; -pub use host::{Dhcp, HostConfig, Manual}; +pub use host::{Dhcp, Host, HostConfig, Manual}; pub use net::MacAddress; pub use uninitialized_device::UninitializedDevice; From f2ac8521b1ac9c061823e6f7e1f6dfa441985ccc Mon Sep 17 00:00:00 2001 From: Lachezar Lechev Date: Fri, 10 Feb 2023 14:51:55 +0200 Subject: [PATCH 2/8] feat: defmt + derive for some UDP types Signed-off-by: Lachezar Lechev --- Cargo.toml | 1 + src/host/dhcp.rs | 2 +- src/socket.rs | 2 ++ src/udp.rs | 5 ++++- 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9054bc2..840572f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ embedded-nal = "0.6.0" bit_field = "0.10.1" derive-try-from-primitive = "1" nb = "1.0.0" +defmt = { version = "0.3", optional = true } [features] no-chip-version-assertion = [] diff --git a/src/host/dhcp.rs b/src/host/dhcp.rs index f792736..521a831 100644 --- a/src/host/dhcp.rs +++ b/src/host/dhcp.rs @@ -4,7 +4,7 @@ use crate::MacAddress; pub struct Dhcp { // settings: HostConfig, -// current: HostConfig, + // current: HostConfig, } impl Dhcp { diff --git a/src/socket.rs b/src/socket.rs index 2da445c..2a9d794 100644 --- a/src/socket.rs +++ b/src/socket.rs @@ -2,6 +2,8 @@ use crate::bus::Bus; use crate::register::socketn; use embedded_nal::Ipv4Addr; +#[derive(Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct Socket { pub index: u8, register: u8, diff --git a/src/udp.rs b/src/udp.rs index cc5ee6b..3e29388 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -6,6 +6,8 @@ use crate::socket::Socket; use core::fmt::Debug; use embedded_nal::{nb, IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4, UdpClientStack, UdpFullStack}; +#[derive(Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct UdpSocket { socket: Socket, } @@ -140,10 +142,11 @@ impl UdpSocket { } #[derive(Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum UdpSocketError { NoMoreSockets, UnsupportedAddress, - Other(E), + Other(#[cfg_attr(feature = "defmt", defmt(Debug2Format))] E), WriteTimeout, } From 1b2ba0bfabaa6b355dc14ea54d60e267c5d73314 Mon Sep 17 00:00:00 2001 From: Lachezar Lechev Date: Fri, 17 Feb 2023 17:51:20 +0200 Subject: [PATCH 3/8] feat: export uninitialized_device & add docs Signed-off-by: Lachezar Lechev --- src/lib.rs | 2 +- src/udp.rs | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 8f9dc5e..ac237a7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,7 +11,7 @@ pub mod register; mod socket; pub mod tcp; pub mod udp; -mod uninitialized_device; +pub mod uninitialized_device; pub use device::{Device, DeviceRefMut, InactiveDevice}; pub use host::{Dhcp, Host, HostConfig, Manual}; diff --git a/src/udp.rs b/src/udp.rs index 3e29388..7566f16 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -78,6 +78,7 @@ impl UdpSocket { } } + /// Sets a new destination before performing the send operation. fn send_to( &mut self, bus: &mut SpiBus, @@ -88,6 +89,9 @@ impl UdpSocket { 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( &mut self, bus: &mut SpiBus, @@ -139,6 +143,12 @@ impl UdpSocket { self.socket.command(bus, socketn::Command::Close)?; Ok(()) } + + /// returns the index of the socket + #[inline] + pub fn index(&self) -> u8 { + self.socket.index + } } #[derive(Debug)] From c3f79c96ed029a65ef5c7b3dd9357089c9cdc9b0 Mon Sep 17 00:00:00 2001 From: Lachezar Lechev Date: Mon, 6 Mar 2023 12:54:55 +0200 Subject: [PATCH 4/8] feat: derive `defmt::Format` for more structs and errors Signed-off-by: Lachezar Lechev --- src/bus/four_wire.rs | 2 ++ src/bus/four_wire_ref.rs | 4 ++++ src/bus/three_wire.rs | 2 ++ src/device.rs | 4 ++++ src/host/dhcp.rs | 2 ++ src/host/manual.rs | 2 ++ src/host/mod.rs | 5 +++++ src/lib.rs | 15 ++++++++++----- src/net.rs | 1 + src/raw_device.rs | 2 ++ src/tcp.rs | 5 ++++- src/uninitialized_device.rs | 2 ++ 12 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/bus/four_wire.rs b/src/bus/four_wire.rs index a55c55f..f33f5d5 100644 --- a/src/bus/four_wire.rs +++ b/src/bus/four_wire.rs @@ -9,6 +9,8 @@ use crate::bus::Bus; const WRITE_MODE_MASK: u8 = 0b00000_1_00; // TODO This name is not ideal, should be renamed to VDM +#[derive(Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct FourWire + Write, ChipSelect: OutputPin> { cs: ChipSelect, spi: Spi, diff --git a/src/bus/four_wire_ref.rs b/src/bus/four_wire_ref.rs index af3265b..0a653a5 100644 --- a/src/bus/four_wire_ref.rs +++ b/src/bus/four_wire_ref.rs @@ -9,6 +9,8 @@ use crate::bus::{Bus, FourWire, FourWireError}; // TODO This name is not ideal, should be renamed to VDM /// This is just like [crate::bus::FourWire] but takes references instead of ownership /// for the SPI bus and the ChipSelect pin +#[derive(Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct FourWireRef<'a, Spi: Transfer + Write, ChipSelect: OutputPin>( FourWire, OutputPinRef<'a, ChipSelect>>, ); @@ -43,6 +45,7 @@ impl + Write, ChipSelect: OutputPin> Bus } #[derive(Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct SpiRef<'a, Spi: Transfer + Write>(pub &'a mut Spi); impl<'a, Spi: Transfer + Write> Transfer for SpiRef<'a, Spi> { @@ -64,6 +67,7 @@ impl<'a, Spi: Transfer + Write> Write for SpiRef<'a, Spi> { } #[derive(Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct OutputPinRef<'a, P: OutputPin>(pub &'a mut P); impl<'a, P: OutputPin> OutputPin for OutputPinRef<'a, P> { diff --git a/src/bus/three_wire.rs b/src/bus/three_wire.rs index fe55615..1a21f82 100644 --- a/src/bus/three_wire.rs +++ b/src/bus/three_wire.rs @@ -12,6 +12,8 @@ const FIXED_DATA_LENGTH_MODE_2: u8 = 0b000000_10; const FIXED_DATA_LENGTH_MODE_4: u8 = 0b000000_11; // TODO This name is not ideal, should be renamed to FDM +#[derive(Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct ThreeWire + Write> { spi: Spi, } diff --git a/src/device.rs b/src/device.rs index 107a600..bf41a7c 100644 --- a/src/device.rs +++ b/src/device.rs @@ -19,6 +19,8 @@ impl From for ResetError { } } +#[derive(Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub(crate) struct DeviceState { host: HostImpl, sockets: [u8; 1], @@ -104,6 +106,8 @@ impl Device { } } +#[derive(Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct InactiveDevice(DeviceState); impl InactiveDevice { diff --git a/src/host/dhcp.rs b/src/host/dhcp.rs index 521a831..b8a933b 100644 --- a/src/host/dhcp.rs +++ b/src/host/dhcp.rs @@ -2,6 +2,8 @@ use crate::bus::Bus; use crate::host::Host; use crate::MacAddress; +#[derive(Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct Dhcp { // settings: HostConfig, // current: HostConfig, diff --git a/src/host/manual.rs b/src/host/manual.rs index 0f61022..5b0b32c 100644 --- a/src/host/manual.rs +++ b/src/host/manual.rs @@ -3,6 +3,8 @@ use crate::host::{Host, HostConfig}; use crate::MacAddress; use embedded_nal::Ipv4Addr; +#[derive(Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct Manual { is_setup: bool, settings: HostConfig, diff --git a/src/host/mod.rs b/src/host/mod.rs index 206ec91..8aa1b79 100644 --- a/src/host/mod.rs +++ b/src/host/mod.rs @@ -8,10 +8,15 @@ use crate::register; use crate::MacAddress; use embedded_nal::Ipv4Addr; +#[derive(Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct HostConfig { mac: MacAddress, + #[cfg_attr(feature = "defmt", defmt(Display2Format))] ip: Ipv4Addr, + #[cfg_attr(feature = "defmt", defmt(Display2Format))] gateway: Ipv4Addr, + #[cfg_attr(feature = "defmt", defmt(Display2Format))] subnet: Ipv4Addr, } diff --git a/src/lib.rs b/src/lib.rs index ac237a7..cbe9761 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,7 +22,8 @@ pub use uninitialized_device::UninitializedDevice; /// Settings for wake on LAN. Allows the W5500 to optionally emit an interrupt upon receiving a packet #[repr(u8)] -#[derive(Copy, Clone, PartialOrd, PartialEq)] +#[derive(Copy, Clone, Debug, PartialOrd, PartialEq)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum OnWakeOnLan { InvokeInterrupt = 0b00100000, Ignore = 0b00000000, @@ -30,7 +31,8 @@ pub enum OnWakeOnLan { /// Settings for ping. Allows the W5500 to respond to or ignore network ping requests #[repr(u8)] -#[derive(Copy, Clone, PartialOrd, PartialEq)] +#[derive(Copy, Clone, Debug, PartialOrd, PartialEq)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum OnPingRequest { Respond = 0b00000000, Ignore = 0b00010000, @@ -39,20 +41,23 @@ pub enum OnPingRequest { /// Use [ConnectionType::PPoE] when talking /// to an ADSL modem. Otherwise use [ConnectionType::Ethernet] #[repr(u8)] -#[derive(Copy, Clone, PartialOrd, PartialEq)] +#[derive(Copy, Clone, Debug, PartialOrd, PartialEq)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum ConnectionType { PPoE = 0b00001000, Ethernet = 0b00000000, } -#[derive(Copy, Clone, PartialOrd, PartialEq)] +#[derive(Copy, Clone, Debug, PartialOrd, PartialEq)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] #[repr(u8)] pub enum ArpResponses { Cache = 0b00000000, DropAfterUse = 0b00000010, } -#[derive(Copy, Clone, PartialEq)] +#[derive(Copy, Clone, Debug, PartialEq)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct Mode { pub on_wake_on_lan: OnWakeOnLan, pub on_ping_request: OnPingRequest, diff --git a/src/net.rs b/src/net.rs index c77cef2..b504bdd 100644 --- a/src/net.rs +++ b/src/net.rs @@ -18,6 +18,7 @@ pub use embedded_nal::Ipv4Addr; /// /// This is an EUI-48 MAC address (previously called MAC-48). #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash, Default)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct MacAddress { /// Octets of the MAC address. pub octets: [u8; 6], diff --git a/src/raw_device.rs b/src/raw_device.rs index fe80a6d..25413fc 100644 --- a/src/raw_device.rs +++ b/src/raw_device.rs @@ -1,6 +1,8 @@ use crate::{bus::Bus, register, socket::Socket, uninitialized_device::InitializeError}; /// The W5500 operating in MACRAW mode to send and receive ethernet frames. +#[derive(Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct RawDevice { bus: SpiBus, raw_socket: Socket, diff --git a/src/tcp.rs b/src/tcp.rs index 99ecc11..14e7c57 100644 --- a/src/tcp.rs +++ b/src/tcp.rs @@ -11,11 +11,12 @@ use embedded_nal::{nb, IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4, TcpClientStac use core::convert::TryFrom; #[derive(Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum TcpSocketError { NoMoreSockets, NotReady, UnsupportedAddress, - Other(E), + Other(#[cfg_attr(feature = "defmt", defmt(Debug2Format))] E), UnsupportedMode, } @@ -25,6 +26,8 @@ impl From for TcpSocketError { } } +#[derive(Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct TcpSocket { socket: Socket, } diff --git a/src/uninitialized_device.rs b/src/uninitialized_device.rs index 30c7975..ae230a3 100644 --- a/src/uninitialized_device.rs +++ b/src/uninitialized_device.rs @@ -9,6 +9,8 @@ use crate::raw_device::RawDevice; use crate::register; use crate::{MacAddress, Mode}; +#[derive(Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct UninitializedDevice { bus: SpiBus, } From c94b8c624143c1da9563ae4fb342ea5477e275e8 Mon Sep 17 00:00:00 2001 From: Lachezar Lechev Date: Mon, 6 Mar 2023 12:55:14 +0200 Subject: [PATCH 5/8] chore: Update CHANGELOG adding `defmt` feature Signed-off-by: Lachezar Lechev --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d68a2e..3b68d4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Add support for MACRAW operation mode [#33](https://github.com/kellerkindt/w5500/pull/33) +- Add `defmt` features for enabling `defmt::Format` to most structs and errors [#39](https://github.com/kellerkindt/w5500/pull/39) ## [0.4.0] - January 22nd, 2022 From 19343e3886d0f2f36aad444242dbd23d29e47726 Mon Sep 17 00:00:00 2001 From: Lachezar Lechev Date: Mon, 6 Mar 2023 13:20:37 +0200 Subject: [PATCH 6/8] fix: CHANGELOG to unreleased version Signed-off-by: Lachezar Lechev --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32a7112..367b67c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add `defmt` features for enabling `defmt::Format` to most structs and errors by [@elpiel](https://github.com/elpiel) ([#39](https://github.com/kellerkindt/w5500/issues/39)) ## [0.4.1] - January 2nd, 2023 @@ -13,7 +16,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix indexing for phy configuration register by [@Wassasin](https://github.com/Wassasin) ([#32](https://github.com/kellerkindt/w5500/issues/32)) - Add support for MACRAW operation mode by [@ryan-summers](https://github.com/ryan-summers) ([#33](https://github.com/kellerkindt/w5500/issues/33)) -- Add `defmt` features for enabling `defmt::Format` to most structs and errors by [@elpiel](https://github.com/elpiel) ([#39](https://github.com/kellerkindt/w5500/issues/39)) ## [0.4.0] - January 22nd, 2022 From 3e41fcfe614e8a7ae64b12a847b53d4cfb9415ea Mon Sep 17 00:00:00 2001 From: Lachezar Lechev Date: Mon, 6 Mar 2023 13:20:49 +0200 Subject: [PATCH 7/8] ci: update rust workflow Signed-off-by: Lachezar Lechev --- .github/workflows/rust.yml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 84b5811..82fcbc6 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -11,7 +11,7 @@ jobs: name: Check runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - uses: actions-rs/toolchain@v1 with: profile: minimal @@ -20,12 +20,13 @@ jobs: - uses: actions-rs/cargo@v1 with: command: check + args: --all-features test: name: Test Suite runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - uses: actions-rs/toolchain@v1 with: profile: minimal @@ -34,12 +35,13 @@ jobs: - uses: actions-rs/cargo@v1 with: command: test + args: --all-features fmt: name: Rustfmt runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - uses: actions-rs/toolchain@v1 with: profile: minimal @@ -55,7 +57,7 @@ jobs: name: Clippy runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - uses: actions-rs/toolchain@v1 with: profile: minimal From a0c8a42d64c6b41643b7f5f461246b58b77be0ec Mon Sep 17 00:00:00 2001 From: Lachezar Lechev Date: Mon, 6 Mar 2023 14:53:48 +0200 Subject: [PATCH 8/8] fix: export only InitializeError from the module Signed-off-by: Lachezar Lechev --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index cbe9761..d3f9ff7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,12 +11,12 @@ pub mod register; mod socket; pub mod tcp; pub mod udp; -pub mod uninitialized_device; +mod uninitialized_device; pub use device::{Device, DeviceRefMut, InactiveDevice}; pub use host::{Dhcp, Host, HostConfig, Manual}; pub use net::MacAddress; -pub use uninitialized_device::UninitializedDevice; +pub use uninitialized_device::{InitializeError, UninitializedDevice}; // TODO add better docs to all public items, add unit tests.