Skip to content

Commit 1537507

Browse files
committed
STM32H5 Fixes - Fixes DHCP issue broken in PR #65
1 parent 079235f commit 1537507

2 files changed

Lines changed: 48 additions & 6 deletions

File tree

src/port/stm32h563/main.c

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,13 @@ static void uart_puthex(uint32_t val)
334334
}
335335
}
336336

337+
static void uart_puthex8(uint8_t val)
338+
{
339+
const char hex[] = "0123456789ABCDEF";
340+
uart_putc(hex[(val >> 4) & 0xF]);
341+
uart_putc(hex[val & 0xF]);
342+
}
343+
337344
static void uart_putdec(uint32_t val)
338345
{
339346
char buf[12];
@@ -569,26 +576,42 @@ int main(void)
569576
uart_puts((ret & 0x100) ? "UP" : "DOWN");
570577
uart_puts(", PHY addr: ");
571578
uart_puthex(ret & 0xFF);
579+
uart_puts("\n MAC: ");
580+
{
581+
int mi;
582+
for (mi = 0; mi < 6; mi++) {
583+
if (mi > 0) uart_puts(":");
584+
uart_puthex8(ll->mac[mi]);
585+
}
586+
}
572587
uart_puts("\n");
573588
}
574589

575590
#ifdef DHCP
576591
{
577592
uint32_t dhcp_start_tick;
578593
uint32_t dhcp_timeout;
594+
int dhcp_ret;
579595

580-
dhcp_timeout = 30000; /* 30 seconds timeout */
596+
dhcp_timeout = 15000; /* 15 seconds timeout */
581597

582-
if (dhcp_client_init(IPStack) >= 0) {
598+
uart_puts("Starting DHCP client...\n");
599+
dhcp_ret = dhcp_client_init(IPStack);
600+
if (dhcp_ret < 0) {
601+
uart_puts(" DHCP init failed (-");
602+
uart_putdec((uint32_t)(-dhcp_ret));
603+
uart_puts(")\n");
604+
} else {
605+
uart_puts(" DHCP discover sent, waiting for lease...\n");
583606
/* Wait for DHCP to complete - poll frequently */
584607
dhcp_start_tick = tick;
585608
while (!dhcp_bound(IPStack)) {
586609
/* Poll the stack - this processes received packets and sends pending data */
587610
(void)wolfIP_poll(IPStack, tick);
588-
/* Increment tick counter (approximate 1ms per iteration) */
611+
/* Increment tick counter (approximate 1ms per iteration at 64MHz HSI)
612+
* volatile loop ~8 cycles/iter: 8000 * 8 / 64MHz = 1ms */
589613
tick++;
590-
/* Small delay to allow Ethernet DMA to work */
591-
delay(1000);
614+
delay(8000);
592615
/* Check for timeout */
593616
if ((tick - dhcp_start_tick) > dhcp_timeout)
594617
break;
@@ -604,6 +627,21 @@ int main(void)
604627
uart_puts("\n GW: ");
605628
uart_putip4(gw);
606629
uart_puts("\n");
630+
} else {
631+
uint32_t polls = 0, pkts = 0;
632+
stm32_eth_get_stats(&polls, &pkts);
633+
uart_puts(" DHCP timeout - no lease obtained\n");
634+
uart_puts(" ETH stats: polls=");
635+
uart_puthex(polls);
636+
uart_puts(" pkts=");
637+
uart_puthex(pkts);
638+
uart_puts("\n DMACSR=");
639+
uart_puthex(stm32_eth_get_dmacsr());
640+
uart_puts(" RX_DES3=");
641+
uart_puthex(stm32_eth_get_rx_des3());
642+
uart_puts("\n ticks=");
643+
uart_puthex((uint32_t)(tick - dhcp_start_tick));
644+
uart_puts("\n");
607645
}
608646
}
609647
}
@@ -711,7 +749,7 @@ int main(void)
711749

712750
uart_puts("Entering main loop. Ready for connections!\n");
713751
uart_puts(" TCP Echo: port 7\n");
714-
#ifdef ENABLE_TLS
752+
#ifdef ENABLE_TLS_CLIENT
715753
uart_puts(" TLS Client: will connect to Google after ~2s\n");
716754
#endif
717755
#ifdef ENABLE_HTTPS

src/wolfip.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4989,6 +4989,10 @@ static int dhcp_send_request(struct wolfIP *s)
49894989
sin.sin_family = AF_INET;
49904990
wolfIP_sock_sendto(s, s->dhcp_udp_sd, &req, DHCP_HEADER_LEN + opt_sz, 0,
49914991
(struct wolfIP_sockaddr *)&sin, sizeof(struct wolfIP_sockaddr_in));
4992+
/* Reset local_ip so DHCP ACK matches via DHCP_IS_RUNNING path in
4993+
* udp_try_recv(). wolfIP_sock_sendto() sets local_ip from conf->ip
4994+
* (the offered IP), but we haven't confirmed the lease yet. */
4995+
s->udpsockets[SOCKET_UNMARK(s->dhcp_udp_sd)].local_ip = 0;
49924996
tmr.expires = s->last_tick + DHCP_REQUEST_TIMEOUT + (wolfIP_getrandom() % 200);
49934997
tmr.arg = s;
49944998
tmr.cb = dhcp_timer_cb;

0 commit comments

Comments
 (0)