diff --git a/src/main.rs b/src/main.rs index 71275e2..820beeb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -159,19 +159,16 @@ fn main() -> ! { } }); } - net::net::for_each(|mut socket, id| { - if net::net::eth_is_socket_active(socket) { - cortex_m::interrupt::free(|cs| - { - eth_is_pending = net::net::is_pending(cs); - } - ); - - if eth_is_pending { + cortex_m::interrupt::free(|cs| + { + eth_is_pending = net::net::is_pending(cs); + net::net::clear_pending(cs); + } + ); + if eth_is_pending { + net::net::for_each(|mut socket, id| { + if net::net::eth_is_socket_active(socket) && net::net::eth_is_socket_connected(socket){ unsafe{ - cortex_m::interrupt::free(|cs| { - net::net::clear_pending(cs); - }); let bytes = net::net::eth_recv(&mut ETH_DATA_BUFFER, socket); if bytes != 0 { info!("Ts: {:?}", sys_timer::now()); @@ -184,8 +181,8 @@ fn main() -> ! { if has_temp_reading { thermostat.start_tec_readings_conversion(); } - } - }); + }) + }; } State::SaveFlashSettings => { // State Transition diff --git a/src/net/net.rs b/src/net/net.rs index 2bba8e8..44d28a8 100644 --- a/src/net/net.rs +++ b/src/net/net.rs @@ -207,8 +207,11 @@ impl ServerHandle { self.link_was_up = self.phy.phy_link_up(); } - pub fn recv(&mut self, buffer: &mut [u8], socket_handles: SocketHandle)-> Result { + pub fn poll_iface(&mut self) { self.iface.poll(now_fn(), &mut &mut self.dma, &mut self.socket_set); + } + + pub fn recv(&mut self, buffer: &mut [u8], socket_handles: SocketHandle)-> Result { let socket = self.socket_set.get_mut::(socket_handles); socket.recv_slice(buffer) @@ -218,11 +221,9 @@ impl ServerHandle { let socket = self.socket_set.get_mut::(socket_handles); if num_bytes > 0 { socket.send_slice(&buffer[..num_bytes]).ok(); + self.poll_iface(); info!("Sent {} bytes.", num_bytes); } - - // Send bytes out - self.iface.poll(now_fn(), &mut &mut self.dma, &mut self.socket_set); } pub fn is_socket_connected(&mut self, socket_handles: SocketHandle)->bool { @@ -362,6 +363,17 @@ pub fn eth_poll_and_update_link_speed() { } } +pub fn eth_poll_iface() { + unsafe { + if let Some(ref mut server_handle ) = SERVER_HANDLE { + server_handle.poll_iface(); + } + else { + panic!("eth_poll_packet is called before init"); + } + } +} + pub fn eth_send(buffer: &mut [u8], num_bytes: usize, socket_handles: SocketHandle) { unsafe { if let Some(ref mut server_handle ) = SERVER_HANDLE { @@ -442,10 +454,12 @@ pub fn for_each(mut callback: F) { fn ETH() { let interrupt_reason = stm32_eth::eth_interrupt_handler(); cortex_m::interrupt::free(|cs| { - *NET_PENDING.borrow(cs) - .borrow_mut() = true; + if interrupt_reason.rx { + *NET_PENDING.borrow(cs) + .borrow_mut() = true; + eth_poll_iface(); + } }); - debug!("Ethernet Interrupt{:?}", interrupt_reason); }