Skip to content

Commit 1a3d67a

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

File tree

5 files changed

+101
-39
lines changed

5 files changed

+101
-39
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: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include "py/mperrno.h"
3232
#include "ticks.h"
3333

34-
#if defined(IOMUX_TABLE_ENET)
34+
#if defined(ENET_PHY_ADDRESS) || defined(ENET_1_PHY_ADDRESS)
3535

3636
#include "pin.h"
3737
#include "shared/netutils/netutils.h"
@@ -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)
@@ -357,6 +358,8 @@ static void eth_phy_init(phy_handle_t *phyHandle, phy_config_t *phy_config,
357358
mp_hal_delay_us(phy_settle_time);
358359
}
359360

361+
#if defined(ENET_PHY_ADDRESS)
362+
360363
// eth_init: Set up GPIO and the transceiver
361364
void eth_init_0(eth_t *self, int eth_id, const phy_operations_t *phy_ops, int phy_addr, bool phy_clock) {
362365
// Configuration values
@@ -411,7 +414,9 @@ void eth_init_0(eth_t *self, int eth_id, const phy_operations_t *phy_ops, int ph
411414
ENET_ActiveRead(ENET);
412415
}
413416

414-
#if defined(ENET_DUAL_PORT)
417+
#endif // defined(ENET_PHY_ADDRESS)
418+
419+
#if defined(ENET_1_PHY_ADDRESS)
415420

416421
// eth_init: Set up GPIO and the transceiver
417422
void eth_init_1(eth_t *self, int eth_id, const phy_operations_t *phy_ops, int phy_addr, bool phy_clock) {
@@ -474,7 +479,8 @@ void eth_init_1(eth_t *self, int eth_id, const phy_operations_t *phy_ops, int ph
474479
ENET_ActiveRead(ENET_1);
475480
}
476481

477-
#endif
482+
#endif // defined(ENET_1_PHY_ADDRESS)
483+
478484
// Initialize the phy interface
479485
static int eth_mac_init(eth_t *self) {
480486
return 0;
@@ -512,14 +518,26 @@ static err_t eth_send_frame_blocking(ENET_Type *base, enet_handle_t *handle, uin
512518
static err_t eth_netif_output(struct netif *netif, struct pbuf *p) {
513519
// This function should always be called from a context where PendSV-level IRQs are disabled
514520
status_t status;
515-
ENET_Type *enet = ENET;
516-
enet_handle_t *handle = &g_handle;
521+
ENET_Type *enet;
522+
enet_handle_t *handle;
517523

518-
#if defined ENET_DUAL_PORT
524+
#if defined(ENET_PHY_ADDRESS) && defined(ENET_1_PHY_ADDRESS)
525+
// Dual port: select based on netif->state
519526
if (netif->state == &eth_instance1) {
520527
enet = ENET_1;
521528
handle = &g_handle_1;
529+
} else {
530+
enet = ENET;
531+
handle = &g_handle;
522532
}
533+
#elif defined(ENET_1_PHY_ADDRESS)
534+
// Only ENET_1 available
535+
enet = ENET_1;
536+
handle = &g_handle_1;
537+
#else
538+
// Only ENET available
539+
enet = ENET;
540+
handle = &g_handle;
523541
#endif
524542

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

564582
self->netif.hwaddr_len = 6;
583+
#if defined(ENET_PHY_ADDRESS)
565584
if (self == &eth_instance0) {
566585
memcpy(self->netif.hwaddr, hw_addr, 6);
567586
IP4_ADDR(&ipconfig[0], 192, 168, 0, 2);
568-
#if defined ENET_DUAL_PORT
569-
} else {
587+
}
588+
#endif
589+
#if defined(ENET_1_PHY_ADDRESS)
590+
if (self == &eth_instance1) {
570591
memcpy(self->netif.hwaddr, hw_addr_1, 6);
571592
IP4_ADDR(&ipconfig[0], 192, 168, 0, 3);
572-
#endif
573593
}
594+
#endif
574595
IP4_ADDR(&ipconfig[1], 255, 255, 255, 0);
575596
IP4_ADDR(&ipconfig[2], 192, 168, 0, 1);
576597
IP4_ADDR(&ipconfig[3], 8, 8, 8, 8);
577598

578599
MICROPY_PY_LWIP_ENTER
579600

580601
n->name[0] = 'e';
602+
#if defined(ENET_PHY_ADDRESS)
581603
n->name[1] = (self == &eth_instance0 ? '0' : '1');
604+
#else
605+
n->name[1] = '1';
606+
#endif
582607
netif_add(n, &ipconfig[0], &ipconfig[1], &ipconfig[2], self, eth_netif_init, ethernet_input);
583608
netif_set_hostname(n, mod_network_hostname_data);
584609
netif_set_default(n);
@@ -620,8 +645,10 @@ int eth_link_status(eth_t *self) {
620645
}
621646
} else {
622647
bool link;
623-
#if defined ENET_DUAL_PORT
648+
#if defined(ENET_PHY_ADDRESS) && defined(ENET_1_PHY_ADDRESS)
624649
PHY_GetLinkStatus(self == &eth_instance0 ? &phyHandle : &phyHandle_1, &link);
650+
#elif defined(ENET_1_PHY_ADDRESS)
651+
PHY_GetLinkStatus(&phyHandle_1, &link);
625652
#else
626653
PHY_GetLinkStatus(&phyHandle, &link);
627654
#endif
@@ -654,10 +681,12 @@ int eth_stop(eth_t *self) {
654681
}
655682

656683
void eth_low_power_mode(eth_t *self, bool enable) {
657-
#if defined ENET_DUAL_PORT
684+
#if defined(ENET_PHY_ADDRESS) && defined(ENET_1_PHY_ADDRESS)
658685
ENET_EnableSleepMode(self == &eth_instance0 ? ENET : ENET_1, enable);
686+
#elif defined(ENET_1_PHY_ADDRESS)
687+
ENET_EnableSleepMode(ENET_1, enable);
659688
#else
660689
ENET_EnableSleepMode(ENET, enable);
661690
#endif
662691
}
663-
#endif // defined(IOMUX_TABLE_ENET)
692+
#endif // defined(ENET_PHY_ADDRESS) || defined(ENET_1_PHY_ADDRESS)

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/mpconfigport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ uint32_t trng_random_u32(void);
176176

177177
// Hooks to add builtins
178178

179-
#if defined(IOMUX_TABLE_ENET)
179+
#if defined(ENET_PHY_ADDRESS) || defined(ENET_1_PHY_ADDRESS)
180180
extern const struct _mp_obj_type_t network_lan_type;
181181
#define MICROPY_HW_NIC_ETH { MP_ROM_QSTR(MP_QSTR_LAN), MP_ROM_PTR(&network_lan_type) },
182182
#else

ports/mimxrt/network_lan.c

Lines changed: 46 additions & 14 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;
@@ -132,17 +160,21 @@ static mp_obj_t network_lan_make_new(const mp_obj_type_t *type, size_t n_args, s
132160
phy_clock = args[ARG_ref_clk_mode].u_int;
133161
}
134162

135-
// Prepare for two ETH interfaces.
136-
const network_lan_obj_t *self;
163+
// Initialize the appropriate ETH interface
164+
const network_lan_obj_t *self = NULL;
165+
#if defined(ENET_PHY_ADDRESS)
137166
if (mac_id == 0) {
138167
self = &network_lan_eth0;
139168
eth_init_0(self->eth, MP_HAL_MAC_ETH0, phy_ops, phy_addr, phy_clock);
140-
#if defined(ENET_DUAL_PORT)
141-
} else if (mac_id == 1) {
169+
}
170+
#endif
171+
#if defined(ENET_1_PHY_ADDRESS)
172+
if (mac_id == 1) {
142173
self = &network_lan_eth1;
143174
eth_init_1(self->eth, MP_HAL_MAC_ETH1, phy_ops, phy_addr, phy_clock);
175+
}
144176
#endif
145-
} else {
177+
if (self == NULL) {
146178
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Invalid LAN interface %d"), mac_id);
147179
}
148180

@@ -274,4 +306,4 @@ MP_DEFINE_CONST_OBJ_TYPE(
274306
);
275307

276308

277-
#endif // defined(IOMUX_TABLE_ENET)
309+
#endif // defined(ENET_PHY_ADDRESS) || defined(ENET_1_PHY_ADDRESS)

0 commit comments

Comments
 (0)