From 7d2e14ef2f8b83d3a3f76e4981717bc3f18882e1 Mon Sep 17 00:00:00 2001 From: linuswck Date: Wed, 20 Mar 2024 14:25:35 +0800 Subject: [PATCH] eth: Poll Iface In ETH IRQ - Improve responsiveness of TCP packets handling - Fix a bug when one client is connected and active report is ON, the other client's cmds do not receive Ack from Kirdy --- src/main.rs | 25 +++++++++++-------------- src/net/net.rs | 28 +++++++++++++++++++++------- 2 files changed, 32 insertions(+), 21 deletions(-) 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); }