Updated embedded-nal and removed (now-redundent) Interface struct
This commit is contained in:
parent
d6574b8c23
commit
55097322a0
5 changed files with 14 additions and 71 deletions
|
|
@ -13,6 +13,6 @@ edition = "2018"
|
|||
[dependencies]
|
||||
byteorder = { version = "1.3.4", default-features = false }
|
||||
embedded-hal = "0.2.4"
|
||||
embedded-nal = { git = "https://github.com/rust-embedded-community/embedded-nal.git", rev = "0bc4d77" }
|
||||
embedded-nal = "0.3.0"
|
||||
bit_field = "0.10.1"
|
||||
nb = "1.0.0"
|
||||
|
|
|
|||
|
|
@ -74,10 +74,6 @@ impl<SpiBus: ActiveBus, HostImpl: Host> Device<SpiBus, HostImpl> {
|
|||
Ok(phy[0].into())
|
||||
}
|
||||
|
||||
pub fn into_interface(self) -> Interface<SpiBus, HostImpl> {
|
||||
self.into()
|
||||
}
|
||||
|
||||
pub fn release_socket(&mut self, socket: Socket) {
|
||||
self.sockets.set_bit(socket.index.into(), true);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,46 +0,0 @@
|
|||
use embedded_hal::digital::v2::OutputPin;
|
||||
use embedded_hal::spi::FullDuplex;
|
||||
use embedded_nal::Ipv4Addr;
|
||||
|
||||
use crate::bus::{ActiveBus, ActiveFourWire, FourWire};
|
||||
use crate::device::Device;
|
||||
use crate::host::{Host, Manual};
|
||||
use crate::uninitialized_device::{InitializeError, UninitializedDevice};
|
||||
use crate::{MacAddress, Mode};
|
||||
|
||||
pub struct Interface<SpiBus: ActiveBus, HostImpl: Host> {
|
||||
pub device: Device<SpiBus, HostImpl>,
|
||||
}
|
||||
|
||||
impl<SpiBus: ActiveBus, HostImpl: Host> Interface<SpiBus, HostImpl> {
|
||||
fn new(device: Device<SpiBus, HostImpl>) -> Self {
|
||||
Self { device }
|
||||
}
|
||||
|
||||
pub fn release(self) -> Device<SpiBus, HostImpl> {
|
||||
self.device
|
||||
}
|
||||
}
|
||||
|
||||
impl<Spi: FullDuplex<u8>, ChipSelect: OutputPin>
|
||||
Interface<ActiveFourWire<Spi, ChipSelect>, Manual>
|
||||
{
|
||||
pub fn setup(
|
||||
spi: Spi,
|
||||
cs: ChipSelect,
|
||||
mac: MacAddress,
|
||||
ip: Ipv4Addr,
|
||||
) -> Result<Self, InitializeError<<ActiveFourWire<Spi, ChipSelect> as ActiveBus>::Error>> {
|
||||
Ok(UninitializedDevice::new(FourWire::new(cs).activate(spi))
|
||||
.initialize_manual(mac, ip, Mode::default())?
|
||||
.into_interface())
|
||||
}
|
||||
}
|
||||
|
||||
impl<SpiBus: ActiveBus, HostImpl: Host> From<Device<SpiBus, HostImpl>>
|
||||
for Interface<SpiBus, HostImpl>
|
||||
{
|
||||
fn from(device: Device<SpiBus, HostImpl>) -> Interface<SpiBus, HostImpl> {
|
||||
Interface::<SpiBus, HostImpl>::new(device)
|
||||
}
|
||||
}
|
||||
|
|
@ -74,13 +74,10 @@ pub mod bus;
|
|||
mod device;
|
||||
mod host;
|
||||
mod inactive_device;
|
||||
pub mod interface;
|
||||
mod network;
|
||||
pub mod net;
|
||||
pub mod register;
|
||||
mod socket;
|
||||
mod udp;
|
||||
pub mod uninitialized_device;
|
||||
|
||||
pub use bus::ActiveFourWire;
|
||||
pub use interface::Interface;
|
||||
pub use net::MacAddress;
|
||||
|
|
|
|||
28
src/udp.rs
28
src/udp.rs
|
|
@ -1,6 +1,6 @@
|
|||
use crate::bus::ActiveBus;
|
||||
use crate::device::Device;
|
||||
use crate::host::Host;
|
||||
use crate::interface::Interface;
|
||||
use crate::register::socketn;
|
||||
use crate::socket::Socket;
|
||||
use core::fmt::Debug;
|
||||
|
|
@ -178,7 +178,7 @@ impl<E: Debug> From<NbError<E>> for nb::Error<E> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<SpiBus, HostImpl> UdpClientStack for Interface<SpiBus, HostImpl>
|
||||
impl<SpiBus, HostImpl> UdpClientStack for Device<SpiBus, HostImpl>
|
||||
where
|
||||
SpiBus: ActiveBus,
|
||||
HostImpl: Host,
|
||||
|
|
@ -187,8 +187,7 @@ where
|
|||
type Error = UdpSocketError<SpiBus::Error>;
|
||||
|
||||
fn socket(&mut self) -> Result<Self::UdpSocket, Self::Error> {
|
||||
let mut device = &mut self.device;
|
||||
if let Some(socket) = device.take_socket() {
|
||||
if let Some(socket) = self.take_socket() {
|
||||
Ok(UdpSocket::new(socket))
|
||||
} else {
|
||||
Err(Self::Error::NoMoreSockets)
|
||||
|
|
@ -200,18 +199,17 @@ where
|
|||
socket: &mut Self::UdpSocket,
|
||||
remote: SocketAddr,
|
||||
) -> Result<(), Self::Error> {
|
||||
let mut device = &mut self.device;
|
||||
if let SocketAddr::V4(remote) = remote {
|
||||
// TODO find a random port
|
||||
socket.open(&mut device.bus, 4000)?;
|
||||
socket.set_destination(&mut device.bus, remote)?;
|
||||
socket.open(&mut self.bus, 4000)?;
|
||||
socket.set_destination(&mut self.bus, remote)?;
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Self::Error::UnsupportedAddress)
|
||||
}
|
||||
}
|
||||
fn send(&mut self, socket: &mut Self::UdpSocket, buffer: &[u8]) -> nb::Result<(), Self::Error> {
|
||||
socket.send(&mut self.device.bus, buffer)?;
|
||||
socket.send(&mut self.bus, buffer)?;
|
||||
Ok(())
|
||||
}
|
||||
fn receive(
|
||||
|
|
@ -219,24 +217,22 @@ where
|
|||
socket: &mut Self::UdpSocket,
|
||||
buffer: &mut [u8],
|
||||
) -> nb::Result<(usize, SocketAddr), Self::Error> {
|
||||
Ok(socket.receive(&mut self.device.bus, buffer)?)
|
||||
Ok(socket.receive(&mut self.bus, buffer)?)
|
||||
}
|
||||
fn close(&mut self, socket: Self::UdpSocket) -> Result<(), Self::Error> {
|
||||
let mut device = &mut self.device;
|
||||
socket.close(&mut device.bus)?;
|
||||
device.release_socket(socket.socket);
|
||||
socket.close(&mut self.bus)?;
|
||||
self.release_socket(socket.socket);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<SpiBus, HostImpl> UdpFullStack for Interface<SpiBus, HostImpl>
|
||||
impl<SpiBus, HostImpl> UdpFullStack for Device<SpiBus, HostImpl>
|
||||
where
|
||||
SpiBus: ActiveBus,
|
||||
HostImpl: Host,
|
||||
{
|
||||
fn bind(&mut self, socket: &mut Self::UdpSocket, local_port: u16) -> Result<(), Self::Error> {
|
||||
let mut device = &mut self.device;
|
||||
socket.open(&mut device.bus, local_port)?;
|
||||
socket.open(&mut self.bus, local_port)?;
|
||||
Ok(())
|
||||
}
|
||||
fn send_to(
|
||||
|
|
@ -246,7 +242,7 @@ where
|
|||
buffer: &[u8],
|
||||
) -> nb::Result<(), Self::Error> {
|
||||
if let SocketAddr::V4(remote) = remote {
|
||||
socket.send_to(&mut self.device.bus, remote, buffer)?;
|
||||
socket.send_to(&mut self.bus, remote, buffer)?;
|
||||
Ok(())
|
||||
} else {
|
||||
Err(nb::Error::Other(Self::Error::UnsupportedAddress))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue