Compare commits

...

4 Commits

Author SHA1 Message Date
Harry Ho 6d17703e6b Add Enc424j600::init() for complete initialisation 2021-04-29 17:08:19 +08:00
Harry Ho 78e4d82660 examples: Simplify & fix naming 2021-04-29 17:08:18 +08:00
Harry Ho b9b28f0725 Rename functions & classes for clarity
* EthController → EthPhy
  * ::receive_next() → ::recv_packet()
  * ::send_raw_packet() → ::send_packet()
* SpiEth -> Enc424j600
  * ::read_from_mac() → ::read_mac_addr()
  * ::write_mac_address() → ::write_mac_addr()
* EthControllerError → Error
  * ::GeneralError → ::RegisterError
* spi::SpiPortError -> spi::Error
2021-04-29 17:07:28 +08:00
Harry Ho 3529fcd192 Turn EthController trait methods unrelated to PHY into instance methods 2021-04-29 17:03:51 +08:00
6 changed files with 112 additions and 111 deletions

View File

@ -17,8 +17,7 @@ use stm32f4xx_hal::{
spi::Spi,
time::Hertz
};
use enc424j600;
use enc424j600::{smoltcp_phy, EthController};
use enc424j600::smoltcp_phy;
use smoltcp::wire::{
EthernetAddress, IpAddress, IpCidr, Ipv6Cidr
@ -78,7 +77,7 @@ use stm32f4xx_hal::{
Alternate, AF5, Output, PushPull
}
};
type BoosterSpiEth = enc424j600::SpiEth<
type SpiEth = enc424j600::Enc424j600<
Spi<SPI1, (PA5<Alternate<AF5>>, PA6<Alternate<AF5>>, PA7<Alternate<AF5>>)>,
PA4<Output<PushPull>>,
fn(u32) -> ()
@ -102,7 +101,7 @@ const APP: () = {
struct Resources {
eth_iface: EthernetInterface<
'static,
smoltcp_phy::SmoltcpDevice<BoosterSpiEth>>,
smoltcp_phy::SmoltcpDevice<SpiEth>>,
itm: ITM
}
@ -157,11 +156,11 @@ const APP: () = {
let delay_ns_fp: fn(u32) -> () = |time_ns| {
cortex_m::asm::delay((time_ns*21)/125 + 1)
};
enc424j600::SpiEth::new(spi_eth_port, spi1_nss, delay_ns_fp)
SpiEth::new(spi_eth_port, spi1_nss, delay_ns_fp)
};
// Init controller
match spi_eth.init_dev() {
match spi_eth.reset() {
Ok(_) => {
iprintln!(stim0, "Initializing Ethernet...")
}
@ -172,7 +171,7 @@ const APP: () = {
// Read MAC
let mut eth_mac_addr: [u8; 6] = [0; 6];
spi_eth.read_from_mac(&mut eth_mac_addr);
spi_eth.read_mac_addr(&mut eth_mac_addr);
for i in 0..6 {
let byte = eth_mac_addr[i];
match i {

View File

@ -17,8 +17,7 @@ use stm32f4xx_hal::{
spi::Spi,
time::Hertz
};
use enc424j600;
use enc424j600::EthController;
use enc424j600::EthPhy;
///
use stm32f4xx_hal::{
@ -28,15 +27,16 @@ use stm32f4xx_hal::{
Alternate, AF5, Output, PushPull
},
};
type BoosterSpiEth = enc424j600::SpiEth<
type SpiEth = enc424j600::Enc424j600<
Spi<SPI1, (PA5<Alternate<AF5>>, PA6<Alternate<AF5>>, PA7<Alternate<AF5>>)>,
PA4<Output<PushPull>>,
fn(u32)>;
fn(u32) -> ()
>;
#[rtic::app(device = stm32f4xx_hal::stm32, peripherals = true, monotonic = rtic::cyccnt::CYCCNT)]
const APP: () = {
struct Resources {
spi_eth: BoosterSpiEth,
spi_eth: SpiEth,
delay: Delay,
itm: ITM,
}
@ -87,11 +87,11 @@ const APP: () = {
let delay_ns: fn(u32) -> () = |time_ns| {
cortex_m::asm::delay((time_ns*21)/125 + 1)
};
enc424j600::SpiEth::new(spi_eth_port, spi1_nss, delay_ns)
SpiEth::new(spi_eth_port, spi1_nss, delay_ns)
};
// Init
match spi_eth.init_dev() {
match spi_eth.reset() {
Ok(_) => {
iprintln!(stim0, "Initializing Ethernet...")
}
@ -102,7 +102,7 @@ const APP: () = {
// Read MAC
let mut eth_mac_addr: [u8; 6] = [0; 6];
spi_eth.read_from_mac(&mut eth_mac_addr);
spi_eth.read_mac_addr(&mut eth_mac_addr);
for i in 0..6 {
let byte = eth_mac_addr[i];
match i {
@ -157,7 +157,7 @@ const APP: () = {
_ => ()
};
}
c.resources.spi_eth.send_raw_packet(&eth_tx_packet);
c.resources.spi_eth.send_packet(&eth_tx_packet);
iprintln!(stim0, "Packet sent");
c.resources.delay.delay_ms(100_u32);
}

View File

@ -20,36 +20,31 @@ pub mod nal;
/// Max raw frame array size
pub const RAW_FRAME_LENGTH_MAX: usize = 1518;
pub trait EthController {
fn init_dev(&mut self) -> Result<(), EthControllerError>;
fn init_rxbuf(&mut self) -> Result<(), EthControllerError>;
fn init_txbuf(&mut self) -> Result<(), EthControllerError>;
fn receive_next(&mut self, is_poll: bool) -> Result<rx::RxPacket, EthControllerError>;
fn send_raw_packet(&mut self, packet: &tx::TxPacket) -> Result<(), EthControllerError>;
fn set_promiscuous(&mut self) -> Result<(), EthControllerError>;
fn read_from_mac(&mut self, mac: &mut [u8]) -> Result<(), EthControllerError>;
fn write_mac_address(&mut self, mac: &[u8]) -> Result<(), EthControllerError>;
/// Trait representing PHY layer of ENC424J600
pub trait EthPhy {
fn recv_packet(&mut self, is_poll: bool) -> Result<rx::RxPacket, Error>;
fn send_packet(&mut self, packet: &tx::TxPacket) -> Result<(), Error>;
}
/// TODO: Improve these error types
#[derive(Debug)]
pub enum EthControllerError {
pub enum Error {
SpiPortError,
GeneralError,
RegisterError,
// TODO: Better name?
NoRxPacketError
}
impl From<spi::SpiPortError> for EthControllerError {
fn from(_: spi::SpiPortError) -> EthControllerError {
EthControllerError::SpiPortError
impl From<spi::Error> for Error {
fn from(_: spi::Error) -> Error {
Error::SpiPortError
}
}
/// Ethernet controller using SPI interface
pub struct SpiEth<SPI: Transfer<u8>,
NSS: OutputPin,
F: FnMut(u32) -> ()> {
/// ENC424J600 controller in SPI mode
pub struct Enc424j600<SPI: Transfer<u8>,
NSS: OutputPin,
F: FnMut(u32) -> ()> {
spi_port: spi::SpiPort<SPI, NSS, F>,
rx_buf: rx::RxBuffer,
tx_buf: tx::TxBuffer
@ -57,26 +52,29 @@ pub struct SpiEth<SPI: Transfer<u8>,
impl <SPI: Transfer<u8>,
NSS: OutputPin,
F: FnMut(u32) -> ()> SpiEth<SPI, NSS, F> {
F: FnMut(u32) -> ()> Enc424j600<SPI, NSS, F> {
pub fn new(spi: SPI, nss: NSS, delay_ns: F) -> Self {
SpiEth {
Enc424j600 {
spi_port: spi::SpiPort::new(spi, nss, delay_ns),
rx_buf: rx::RxBuffer::new(),
tx_buf: tx::TxBuffer::new()
}
}
}
impl <SPI: Transfer<u8>,
NSS: OutputPin,
F: FnMut(u32) -> ()> EthController for SpiEth<SPI, NSS, F> {
fn init_dev(&mut self) -> Result<(), EthControllerError> {
pub fn init(&mut self) -> Result<(), Error> {
self.reset()?;
self.init_rxbuf()?;
self.init_txbuf()?;
Ok(())
}
pub fn reset(&mut self) -> Result<(), Error> {
// Write 0x1234 to EUDAST
self.spi_port.write_reg_16b(spi::addrs::EUDAST, 0x1234)?;
// Verify that EUDAST is 0x1234
let mut eudast = self.spi_port.read_reg_16b(spi::addrs::EUDAST)?;
if eudast != 0x1234 {
return Err(EthControllerError::GeneralError)
return Err(Error::RegisterError)
}
// Poll CLKRDY (ESTAT<12>) to check if it is set
loop {
@ -90,13 +88,13 @@ impl <SPI: Transfer<u8>,
// Verify that EUDAST is 0x0000
eudast = self.spi_port.read_reg_16b(spi::addrs::EUDAST)?;
if eudast != 0x0000 {
return Err(EthControllerError::GeneralError)
return Err(Error::RegisterError)
}
self.spi_port.delay_us(256);
Ok(())
}
fn init_rxbuf(&mut self) -> Result<(), EthControllerError> {
pub fn init_rxbuf(&mut self) -> Result<(), Error> {
// Set ERXST pointer
self.spi_port.write_reg_16b(spi::addrs::ERXST, self.rx_buf.get_wrap_addr())?;
// Set ERXTAIL pointer
@ -109,22 +107,57 @@ impl <SPI: Transfer<u8>,
Ok(())
}
fn init_txbuf(&mut self) -> Result<(), EthControllerError> {
pub fn init_txbuf(&mut self) -> Result<(), Error> {
// Set EGPWRPT pointer
self.spi_port.write_reg_16b(spi::addrs::EGPWRPT, 0x0000)?;
Ok(())
}
/// Set controller to Promiscuous Mode
pub fn set_promiscuous(&mut self) -> Result<(), Error> {
// From Section 10.12, ENC424J600 Data Sheet:
// "To accept all incoming frames regardless of content (Promiscuous mode),
// set the CRCEN, RUNTEN, UCEN, NOTMEEN and MCEN bits."
let erxfcon_lo = self.spi_port.read_reg_8b(spi::addrs::ERXFCON)?;
self.spi_port.write_reg_8b(spi::addrs::ERXFCON, 0b0101_1110 | (erxfcon_lo & 0b1010_0001))?;
Ok(())
}
/// Read MAC to [u8; 6]
pub fn read_mac_addr(&mut self, mac: &mut [u8]) -> Result<(), Error> {
mac[0] = self.spi_port.read_reg_8b(spi::addrs::MAADR1)?;
mac[1] = self.spi_port.read_reg_8b(spi::addrs::MAADR1 + 1)?;
mac[2] = self.spi_port.read_reg_8b(spi::addrs::MAADR2)?;
mac[3] = self.spi_port.read_reg_8b(spi::addrs::MAADR2 + 1)?;
mac[4] = self.spi_port.read_reg_8b(spi::addrs::MAADR3)?;
mac[5] = self.spi_port.read_reg_8b(spi::addrs::MAADR3 + 1)?;
Ok(())
}
pub fn write_mac_addr(&mut self, mac: &[u8]) -> Result<(), Error> {
self.spi_port.write_reg_8b(spi::addrs::MAADR1, mac[0])?;
self.spi_port.write_reg_8b(spi::addrs::MAADR1 + 1, mac[1])?;
self.spi_port.write_reg_8b(spi::addrs::MAADR2, mac[2])?;
self.spi_port.write_reg_8b(spi::addrs::MAADR2 + 1, mac[3])?;
self.spi_port.write_reg_8b(spi::addrs::MAADR3, mac[4])?;
self.spi_port.write_reg_8b(spi::addrs::MAADR3 + 1, mac[5])?;
Ok(())
}
}
impl <SPI: Transfer<u8>,
NSS: OutputPin,
F: FnMut(u32) -> ()> EthPhy for Enc424j600<SPI, NSS, F> {
/// Receive the next packet and return it
/// Set is_poll to true for returning until PKTIF is set;
/// Set is_poll to false for returning Err when PKTIF is not set
fn receive_next(&mut self, is_poll: bool) -> Result<rx::RxPacket, EthControllerError> {
fn recv_packet(&mut self, is_poll: bool) -> Result<rx::RxPacket, Error> {
// Poll PKTIF (EIR<4>) to check if it is set
loop {
let eir = self.spi_port.read_reg_16b(spi::addrs::EIR)?;
if eir & 0x40 == 0x40 { break }
if !is_poll {
return Err(EthControllerError::NoRxPacketError)
return Err(Error::NoRxPacketError)
}
}
// Set ERXRDPT pointer to next_addr
@ -160,7 +193,7 @@ impl <SPI: Transfer<u8>,
}
/// Send an established packet
fn send_raw_packet(&mut self, packet: &tx::TxPacket) -> Result<(), EthControllerError> {
fn send_packet(&mut self, packet: &tx::TxPacket) -> Result<(), Error> {
// Set EGPWRPT pointer to next_addr
self.spi_port.write_reg_16b(spi::addrs::EGPWRPT, self.tx_buf.get_next_addr())?;
// Copy packet data to SRAM Buffer
@ -187,35 +220,4 @@ impl <SPI: Transfer<u8>,
tx::GPBUFEN_DEFAULT);
Ok(())
}
/// Set controller to Promiscuous Mode
fn set_promiscuous(&mut self) -> Result<(), EthControllerError> {
// From Section 10.12, ENC424J600 Data Sheet:
// "To accept all incoming frames regardless of content (Promiscuous mode),
// set the CRCEN, RUNTEN, UCEN, NOTMEEN and MCEN bits."
let erxfcon_lo = self.spi_port.read_reg_8b(spi::addrs::ERXFCON)?;
self.spi_port.write_reg_8b(spi::addrs::ERXFCON, 0b0101_1110 | (erxfcon_lo & 0b1010_0001))?;
Ok(())
}
/// Read MAC to [u8; 6]
fn read_from_mac(&mut self, mac: &mut [u8]) -> Result<(), EthControllerError> {
mac[0] = self.spi_port.read_reg_8b(spi::addrs::MAADR1)?;
mac[1] = self.spi_port.read_reg_8b(spi::addrs::MAADR1 + 1)?;
mac[2] = self.spi_port.read_reg_8b(spi::addrs::MAADR2)?;
mac[3] = self.spi_port.read_reg_8b(spi::addrs::MAADR2 + 1)?;
mac[4] = self.spi_port.read_reg_8b(spi::addrs::MAADR3)?;
mac[5] = self.spi_port.read_reg_8b(spi::addrs::MAADR3 + 1)?;
Ok(())
}
fn write_mac_address(&mut self, mac: &[u8]) -> Result<(), EthControllerError> {
self.spi_port.write_reg_8b(spi::addrs::MAADR1, mac[0])?;
self.spi_port.write_reg_8b(spi::addrs::MAADR1 + 1, mac[1])?;
self.spi_port.write_reg_8b(spi::addrs::MAADR2, mac[2])?;
self.spi_port.write_reg_8b(spi::addrs::MAADR2 + 1, mac[3])?;
self.spi_port.write_reg_8b(spi::addrs::MAADR3, mac[4])?;
self.spi_port.write_reg_8b(spi::addrs::MAADR3 + 1, mac[5])?;
Ok(())
}
}

View File

@ -24,7 +24,7 @@ pub enum NetworkError {
pub type NetworkInterface<SPI, NSS> = net::iface::EthernetInterface<
'static,
crate::smoltcp_phy::SmoltcpDevice<
crate::SpiEth<SPI, NSS, fn(u32)>
crate::Enc424j600<SPI, NSS, fn(u32)>
>,
>;

View File

@ -1,5 +1,5 @@
use crate::{
EthController, tx, RAW_FRAME_LENGTH_MAX
EthPhy, tx, RAW_FRAME_LENGTH_MAX
};
use core::cell;
use smoltcp::{
@ -8,25 +8,25 @@ use smoltcp::{
Error
};
pub struct SmoltcpDevice<EC: EthController> {
pub eth_controller: cell::RefCell<EC>,
pub struct SmoltcpDevice<E: EthPhy> {
pub eth_phy: cell::RefCell<E>,
rx_packet_buf: [u8; RAW_FRAME_LENGTH_MAX],
tx_packet_buf: [u8; RAW_FRAME_LENGTH_MAX]
}
impl<EC: EthController> SmoltcpDevice<EC> {
pub fn new(eth_controller: EC) -> Self {
impl<E: EthPhy> SmoltcpDevice<E> {
pub fn new(eth_phy: E) -> Self {
SmoltcpDevice {
eth_controller: cell::RefCell::new(eth_controller),
eth_phy: cell::RefCell::new(eth_phy),
rx_packet_buf: [0; RAW_FRAME_LENGTH_MAX],
tx_packet_buf: [0; RAW_FRAME_LENGTH_MAX]
}
}
}
impl<'a, EC: 'a + EthController> Device<'a> for SmoltcpDevice<EC> {
impl<'a, E: 'a + EthPhy> Device<'a> for SmoltcpDevice<E> {
type RxToken = EthRxToken<'a>;
type TxToken = EthTxToken<'a, EC>;
type TxToken = EthTxToken<'a, E>;
fn capabilities(&self) -> DeviceCapabilities {
let mut caps = DeviceCapabilities::default();
@ -35,8 +35,8 @@ impl<'a, EC: 'a + EthController> Device<'a> for SmoltcpDevice<EC> {
}
fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> {
let self_p = (&mut *self) as *mut SmoltcpDevice<EC>;
match self.eth_controller.borrow_mut().receive_next(false) {
let self_p = (&mut *self) as *mut SmoltcpDevice<E>;
match self.eth_phy.borrow_mut().recv_packet(false) {
Ok(rx_packet) => {
// Write received packet to RX packet buffer
rx_packet.write_frame_to(&mut self.rx_packet_buf);
@ -57,7 +57,7 @@ impl<'a, EC: 'a + EthController> Device<'a> for SmoltcpDevice<EC> {
}
fn transmit(&'a mut self) -> Option<Self::TxToken> {
let self_p = (&mut *self) as *mut SmoltcpDevice<EC>;
let self_p = (&mut *self) as *mut SmoltcpDevice<E>;
// Construct a blank TxToken
let tx_token = EthTxToken {
buf: &mut self.tx_packet_buf,
@ -81,12 +81,12 @@ impl<'a> RxToken for EthRxToken<'a> {
}
}
pub struct EthTxToken<'a, EC: EthController> {
pub struct EthTxToken<'a, E: EthPhy> {
buf: &'a mut [u8],
dev: *mut SmoltcpDevice<EC>
dev: *mut SmoltcpDevice<E>
}
impl<'a, EC: 'a + EthController> TxToken for EthTxToken<'a, EC> {
impl<'a, E: 'a + EthPhy> TxToken for EthTxToken<'a, E> {
fn consume<R, F>(self, _timestamp: Instant, len: usize, f: F) -> Result<R, Error>
where
F: FnOnce(&mut [u8]) -> Result<R, Error>,
@ -97,10 +97,10 @@ impl<'a, EC: 'a + EthController> TxToken for EthTxToken<'a, EC> {
// Update frame length and write frame bytes
tx_packet.update_frame(&mut self.buf[..len], len);
// Send the packet as raw
let eth_controller = unsafe {
&mut (*self.dev).eth_controller
let eth_phy = unsafe {
&mut (*self.dev).eth_phy
};
match eth_controller.borrow_mut().send_raw_packet(&tx_packet) {
match eth_phy.borrow_mut().send_packet(&tx_packet) {
Ok(_) => { result },
Err(_) => Err(Error::Exhausted)
}

View File

@ -59,7 +59,7 @@ pub struct SpiPort<SPI: Transfer<u8>,
delay_ns: F,
}
pub enum SpiPortError {
pub enum Error {
TransferError
}
@ -78,13 +78,13 @@ impl <SPI: Transfer<u8>,
}
}
pub fn read_reg_8b(&mut self, addr: u8) -> Result<u8, SpiPortError> {
pub fn read_reg_8b(&mut self, addr: u8) -> Result<u8, Error> {
// Using RCRU instruction to read using unbanked (full) address
let r_data = self.rw_addr_u8(opcodes::RCRU, addr, 0)?;
Ok(r_data)
}
pub fn read_reg_16b(&mut self, lo_addr: u8) -> Result<u16, SpiPortError> {
pub fn read_reg_16b(&mut self, lo_addr: u8) -> Result<u16, Error> {
let r_data_lo = self.read_reg_8b(lo_addr)?;
let r_data_hi = self.read_reg_8b(lo_addr + 1)?;
// Combine top and bottom 8-bit to return 16-bit
@ -93,7 +93,7 @@ impl <SPI: Transfer<u8>,
// Currently requires manual slicing (buf[1..]) for the data read back
pub fn read_rxdat<'a>(&mut self, buf: &'a mut [u8], data_length: usize)
-> Result<(), SpiPortError> {
-> Result<(), Error> {
let r_valid = self.r_n(buf, opcodes::RERXDATA, data_length)?;
Ok(r_valid)
}
@ -101,19 +101,19 @@ impl <SPI: Transfer<u8>,
// Currenly requires actual data to be stored in buf[1..] instead of buf[0..]
// TODO: Maybe better naming?
pub fn write_txdat<'a>(&mut self, buf: &'a mut [u8], data_length: usize)
-> Result<(), SpiPortError> {
-> Result<(), Error> {
let w_valid = self.w_n(buf, opcodes::WEGPDATA, data_length)?;
Ok(w_valid)
}
pub fn write_reg_8b(&mut self, addr: u8, data: u8) -> Result<(), SpiPortError> {
pub fn write_reg_8b(&mut self, addr: u8, data: u8) -> Result<(), Error> {
// TODO: addr should be separated from w_data
// Using WCRU instruction to write using unbanked (full) address
self.rw_addr_u8(opcodes::WCRU, addr, data)?;
Ok(())
}
pub fn write_reg_16b(&mut self, lo_addr: u8, data: u16) -> Result<(), SpiPortError> {
pub fn write_reg_16b(&mut self, lo_addr: u8, data: u16) -> Result<(), Error> {
self.write_reg_8b(lo_addr, (data & 0xff) as u8)?;
self.write_reg_8b(lo_addr + 1, ((data & 0xff00) >> 8) as u8)?;
Ok(())
@ -127,7 +127,7 @@ impl <SPI: Transfer<u8>,
// TODO: (Make data read/write as reference to array)
// Currently requires 1-byte addr, read/write data is only 1-byte
fn rw_addr_u8(&mut self, opcode: u8, addr: u8, data: u8)
-> Result<u8, SpiPortError> {
-> Result<u8, Error> {
// Enable chip select
self.nss.set_low();
// Start writing to SLAVE
@ -150,7 +150,7 @@ impl <SPI: Transfer<u8>,
(self.delay_ns)(60);
self.nss.set_high();
(self.delay_ns)(30);
Err(SpiPortError::TransferError)
Err(Error::TransferError)
}
}
}
@ -161,7 +161,7 @@ impl <SPI: Transfer<u8>,
// Note: buf must be at least (data_length + 1)-byte long
// TODO: Check and raise error for array size < (data_length + 1)
fn r_n<'a>(&mut self, buf: &'a mut [u8], opcode: u8, data_length: usize)
-> Result<(), SpiPortError> {
-> Result<(), Error> {
// Enable chip select
self.nss.set_low();
// Start writing to SLAVE
@ -176,7 +176,7 @@ impl <SPI: Transfer<u8>,
Err(_) => {
// Disable chip select
self.nss.set_high();
Err(SpiPortError::TransferError)
Err(Error::TransferError)
}
}
}
@ -184,7 +184,7 @@ impl <SPI: Transfer<u8>,
// Note: buf[0] is currently reserved for opcode to overwrite
// TODO: Actual data should start from buf[0], not buf[1]
fn w_n<'a>(&mut self, buf: &'a mut [u8], opcode: u8, data_length: usize)
-> Result<(), SpiPortError> {
-> Result<(), Error> {
// Enable chip select
self.nss.set_low();
// Start writing to SLAVE
@ -200,8 +200,8 @@ impl <SPI: Transfer<u8>,
Err(_) => {
// Disable chip select
self.nss.set_high();
Err(SpiPortError::TransferError)
Err(Error::TransferError)
}
}
}
}
}