fix: stabilize lwip ethernet recovery

This commit is contained in:
2026-04-29 04:36:29 +08:00
parent a6040e7d68
commit 8c204aad77
6 changed files with 493 additions and 103 deletions
+67 -2
View File
@@ -16,6 +16,64 @@
#include "app_runtime.h"
#include "debug_log.h"
#define CH390_RESTART_HOLD_DOWN_MS 500u
#define NETWORK_TASK_DELETE_SETTLE_MS 50u
#define CH390_EXPECTED_VENDOR_ID 0x1C00u
#define CH390_EXPECTED_PRODUCT_ID 0x9151u
static void net_poll_wait_for_network_tasks_stop(void)
{
while (app_network_tasks_are_stopped() == pdFALSE) {
vTaskDelay(pdMS_TO_TICKS(20));
}
}
static BaseType_t net_poll_restart_network_stack(const device_config_t *cfg)
{
#if !DIAG_CH390_RAW_POLL
ip4_addr_t ipaddr;
ip4_addr_t netmask;
ip4_addr_t gateway;
uint16_t vendor_id;
uint16_t product_id;
uint8_t revision;
IP4_ADDR(&ipaddr, cfg->net.ip[0], cfg->net.ip[1], cfg->net.ip[2], cfg->net.ip[3]);
IP4_ADDR(&netmask, cfg->net.mask[0], cfg->net.mask[1], cfg->net.mask[2], cfg->net.mask[3]);
IP4_ADDR(&gateway, cfg->net.gw[0], cfg->net.gw[1], cfg->net.gw[2], cfg->net.gw[3]);
#endif
ethernetif_force_link_down();
g_netif_ready = pdFALSE;
app_request_network_task_stop();
net_poll_wait_for_network_tasks_stop();
vTaskDelay(pdMS_TO_TICKS(NETWORK_TASK_DELETE_SETTLE_MS));
vTaskDelay(pdMS_TO_TICKS(CH390_RESTART_HOLD_DOWN_MS));
#if DIAG_CH390_RAW_POLL
ethernetif_diag_ch390_init();
#else
ethernetif_force_full_recovery(&ipaddr, &netmask, &gateway, cfg->net.mac);
vendor_id = ethernetif_ch390_get_vendor_id();
product_id = ethernetif_ch390_get_product_id();
revision = ethernetif_ch390_get_revision();
if ((vendor_id != CH390_EXPECTED_VENDOR_ID) || (product_id != CH390_EXPECTED_PRODUCT_ID)) {
debug_log_printf("[NET] restart-recovery id-warn vid=0x%04X pid=0x%04X rev=0x%02X free=%lu min=%lu\r\n",
(unsigned int)vendor_id,
(unsigned int)product_id,
(unsigned int)revision,
(unsigned long)xPortGetFreeHeapSize(),
(unsigned long)xPortGetMinimumEverFreeHeapSize());
}
#endif
app_clear_network_task_stop();
g_netif_ready = pdTRUE;
app_start_network_tasks();
app_clear_network_restart_request();
return pdTRUE;
}
void NetPollTask(void *argument)
{
const device_config_t *cfg;
@@ -98,6 +156,11 @@ void NetPollTask(void *argument)
debug_log_write("[NET] loop-enter\r\n");
loop_logged = pdTRUE;
}
if (app_network_restart_requested() != pdFALSE) {
(void)net_poll_restart_network_stack(cfg);
}
(void)xSemaphoreTake(xNetSemaphore, pdMS_TO_TICKS(2));
#if DIAG_CH390_RAW_POLL
@@ -120,8 +183,10 @@ void NetPollTask(void *argument)
}
}
#else
ethernetif_poll();
ethernetif_check_link();
if (g_netif_ready != pdFALSE) {
ethernetif_poll();
ethernetif_check_link();
}
#endif
}
}