Ran cargo fmt
This commit is contained in:
parent
1d533b2104
commit
79b1f52102
6 changed files with 44 additions and 44 deletions
|
|
@ -4,10 +4,10 @@ use bit_field::BitArray;
|
||||||
use bus::{ActiveBus, ActiveFourWire, ActiveThreeWire, FourWire, ThreeWire};
|
use bus::{ActiveBus, ActiveFourWire, ActiveThreeWire, FourWire, ThreeWire};
|
||||||
use embedded_hal::digital::v2::OutputPin;
|
use embedded_hal::digital::v2::OutputPin;
|
||||||
use embedded_hal::spi::FullDuplex;
|
use embedded_hal::spi::FullDuplex;
|
||||||
|
use interface::Interface;
|
||||||
use network::Network;
|
use network::Network;
|
||||||
use register;
|
use register;
|
||||||
use socket::Socket;
|
use socket::Socket;
|
||||||
use interface::Interface;
|
|
||||||
|
|
||||||
pub struct Device<SpiBus: ActiveBus, NetworkImpl: Network> {
|
pub struct Device<SpiBus: ActiveBus, NetworkImpl: Network> {
|
||||||
pub bus: SpiBus,
|
pub bus: SpiBus,
|
||||||
|
|
@ -47,7 +47,8 @@ impl<SpiBus: ActiveBus, NetworkImpl: Network> Device<SpiBus, NetworkImpl> {
|
||||||
fn clear_mode(&mut self) -> Result<(), SpiBus::Error> {
|
fn clear_mode(&mut self) -> Result<(), SpiBus::Error> {
|
||||||
// reset bit
|
// reset bit
|
||||||
let mut mode = [0b10000000];
|
let mut mode = [0b10000000];
|
||||||
self.bus.write_frame(register::COMMON, register::common::MODE, &mut mode)?;
|
self.bus
|
||||||
|
.write_frame(register::COMMON, register::common::MODE, &mut mode)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
use bus::{ActiveFourWire, ActiveThreeWire, Bus, FourWire, ThreeWire};
|
use bus::{ActiveFourWire, ActiveThreeWire, Bus, FourWire, ThreeWire};
|
||||||
|
use device::Device;
|
||||||
use embedded_hal::digital::v2::OutputPin;
|
use embedded_hal::digital::v2::OutputPin;
|
||||||
use embedded_hal::spi::FullDuplex;
|
use embedded_hal::spi::FullDuplex;
|
||||||
use network::Network;
|
use network::Network;
|
||||||
use device::Device;
|
|
||||||
|
|
||||||
pub struct InactiveDevice<SpiBus: Bus, NetworkImpl: Network> {
|
pub struct InactiveDevice<SpiBus: Bus, NetworkImpl: Network> {
|
||||||
bus: SpiBus,
|
bus: SpiBus,
|
||||||
|
|
@ -15,7 +15,9 @@ impl<SpiBus: Bus, NetworkImpl: Network> InactiveDevice<SpiBus, NetworkImpl> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<ChipSelect: OutputPin, NetworkImpl: Network> InactiveDevice<FourWire<ChipSelect>, NetworkImpl> {
|
impl<ChipSelect: OutputPin, NetworkImpl: Network>
|
||||||
|
InactiveDevice<FourWire<ChipSelect>, NetworkImpl>
|
||||||
|
{
|
||||||
pub fn activate<Spi: FullDuplex<u8>>(
|
pub fn activate<Spi: FullDuplex<u8>>(
|
||||||
self,
|
self,
|
||||||
spi: Spi,
|
spi: Spi,
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,13 @@ use embedded_hal::digital::v2::OutputPin;
|
||||||
use embedded_hal::spi::FullDuplex;
|
use embedded_hal::spi::FullDuplex;
|
||||||
use embedded_nal::Ipv4Addr;
|
use embedded_nal::Ipv4Addr;
|
||||||
|
|
||||||
use crate::{MacAddress,Mode};
|
use crate::{MacAddress, Mode};
|
||||||
|
|
||||||
use bus::{ActiveBus,FourWire,ActiveFourWire};
|
use bus::{ActiveBus, ActiveFourWire, FourWire};
|
||||||
use device::Device;
|
|
||||||
use network::{Network,Manual};
|
|
||||||
use core::cell::RefCell;
|
use core::cell::RefCell;
|
||||||
use uninitialized_device::{UninitializedDevice,InitializeError};
|
use device::Device;
|
||||||
|
use network::{Manual, Network};
|
||||||
|
use uninitialized_device::{InitializeError, UninitializedDevice};
|
||||||
|
|
||||||
pub struct Interface<SpiBus: ActiveBus, NetworkImpl: Network> {
|
pub struct Interface<SpiBus: ActiveBus, NetworkImpl: Network> {
|
||||||
pub device: RefCell<Device<SpiBus, NetworkImpl>>,
|
pub device: RefCell<Device<SpiBus, NetworkImpl>>,
|
||||||
|
|
@ -16,7 +16,9 @@ pub struct Interface<SpiBus: ActiveBus, NetworkImpl: Network> {
|
||||||
|
|
||||||
impl<SpiBus: ActiveBus, NetworkImpl: Network> Interface<SpiBus, NetworkImpl> {
|
impl<SpiBus: ActiveBus, NetworkImpl: Network> Interface<SpiBus, NetworkImpl> {
|
||||||
fn new(device: Device<SpiBus, NetworkImpl>) -> Self {
|
fn new(device: Device<SpiBus, NetworkImpl>) -> Self {
|
||||||
Self { device: RefCell::new(device) }
|
Self {
|
||||||
|
device: RefCell::new(device),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn release(self) -> Device<SpiBus, NetworkImpl> {
|
pub fn release(self) -> Device<SpiBus, NetworkImpl> {
|
||||||
|
|
@ -24,16 +26,25 @@ impl<SpiBus: ActiveBus, NetworkImpl: Network> Interface<SpiBus, NetworkImpl> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Spi: FullDuplex<u8>, ChipSelect: OutputPin> Interface<ActiveFourWire<Spi, ChipSelect>, Manual> {
|
impl<Spi: FullDuplex<u8>, ChipSelect: OutputPin>
|
||||||
pub fn setup(spi: Spi, cs: ChipSelect, mac: MacAddress, ip: Ipv4Addr) -> Result<Self, InitializeError<<ActiveFourWire<Spi, ChipSelect> as ActiveBus>::Error>> {
|
Interface<ActiveFourWire<Spi, ChipSelect>, Manual>
|
||||||
Ok(UninitializedDevice::new(FourWire::new(cs).activate(spi)).initialize_manual(mac, ip, Mode::default())?.into_interface())
|
{
|
||||||
|
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, NetworkImpl: Network> From<Device<SpiBus, NetworkImpl>>
|
||||||
impl<SpiBus: ActiveBus, NetworkImpl: Network> From<Device<SpiBus, NetworkImpl>> for Interface<SpiBus, NetworkImpl> {
|
for Interface<SpiBus, NetworkImpl>
|
||||||
|
{
|
||||||
fn from(device: Device<SpiBus, NetworkImpl>) -> Interface<SpiBus, NetworkImpl> {
|
fn from(device: Device<SpiBus, NetworkImpl>) -> Interface<SpiBus, NetworkImpl> {
|
||||||
Interface::<SpiBus, NetworkImpl>::new(device)
|
Interface::<SpiBus, NetworkImpl>::new(device)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,9 @@ pub struct MacAddress {
|
||||||
|
|
||||||
impl MacAddress {
|
impl MacAddress {
|
||||||
pub fn new(n0: u8, n1: u8, n2: u8, n3: u8, n4: u8, n5: u8) -> Self {
|
pub fn new(n0: u8, n1: u8, n2: u8, n3: u8, n4: u8, n5: u8) -> Self {
|
||||||
MacAddress { address: [n0, n1, n2, n3, n4, n5] }
|
MacAddress {
|
||||||
|
address: [n0, n1, n2, n3, n4, n5],
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -74,14 +76,14 @@ impl Default for Mode {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod bus;
|
pub mod bus;
|
||||||
|
mod device;
|
||||||
mod inactive_device;
|
mod inactive_device;
|
||||||
|
pub mod interface;
|
||||||
mod network;
|
mod network;
|
||||||
pub mod register;
|
pub mod register;
|
||||||
mod socket;
|
mod socket;
|
||||||
mod udp;
|
mod udp;
|
||||||
pub mod uninitialized_device;
|
pub mod uninitialized_device;
|
||||||
mod device;
|
|
||||||
pub mod interface;
|
|
||||||
|
|
||||||
pub use bus::ActiveFourWire;
|
pub use bus::ActiveFourWire;
|
||||||
pub use interface::Interface;
|
pub use interface::Interface;
|
||||||
|
|
|
||||||
32
src/udp.rs
32
src/udp.rs
|
|
@ -1,10 +1,10 @@
|
||||||
use crate::bus::ActiveBus;
|
use crate::bus::ActiveBus;
|
||||||
|
use crate::interface::Interface;
|
||||||
use crate::network::Network;
|
use crate::network::Network;
|
||||||
use crate::register::socketn;
|
use crate::register::socketn;
|
||||||
use crate::socket::Socket;
|
use crate::socket::Socket;
|
||||||
use crate::interface::Interface;
|
|
||||||
use core::fmt::Debug;
|
use core::fmt::Debug;
|
||||||
use embedded_nal::{nb, IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4, UdpServer, UdpClient};
|
use embedded_nal::{nb, IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4, UdpClient, UdpServer};
|
||||||
|
|
||||||
pub struct UdpSocket {
|
pub struct UdpSocket {
|
||||||
socket: Socket,
|
socket: Socket,
|
||||||
|
|
@ -56,26 +56,15 @@ impl UdpSocket {
|
||||||
.and_then(|_| self.socket.command(bus, socketn::Command::Send))?;
|
.and_then(|_| self.socket.command(bus, socketn::Command::Send))?;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if self
|
if self.socket.get_tx_read_pointer(bus)? == self.socket.get_tx_write_pointer(bus)? {
|
||||||
.socket
|
if self.socket.has_interrupt(bus, socketn::Interrupt::SendOk)? {
|
||||||
.get_tx_read_pointer(bus)?
|
self.socket.reset_interrupt(bus, socketn::Interrupt::All)?;
|
||||||
== self
|
|
||||||
.socket
|
|
||||||
.get_tx_write_pointer(bus)?
|
|
||||||
{
|
|
||||||
if self
|
|
||||||
.socket
|
|
||||||
.has_interrupt(bus, socketn::Interrupt::SendOk)?
|
|
||||||
{
|
|
||||||
self.socket
|
|
||||||
.reset_interrupt(bus, socketn::Interrupt::All)?;
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
} else if self
|
} else if self
|
||||||
.socket
|
.socket
|
||||||
.has_interrupt(bus, socketn::Interrupt::Timeout)?
|
.has_interrupt(bus, socketn::Interrupt::Timeout)?
|
||||||
{
|
{
|
||||||
self.socket
|
self.socket.reset_interrupt(bus, socketn::Interrupt::All)?;
|
||||||
.reset_interrupt(bus, socketn::Interrupt::All)?;
|
|
||||||
return Err(NbError::Other(UdpSocketError::WriteTimeout));
|
return Err(NbError::Other(UdpSocketError::WriteTimeout));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -112,9 +101,7 @@ impl UdpSocket {
|
||||||
*/
|
*/
|
||||||
// TODO loop until RX received size stops changing, or it's larger than
|
// TODO loop until RX received size stops changing, or it's larger than
|
||||||
// receive_buffer.len()
|
// receive_buffer.len()
|
||||||
let read_pointer = self
|
let read_pointer = self.socket.get_rx_read_pointer(bus)?;
|
||||||
.socket
|
|
||||||
.get_rx_read_pointer(bus)?;
|
|
||||||
let mut header = [0u8; 8];
|
let mut header = [0u8; 8];
|
||||||
bus.read_frame(self.socket.rx_buffer(), read_pointer, &mut header)?;
|
bus.read_frame(self.socket.rx_buffer(), read_pointer, &mut header)?;
|
||||||
let remote = SocketAddr::new(
|
let remote = SocketAddr::new(
|
||||||
|
|
@ -130,9 +117,7 @@ impl UdpSocket {
|
||||||
&mut receive_buffer[0..packet_size],
|
&mut receive_buffer[0..packet_size],
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
let tx_write_pointer = self
|
let tx_write_pointer = self.socket.get_tx_write_pointer(bus)?;
|
||||||
.socket
|
|
||||||
.get_tx_write_pointer(bus)?;
|
|
||||||
self.socket
|
self.socket
|
||||||
.set_rx_read_pointer(bus, tx_write_pointer)
|
.set_rx_read_pointer(bus, tx_write_pointer)
|
||||||
.and_then(|_| self.socket.command(bus, socketn::Command::Receive))?;
|
.and_then(|_| self.socket.command(bus, socketn::Command::Receive))?;
|
||||||
|
|
@ -190,7 +175,6 @@ impl<E: Debug> From<NbError<E>> for nb::Error<E> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl<SpiBus, NetworkImpl> UdpClient for Interface<SpiBus, NetworkImpl>
|
impl<SpiBus, NetworkImpl> UdpClient for Interface<SpiBus, NetworkImpl>
|
||||||
where
|
where
|
||||||
SpiBus: ActiveBus,
|
SpiBus: ActiveBus,
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
use crate::network::{Dhcp, Manual, Network};
|
use crate::network::{Dhcp, Manual, Network};
|
||||||
use crate::{MacAddress, Mode};
|
use crate::{MacAddress, Mode};
|
||||||
use bus::{ActiveBus, ActiveFourWire, ActiveThreeWire};
|
use bus::{ActiveBus, ActiveFourWire, ActiveThreeWire};
|
||||||
|
use device::Device;
|
||||||
use embedded_hal::digital::v2::OutputPin;
|
use embedded_hal::digital::v2::OutputPin;
|
||||||
use embedded_hal::spi::FullDuplex;
|
use embedded_hal::spi::FullDuplex;
|
||||||
use embedded_nal::Ipv4Addr;
|
use embedded_nal::Ipv4Addr;
|
||||||
use register;
|
use register;
|
||||||
use device::Device;
|
|
||||||
|
|
||||||
pub struct UninitializedDevice<SpiBus: ActiveBus> {
|
pub struct UninitializedDevice<SpiBus: ActiveBus> {
|
||||||
bus: SpiBus,
|
bus: SpiBus,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue