Skip to content

Commit b1c4c7c

Browse files
committed
mimxrt/eth: Improve Dual Ethernet configuration.
Signed-off-by: Andrew Leech <[email protected]>
1 parent d1e47a5 commit b1c4c7c

File tree

4 files changed

+93
-35
lines changed

4 files changed

+93
-35
lines changed

ports/mimxrt/boards/MIMXRT1170_EVK/mpconfigboard.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,7 @@
167167
{ IOMUXC_GPIO_AD_33_ENET_MDIO, 0, 0x06u }, \
168168
{ IOMUXC_GPIO_AD_32_ENET_MDC, 0, 0x06u },
169169

170-
// A second ETH port is present.
171-
#define ENET_DUAL_PORT (1)
172-
// 1G Transceiver Phy Parameters
170+
// 1G Transceiver Phy Parameters (second ETH port)
173171
#define ENET_1_PHY_ADDRESS (1)
174172
#define ENET_1_PHY RTL8211F
175173
#define ENET_1_PHY_OPS phyrtl8211f_ops

ports/mimxrt/eth.c

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ typedef struct _iomux_table_t {
7575
uint32_t configValue;
7676
} iomux_table_t;
7777

78+
#if defined(ENET_PHY_ADDRESS)
79+
7880
// ETH0 buffers and handles
7981
static AT_NONCACHEABLE_SECTION_ALIGN(enet_rx_bd_struct_t g_rxBuffDescrip[ENET_RXBD_NUM], ENET_BUFF_ALIGNMENT);
8082
static AT_NONCACHEABLE_SECTION_ALIGN(enet_tx_bd_struct_t g_txBuffDescrip[ENET_TXBD_NUM], ENET_BUFF_ALIGNMENT);
@@ -111,7 +113,9 @@ static const iomux_table_t iomux_table_enet[] = {
111113

112114
static uint8_t hw_addr[6]; // The MAC address field
113115

114-
#if defined(ENET_DUAL_PORT)
116+
#endif // defined(ENET_PHY_ADDRESS)
117+
118+
#if defined(ENET_1_PHY_ADDRESS)
115119

116120
// ETH1 buffers and handles
117121
static AT_NONCACHEABLE_SECTION_ALIGN(enet_rx_bd_struct_t g_rxBuffDescrip_1[ENET_RXBD_NUM], ENET_BUFF_ALIGNMENT);
@@ -148,17 +152,14 @@ static const iomux_table_t iomux_table_enet_1[] = {
148152

149153
static uint8_t hw_addr_1[6]; // The MAC address field
150154

151-
#endif
152-
153-
#if defined(ENET_DUAL_PORT)
155+
// Define ENET_1 to the appropriate controller for this hardware
154156
#if defined MIMXRT117x_SERIES
155157
#define ENET_1 ENET_1G
156158
#else
157159
#define ENET_1 ENET2
158160
#endif
159-
#else
160-
#define ENET_1 ENET
161-
#endif
161+
162+
#endif // defined(ENET_1_PHY_ADDRESS)
162163

163164
#define PHY_AUTONEGO_TIMEOUT_US (5000000)
164165
#define PHY_SETTLE_TIME_US (1000)
@@ -411,7 +412,7 @@ void eth_init_0(eth_t *self, int eth_id, const phy_operations_t *phy_ops, int ph
411412
ENET_ActiveRead(ENET);
412413
}
413414

414-
#if defined(ENET_DUAL_PORT)
415+
#if defined(ENET_1_PHY_ADDRESS)
415416

416417
// eth_init: Set up GPIO and the transceiver
417418
void eth_init_1(eth_t *self, int eth_id, const phy_operations_t *phy_ops, int phy_addr, bool phy_clock) {
@@ -474,7 +475,8 @@ void eth_init_1(eth_t *self, int eth_id, const phy_operations_t *phy_ops, int ph
474475
ENET_ActiveRead(ENET_1);
475476
}
476477

477-
#endif
478+
#endif // defined(ENET_1_PHY_ADDRESS)
479+
478480
// Initialize the phy interface
479481
static int eth_mac_init(eth_t *self) {
480482
return 0;
@@ -512,14 +514,26 @@ static err_t eth_send_frame_blocking(ENET_Type *base, enet_handle_t *handle, uin
512514
static err_t eth_netif_output(struct netif *netif, struct pbuf *p) {
513515
// This function should always be called from a context where PendSV-level IRQs are disabled
514516
status_t status;
515-
ENET_Type *enet = ENET;
516-
enet_handle_t *handle = &g_handle;
517+
ENET_Type *enet;
518+
enet_handle_t *handle;
517519

518-
#if defined ENET_DUAL_PORT
520+
#if defined(ENET_PHY_ADDRESS) && defined(ENET_1_PHY_ADDRESS)
521+
// Dual port: select based on netif->state
519522
if (netif->state == &eth_instance1) {
520523
enet = ENET_1;
521524
handle = &g_handle_1;
525+
} else {
526+
enet = ENET;
527+
handle = &g_handle;
522528
}
529+
#elif defined(ENET_1_PHY_ADDRESS)
530+
// Only ENET_1 available
531+
enet = ENET_1;
532+
handle = &g_handle_1;
533+
#else
534+
// Only ENET available
535+
enet = ENET;
536+
handle = &g_handle;
523537
#endif
524538

525539
eth_trace(netif->state, (size_t)-1, p, NETUTILS_TRACE_IS_TX | NETUTILS_TRACE_NEWLINE);
@@ -562,23 +576,30 @@ static void eth_lwip_init(eth_t *self) {
562576
ip_addr_t ipconfig[4];
563577

564578
self->netif.hwaddr_len = 6;
579+
#if defined(ENET_PHY_ADDRESS)
565580
if (self == &eth_instance0) {
566581
memcpy(self->netif.hwaddr, hw_addr, 6);
567582
IP4_ADDR(&ipconfig[0], 192, 168, 0, 2);
568-
#if defined ENET_DUAL_PORT
569-
} else {
583+
}
584+
#endif
585+
#if defined(ENET_1_PHY_ADDRESS)
586+
if (self == &eth_instance1) {
570587
memcpy(self->netif.hwaddr, hw_addr_1, 6);
571588
IP4_ADDR(&ipconfig[0], 192, 168, 0, 3);
572-
#endif
573589
}
590+
#endif
574591
IP4_ADDR(&ipconfig[1], 255, 255, 255, 0);
575592
IP4_ADDR(&ipconfig[2], 192, 168, 0, 1);
576593
IP4_ADDR(&ipconfig[3], 8, 8, 8, 8);
577594

578595
MICROPY_PY_LWIP_ENTER
579596

580597
n->name[0] = 'e';
598+
#if defined(ENET_PHY_ADDRESS)
581599
n->name[1] = (self == &eth_instance0 ? '0' : '1');
600+
#else
601+
n->name[1] = '1';
602+
#endif
582603
netif_add(n, &ipconfig[0], &ipconfig[1], &ipconfig[2], self, eth_netif_init, ethernet_input);
583604
netif_set_hostname(n, mod_network_hostname_data);
584605
netif_set_default(n);
@@ -620,8 +641,10 @@ int eth_link_status(eth_t *self) {
620641
}
621642
} else {
622643
bool link;
623-
#if defined ENET_DUAL_PORT
644+
#if defined(ENET_PHY_ADDRESS) && defined(ENET_1_PHY_ADDRESS)
624645
PHY_GetLinkStatus(self == &eth_instance0 ? &phyHandle : &phyHandle_1, &link);
646+
#elif defined(ENET_1_PHY_ADDRESS)
647+
PHY_GetLinkStatus(&phyHandle_1, &link);
625648
#else
626649
PHY_GetLinkStatus(&phyHandle, &link);
627650
#endif
@@ -654,8 +677,10 @@ int eth_stop(eth_t *self) {
654677
}
655678

656679
void eth_low_power_mode(eth_t *self, bool enable) {
657-
#if defined ENET_DUAL_PORT
680+
#if defined(ENET_PHY_ADDRESS) && defined(ENET_1_PHY_ADDRESS)
658681
ENET_EnableSleepMode(self == &eth_instance0 ? ENET : ENET_1, enable);
682+
#elif defined(ENET_1_PHY_ADDRESS)
683+
ENET_EnableSleepMode(ENET_1, enable);
659684
#else
660685
ENET_EnableSleepMode(ENET, enable);
661686
#endif

ports/mimxrt/eth.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@
2828
#define MICROPY_INCLUDED_MIMXRT_ETH_H
2929

3030
typedef struct _eth_t eth_t;
31+
32+
#if defined(ENET_PHY_ADDRESS)
3133
extern eth_t eth_instance0;
32-
extern eth_t eth_instance1;
3334
void eth_init_0(eth_t *self, int mac_idx, const phy_operations_t *phy_ops, int phy_addr, bool phy_clock);
35+
#endif
3436

35-
#if defined(ENET_DUAL_PORT)
37+
#if defined(ENET_1_PHY_ADDRESS)
38+
extern eth_t eth_instance1;
3639
void eth_init_1(eth_t *self, int mac_idx, const phy_operations_t *phy_ops, int phy_addr, bool phy_clock);
3740
#endif
3841

ports/mimxrt/network_lan.c

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include "py/mphal.h"
2929
#include "extmod/modnetwork.h"
3030

31-
#if defined(IOMUX_TABLE_ENET)
31+
#if defined(ENET_PHY_ADDRESS) || defined(ENET_1_PHY_ADDRESS)
3232

3333
#include "fsl_phy.h"
3434
#include "eth.h"
@@ -55,18 +55,33 @@ typedef struct _network_lan_obj_t {
5555
eth_t *eth;
5656
} network_lan_obj_t;
5757

58+
// Forward declaration of the type
59+
extern const mp_obj_type_t network_lan_type;
5860

61+
#if defined(ENET_PHY_ADDRESS)
5962
static const network_lan_obj_t network_lan_eth0 = { { &network_lan_type }, &eth_instance0 };
60-
#if defined(ENET_DUAL_PORT)
63+
#endif
64+
#if defined(ENET_1_PHY_ADDRESS)
6165
static const network_lan_obj_t network_lan_eth1 = { { &network_lan_type }, &eth_instance1 };
6266
#endif
6367

6468
static void network_lan_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
6569
network_lan_obj_t *self = MP_OBJ_TO_PTR(self_in);
6670
struct netif *netif = eth_netif(self->eth);
6771
int status = eth_link_status(self->eth);
72+
int eth_id = 0;
73+
#if defined(ENET_PHY_ADDRESS)
74+
if (self->eth == &eth_instance0) {
75+
eth_id = 0;
76+
}
77+
#endif
78+
#if defined(ENET_1_PHY_ADDRESS)
79+
if (self->eth == &eth_instance1) {
80+
eth_id = 1;
81+
}
82+
#endif
6883
mp_printf(print, "<ETH%d status=%u ip=%u.%u.%u.%u>",
69-
self->eth == &eth_instance0 ? 0 : 1,
84+
eth_id,
7085
status,
7186
netif->ip_addr.addr & 0xff,
7287
netif->ip_addr.addr >> 8 & 0xff,
@@ -77,8 +92,18 @@ static void network_lan_print(const mp_print_t *print, mp_obj_t self_in, mp_prin
7792

7893
static mp_obj_t network_lan_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
7994
enum { ARG_id, ARG_phy_type, ARG_phy_addr, ARG_ref_clk_mode};
95+
96+
// Default to port 0 if ENET available, else port 1 if only ENET_1 available
97+
#if defined(ENET_PHY_ADDRESS)
98+
#define DEFAULT_ETH_ID 0
99+
#elif defined(ENET_1_PHY_ADDRESS)
100+
#define DEFAULT_ETH_ID 1
101+
#else
102+
#error "No Ethernet PHY configured"
103+
#endif
104+
80105
static const mp_arg_t allowed_args[] = {
81-
{ MP_QSTR_id, MP_ARG_INT, {.u_int = 0} },
106+
{ MP_QSTR_id, MP_ARG_INT, {.u_int = DEFAULT_ETH_ID} },
82107
{ MP_QSTR_phy_type, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
83108
{ MP_QSTR_phy_addr, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
84109
{ MP_QSTR_ref_clk_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
@@ -92,18 +117,21 @@ static mp_obj_t network_lan_make_new(const mp_obj_type_t *type, size_t n_args, s
92117
bool phy_clock;
93118

94119
int mac_id = args[ARG_id].u_int;
95-
// set default
120+
// set default based on which interface is being used
121+
#if defined(ENET_PHY_ADDRESS)
96122
if (mac_id == 0) {
97123
phy_ops = &ENET_PHY_OPS;
98124
phy_addr = ENET_PHY_ADDRESS;
99125
phy_clock = ENET_TX_CLK_OUTPUT;
100-
#if defined(ENET_DUAL_PORT)
101-
} else {
126+
}
127+
#endif
128+
#if defined(ENET_1_PHY_ADDRESS)
129+
if (mac_id == 1) {
102130
phy_ops = &ENET_1_PHY_OPS;
103131
phy_addr = ENET_1_PHY_ADDRESS;
104132
phy_clock = ENET_1_TX_CLK_OUTPUT;
105-
#endif
106133
}
134+
#endif
107135

108136
// Select PHY driver
109137
int phy_type = args[ARG_phy_type].u_int;
@@ -130,17 +158,21 @@ static mp_obj_t network_lan_make_new(const mp_obj_type_t *type, size_t n_args, s
130158
phy_clock = args[ARG_ref_clk_mode].u_int;
131159
}
132160

133-
// Prepare for two ETH interfaces.
134-
const network_lan_obj_t *self;
161+
// Initialize the appropriate ETH interface
162+
const network_lan_obj_t *self = NULL;
163+
#if defined(ENET_PHY_ADDRESS)
135164
if (mac_id == 0) {
136165
self = &network_lan_eth0;
137166
eth_init_0(self->eth, MP_HAL_MAC_ETH0, phy_ops, phy_addr, phy_clock);
138-
#if defined(ENET_DUAL_PORT)
139-
} else if (mac_id == 1) {
167+
}
168+
#endif
169+
#if defined(ENET_1_PHY_ADDRESS)
170+
if (mac_id == 1) {
140171
self = &network_lan_eth1;
141172
eth_init_1(self->eth, MP_HAL_MAC_ETH1, phy_ops, phy_addr, phy_clock);
173+
}
142174
#endif
143-
} else {
175+
if (self == NULL) {
144176
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Invalid LAN interface %d"), mac_id);
145177
}
146178

0 commit comments

Comments
 (0)