Take ownership of Spi in activate_ref
This commit is contained in:
parent
3489574603
commit
96c6edfd81
6 changed files with 35 additions and 35 deletions
|
|
@ -65,7 +65,7 @@ Usage of borrowed SPI-Bus and previously initialized `Device`:
|
||||||
let mut buffer = [0u8; 1024];
|
let mut buffer = [0u8; 1024];
|
||||||
match device
|
match device
|
||||||
// scoped activation & usage of the SPI bus without move
|
// 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)
|
.receive(socket, &mut buffer)
|
||||||
{
|
{
|
||||||
Ok(..) => todo!(),
|
Ok(..) => todo!(),
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,6 @@ use embedded_hal::digital::v2::OutputPin;
|
||||||
|
|
||||||
use crate::bus::{Bus, FourWire, FourWireError};
|
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
|
// TODO This name is not ideal, should be renamed to VDM
|
||||||
/// This is just like [crate::bus::FourWire] but takes references instead of ownership
|
/// This is just like [crate::bus::FourWire] but takes references instead of ownership
|
||||||
/// for the SPI bus and the ChipSelect pin
|
/// for the SPI bus and the ChipSelect pin
|
||||||
|
|
@ -45,7 +43,7 @@ impl<Spi: Transfer<u8> + Write<u8>, ChipSelect: OutputPin> Bus
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[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> {
|
impl<'a, Spi: Transfer<u8> + Write<u8>> Transfer<u8> for SpiRef<'a, Spi> {
|
||||||
type Error = <Spi as Transfer<u8>>::Error;
|
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)]
|
#[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> {
|
impl<'a, P: OutputPin> OutputPin for OutputPinRef<'a, P> {
|
||||||
type Error = P::Error;
|
type Error = P::Error;
|
||||||
|
|
|
||||||
|
|
@ -19,3 +19,19 @@ pub trait Bus {
|
||||||
|
|
||||||
fn write_frame(&mut self, block: u8, address: u16, data: &[u8]) -> Result<(), Self::Error>;
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
use bit_field::BitArray;
|
use bit_field::BitArray;
|
||||||
use embedded_hal::digital::v2::OutputPin;
|
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::host::Host;
|
||||||
use crate::net::Ipv4Addr;
|
use crate::net::Ipv4Addr;
|
||||||
use crate::socket::Socket;
|
use crate::socket::Socket;
|
||||||
|
|
@ -88,9 +88,9 @@ impl<SpiBus: Bus, HostImpl: Host> Device<SpiBus, HostImpl> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn as_mut(&mut self) -> DeviceRefMut<'_, SpiBus, HostImpl> {
|
pub(crate) fn as_mut(&mut self) -> DeviceRefMut<'_, BusRef<'_, SpiBus>, HostImpl> {
|
||||||
DeviceRefMut {
|
DeviceRefMut {
|
||||||
bus: &mut self.bus,
|
bus: BusRef(&mut self.bus),
|
||||||
state: &mut self.state,
|
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>);
|
pub struct InactiveDevice<HostImpl: Host>(DeviceState<HostImpl>);
|
||||||
|
|
||||||
impl<HostImpl: Host> InactiveDevice<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> {
|
pub fn activate<SpiBus: Bus>(self, bus: SpiBus) -> Device<SpiBus, HostImpl> {
|
||||||
Device { bus, state: self.0 }
|
Device { bus, state: self.0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Activates the device by borrowing
|
/// Activates the device by borrowing it
|
||||||
pub fn activate_ref<'a, SpiBus: Bus>(
|
pub fn activate_ref<SpiBus: Bus>(&mut self, bus: SpiBus) -> DeviceRefMut<SpiBus, HostImpl> {
|
||||||
&'a mut self,
|
|
||||||
bus: &'a mut SpiBus,
|
|
||||||
) -> DeviceRefMut<'a, SpiBus, HostImpl> {
|
|
||||||
DeviceRefMut {
|
DeviceRefMut {
|
||||||
bus,
|
bus,
|
||||||
state: &mut self.0,
|
state: &mut self.0,
|
||||||
|
|
@ -136,7 +122,7 @@ impl<HostImpl: Host> InactiveDevice<HostImpl> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct DeviceRefMut<'a, SpiBus: Bus, HostImpl: Host> {
|
pub struct DeviceRefMut<'a, SpiBus: Bus, HostImpl: Host> {
|
||||||
pub(crate) bus: &'a mut SpiBus,
|
pub(crate) bus: SpiBus,
|
||||||
state: &'a mut DeviceState<HostImpl>,
|
state: &'a mut DeviceState<HostImpl>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ mod socket;
|
||||||
pub mod udp;
|
pub mod udp;
|
||||||
mod uninitialized_device;
|
mod uninitialized_device;
|
||||||
|
|
||||||
pub use device::{Device, InactiveDevice};
|
pub use device::{Device, DeviceRefMut, InactiveDevice};
|
||||||
pub use host::{Dhcp, HostConfig, Manual};
|
pub use host::{Dhcp, HostConfig, Manual};
|
||||||
pub use net::MacAddress;
|
pub use net::MacAddress;
|
||||||
pub use uninitialized_device::UninitializedDevice;
|
pub use uninitialized_device::UninitializedDevice;
|
||||||
|
|
|
||||||
16
src/udp.rs
16
src/udp.rs
|
|
@ -245,9 +245,9 @@ where
|
||||||
) -> Result<(), Self::Error> {
|
) -> Result<(), Self::Error> {
|
||||||
if let SocketAddr::V4(remote) = remote {
|
if let SocketAddr::V4(remote) = remote {
|
||||||
// TODO dynamically select a random port
|
// TODO dynamically select a random port
|
||||||
socket.open(&mut *self.bus, 49849 + u16::from(socket.socket.index))?; // chosen by fair dice roll.
|
socket.open(&mut self.bus, 49849 + u16::from(socket.socket.index))?; // chosen by fair dice roll.
|
||||||
// guaranteed to be random.
|
// guaranteed to be random.
|
||||||
socket.set_destination(self.bus, remote)?;
|
socket.set_destination(&mut self.bus, remote)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(Self::Error::UnsupportedAddress)
|
Err(Self::Error::UnsupportedAddress)
|
||||||
|
|
@ -255,7 +255,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send(&mut self, socket: &mut Self::UdpSocket, buffer: &[u8]) -> nb::Result<(), Self::Error> {
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -264,11 +264,11 @@ where
|
||||||
socket: &mut Self::UdpSocket,
|
socket: &mut Self::UdpSocket,
|
||||||
buffer: &mut [u8],
|
buffer: &mut [u8],
|
||||||
) -> nb::Result<(usize, SocketAddr), Self::Error> {
|
) -> 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> {
|
fn close(&mut self, socket: Self::UdpSocket) -> Result<(), Self::Error> {
|
||||||
socket.close(self.bus)?;
|
socket.close(&mut self.bus)?;
|
||||||
self.release_socket(socket.socket);
|
self.release_socket(socket.socket);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
@ -301,7 +301,7 @@ where
|
||||||
HostImpl: Host,
|
HostImpl: Host,
|
||||||
{
|
{
|
||||||
fn bind(&mut self, socket: &mut Self::UdpSocket, local_port: u16) -> Result<(), Self::Error> {
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -312,7 +312,7 @@ where
|
||||||
buffer: &[u8],
|
buffer: &[u8],
|
||||||
) -> nb::Result<(), Self::Error> {
|
) -> nb::Result<(), Self::Error> {
|
||||||
if let SocketAddr::V4(remote) = remote {
|
if let SocketAddr::V4(remote) = remote {
|
||||||
socket.send_to(self.bus, remote, buffer)?;
|
socket.send_to(&mut self.bus, remote, buffer)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(nb::Error::Other(Self::Error::UnsupportedAddress))
|
Err(nb::Error::Other(Self::Error::UnsupportedAddress))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue