Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 44 additions & 6 deletions src/port/stm32h563/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,13 @@ static void uart_puthex(uint32_t val)
}
}

static void uart_puthex8(uint8_t val)
{
const char hex[] = "0123456789ABCDEF";
uart_putc(hex[(val >> 4) & 0xF]);
uart_putc(hex[val & 0xF]);
}

static void uart_putdec(uint32_t val)
{
char buf[12];
Expand Down Expand Up @@ -569,26 +576,42 @@ int main(void)
uart_puts((ret & 0x100) ? "UP" : "DOWN");
uart_puts(", PHY addr: ");
uart_puthex(ret & 0xFF);
uart_puts("\n MAC: ");
{
int mi;
for (mi = 0; mi < 6; mi++) {
if (mi > 0) uart_puts(":");
uart_puthex8(ll->mac[mi]);
}
}
uart_puts("\n");
}

#ifdef DHCP
{
uint32_t dhcp_start_tick;
uint32_t dhcp_timeout;
int dhcp_ret;

dhcp_timeout = 30000; /* 30 seconds timeout */
dhcp_timeout = 15000; /* 15 seconds timeout */

if (dhcp_client_init(IPStack) >= 0) {
uart_puts("Starting DHCP client...\n");
dhcp_ret = dhcp_client_init(IPStack);
if (dhcp_ret < 0) {
uart_puts(" DHCP init failed (-");
uart_putdec((uint32_t)(-dhcp_ret));
uart_puts(")\n");
} else {
uart_puts(" DHCP discover sent, waiting for lease...\n");
/* Wait for DHCP to complete - poll frequently */
dhcp_start_tick = tick;
while (!dhcp_bound(IPStack)) {
/* Poll the stack - this processes received packets and sends pending data */
(void)wolfIP_poll(IPStack, tick);
/* Increment tick counter (approximate 1ms per iteration) */
/* Increment tick counter (approximate 1ms per iteration at 64MHz HSI)
* volatile loop ~8 cycles/iter: 8000 * 8 / 64MHz = 1ms */
tick++;
/* Small delay to allow Ethernet DMA to work */
delay(1000);
delay(8000);
Comment on lines +611 to +614
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loop assumes a specific core clock (64MHz HSI) and a specific delay() loop cost, but tick is still incremented by 1 regardless of the actual elapsed time, making DHCP timeout behavior fragile across clock configuration changes/optimizations. Consider driving the timeout from a real timebase (SysTick/HAL tick/monotonic timer) or deriving the delay count from SystemCoreClock and updating tick based on measured/actual elapsed time.

Copilot uses AI. Check for mistakes.
/* Check for timeout */
if ((tick - dhcp_start_tick) > dhcp_timeout)
break;
Expand All @@ -604,6 +627,21 @@ int main(void)
uart_puts("\n GW: ");
uart_putip4(gw);
uart_puts("\n");
} else {
uint32_t polls = 0, pkts = 0;
stm32_eth_get_stats(&polls, &pkts);
uart_puts(" DHCP timeout - no lease obtained\n");
uart_puts(" ETH stats: polls=");
uart_puthex(polls);
uart_puts(" pkts=");
uart_puthex(pkts);
uart_puts("\n DMACSR=");
uart_puthex(stm32_eth_get_dmacsr());
uart_puts(" RX_DES3=");
uart_puthex(stm32_eth_get_rx_des3());
uart_puts("\n ticks=");
uart_puthex((uint32_t)(tick - dhcp_start_tick));
uart_puts("\n");
}
}
}
Expand Down Expand Up @@ -711,7 +749,7 @@ int main(void)

uart_puts("Entering main loop. Ready for connections!\n");
uart_puts(" TCP Echo: port 7\n");
#ifdef ENABLE_TLS
#ifdef ENABLE_TLS_CLIENT
uart_puts(" TLS Client: will connect to Google after ~2s\n");
#endif
#ifdef ENABLE_HTTPS
Expand Down
4 changes: 4 additions & 0 deletions src/wolfip.c
Original file line number Diff line number Diff line change
Expand Up @@ -4989,6 +4989,10 @@ static int dhcp_send_request(struct wolfIP *s)
sin.sin_family = AF_INET;
wolfIP_sock_sendto(s, s->dhcp_udp_sd, &req, DHCP_HEADER_LEN + opt_sz, 0,
(struct wolfIP_sockaddr *)&sin, sizeof(struct wolfIP_sockaddr_in));
/* Reset local_ip so DHCP ACK matches via DHCP_IS_RUNNING path in
* udp_try_recv(). wolfIP_sock_sendto() sets local_ip from conf->ip
* (the offered IP), but we haven't confirmed the lease yet. */
s->udpsockets[SOCKET_UNMARK(s->dhcp_udp_sd)].local_ip = 0;
Comment on lines +4992 to +4995
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This directly mutates internal socket state (s->udpsockets[...]) from DHCP code, which tightly couples DHCP behavior to socket internals. Consider encapsulating this in a small helper (e.g., a socket API to clear/reset local_ip for a given fd) or adjusting the wolfIP_sock_sendto() behavior for DHCP sockets so DHCP doesn’t need to reach into udpsockets internals.

Copilot uses AI. Check for mistakes.
tmr.expires = s->last_tick + DHCP_REQUEST_TIMEOUT + (wolfIP_getrandom() % 200);
tmr.arg = s;
tmr.cb = dhcp_timer_cb;
Expand Down