Renamed Settings to Mode since it only applies to network mode byte
This commit is contained in:
parent
ce36644d9c
commit
7dd4c04fe4
2 changed files with 51 additions and 15 deletions
|
|
@ -136,14 +136,14 @@ pub enum ArpResponses {
|
|||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
pub struct Settings {
|
||||
pub struct Mode {
|
||||
on_wake_on_lan: OnWakeOnLan,
|
||||
on_ping_request: OnPingRequest,
|
||||
connection_type: ConnectionType,
|
||||
arp_responses: ArpResponses
|
||||
arp_responses: ArpResponses,
|
||||
}
|
||||
|
||||
impl Default for Settings {
|
||||
impl Default for Mode {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
on_wake_on_lan: OnWakeOnLan::Ignore,
|
||||
|
|
|
|||
|
|
@ -2,33 +2,69 @@ use bus::{ActiveBus, ActiveFourWire, ActiveThreeWire};
|
|||
use embedded_hal::digital::v2::OutputPin;
|
||||
use embedded_hal::spi::FullDuplex;
|
||||
use w5500::W5500;
|
||||
use crate::Settings;
|
||||
use crate::{Mode, IpAddress, MacAddress};
|
||||
use crate::network::{Network,Manual,Dhcp};
|
||||
use register;
|
||||
|
||||
pub struct UninitializedW5500<SpiBus: ActiveBus> {
|
||||
bus: SpiBus,
|
||||
}
|
||||
|
||||
pub enum InitializeError<SpiError> {
|
||||
SpiError(SpiError),
|
||||
ChipNotConnected,
|
||||
}
|
||||
|
||||
impl<SpiBus: ActiveBus> UninitializedW5500<SpiBus> {
|
||||
pub fn initialize(mut self, settings: Settings) -> Result<W5500<SpiBus>, SpiBus::Error> {
|
||||
self.set_mode(settings)?;
|
||||
// TODO set up IP/etc
|
||||
// TODO give ownership of all sockets
|
||||
Ok(W5500::new(self.bus))
|
||||
}
|
||||
pub fn new(bus: SpiBus) -> UninitializedW5500<SpiBus> {
|
||||
|
||||
UninitializedW5500 { bus: bus }
|
||||
}
|
||||
|
||||
pub fn initialize(self, mac: MacAddress, mode_options: Mode) -> Result<W5500<SpiBus, Dhcp>, InitializeError<SpiBus::Error>> {
|
||||
let network = Dhcp::new(mac);
|
||||
self.initialize_with_network(network, mode_options)
|
||||
}
|
||||
|
||||
pub fn initialize_manual(self, mac: MacAddress, ip: IpAddress, mode_options: Mode) -> Result<W5500<SpiBus, Manual>, InitializeError<SpiBus::Error>> {
|
||||
let mut gateway = ip;
|
||||
gateway.address[3] = 1;
|
||||
let subnet = IpAddress::new(255, 255, 255, 0);
|
||||
self.initialize_advanced(mac, ip, gateway, subnet, mode_options)
|
||||
}
|
||||
|
||||
pub fn initialize_advanced(self, mac: MacAddress, ip: IpAddress, gateway: IpAddress, subnet: IpAddress, mode_options: Mode) -> Result<W5500<SpiBus, Manual>, InitializeError<SpiBus::Error>>{
|
||||
let network = Manual::new(mac, ip, gateway, subnet);
|
||||
self.initialize_with_network(network, mode_options)
|
||||
}
|
||||
|
||||
fn initialize_with_network<NetworkImpl: Network>(mut self, mut network: NetworkImpl, mode_options: Mode) -> Result<W5500<SpiBus, NetworkImpl>, InitializeError<SpiBus::Error>> {
|
||||
self.assert_chip_version(0x4)?;
|
||||
self.set_mode(mode_options).map_err(|e| InitializeError::SpiError(e))?;
|
||||
network.refresh(&mut self.bus).map_err(|e| InitializeError::SpiError(e))?;
|
||||
Ok(W5500::new(self.bus, network))
|
||||
// TODO give ownership of all sockets
|
||||
}
|
||||
|
||||
fn assert_chip_version(&mut self, expected_version: u8) -> Result<(), InitializeError<SpiBus::Error>> {
|
||||
let mut version = [0];
|
||||
block!(self.bus.transfer_frame(register::COMMON, register::common::VERSION, false, &mut version)).map_err(|e| InitializeError::SpiError(e))?;
|
||||
if version[0] != expected_version {
|
||||
Err(InitializeError::ChipNotConnected)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn set_mode(
|
||||
&mut self,
|
||||
settings: Settings,
|
||||
mode_options: Mode,
|
||||
) -> Result<(), SpiBus::Error> {
|
||||
let mut mode = [0];
|
||||
mode[0] |= settings.on_wake_on_lan as u8;
|
||||
mode[0] |= settings.on_ping_request as u8;
|
||||
mode[0] |= settings.connection_type as u8;
|
||||
mode[0] |= settings.arp_responses as u8;
|
||||
mode[0] |= mode_options.on_wake_on_lan as u8;
|
||||
mode[0] |= mode_options.on_ping_request as u8;
|
||||
mode[0] |= mode_options.connection_type as u8;
|
||||
mode[0] |= mode_options.arp_responses as u8;
|
||||
block!(self.bus.transfer_frame(register::COMMON, register::common::MODE, true, &mut mode))?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue