diff --git a/Cargo.lock b/Cargo.lock index feb2d88..4173ba7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,6 +4,7 @@ name = "adc2tcp" version = "0.0.0" dependencies = [ + "bare-metal 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "cortex-m 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", "cortex-m-log 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "cortex-m-rt 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 7839098..1eb40b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ default-target = "thumbv7em-none-eabihf" panic-abort = { version = "0.3.1" } panic-semihosting = { version = "0.5.1", optional = true } log = "0.4" +bare-metal = "0.2" cortex-m = "0.5" cortex-m-rt = { version = "0.6", features = ["device"] } cortex-m-log = { version = "0.4", features = ["log-integration"] } diff --git a/src/main.rs b/src/main.rs index 8b6e625..a66260b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -122,14 +122,16 @@ fn main() -> ! { // Update watchdog wd.feed(); - led_red.off(); - // Wait for interrupts - // if net.is_pending() { - led_green.on(); - wfi(); - led_green.off(); - // } + led_red.on(); + cortex_m::interrupt::free(|cs| { + if !net::is_pending(cs) { + // Wait for interrupts + wfi(); + net::clear_pending(cs); + } + }); + led_red.off(); } }) } diff --git a/src/net.rs b/src/net.rs index 0397056..9196de2 100644 --- a/src/net.rs +++ b/src/net.rs @@ -1,6 +1,9 @@ //! As there is only one peripheral, supporting data structures are //! declared once and globally. +use core::cell::RefCell; +use cortex_m::interrupt::Mutex; +use bare_metal::CriticalSection; use stm32f4xx_hal::{ stm32::{interrupt, Peripherals, NVIC, ETHERNET_MAC, ETHERNET_DMA}, }; @@ -17,6 +20,7 @@ static mut TX_RING: Option<[RingEntry; 2]> = None; // TODO: generate one from device id const SRC_MAC: [u8; 6] = [0x00, 0x00, 0xDE, 0xAD, 0xBE, 0xEF]; +static NET_PENDING: Mutex> = Mutex::new(RefCell::new(false)); pub fn run(nvic: &mut NVIC, ethernet_mac: ETHERNET_MAC, ethernet_dma: ETHERNET_DMA, f: F) -> ! where @@ -79,6 +83,21 @@ impl<'a> NetInterface<'a> { /// and TODO: set pending flag #[interrupt] fn ETH() { + cortex_m::interrupt::free(|cs| { + *NET_PENDING.borrow(cs) + .borrow_mut() = true; + }); + let p = unsafe { Peripherals::steal() }; stm32_eth::eth_interrupt_handler(&p.ETHERNET_DMA); } + +pub fn is_pending(cs: &CriticalSection) -> bool { + *NET_PENDING.borrow(cs) + .borrow() +} + +pub fn clear_pending(cs: &CriticalSection) { + *NET_PENDING.borrow(cs) + .borrow_mut() = false; +}