From 96c6edfd810484bb2d0d5565f7ec72bae588c9d9 Mon Sep 17 00:00:00 2001 From: Michael Watzko Date: Fri, 16 Apr 2021 01:28:27 +0200 Subject: [PATCH] Take ownership of Spi in activate_ref --- README.md | 2 +- src/bus/four_wire_ref.rs | 6 ++---- src/bus/mod.rs | 16 ++++++++++++++++ src/device.rs | 28 +++++++--------------------- src/lib.rs | 2 +- src/udp.rs | 16 ++++++++-------- 6 files changed, 35 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index ba57530..c14f7a1 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ Usage of borrowed SPI-Bus and previously initialized `Device`: let mut buffer = [0u8; 1024]; match device // scoped activation & usage of the SPI bus without move - .activate_ref(&mut FourWireRef::new(&mut spi, &mut cs)) + .activate_ref(FourWireRef::new(&mut spi, &mut cs)) .receive(socket, &mut buffer) { Ok(..) => todo!(), diff --git a/src/bus/four_wire_ref.rs b/src/bus/four_wire_ref.rs index 4bb6fa0..af3265b 100644 --- a/src/bus/four_wire_ref.rs +++ b/src/bus/four_wire_ref.rs @@ -6,8 +6,6 @@ use embedded_hal::digital::v2::OutputPin; use crate::bus::{Bus, FourWire, FourWireError}; -const WRITE_MODE_MASK: u8 = 0b00000_1_00; - // 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 @@ -45,7 +43,7 @@ impl + Write, ChipSelect: OutputPin> Bus } #[derive(Debug)] -pub struct SpiRef<'a, Spi: Transfer + Write>(&'a mut Spi); +pub struct SpiRef<'a, Spi: Transfer + Write>(pub &'a mut Spi); impl<'a, Spi: Transfer + Write> Transfer for SpiRef<'a, Spi> { type Error = >::Error; @@ -66,7 +64,7 @@ impl<'a, Spi: Transfer + Write> Write for SpiRef<'a, Spi> { } #[derive(Debug)] -pub struct OutputPinRef<'a, P: OutputPin>(&'a mut P); +pub struct OutputPinRef<'a, P: OutputPin>(pub &'a mut P); impl<'a, P: OutputPin> OutputPin for OutputPinRef<'a, P> { type Error = P::Error; diff --git a/src/bus/mod.rs b/src/bus/mod.rs index a51c0c5..f7b7f17 100644 --- a/src/bus/mod.rs +++ b/src/bus/mod.rs @@ -19,3 +19,19 @@ pub trait Bus { fn write_frame(&mut self, block: u8, address: u16, data: &[u8]) -> Result<(), Self::Error>; } + +pub struct BusRef<'a, B: Bus>(pub &'a mut B); + +impl Bus for BusRef<'_, B> { + type Error = B::Error; + + #[inline] + fn read_frame(&mut self, block: u8, address: u16, data: &mut [u8]) -> Result<(), Self::Error> { + self.0.read_frame(block, address, data) + } + + #[inline] + fn write_frame(&mut self, block: u8, address: u16, data: &[u8]) -> Result<(), Self::Error> { + self.0.write_frame(block, address, data) + } +} diff --git a/src/device.rs b/src/device.rs index f3b9e14..107a600 100644 --- a/src/device.rs +++ b/src/device.rs @@ -1,7 +1,7 @@ use bit_field::BitArray; use embedded_hal::digital::v2::OutputPin; -use crate::bus::{Bus, FourWire, ThreeWire}; +use crate::bus::{Bus, BusRef, FourWire, SpiRef, ThreeWire}; use crate::host::Host; use crate::net::Ipv4Addr; use crate::socket::Socket; @@ -88,9 +88,9 @@ impl Device { } #[inline] - pub(crate) fn as_mut(&mut self) -> DeviceRefMut<'_, SpiBus, HostImpl> { + pub(crate) fn as_mut(&mut self) -> DeviceRefMut<'_, BusRef<'_, SpiBus>, HostImpl> { DeviceRefMut { - bus: &mut self.bus, + bus: BusRef(&mut self.bus), state: &mut self.state, } } @@ -104,30 +104,16 @@ impl Device { } } -impl<'a, SpiBus: Bus, HostImpl: Host> From<&'a mut Device> - for DeviceRefMut<'a, SpiBus, HostImpl> -{ - fn from(device: &'a mut Device) -> Self { - DeviceRefMut { - bus: &mut device.bus, - state: &mut device.state, - } - } -} - pub struct InactiveDevice(DeviceState); impl InactiveDevice { - /// Activates the device by ownership + /// Activates the device by taking ownership pub fn activate(self, bus: SpiBus) -> Device { Device { bus, state: self.0 } } - /// Activates the device by borrowing - pub fn activate_ref<'a, SpiBus: Bus>( - &'a mut self, - bus: &'a mut SpiBus, - ) -> DeviceRefMut<'a, SpiBus, HostImpl> { + /// Activates the device by borrowing it + pub fn activate_ref(&mut self, bus: SpiBus) -> DeviceRefMut { DeviceRefMut { bus, state: &mut self.0, @@ -136,7 +122,7 @@ impl InactiveDevice { } pub struct DeviceRefMut<'a, SpiBus: Bus, HostImpl: Host> { - pub(crate) bus: &'a mut SpiBus, + pub(crate) bus: SpiBus, state: &'a mut DeviceState, } diff --git a/src/lib.rs b/src/lib.rs index 2bde081..0e795d0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,7 +11,7 @@ mod socket; pub mod udp; mod uninitialized_device; -pub use device::{Device, InactiveDevice}; +pub use device::{Device, DeviceRefMut, InactiveDevice}; pub use host::{Dhcp, HostConfig, Manual}; pub use net::MacAddress; pub use uninitialized_device::UninitializedDevice; diff --git a/src/udp.rs b/src/udp.rs index 63c6087..b92d46e 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -245,9 +245,9 @@ where ) -> 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(self.bus, remote)?; + 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) @@ -255,7 +255,7 @@ where } fn send(&mut self, socket: &mut Self::UdpSocket, buffer: &[u8]) -> nb::Result<(), Self::Error> { - socket.send(self.bus, buffer)?; + socket.send(&mut self.bus, buffer)?; Ok(()) } @@ -264,11 +264,11 @@ where socket: &mut Self::UdpSocket, buffer: &mut [u8], ) -> nb::Result<(usize, SocketAddr), Self::Error> { - Ok(socket.receive(self.bus, buffer)?) + Ok(socket.receive(&mut self.bus, buffer)?) } fn close(&mut self, socket: Self::UdpSocket) -> Result<(), Self::Error> { - socket.close(self.bus)?; + socket.close(&mut self.bus)?; self.release_socket(socket.socket); Ok(()) } @@ -301,7 +301,7 @@ where HostImpl: Host, { fn bind(&mut self, socket: &mut Self::UdpSocket, local_port: u16) -> Result<(), Self::Error> { - socket.open(self.bus, local_port)?; + socket.open(&mut self.bus, local_port)?; Ok(()) } @@ -312,7 +312,7 @@ where buffer: &[u8], ) -> nb::Result<(), Self::Error> { if let SocketAddr::V4(remote) = remote { - socket.send_to(self.bus, remote, buffer)?; + socket.send_to(&mut self.bus, remote, buffer)?; Ok(()) } else { Err(nb::Error::Other(Self::Error::UnsupportedAddress))