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]
|
[dependencies]
|
||||||
byteorder = { version = "1.3.4", default-features = false }
|
byteorder = { version = "1.3.4", default-features = false }
|
||||||
embedded-hal = "0.2.4"
|
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"
|
bit_field = "0.10.1"
|
||||||
nb = "1.0.0"
|
nb = "1.0.0"
|
||||||
|
|
|
||||||
|
|
@ -74,10 +74,6 @@ impl<SpiBus: ActiveBus, HostImpl: Host> Device<SpiBus, HostImpl> {
|
||||||
Ok(phy[0].into())
|
Ok(phy[0].into())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn into_interface(self) -> Interface<SpiBus, HostImpl> {
|
|
||||||
self.into()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn release_socket(&mut self, socket: Socket) {
|
pub fn release_socket(&mut self, socket: Socket) {
|
||||||
self.sockets.set_bit(socket.index.into(), true);
|
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 device;
|
||||||
mod host;
|
mod host;
|
||||||
mod inactive_device;
|
mod inactive_device;
|
||||||
pub mod interface;
|
|
||||||
mod network;
|
|
||||||
pub mod net;
|
pub mod net;
|
||||||
pub mod register;
|
pub mod register;
|
||||||
mod socket;
|
mod socket;
|
||||||
mod udp;
|
mod udp;
|
||||||
pub mod uninitialized_device;
|
pub mod uninitialized_device;
|
||||||
|
|
||||||
pub use bus::ActiveFourWire;
|
pub use net::MacAddress;
|
||||||
pub use interface::Interface;
|
|
||||||
|
|
|
||||||
28
src/udp.rs
28
src/udp.rs
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::bus::ActiveBus;
|
use crate::bus::ActiveBus;
|
||||||
|
use crate::device::Device;
|
||||||
use crate::host::Host;
|
use crate::host::Host;
|
||||||
use crate::interface::Interface;
|
|
||||||
use crate::register::socketn;
|
use crate::register::socketn;
|
||||||
use crate::socket::Socket;
|
use crate::socket::Socket;
|
||||||
use core::fmt::Debug;
|
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
|
where
|
||||||
SpiBus: ActiveBus,
|
SpiBus: ActiveBus,
|
||||||
HostImpl: Host,
|
HostImpl: Host,
|
||||||
|
|
@ -187,8 +187,7 @@ where
|
||||||
type Error = UdpSocketError<SpiBus::Error>;
|
type Error = UdpSocketError<SpiBus::Error>;
|
||||||
|
|
||||||
fn socket(&mut self) -> Result<Self::UdpSocket, Self::Error> {
|
fn socket(&mut self) -> Result<Self::UdpSocket, Self::Error> {
|
||||||
let mut device = &mut self.device;
|
if let Some(socket) = self.take_socket() {
|
||||||
if let Some(socket) = device.take_socket() {
|
|
||||||
Ok(UdpSocket::new(socket))
|
Ok(UdpSocket::new(socket))
|
||||||
} else {
|
} else {
|
||||||
Err(Self::Error::NoMoreSockets)
|
Err(Self::Error::NoMoreSockets)
|
||||||
|
|
@ -200,18 +199,17 @@ where
|
||||||
socket: &mut Self::UdpSocket,
|
socket: &mut Self::UdpSocket,
|
||||||
remote: SocketAddr,
|
remote: SocketAddr,
|
||||||
) -> Result<(), Self::Error> {
|
) -> Result<(), Self::Error> {
|
||||||
let mut device = &mut self.device;
|
|
||||||
if let SocketAddr::V4(remote) = remote {
|
if let SocketAddr::V4(remote) = remote {
|
||||||
// TODO find a random port
|
// TODO find a random port
|
||||||
socket.open(&mut device.bus, 4000)?;
|
socket.open(&mut self.bus, 4000)?;
|
||||||
socket.set_destination(&mut device.bus, remote)?;
|
socket.set_destination(&mut self.bus, remote)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(Self::Error::UnsupportedAddress)
|
Err(Self::Error::UnsupportedAddress)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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(&mut self.device.bus, buffer)?;
|
socket.send(&mut self.bus, buffer)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
fn receive(
|
fn receive(
|
||||||
|
|
@ -219,24 +217,22 @@ 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(&mut self.device.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> {
|
||||||
let mut device = &mut self.device;
|
socket.close(&mut self.bus)?;
|
||||||
socket.close(&mut device.bus)?;
|
self.release_socket(socket.socket);
|
||||||
device.release_socket(socket.socket);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<SpiBus, HostImpl> UdpFullStack for Interface<SpiBus, HostImpl>
|
impl<SpiBus, HostImpl> UdpFullStack for Device<SpiBus, HostImpl>
|
||||||
where
|
where
|
||||||
SpiBus: ActiveBus,
|
SpiBus: ActiveBus,
|
||||||
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> {
|
||||||
let mut device = &mut self.device;
|
socket.open(&mut self.bus, local_port)?;
|
||||||
socket.open(&mut device.bus, local_port)?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
fn send_to(
|
fn send_to(
|
||||||
|
|
@ -246,7 +242,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(&mut self.device.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