Moved register addresses into register modules, fixed borrow error

This commit is contained in:
Jonah Dahlquist 2019-09-05 13:21:25 -05:00 committed by Jonah Dahlquist
commit 65a9552816
4 changed files with 86 additions and 37 deletions

View file

@ -1,5 +1,4 @@
pub const COMMON: u8 = 0; pub const COMMON: u8 = 0;
pub mod common { pub mod common {
pub const MODE: u16 = 0x0; pub const MODE: u16 = 0x0;
pub const GATEWAY: u16 = 0x1; pub const GATEWAY: u16 = 0x1;
@ -8,3 +7,51 @@ pub mod common {
pub const IP: u16 = 0xF; pub const IP: u16 = 0xF;
pub const VERSION: u16 = 0x39; pub const VERSION: u16 = 0x39;
} }
pub const SOCKET0: u8 = 0b000_00001;
pub mod socket0 {
pub const TX_BUFFER: u8 = 0b000_00010;
pub const RX_BUFFER: u8 = 0b000_00011;
}
pub const SOCKET1: u8 = 0b000_00101;
pub mod socket1 {
pub const TX_BUFFER: u8 = 0b000_00110;
pub const RX_BUFFER: u8 = 0b000_00111;
}
pub const SOCKET2: u8 = 0b000_01001;
pub mod socket2 {
pub const TX_BUFFER: u8 = 0b000_01010;
pub const RX_BUFFER: u8 = 0b000_01011;
}
pub const SOCKET3: u8 = 0b000_01101;
pub mod socket3 {
pub const TX_BUFFER: u8 = 0b000_01110;
pub const RX_BUFFER: u8 = 0b000_01111;
}
pub const SOCKET4: u8 = 0b000_10001;
pub mod socket4 {
pub const TX_BUFFER: u8 = 0b000_10010;
pub const RX_BUFFER: u8 = 0b000_10011;
}
pub const SOCKET5: u8 = 0b000_10101;
pub mod socket5 {
pub const TX_BUFFER: u8 = 0b000_10110;
pub const RX_BUFFER: u8 = 0b000_10111;
}
pub const SOCKET6: u8 = 0b000_11001;
pub mod socket6 {
pub const TX_BUFFER: u8 = 0b000_11010;
pub const RX_BUFFER: u8 = 0b000_11011;
}
pub const SOCKET7: u8 = 0b000_11101;
pub mod socket7 {
pub const TX_BUFFER: u8 = 0b000_11110;
pub const RX_BUFFER: u8 = 0b000_11111;
}

View file

@ -1,5 +1,7 @@
use crate::register;
pub trait Socket { pub trait Socket {
fn is_owned_by(&self, sockets: OwnedSockets) -> bool; fn is_owned_by(&self, sockets: &OwnedSockets) -> bool;
fn register(&self) -> u8; fn register(&self) -> u8;
fn tx_buffer(&self) -> u8; fn tx_buffer(&self) -> u8;
fn rx_buffer(&self) -> u8; fn rx_buffer(&self) -> u8;
@ -28,121 +30,121 @@ pub type Sockets<'a> = (
pub struct Socket0 {} pub struct Socket0 {}
impl Socket for Socket0 { impl Socket for Socket0 {
fn is_owned_by(&self, sockets: OwnedSockets) -> bool { fn is_owned_by(&self, sockets: &OwnedSockets) -> bool {
self as *const _ == &sockets.0 as *const _ self as *const _ == &sockets.0 as *const _
} }
fn register(&self) -> u8 { fn register(&self) -> u8 {
0b000_00001 register::SOCKET0
} }
fn tx_buffer(&self) -> u8 { fn tx_buffer(&self) -> u8 {
0b000_00010 register::socket0::TX_BUFFER
} }
fn rx_buffer(&self) -> u8 { fn rx_buffer(&self) -> u8 {
0b000_00011 register::socket0::RX_BUFFER
} }
} }
pub struct Socket1 {} pub struct Socket1 {}
impl Socket for Socket1 { impl Socket for Socket1 {
fn is_owned_by(&self, sockets: OwnedSockets) -> bool { fn is_owned_by(&self, sockets: &OwnedSockets) -> bool {
self as *const _ == &sockets.1 as *const _ self as *const _ == &sockets.1 as *const _
} }
fn register(&self) -> u8 { fn register(&self) -> u8 {
0b000_00101 register::SOCKET1
} }
fn tx_buffer(&self) -> u8 { fn tx_buffer(&self) -> u8 {
0b000_00110 register::socket1::TX_BUFFER
} }
fn rx_buffer(&self) -> u8 { fn rx_buffer(&self) -> u8 {
0b000_00111 register::socket1::RX_BUFFER
} }
} }
pub struct Socket2 {} pub struct Socket2 {}
impl Socket for Socket2 { impl Socket for Socket2 {
fn is_owned_by(&self, sockets: OwnedSockets) -> bool { fn is_owned_by(&self, sockets: &OwnedSockets) -> bool {
self as *const _ == &sockets.2 as *const _ self as *const _ == &sockets.2 as *const _
} }
fn register(&self) -> u8 { fn register(&self) -> u8 {
0b000_01001 register::SOCKET2
} }
fn tx_buffer(&self) -> u8 { fn tx_buffer(&self) -> u8 {
0b000_01010 register::socket2::TX_BUFFER
} }
fn rx_buffer(&self) -> u8 { fn rx_buffer(&self) -> u8 {
0b000_01011 register::socket2::RX_BUFFER
} }
} }
pub struct Socket3 {} pub struct Socket3 {}
impl Socket for Socket3 { impl Socket for Socket3 {
fn is_owned_by(&self, sockets: OwnedSockets) -> bool { fn is_owned_by(&self, sockets: &OwnedSockets) -> bool {
self as *const _ == &sockets.3 as *const _ self as *const _ == &sockets.3 as *const _
} }
fn register(&self) -> u8 { fn register(&self) -> u8 {
0b000_01101 register::SOCKET3
} }
fn tx_buffer(&self) -> u8 { fn tx_buffer(&self) -> u8 {
0b000_01110 register::socket3::TX_BUFFER
} }
fn rx_buffer(&self) -> u8 { fn rx_buffer(&self) -> u8 {
0b000_01111 register::socket3::RX_BUFFER
} }
} }
pub struct Socket4 {} pub struct Socket4 {}
impl Socket for Socket4 { impl Socket for Socket4 {
fn is_owned_by(&self, sockets: OwnedSockets) -> bool { fn is_owned_by(&self, sockets: &OwnedSockets) -> bool {
self as *const _ == &sockets.4 as *const _ self as *const _ == &sockets.4 as *const _
} }
fn register(&self) -> u8 { fn register(&self) -> u8 {
0b000_10001 register::SOCKET4
} }
fn tx_buffer(&self) -> u8 { fn tx_buffer(&self) -> u8 {
0b000_10010 register::socket4::TX_BUFFER
} }
fn rx_buffer(&self) -> u8 { fn rx_buffer(&self) -> u8 {
0b000_10011 register::socket4::RX_BUFFER
} }
} }
pub struct Socket5 {} pub struct Socket5 {}
impl Socket for Socket5 { impl Socket for Socket5 {
fn is_owned_by(&self, sockets: OwnedSockets) -> bool { fn is_owned_by(&self, sockets: &OwnedSockets) -> bool {
self as *const _ == &sockets.5 as *const _ self as *const _ == &sockets.5 as *const _
} }
fn register(&self) -> u8 { fn register(&self) -> u8 {
0b000_10101 register::SOCKET5
} }
fn tx_buffer(&self) -> u8 { fn tx_buffer(&self) -> u8 {
0b000_10110 register::socket5::TX_BUFFER
} }
fn rx_buffer(&self) -> u8 { fn rx_buffer(&self) -> u8 {
0b000_10111 register::socket5::RX_BUFFER
} }
} }
pub struct Socket6 {} pub struct Socket6 {}
impl Socket for Socket6 { impl Socket for Socket6 {
fn is_owned_by(&self, sockets: OwnedSockets) -> bool { fn is_owned_by(&self, sockets: &OwnedSockets) -> bool {
self as *const _ == &sockets.6 as *const _ self as *const _ == &sockets.6 as *const _
} }
fn register(&self) -> u8 { fn register(&self) -> u8 {
0b000_11001 register::SOCKET6
} }
fn tx_buffer(&self) -> u8 { fn tx_buffer(&self) -> u8 {
0b000_11010 register::socket6::TX_BUFFER
} }
fn rx_buffer(&self) -> u8 { fn rx_buffer(&self) -> u8 {
0b000_11011 register::socket6::RX_BUFFER
} }
} }
pub struct Socket7 {} pub struct Socket7 {}
impl Socket for Socket7 { impl Socket for Socket7 {
fn is_owned_by(&self, sockets: OwnedSockets) -> bool { fn is_owned_by(&self, sockets: &OwnedSockets) -> bool {
self as *const _ == &sockets.7 as *const _ self as *const _ == &sockets.7 as *const _
} }
fn register(&self) -> u8 { fn register(&self) -> u8 {
0b000_11101 register::SOCKET7
} }
fn tx_buffer(&self) -> u8 { fn tx_buffer(&self) -> u8 {
0b000_11110 register::socket7::TX_BUFFER
} }
fn rx_buffer(&self) -> u8 { fn rx_buffer(&self) -> u8 {
0b000_11111 register::socket7::RX_BUFFER
} }
} }

View file

@ -16,7 +16,7 @@ impl<'a, SocketImpl: Socket> InactiveUdpSocket<'a, SocketImpl> {
pub fn activate<SpiBus: ActiveBus, NetworkImpl: Network>(self, w5500: W5500<SpiBus, NetworkImpl>) -> Result<UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>, ForeignSocketError> { pub fn activate<SpiBus: ActiveBus, NetworkImpl: Network>(self, w5500: W5500<SpiBus, NetworkImpl>) -> Result<UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>, ForeignSocketError> {
let (bus, network, sockets) = w5500.release(); let (bus, network, sockets) = w5500.release();
if self.socket.is_owned_by(sockets) { if self.socket.is_owned_by(&sockets) {
Ok(UdpSocket::new(bus, network, sockets, self.socket)) Ok(UdpSocket::new(bus, network, sockets, self.socket))
} else { } else {
Err(ForeignSocketError {}) Err(ForeignSocketError {})

View file

@ -51,10 +51,10 @@ impl<SpiBus: ActiveBus, NetworkImpl: Network> W5500<SpiBus, NetworkImpl> {
} }
pub fn open_udp_socket<'a, SocketImpl: Socket>( pub fn open_udp_socket<'a, SocketImpl: Socket>(
&self, self,
socket: &'a mut SocketImpl, socket: &'a mut SocketImpl,
) -> Result<UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>, ForeignSocketError> { ) -> Result<UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>, ForeignSocketError> {
if socket.is_owned_by(self.sockets) { if socket.is_owned_by(&self.sockets) {
Ok(UdpSocket::new(self.bus, self.network, self.sockets, socket)) Ok(UdpSocket::new(self.bus, self.network, self.sockets, socket))
} else { } else {
Err(ForeignSocketError {}) Err(ForeignSocketError {})