Moved more functionality into socket

This commit is contained in:
Jonah Dahlquist 2019-09-10 20:58:46 -05:00 committed by Jonah Dahlquist
commit 332ab924db
3 changed files with 20 additions and 13 deletions

View file

@ -68,5 +68,4 @@ pub mod socketn {
pub const RECEIVED_SIZE: u16 = 0x26;
pub const RX_DATA_READ_POINTER: u16 = 0x28;
pub const RX_DATA_WRITE_POINTER: u16 = 0x2A;
}

View file

@ -9,6 +9,7 @@ pub trait Socket {
fn register(&self) -> u8;
fn tx_buffer(&self) -> u8;
fn rx_buffer(&self) -> u8;
fn set_mode<SpiBus: ActiveBus>(
&self,
bus: &mut SpiBus,
@ -18,6 +19,7 @@ pub trait Socket {
block!(bus.transfer_frame(self.register(), socketn::MODE, true, &mut mode))?;
Ok(())
}
fn reset_interrupt<SpiBus: ActiveBus>(
&self,
bus: &mut SpiBus,
@ -27,6 +29,7 @@ pub trait Socket {
block!(bus.transfer_frame(self.register(), socketn::INTERRUPT, true, &mut data))?;
Ok(())
}
fn set_source_port<SpiBus: ActiveBus>(
&self,
bus: &mut SpiBus,
@ -37,6 +40,7 @@ pub trait Socket {
block!(bus.transfer_frame(self.register(), socketn::SOURCE_PORT, true, &mut data))?;
Ok(())
}
fn has_received<SpiBus: ActiveBus>(
&self,
bus: &mut SpiBus,
@ -68,6 +72,19 @@ pub trait Socket {
block!(bus.transfer_frame(self.register(), socketn::COMMAND, true, &mut data))?;
Ok(())
}
fn get_receive_size<SpiBus: ActiveBus>(&self, bus: &mut SpiBus) -> Result<u16, SpiBus::Error> {
loop {
// Section 4.2 of datasheet, Sn_TX_FSR address docs indicate that read must be repeated until two sequential reads are stable
let mut sample_0 = [0u8; 2];
block!(bus.transfer_frame(self.register(), socketn::RECEIVED_SIZE, false, &mut sample_0))?;
let mut sample_1 = [0u8; 2];
block!(bus.transfer_frame(self.register(), socketn::RECEIVED_SIZE, false, &mut sample_1))?;
if sample_0 == sample_1 && sample_0[0] >= 8 {
break Ok(BigEndian::read_u16(&sample_0));
}
}
}
}
pub type OwnedSockets = (

View file

@ -17,7 +17,9 @@ pub struct UdpPacket<UdpSocket> {
impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket> UdpPacket<UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>>
{
pub fn new(mut udp_socket: UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>) -> Result<Self, SpiBus::Error> {
let receive_size = Self::block_until_receive_size_known(&mut udp_socket)?;
let receive_size = udp_socket.socket.get_receive_size(&mut udp_socket.bus)?;
// Packet frame, as described in W5200 docs sectino 5.2.2.1
// |<-- read_pointer read_pointer + received_size -->|
// |Destination IP Address | Destination Port | Byte Size of DATA | Actual DATA ... |
// | --- 4 Bytes --- | --- 2 Bytes --- | --- 2 Bytes --- | .... |
@ -53,17 +55,6 @@ impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket> UdpPacket<
Ok(self.udp_socket)
}
fn block_until_receive_size_known(udp_socket: &mut UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>) -> Result<u16, SpiBus::Error> {
loop {
let mut sample_0 = [0u8; 2];
block!(udp_socket.bus.transfer_frame(udp_socket.socket.register(), socketn::RECEIVED_SIZE, false, &mut sample_0))?;
let mut sample_1 = [0u8; 2];
block!(udp_socket.bus.transfer_frame(udp_socket.socket.register(), socketn::RECEIVED_SIZE, false, &mut sample_1))?;
if sample_0 == sample_1 && sample_0[0] >= 8 {
break Ok(BigEndian::read_u16(&sample_0));
}
}
}
}
impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket> Iterator for UdpPacket<UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>> {