Restore embedded_hal DelayUs for device init

This commit is contained in:
Harry Ho 2021-04-27 16:14:22 +08:00
parent ec20970a50
commit 9de8d77a24
1 changed files with 6 additions and 5 deletions

View File

@ -4,6 +4,7 @@ pub mod spi;
use embedded_hal::{ use embedded_hal::{
blocking::{ blocking::{
spi::Transfer, spi::Transfer,
delay::DelayUs,
}, },
digital::v2::OutputPin, digital::v2::OutputPin,
}; };
@ -61,14 +62,14 @@ impl <SPI: Transfer<u8>,
} }
} }
pub fn init(&mut self) -> Result<(), Error> { pub fn init(&mut self, delay: &mut impl DelayUs<u16>) -> Result<(), Error> {
self.reset()?; self.reset(delay)?;
self.init_rxbuf()?; self.init_rxbuf()?;
self.init_txbuf()?; self.init_txbuf()?;
Ok(()) Ok(())
} }
pub fn reset(&mut self) -> Result<(), Error> { pub fn reset(&mut self, delay: &mut impl DelayUs<u16>) -> Result<(), Error> {
// Write 0x1234 to EUDAST // Write 0x1234 to EUDAST
self.spi_port.write_reg_16b(spi::addrs::EUDAST, 0x1234)?; self.spi_port.write_reg_16b(spi::addrs::EUDAST, 0x1234)?;
// Verify that EUDAST is 0x1234 // Verify that EUDAST is 0x1234
@ -83,13 +84,13 @@ impl <SPI: Transfer<u8>,
} }
// Issue system reset - set ETHRST (ECON2<4>) to 1 // Issue system reset - set ETHRST (ECON2<4>) to 1
self.spi_port.send_opcode(spi::opcodes::SETETHRST)?; self.spi_port.send_opcode(spi::opcodes::SETETHRST)?;
self.spi_port.delay_us(25); delay.delay_us(25);
// Verify that EUDAST is 0x0000 // Verify that EUDAST is 0x0000
eudast = self.spi_port.read_reg_16b(spi::addrs::EUDAST)?; eudast = self.spi_port.read_reg_16b(spi::addrs::EUDAST)?;
if eudast != 0x0000 { if eudast != 0x0000 {
return Err(Error::RegisterError) return Err(Error::RegisterError)
} }
self.spi_port.delay_us(256); delay.delay_us(256);
Ok(()) Ok(())
} }