Take ownership of Spi in activate_ref

This commit is contained in:
Michael Watzko 2021-04-16 01:28:27 +02:00
commit 96c6edfd81
6 changed files with 35 additions and 35 deletions

View file

@ -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!(),

View file

@ -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<Spi: Transfer<u8> + Write<u8>, ChipSelect: OutputPin> Bus
}
#[derive(Debug)]
pub struct SpiRef<'a, Spi: Transfer<u8> + Write<u8>>(&'a mut Spi);
pub struct SpiRef<'a, Spi: Transfer<u8> + Write<u8>>(pub &'a mut Spi);
impl<'a, Spi: Transfer<u8> + Write<u8>> Transfer<u8> for SpiRef<'a, Spi> {
type Error = <Spi as Transfer<u8>>::Error;
@ -66,7 +64,7 @@ impl<'a, Spi: Transfer<u8> + Write<u8>> Write<u8> 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;

View file

@ -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<B: Bus> 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)
}
}

View file

@ -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<SpiBus: Bus, HostImpl: Host> Device<SpiBus, HostImpl> {
}
#[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<SpiBus: Bus, HostImpl: Host> Device<SpiBus, HostImpl> {
}
}
impl<'a, SpiBus: Bus, HostImpl: Host> From<&'a mut Device<SpiBus, HostImpl>>
for DeviceRefMut<'a, SpiBus, HostImpl>
{
fn from(device: &'a mut Device<SpiBus, HostImpl>) -> Self {
DeviceRefMut {
bus: &mut device.bus,
state: &mut device.state,
}
}
}
pub struct InactiveDevice<HostImpl: Host>(DeviceState<HostImpl>);
impl<HostImpl: Host> InactiveDevice<HostImpl> {
/// Activates the device by ownership
/// Activates the device by taking ownership
pub fn activate<SpiBus: Bus>(self, bus: SpiBus) -> Device<SpiBus, HostImpl> {
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<SpiBus: Bus>(&mut self, bus: SpiBus) -> DeviceRefMut<SpiBus, HostImpl> {
DeviceRefMut {
bus,
state: &mut self.0,
@ -136,7 +122,7 @@ impl<HostImpl: Host> InactiveDevice<HostImpl> {
}
pub struct DeviceRefMut<'a, SpiBus: Bus, HostImpl: Host> {
pub(crate) bus: &'a mut SpiBus,
pub(crate) bus: SpiBus,
state: &'a mut DeviceState<HostImpl>,
}

View file

@ -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;

View file

@ -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))