From 44f48f6e0ff775819bf5fedf3adda4ae4f116595 Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 8 Aug 2019 00:54:37 +0200 Subject: [PATCH] main: construct softspi --- firmware/src/board/mod.rs | 6 +++--- firmware/src/board/softspi.rs | 14 +++++++++----- firmware/src/main.rs | 16 ++++++++++++++++ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/firmware/src/board/mod.rs b/firmware/src/board/mod.rs index 01eb1d5..ede9436 100644 --- a/firmware/src/board/mod.rs +++ b/firmware/src/board/mod.rs @@ -1,9 +1,9 @@ use cortex_m; use tm4c129x; -mod gpio; -mod softspi; -mod delay; +pub mod gpio; +pub mod softspi; +pub mod delay; const LED1: u8 = 0x10; // PK4 diff --git a/firmware/src/board/softspi.rs b/firmware/src/board/softspi.rs index 69d3d4c..f5bd286 100644 --- a/firmware/src/board/softspi.rs +++ b/firmware/src/board/softspi.rs @@ -74,7 +74,7 @@ impl SoftSpi { } } - pub fn run(&mut self, delay: &'_ F) { + pub fn run(&mut self, delay: &'_ mut F) { while self.state != State::Idle { self.tick(); delay(); @@ -112,12 +112,16 @@ impl FullDuplex for SoftSpi } } -pub struct SyncSoftSpi<'d, SCK: OutputPin, MOSI: OutputPin, MISO: InputPin, D: Fn()> { +pub struct SyncSoftSpi<'d, SCK: OutputPin, MOSI: OutputPin, MISO: InputPin, D: FnMut()> { spi: SoftSpi, - delay: &'d D, + delay: &'d mut D, } -impl<'d, SCK: OutputPin, MOSI: OutputPin, MISO: InputPin, D: Fn()> SyncSoftSpi<'d, SCK, MOSI, MISO, D> { +impl<'d, SCK: OutputPin, MOSI: OutputPin, MISO: InputPin, D: FnMut()> SyncSoftSpi<'d, SCK, MOSI, MISO, D> { + pub fn new(spi: SoftSpi, delay: &'d mut D) -> Self { + SyncSoftSpi { spi, delay } + } + fn retry(&mut self, f: &F) -> Result where F: Fn(&'_ mut SoftSpi) -> Result> @@ -132,7 +136,7 @@ impl<'d, SCK: OutputPin, MOSI: OutputPin, MISO: InputPin, D: Fn()> SyncSoftSpi<' } } -impl<'d, SCK: OutputPin, MOSI: OutputPin, MISO: InputPin, D: Fn()> Transfer for SyncSoftSpi<'d, SCK, MOSI, MISO, D> { +impl<'d, SCK: OutputPin, MOSI: OutputPin, MISO: InputPin, D: FnMut()> Transfer for SyncSoftSpi<'d, SCK, MOSI, MISO, D> { type Error = (); fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> { for b in words.iter_mut() { diff --git a/firmware/src/main.rs b/firmware/src/main.rs index 986756c..deb0bbf 100644 --- a/firmware/src/main.rs +++ b/firmware/src/main.rs @@ -15,6 +15,8 @@ extern crate nb; use core::cell::{Cell, RefCell}; use core::fmt; +use embedded_hal::digital::{InputPin, OutputPin}; +use embedded_hal::blocking::delay::DelayUs; use cortex_m::interrupt::Mutex; use smoltcp::time::Instant; use smoltcp::wire::EthernetAddress; @@ -44,6 +46,7 @@ pub fn panic_fmt(info: &core::panic::PanicInfo) -> ! { #[macro_use] mod board; +use board::gpio::Gpio; mod eeprom; mod config; mod ethmac; @@ -112,6 +115,19 @@ fn main() -> ! { | | |_| "#); + let mut delay = board::delay::Delay::new(); + // SCK + let pb4 = board::gpio::PB4.into_output(); + // SCLK + let pb5 = board::gpio::PB5.into_output(); + // MOSI + let pe4 = board::gpio::PE4.into_output(); + // MISO + let pe5 = board::gpio::PE5.into_input(); + let spi = board::softspi::SyncSoftSpi::new( + board::softspi::SoftSpi::new(pb4, pe4, pe5), + &mut || delay.delay_us(1u32) + ); let mut hardware_addr = EthernetAddress(board::get_mac_address()); if hardware_addr.is_multicast() {