Skip to content

Commit 667d839

Browse files
authored
Merge pull request #13 from dgarske/stm32h5_dhcp
Support for DHCP (in STM32H5 example)
2 parents c1b33d2 + 2550336 commit 667d839

4 files changed

Lines changed: 134 additions & 28 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: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,43 @@
2222
#define WOLF_CONFIG_H
2323

2424
#ifndef CONFIG_IPFILTER
25-
#define CONFIG_IPFILTER 0
25+
#define CONFIG_IPFILTER 0
2626
#endif
2727

2828
#define ETHERNET
29-
#define LINK_MTU 1536
29+
#define LINK_MTU 1536
3030

31-
#define MAX_TCPSOCKETS 4
32-
#define MAX_UDPSOCKETS 2
33-
#define MAX_ICMPSOCKETS 2
34-
#define RXBUF_SIZE (LINK_MTU * 16)
35-
#define TXBUF_SIZE (LINK_MTU * 16)
31+
#define MAX_TCPSOCKETS 4
32+
#define MAX_UDPSOCKETS 2
33+
#define MAX_ICMPSOCKETS 2
34+
#define RXBUF_SIZE (LINK_MTU * 16)
35+
#define TXBUF_SIZE (LINK_MTU * 16)
3636

37-
#define MAX_NEIGHBORS 16
37+
#define MAX_NEIGHBORS 16
3838

3939
#ifndef WOLFIP_MAX_INTERFACES
40-
#define WOLFIP_MAX_INTERFACES 1
40+
#define WOLFIP_MAX_INTERFACES 1
4141
#endif
4242

4343
#ifndef WOLFIP_ENABLE_FORWARDING
4444
#define WOLFIP_ENABLE_FORWARDING 0
4545
#endif
4646

4747
#ifndef WOLFIP_ENABLE_LOOPBACK
48-
#define WOLFIP_ENABLE_LOOPBACK 0
48+
#define WOLFIP_ENABLE_LOOPBACK 0
4949
#endif
5050

51-
#define WOLFIP_IP "192.168.12.11"
52-
#define WOLFIP_NETMASK "255.255.255.0"
53-
#define WOLFIP_GW "192.168.12.1"
54-
#define WOLFIP_STATIC_DNS_IP "9.9.9.9"
51+
#ifndef WOLFIP_ENABLE_DHCP
52+
#define WOLFIP_ENABLE_DHCP 1
53+
#endif
5554

55+
#if WOLFIP_ENABLE_DHCP
56+
#define DHCP
57+
#else
58+
#define WOLFIP_IP "192.168.12.11"
59+
#define WOLFIP_NETMASK "255.255.255.0"
60+
#define WOLFIP_GW "192.168.12.1"
61+
#define WOLFIP_STATIC_DNS_IP "9.9.9.9"
5662
#endif
63+
64+
#endif /* WOLF_CONFIG_H */

src/port/stm32h563/main.c

Lines changed: 79 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,57 @@ 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+
if (dhcp_client_init(IPStack) >= 0) {
459+
/* Wait for DHCP to complete - poll frequently */
460+
dhcp_start_tick = tick;
461+
while (!dhcp_bound(IPStack)) {
462+
/* Poll the stack - this processes received packets and sends pending data */
463+
(void)wolfIP_poll(IPStack, tick);
464+
/* Increment tick counter (approximate 1ms per iteration) */
465+
tick++;
466+
/* Small delay to allow Ethernet DMA to work */
467+
delay(1000);
468+
/* Check for timeout */
469+
if ((tick - dhcp_start_tick) > dhcp_timeout)
470+
break;
471+
}
472+
if (dhcp_bound(IPStack)) {
473+
ip4 ip = 0, nm = 0, gw = 0;
474+
wolfIP_ipconfig_get(IPStack, &ip, &nm, &gw);
475+
uart_puts("DHCP configuration received:\n");
476+
uart_puts(" IP: ");
477+
uart_putip4(ip);
478+
uart_puts("\n Mask: ");
479+
uart_putip4(nm);
480+
uart_puts("\n GW: ");
481+
uart_putip4(gw);
482+
uart_puts("\n");
483+
}
484+
}
485+
}
486+
#else
487+
{
488+
ip4 ip = atoip4(WOLFIP_IP);
489+
ip4 nm = atoip4(WOLFIP_NETMASK);
490+
ip4 gw = atoip4(WOLFIP_GW);
491+
uart_puts("Setting IP configuration:\n");
492+
uart_puts(" IP: ");
493+
uart_putip4(ip);
494+
uart_puts("\n Mask: ");
495+
uart_putip4(nm);
496+
uart_puts("\n GW: ");
497+
uart_putip4(gw);
498+
uart_puts("\n");
499+
wolfIP_ipconfig_set(IPStack, ip, nm, gw);
500+
}
501+
#endif
431502

432503
uart_puts("Creating TCP socket on port 7...\n");
433504
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)