Skip to content

Commit ead47f7

Browse files
committed
stm32: Add IPv6 support to ethernet driver.
Add IPv6 support by: - Including lwip/ethip6.h header when LWIP_IPV6 is enabled - Setting output_ip6 to ethip6_output for IPv6 packet handling - Adding NETIF_FLAG_MLD6 flag for IPv6 multicast support - Creating IPv6 link-local address after interface initialization Signed-off-by: Andrew Leech <[email protected]>
1 parent 2f5df81 commit ead47f7

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

ports/stm32/eth.c

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
#include "lwip/dns.h"
4141
#include "lwip/dhcp.h"
4242
#include "netif/ethernet.h"
43+
#if LWIP_IPV6
44+
#include "lwip/ethip6.h"
45+
#endif
4346

4447
// ETH DMA RX and TX descriptor definitions
4548
#if defined(STM32H5)
@@ -764,6 +767,10 @@ static err_t eth_netif_init(struct netif *netif) {
764767
netif->output = etharp_output;
765768
netif->mtu = 1500;
766769
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP;
770+
#if LWIP_IPV6
771+
netif->output_ip6 = ethip6_output;
772+
netif->flags |= NETIF_FLAG_MLD6;
773+
#endif
767774
// Checksums only need to be checked on incoming frames, not computed on outgoing frames
768775
NETIF_SET_CHECKSUM_CTRL(netif,
769776
NETIF_CHECKSUM_CHECK_IP
@@ -776,17 +783,22 @@ static err_t eth_netif_init(struct netif *netif) {
776783

777784
static void eth_lwip_init(eth_t *self) {
778785
ip_addr_t ipconfig[4];
779-
IP4_ADDR(&ipconfig[0], 0, 0, 0, 0);
780-
IP4_ADDR(&ipconfig[2], 192, 168, 0, 1);
781-
IP4_ADDR(&ipconfig[1], 255, 255, 255, 0);
782-
IP4_ADDR(&ipconfig[3], 8, 8, 8, 8);
786+
#if LWIP_IPV6
787+
for (int i = 0; i < 4; i++) {
788+
IP_ADDR4(&ipconfig[i], 0, 0, 0, 0);
789+
}
790+
#endif
791+
IP_ADDR4(&ipconfig[0], 0, 0, 0, 0);
792+
IP_ADDR4(&ipconfig[2], 192, 168, 0, 1);
793+
IP_ADDR4(&ipconfig[1], 255, 255, 255, 0);
794+
IP_ADDR4(&ipconfig[3], 8, 8, 8, 8);
783795

784796
MICROPY_PY_LWIP_ENTER
785797

786798
struct netif *n = &self->netif;
787799
n->name[0] = 'e';
788800
n->name[1] = '0';
789-
netif_add(n, &ipconfig[0], &ipconfig[1], &ipconfig[2], self, eth_netif_init, ethernet_input);
801+
netif_add(n, ip_2_ip4(&ipconfig[0]), ip_2_ip4(&ipconfig[1]), ip_2_ip4(&ipconfig[2]), self, eth_netif_init, ethernet_input);
790802
netif_set_hostname(n, mod_network_hostname_data);
791803
netif_set_default(n);
792804
netif_set_up(n);
@@ -797,6 +809,10 @@ static void eth_lwip_init(eth_t *self) {
797809

798810
netif_set_link_up(n);
799811

812+
#if LWIP_IPV6
813+
netif_create_ip6_linklocal_address(n, 1);
814+
#endif
815+
800816
MICROPY_PY_LWIP_EXIT
801817
}
802818

@@ -805,7 +821,7 @@ static void eth_lwip_deinit(eth_t *self) {
805821
for (struct netif *netif = netif_list; netif != NULL; netif = netif->next) {
806822
if (netif == &self->netif) {
807823
netif_remove(netif);
808-
netif->ip_addr.addr = 0;
824+
netif_set_addr(netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4);
809825
netif->flags = 0;
810826
}
811827
}
@@ -835,7 +851,7 @@ int eth_link_status(eth_t *self) {
835851
struct netif *netif = &self->netif;
836852
if ((netif->flags & (NETIF_FLAG_UP | NETIF_FLAG_LINK_UP))
837853
== (NETIF_FLAG_UP | NETIF_FLAG_LINK_UP)) {
838-
if (netif->ip_addr.addr != 0) {
854+
if (!ip4_addr_isany_val(*netif_ip4_addr(netif))) {
839855
return 3; // link up
840856
} else {
841857
return 2; // link no-ip;

ports/stm32/network_lan.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ static void network_lan_print(const mp_print_t *print, mp_obj_t self_in, mp_prin
4444
network_lan_obj_t *self = MP_OBJ_TO_PTR(self_in);
4545
struct netif *netif = eth_netif(self->eth);
4646
int status = eth_link_status(self->eth);
47+
const ip4_addr_t *ip4 = netif_ip4_addr(netif);
4748
mp_printf(print, "<ETH %u %u.%u.%u.%u>",
4849
status,
49-
netif->ip_addr.addr & 0xff,
50-
netif->ip_addr.addr >> 8 & 0xff,
51-
netif->ip_addr.addr >> 16 & 0xff,
52-
netif->ip_addr.addr >> 24
50+
ip4_addr1(ip4),
51+
ip4_addr2(ip4),
52+
ip4_addr3(ip4),
53+
ip4_addr4(ip4)
5354
);
5455
}
5556

0 commit comments

Comments
 (0)