Skip to content

Commit 2ba6e9c

Browse files
committed
Support for DHCP
1 parent c1b33d2 commit 2ba6e9c

4 files changed

Lines changed: 129 additions & 14 deletions

File tree

src/port/stm32h563/README.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ sudo apt install gcc-arm-none-eabi openocd
2727

2828
```bash
2929
cd src/port/stm32h563
30-
make TZEN=0
30+
make
3131
```
3232

3333
This produces `app.elf` and `app.bin` for use with TZEN=0 (TrustZone disabled).
@@ -94,7 +94,7 @@ picocom -b 115200 /dev/ttyACM0
9494

9595
## Example Output
9696

97-
When the firmware boots successfully, you should see output similar to:
97+
When the firmware boots successfully with DHCP (default), you should see output similar to:
9898

9999
```
100100
=== wolfIP STM32H563 Echo Server ===
@@ -113,11 +113,35 @@ Entering main loop. Ready for connections!
113113
Loop starting...
114114
```
115115

116+
When static IP is enabled, the output will show "Setting IP configuration:" instead of "DHCP configuration received:".
117+
116118
The "PHY link: UP" message indicates the Ethernet PHY has established a link with the network.
117119

118120
## Network Configuration
119121

120-
The example configures the following static IP:
122+
The example can be configured to use either DHCP or static IP. By default, DHCP is enabled.
123+
124+
### DHCP Configuration (Default)
125+
126+
By default, the device uses DHCP to automatically obtain IP address, subnet mask, gateway, and DNS server from a DHCP server on the network. The obtained configuration will be displayed on the serial console.
127+
128+
**Note:** When DHCP is enabled, the device will wait up to 30 seconds for a DHCP server response during initialization. If no DHCP server is available, the device will timeout and continue without network configuration.
129+
130+
### Static IP Configuration
131+
132+
To use static IP instead of DHCP, set `WOLFIP_ENABLE_DHCP` to `0` in `config.h`:
133+
134+
```c
135+
#define WOLFIP_ENABLE_DHCP 0
136+
```
137+
138+
Or compile with:
139+
140+
```bash
141+
make CFLAGS+="-DWOLFIP_ENABLE_DHCP=0"
142+
```
143+
144+
When static IP is enabled, the example configures the following:
121145

122146
| Setting | Value |
123147
|---------|-------|

src/port/stm32h563/config.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,17 @@
4848
#define WOLFIP_ENABLE_LOOPBACK 0
4949
#endif
5050

51+
#ifndef WOLFIP_ENABLE_DHCP
52+
#define WOLFIP_ENABLE_DHCP 1
53+
#endif
54+
55+
#if WOLFIP_ENABLE_DHCP
56+
#define DHCP
57+
#else
5158
#define WOLFIP_IP "192.168.12.11"
5259
#define WOLFIP_NETMASK "255.255.255.0"
5360
#define WOLFIP_GW "192.168.12.1"
5461
#define WOLFIP_STATIC_DNS_IP "9.9.9.9"
62+
#endif
5563

5664
#endif

src/port/stm32h563/main.c

Lines changed: 88 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,34 @@ static void uart_puthex(uint32_t val)
230230
}
231231
}
232232

233+
static void uart_putdec(uint32_t val)
234+
{
235+
char buf[12];
236+
int i = 0;
237+
if (val == 0) {
238+
uart_putc('0');
239+
return;
240+
}
241+
while (val > 0 && i < 11) {
242+
buf[i++] = '0' + (val % 10);
243+
val /= 10;
244+
}
245+
while (i > 0) {
246+
uart_putc(buf[--i]);
247+
}
248+
}
249+
250+
static void uart_putip4(ip4 ip)
251+
{
252+
uart_putdec((ip >> 24) & 0xFF);
253+
uart_putc('.');
254+
uart_putdec((ip >> 16) & 0xFF);
255+
uart_putc('.');
256+
uart_putdec((ip >> 8) & 0xFF);
257+
uart_putc('.');
258+
uart_putdec(ip & 0xFF);
259+
}
260+
233261
/* Configure GPIO pin for Ethernet alternate function (AF11) */
234262
static void gpio_eth_pin(uint32_t base, uint32_t pin)
235263
{
@@ -420,14 +448,66 @@ int main(void)
420448
uart_puts("\n");
421449
}
422450

423-
uart_puts("Setting IP configuration:\n");
424-
uart_puts(" IP: " WOLFIP_IP "\n");
425-
uart_puts(" Mask: " WOLFIP_NETMASK "\n");
426-
uart_puts(" GW: " WOLFIP_GW "\n");
427-
wolfIP_ipconfig_set(IPStack,
428-
atoip4(WOLFIP_IP),
429-
atoip4(WOLFIP_NETMASK),
430-
atoip4(WOLFIP_GW));
451+
#ifdef DHCP
452+
{
453+
uint32_t dhcp_start_tick;
454+
uint32_t dhcp_timeout;
455+
456+
dhcp_timeout = 30000; /* 30 seconds timeout */
457+
458+
(void)wolfIP_poll(IPStack, tick);
459+
if (dhcp_client_init(IPStack) >= 0) {
460+
/* Poll immediately to send the DHCP discover packet */
461+
(void)wolfIP_poll(IPStack, tick);
462+
tick++;
463+
delay(1000);
464+
(void)wolfIP_poll(IPStack, tick);
465+
tick++;
466+
delay(1000);
467+
468+
/* Wait for DHCP to complete - poll frequently */
469+
dhcp_start_tick = tick;
470+
while (!dhcp_bound(IPStack)) {
471+
/* Poll the stack - this processes received packets and sends pending data */
472+
(void)wolfIP_poll(IPStack, tick);
473+
/* Increment tick counter (approximate 1ms per iteration) */
474+
tick++;
475+
/* Small delay to allow Ethernet DMA to work */
476+
delay(1000);
477+
/* Check for timeout */
478+
if ((tick - dhcp_start_tick) > dhcp_timeout)
479+
break;
480+
}
481+
if (dhcp_bound(IPStack)) {
482+
ip4 ip = 0, nm = 0, gw = 0;
483+
wolfIP_ipconfig_get(IPStack, &ip, &nm, &gw);
484+
uart_puts("DHCP configuration received:\n");
485+
uart_puts(" IP: ");
486+
uart_putip4(ip);
487+
uart_puts("\n Mask: ");
488+
uart_putip4(nm);
489+
uart_puts("\n GW: ");
490+
uart_putip4(gw);
491+
uart_puts("\n");
492+
}
493+
}
494+
}
495+
#else
496+
{
497+
ip4 ip = atoip4(WOLFIP_IP);
498+
ip4 nm = atoip4(WOLFIP_NETMASK);
499+
ip4 gw = atoip4(WOLFIP_GW);
500+
uart_puts("Setting IP configuration:\n");
501+
uart_puts(" IP: ");
502+
uart_putip4(ip);
503+
uart_puts("\n Mask: ");
504+
uart_putip4(nm);
505+
uart_puts("\n GW: ");
506+
uart_putip4(gw);
507+
uart_puts("\n");
508+
wolfIP_ipconfig_set(IPStack, ip, nm, gw);
509+
}
510+
#endif
431511

432512
uart_puts("Creating TCP socket on port 7...\n");
433513
listen_fd = wolfIP_sock_socket(IPStack, AF_INET, IPSTACK_SOCK_STREAM, 0);

src/wolfip.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,15 +1235,18 @@ static void udp_try_recv(struct wolfIP *s, unsigned int if_idx, struct wolfIP_ud
12351235
return;
12361236
for (i = 0; i < MAX_UDPSOCKETS; i++) {
12371237
struct tsocket *t = &s->udpsockets[i];
1238-
if (t->src_port == ee16(udp->dst_port) && t->dst_port == ee16(udp->src_port) &&
1238+
uint32_t expected_len;
1239+
if (t->src_port == ee16(udp->dst_port) && (t->dst_port == 0 || t->dst_port == ee16(udp->src_port)) &&
12391240
(((t->local_ip == 0) && DHCP_IS_RUNNING(s)) ||
1240-
(t->local_ip == dst_ip && t->remote_ip != local_ip)) ) {
1241+
(t->local_ip == dst_ip && (t->remote_ip == 0 || t->remote_ip != local_ip))) ) {
12411242

12421243
if (t->local_ip == 0)
12431244
t->if_idx = (uint8_t)if_idx;
12441245

12451246
/* UDP datagram sanity checks */
1246-
if ((int)frame_len != ee16(udp->len) + IP_HEADER_LEN + ETH_HEADER_LEN)
1247+
/* Allow some tolerance for padding/alignment (up to 4 bytes) */
1248+
expected_len = ee16(udp->len) + IP_HEADER_LEN + ETH_HEADER_LEN;
1249+
if ((int)frame_len < (int)expected_len)
12471250
return;
12481251
/* Insert into socket buffer */
12491252
fifo_push(&t->sock.udp.rxbuf, udp, frame_len);

0 commit comments

Comments
 (0)