From ef02c42207b931ca839b9ac2a2d0160a51219347 Mon Sep 17 00:00:00 2001 From: kuwoyuki Date: Sat, 8 Nov 2025 20:18:23 +0600 Subject: [PATCH] fix: warnings --- .vscode/settings.json | 5 ++++- Makefile | 2 -- main.c | 34 ++++++++++++++++++++++++++-------- port/ethernetif.c | 23 +++-------------------- port/lwipopts.h | 7 ++++--- 5 files changed, 37 insertions(+), 34 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 5d16f79..3254d1a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -35,6 +35,9 @@ "list": "c", "locale": "c", "regex": "c", - "vector": "c" + "vector": "c", + "memory_resource": "c", + "__config": "c", + "string": "c" } } \ No newline at end of file diff --git a/Makefile b/Makefile index 1e91c25..d828b97 100644 --- a/Makefile +++ b/Makefile @@ -40,8 +40,6 @@ ADDITIONAL_C_FILES := \ $(LWIP_C_FILES_WITH_PATH) \ $(LWIP_PORT_FILES) -$(info Final ADDITIONAL_C_FILES is: [$(ADDITIONAL_C_FILES)]) - include $(CH32V003FUN)/ch32fun.mk CFLAGS += -Wall -Wextra $(INCLUDE_DIRS) diff --git a/main.c b/main.c index 0304042..5f84abd 100644 --- a/main.c +++ b/main.c @@ -27,6 +27,7 @@ #define STATS_PRINT_INTERVAL_MS 10000 struct netif g_netif; +static volatile int g_httpd_is_initialized = 0; int clock_init(void); void led_init(void); @@ -76,9 +77,21 @@ void led_init(void) { GPIOA->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP) << (4 * LED2_PIN); } +static void link_callback(struct netif* netif) { + if (netif_is_link_up(netif)) { + printf("Link is UP\n"); + printf("Starting DHCP client...\n"); + dhcp_start(netif); + } else { + printf("Link is DOWN\n"); + printf("Stopping DHCP client...\n"); + dhcp_stop(netif); + } +} + static void netif_status_callback(struct netif* netif) { if (netif_is_up(netif) && !ip_addr_isany_val(*netif_ip4_addr(netif))) { - printf("netif UP\n"); + printf("netif is UP with a valid IP\n"); printf(" MAC : %02X:%02X:%02X:%02X:%02X:%02X\n", netif->hwaddr[0], netif->hwaddr[1], netif->hwaddr[2], netif->hwaddr[3], netif->hwaddr[4], netif->hwaddr[5]); @@ -86,12 +99,17 @@ static void netif_status_callback(struct netif* netif) { printf(" Mask : %s\n", ip4addr_ntoa(netif_ip4_netmask(netif))); printf(" GW : %s\n", ip4addr_ntoa(netif_ip4_gw(netif))); - httpd_init(); - printf("HTTP server init\n"); + if (!g_httpd_is_initialized) { + httpd_init(); + printf("HTTP server initialized\n"); + g_httpd_is_initialized = 1; + } + GPIOA->BSHR = (1 << LED2_PIN); } else { - printf("netif DOWN or has no IP address\n"); + printf("netif is DOWN or has no IP address\n"); + GPIOA->BSHR = (1 << (LED2_PIN + 16)); } } @@ -109,12 +127,10 @@ void lwip_stack_init(void) { ðernet_input); netif_set_status_callback(&g_netif, netif_status_callback); + netif_set_link_callback(&g_netif, link_callback); netif_set_default(&g_netif); netif_set_up(&g_netif); - - printf("Starting DHCP client...\n"); - dhcp_start(&g_netif); } #if LWIP_STATS @@ -155,7 +171,9 @@ int main() { uint32_t last_led_toggle_time = 0; uint32_t last_link_poll_time = 0; +#if LWIP_STATS uint32_t last_stats_print_time = 0; +#endif int led_state = 0; while (1) { @@ -185,4 +203,4 @@ int main() { last_led_toggle_time = now; } } -} \ No newline at end of file +} diff --git a/port/ethernetif.c b/port/ethernetif.c index 6867232..39751a8 100644 --- a/port/ethernetif.c +++ b/port/ethernetif.c @@ -124,11 +124,9 @@ static void low_level_init(struct netif* netif) { ETH10M->ERXST = ethernetif->DMARxDescToGet->Buffer1Addr; ETH10M->ECON1 = RB_ETH_ECON1_RXEN; - printf("Resetting PHY...\n"); WritePHYReg(PHY_BMCR, PHY_BMCR_RESET); Delay_Ms(200); - printf("Starting PHY, Mode: 10BASE_T_FD\n"); WritePHYReg(PHY_BMCR, PHY_BMCR_FULL_DUPLEX); ETH10M->EIR = 0xFF; // clear all interrupt flags @@ -136,14 +134,13 @@ static void low_level_init(struct netif* netif) { RB_ETH_EIE_TXERIE | RB_ETH_EIE_RXERIE | RB_ETH_EIE_R_EN50; NVIC_EnableIRQ(ETH_IRQn); - printf("low_level_init: done\n"); } static err_t low_level_output(struct netif* netif, struct pbuf* p) { + (void)netif; + if (DMATxDscrTab[0].Status & ETH_DMATxDesc_OWN) { -#if LWIP_STATS LINK_STATS_INC(link.drop); -#endif return ERR_BUF; } @@ -160,9 +157,7 @@ static err_t low_level_output(struct netif* netif, struct pbuf* p) { DMATxDscrTab[0].Status |= ETH_DMATxDesc_OWN; ETH10M->ECON1 |= RB_ETH_ECON1_TXRTS; -#if LWIP_STATS LINK_STATS_INC(link.xmit); -#endif MIB2_STATS_NETIF_ADD(netif, ifoutoctets, len); return ERR_OK; @@ -178,9 +173,7 @@ static struct pbuf* low_level_input(struct netif* netif) { uint16_t len = ETH10M->ERXLN; if (len < MIN_ETH_FRAME_SIZE || len > ETH_MAX_PACKET_SIZE) { -#if LWIP_STATS LINK_STATS_INC(link.lenerr); -#endif ETH10M->EIR = RB_ETH_EIR_RXIF; ETH10M->ECON1 |= RB_ETH_ECON1_RXEN; return NULL; @@ -196,15 +189,11 @@ static struct pbuf* low_level_input(struct netif* netif) { memcpy(q->payload, current_rx_buffer_ptr + offset, q->len); offset += q->len; } -#if LWIP_STATS LINK_STATS_INC(link.recv); -#endif MIB2_STATS_NETIF_ADD(netif, ifinoctets, len); } else { -#if LWIP_STATS LINK_STATS_INC(link.memerr); LINK_STATS_INC(link.drop); -#endif MIB2_STATS_NETIF_INC(netif, ifindiscards); } @@ -237,13 +226,11 @@ void ethernetif_link_poll(struct netif* netif) { if (bmsr & PHY_BMSR_LINK_STATUS) { if (!netif_is_link_up(netif)) { - printf("Link is UP\n"); ETH10M->MACON2 |= RB_ETH_MACON2_FULDPX; netif_set_link_up(netif); } } else { if (netif_is_link_up(netif)) { - printf("Link is DOWN\n"); netif_set_link_down(netif); } } @@ -261,17 +248,13 @@ void ETH_IRQHandler(void) { if (flags & RB_ETH_EIR_TXERIF) { DMATxDscrTab[0].Status &= ~ETH_DMATxDesc_OWN; ETH10M->EIR = RB_ETH_EIR_TXERIF; -#if LWIP_STATS LINK_STATS_INC(link.err); -#endif } if (flags & RB_ETH_EIR_RXERIF) { ETH10M->EIR = RB_ETH_EIR_RXERIF; ETH10M->ECON1 |= RB_ETH_ECON1_RXEN; -#if LWIP_STATS LINK_STATS_INC(link.err); -#endif } if (flags & RB_ETH_EIR_LINKIF) { @@ -282,7 +265,7 @@ void ETH_IRQHandler(void) { void WritePHYReg(uint8_t reg_add, uint16_t reg_val) { R32_ETH_MIWR = (reg_add & RB_ETH_MIREGADR_MASK) | RB_ETH_MIWR_MIIWR | - RB_ETH_MIWR_DATA_SHIFT; + (reg_val << RB_ETH_MIWR_DATA_SHIFT); } uint16_t ReadPHYReg(uint8_t reg_add) { diff --git a/port/lwipopts.h b/port/lwipopts.h index 85c0d31..a3f6df3 100644 --- a/port/lwipopts.h +++ b/port/lwipopts.h @@ -50,8 +50,9 @@ #define LWIP_SOCKET 0 // Statistics -#define LWIP_STATS 1 -#define LINK_STATS 1 +#define LWIP_STATS 0 +#define LINK_STATS 0 +#define MIB2_STATS 0 #define LWIP_HTTPD 1 // Use a read-only filesystem populated by makefsdata @@ -60,7 +61,7 @@ #define HTTPD_USE_CUSTOM_FSDATA 1 #define LWIP_NETIF_HOSTNAME 1 -// #define LWIP_NETIF_LINK_CALLBACK 1 +#define LWIP_NETIF_LINK_CALLBACK 1 #define LWIP_NETIF_STATUS_CALLBACK 1 #define LWIP_SUPPORT_CUSTOM_PBUF 1