Skip to content

Commit e9a3a5e

Browse files
committed
Add software delays on controller init; add missing SPISEL delay
1 parent ae0d77c commit e9a3a5e

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

examples/tcp_stm32f407.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ use cortex_m::iprintln;
88

99
use cortex_m_rt::entry;
1010
use embedded_hal::digital::v2::OutputPin;
11+
use embedded_hal::blocking::delay::DelayMs;
1112
use stm32f4xx_hal::{
1213
rcc::RccExt,
1314
gpio::GpioExt,
1415
time::U32Ext,
1516
stm32::{CorePeripherals, Peripherals},
17+
delay::Delay,
1618
spi::Spi,
1719
time::Hertz
1820
};
@@ -36,7 +38,7 @@ use stm32f4xx_hal::{
3638
rcc::Clocks,
3739
time::MilliSeconds,
3840
timer::{Timer, Event as TimerEvent},
39-
stm32::SYST,
41+
stm32::SYST
4042
};
4143
/// Rate in Hz
4244
const TIMER_RATE: u32 = 20;
@@ -117,6 +119,7 @@ fn main() -> ! {
117119
// NIC100 / ENC424J600 Set-up
118120
let spi1 = dp.SPI1;
119121
let gpioa = dp.GPIOA.split();
122+
let mut delay = Delay::new(cp.SYST, clocks);
120123
// Mapping: see Table 9, STM32F407ZG Manual
121124
let spi1_sck = gpioa.pa5.into_alternate_af5();
122125
let spi1_miso = gpioa.pa6.into_alternate_af5();
@@ -125,6 +128,7 @@ fn main() -> ! {
125128
// Map SPISEL: see Table 1, NIC100 Manual
126129
let mut spisel = gpioa.pa1.into_push_pull_output();
127130
spisel.set_high().unwrap();
131+
delay.delay_ms(1_u32);
128132
spisel.set_low().unwrap();
129133
// Create SPI1 for HAL
130134
let spi_eth_port = Spi::spi1(
@@ -134,7 +138,7 @@ fn main() -> ! {
134138
clocks);
135139
let mut spi_eth = enc424j600::SpiEth::new(spi_eth_port, spi1_nss);
136140
// Init
137-
match spi_eth.init_dev() {
141+
match spi_eth.init_dev(&mut delay) {
138142
Ok(_) => {
139143
iprintln!(stim0, "Ethernet initialised.")
140144
}
@@ -145,7 +149,7 @@ fn main() -> ! {
145149

146150
// Setup SysTick
147151
// Reference to stm32-eth:examples/ip.rs
148-
timer_setup(cp.SYST, clocks);
152+
timer_setup(delay.free(), clocks);
149153
iprintln!(stim0, "Timer initialised.");
150154

151155
// Read MAC
@@ -165,7 +169,7 @@ fn main() -> ! {
165169
// examples/loopback.rs, examples/multicast.rs
166170
let device = smoltcp_phy::SmoltcpDevice::new(&mut spi_eth);
167171
let mut neighbor_cache_entries = [None; 16];
168-
let mut neighbor_cache = NeighborCache::new(&mut neighbor_cache_entries[..]);
172+
let neighbor_cache = NeighborCache::new(&mut neighbor_cache_entries[..]);
169173
let ip_addr = IpCidr::new(IpAddress::v4(
170174
arg_ip[0], arg_ip[1], arg_ip[2], arg_ip[3]), arg_ip_pref);
171175
let mut ip_addrs = [ip_addr];

examples/tx_stm32f407.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ fn main() -> ! {
6363
clocks);
6464
let mut spi_eth = enc424j600::SpiEth::new(spi_eth_port, spi1_nss);
6565
// Init
66-
match spi_eth.init_dev() {
66+
match spi_eth.init_dev(&mut delay) {
6767
Ok(_) => {
6868
iprintln!(stim0, "Ethernet initialised.")
6969
}

src/lib.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
pub mod spi;
44
use embedded_hal::{
5-
blocking::spi::Transfer,
5+
blocking::{
6+
spi::Transfer,
7+
delay::DelayUs,
8+
},
69
digital::v2::OutputPin,
710
};
811

@@ -13,7 +16,7 @@ pub mod tx;
1316
pub mod smoltcp_phy;
1417

1518
pub trait EthController<'c> {
16-
fn init_dev(&mut self) -> Result<(), EthControllerError>;
19+
fn init_dev(&mut self, delay: &mut dyn DelayUs<u16>) -> Result<(), EthControllerError>;
1720
fn init_rxbuf(&mut self) -> Result<(), EthControllerError>;
1821
fn init_txbuf(&mut self) -> Result<(), EthControllerError>;
1922
fn receive_next(&mut self, is_poll: bool) -> Result<rx::RxPacket, EthControllerError>;
@@ -57,7 +60,7 @@ impl <SPI: Transfer<u8>,
5760

5861
impl <'c, SPI: Transfer<u8>,
5962
NSS: OutputPin> EthController<'c> for SpiEth<SPI, NSS> {
60-
fn init_dev(&mut self) -> Result<(), EthControllerError> {
63+
fn init_dev(&mut self, delay: &mut dyn DelayUs<u16>) -> Result<(), EthControllerError> {
6164
// Write 0x1234 to EUDAST
6265
self.spi_port.write_reg_16b(spi::addrs::EUDAST, 0x1234)?;
6366
// Verify that EUDAST is 0x1234
@@ -73,11 +76,15 @@ impl <'c, SPI: Transfer<u8>,
7376
// Set ETHRST (ECON2<4>) to 1
7477
let econ2 = self.spi_port.read_reg_8b(spi::addrs::ECON2)?;
7578
self.spi_port.write_reg_8b(spi::addrs::ECON2, 0x10 | (econ2 & 0b11101111))?;
79+
// Wait for 25us
80+
delay.delay_us(25_u16);
7681
// Verify that EUDAST is 0x0000
7782
eudast = self.spi_port.read_reg_16b(spi::addrs::EUDAST)?;
7883
if eudast != 0x0000 {
7984
return Err(EthControllerError::GeneralError)
8085
}
86+
// Wait for 256us
87+
delay.delay_us(256_u16);
8188
Ok(())
8289
}
8390

0 commit comments

Comments
 (0)