Fix controller & smoltcp device bugs

* lib.rs: fix transmission status checking (line 176)
* lib.rs: make RAW_FRAME_LENGTH_MAX the same for both RX and TX, for the purpose of setting MAMXFL in controller, as well as setting MTU for smoltcp device
* smoltcp_phy.rs: fix missing MTU definition
This commit is contained in:
Harry Ho 2020-12-30 17:06:50 +08:00
commit d358103664
4 changed files with 19 additions and 15 deletions

View File

@ -15,6 +15,9 @@ pub mod tx;
#[cfg(feature="smoltcp")] #[cfg(feature="smoltcp")]
pub mod smoltcp_phy; pub mod smoltcp_phy;
/// Max raw frame array size
pub const RAW_FRAME_LENGTH_MAX: usize = 0x1000;
pub trait EthController { pub trait EthController {
fn init_dev(&mut self, delay: &mut impl DelayUs<u16>) -> Result<(), EthControllerError>; fn init_dev(&mut self, delay: &mut impl DelayUs<u16>) -> Result<(), EthControllerError>;
fn init_rxbuf(&mut self) -> Result<(), EthControllerError>; fn init_rxbuf(&mut self) -> Result<(), EthControllerError>;
@ -94,7 +97,7 @@ impl <SPI: Transfer<u8>,
// Set ERXTAIL pointer // Set ERXTAIL pointer
self.spi_port.write_reg_16b(spi::addrs::ERXTAIL, self.rx_buf.get_tail_addr())?; self.spi_port.write_reg_16b(spi::addrs::ERXTAIL, self.rx_buf.get_tail_addr())?;
// Set MAMXFL to maximum number of bytes in each accepted packet // Set MAMXFL to maximum number of bytes in each accepted packet
self.spi_port.write_reg_16b(spi::addrs::MAMXFL, rx::RAW_FRAME_LENGTH_MAX as u16)?; self.spi_port.write_reg_16b(spi::addrs::MAMXFL, RAW_FRAME_LENGTH_MAX as u16)?;
// Enable RXEN (ECON1<0>) // Enable RXEN (ECON1<0>)
let econ1 = self.spi_port.read_reg_16b(spi::addrs::ECON1)?; let econ1 = self.spi_port.read_reg_16b(spi::addrs::ECON1)?;
self.spi_port.write_reg_16b(spi::addrs::ECON1, 0x1 | (econ1 & 0xfffe))?; self.spi_port.write_reg_16b(spi::addrs::ECON1, 0x1 | (econ1 & 0xfffe))?;
@ -135,7 +138,7 @@ impl <SPI: Transfer<u8>,
rx_packet.write_to_rsv(&rsv_buf[1..]); rx_packet.write_to_rsv(&rsv_buf[1..]);
rx_packet.update_frame_length(); rx_packet.update_frame_length();
// Read frame bytes // Read frame bytes
let mut frame_buf = [0; rx::RAW_FRAME_LENGTH_MAX]; let mut frame_buf = [0; RAW_FRAME_LENGTH_MAX];
self.spi_port.read_rxdat(&mut frame_buf, rx_packet.get_frame_length())?; self.spi_port.read_rxdat(&mut frame_buf, rx_packet.get_frame_length())?;
rx_packet.copy_frame_from(&frame_buf[1..]); rx_packet.copy_frame_from(&frame_buf[1..]);
// Set ERXTAIL pointer to (next_addr - 2) // Set ERXTAIL pointer to (next_addr - 2)
@ -157,7 +160,7 @@ impl <SPI: Transfer<u8>,
self.spi_port.write_reg_16b(spi::addrs::EGPWRPT, self.tx_buf.get_next_addr())?; self.spi_port.write_reg_16b(spi::addrs::EGPWRPT, self.tx_buf.get_next_addr())?;
// Copy packet data to SRAM Buffer // Copy packet data to SRAM Buffer
// 1-byte Opcode is included // 1-byte Opcode is included
let mut txdat_buf: [u8; tx::RAW_FRAME_LENGTH_MAX + 1] = [0; tx::RAW_FRAME_LENGTH_MAX + 1]; let mut txdat_buf: [u8; RAW_FRAME_LENGTH_MAX + 1] = [0; RAW_FRAME_LENGTH_MAX + 1];
packet.write_frame_to(&mut txdat_buf[1..]); packet.write_frame_to(&mut txdat_buf[1..]);
self.spi_port.write_txdat(&mut txdat_buf, packet.get_frame_length())?; self.spi_port.write_txdat(&mut txdat_buf, packet.get_frame_length())?;
// Set ETXST to packet start address // Set ETXST to packet start address
@ -170,7 +173,7 @@ impl <SPI: Transfer<u8>,
// Poll TXRTS (ECON1<1>) to check if it is reset // Poll TXRTS (ECON1<1>) to check if it is reset
loop { loop {
econ1_lo = self.spi_port.read_reg_8b(spi::addrs::ECON1)?; econ1_lo = self.spi_port.read_reg_8b(spi::addrs::ECON1)?;
if econ1_lo & 0x02 == 0x02 { break } if econ1_lo & 0x02 == 0 { break }
} }
// TODO: Read ETXSTAT to understand Ethernet transmission status // TODO: Read ETXSTAT to understand Ethernet transmission status
// (See: Register 9-2, ENC424J600 Data Sheet) // (See: Register 9-2, ENC424J600 Data Sheet)

View File

@ -1,10 +1,10 @@
use crate::RAW_FRAME_LENGTH_MAX;
/// SRAM Addresses /// SRAM Addresses
pub const ERXST_DEFAULT: u16 = 0x5340; pub const ERXST_DEFAULT: u16 = 0x5340;
pub const ERXTAIL_DEFAULT: u16 = 0x5ffe; pub const ERXTAIL_DEFAULT: u16 = 0x5ffe;
pub const RX_MAX_ADDRESS: u16 = 0x5fff; pub const RX_MAX_ADDRESS: u16 = 0x5fff;
/// Max raw frame array size
pub const RAW_FRAME_LENGTH_MAX: usize = 0x1000;
/// Receive Status Vector Length /// Receive Status Vector Length
pub const RSV_LENGTH: usize = 6; pub const RSV_LENGTH: usize = 6;

View File

@ -1,5 +1,5 @@
use crate::{ use crate::{
EthController, rx, tx EthController, tx, RAW_FRAME_LENGTH_MAX
}; };
use core::cell; use core::cell;
use smoltcp::{ use smoltcp::{
@ -10,16 +10,16 @@ use smoltcp::{
pub struct SmoltcpDevice<EC: EthController> { pub struct SmoltcpDevice<EC: EthController> {
pub eth_controller: cell::RefCell<EC>, pub eth_controller: cell::RefCell<EC>,
rx_packet_buf: [u8; rx::RAW_FRAME_LENGTH_MAX], rx_packet_buf: [u8; RAW_FRAME_LENGTH_MAX],
tx_packet_buf: [u8; tx::RAW_FRAME_LENGTH_MAX] tx_packet_buf: [u8; RAW_FRAME_LENGTH_MAX]
} }
impl<EC: EthController> SmoltcpDevice<EC> { impl<EC: EthController> SmoltcpDevice<EC> {
pub fn new(eth_controller: EC) -> Self { pub fn new(eth_controller: EC) -> Self {
SmoltcpDevice { SmoltcpDevice {
eth_controller: cell::RefCell::new(eth_controller), eth_controller: cell::RefCell::new(eth_controller),
rx_packet_buf: [0; rx::RAW_FRAME_LENGTH_MAX], rx_packet_buf: [0; RAW_FRAME_LENGTH_MAX],
tx_packet_buf: [0; tx::RAW_FRAME_LENGTH_MAX] tx_packet_buf: [0; RAW_FRAME_LENGTH_MAX]
} }
} }
} }
@ -29,7 +29,9 @@ impl<'a, EC: 'a + EthController> Device<'a> for SmoltcpDevice<EC> {
type TxToken = EthTxToken<'a, EC>; type TxToken = EthTxToken<'a, EC>;
fn capabilities(&self) -> DeviceCapabilities { fn capabilities(&self) -> DeviceCapabilities {
DeviceCapabilities::default() let mut caps = DeviceCapabilities::default();
caps.max_transmission_unit = RAW_FRAME_LENGTH_MAX;
caps
} }
fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> { fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> {

View File

@ -1,10 +1,9 @@
use crate::RAW_FRAME_LENGTH_MAX;
/// SRAM Addresses /// SRAM Addresses
pub const GPBUFST_DEFAULT: u16 = 0x0000; // Start of General-Purpose SRAM Buffer pub const GPBUFST_DEFAULT: u16 = 0x0000; // Start of General-Purpose SRAM Buffer
pub const GPBUFEN_DEFAULT: u16 = 0x5340; // End of General-Purpose SRAM Buffer == ERXST default pub const GPBUFEN_DEFAULT: u16 = 0x5340; // End of General-Purpose SRAM Buffer == ERXST default
/// Max raw frame array size
pub const RAW_FRAME_LENGTH_MAX: usize = 0x1000;
/// Struct for TX Buffer on the hardware /// Struct for TX Buffer on the hardware
/// TODO: Should be a singleton /// TODO: Should be a singleton
pub struct TxBuffer { pub struct TxBuffer {