8000 ports/esp32/*: Broke out network LAN code into separate files · nickzoic/micropython-esp32@47308aa · GitHub
[go: up one dir, main page]

Skip to content

Commit 47308aa

Browse files
committed
ports/esp32/*: Broke out network LAN code into separate files
1 parent 731fed8 commit 47308aa

File tree

4 files changed

+277
-189
lines changed

4 files changed

+277
-189
lines changed

ports/esp32/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ SRC_C = \
136136
machine_uart.c \
137137
modmachine.c \
138138
modnetwork.c \
139+
network_lan.c \
139140
modsocket.c \
140141
modesp.c \
141142
moduhashlib.c \

ports/esp32/modnetwork.c

Lines changed: 5 additions & 189 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include <stdint.h>
3535
#include <string.h>
3636

37+
#include "network_lan.h"
38+
3739
#include "py/nlr.h"
3840
#include "py/objlist.h"
3941
#include "py/runtime.h"
@@ -47,12 +49,11 @@
4749
#include "esp_log.h"
4850
#include "lwip/dns.h"
4951
#include "tcpip_adapter.h"
50-
#include "eth_phy/phy.h"
51-
#include "eth_phy/phy_tlk110.h"
52-
#include "eth_phy/phy_lan8720.h"
5352

5453
#define MODNETWORK_INCLUDE_CONSTANTS (1)
5554

55+
MP_DECLARE_CONST_FUN_OBJ_KW(get_lan_obj);
56+
5657
NORETURN void _esp_exceptions(esp_err_t e) {
5758
switch (e) {
5859
case ESP_ERR_WIFI_NOT_INIT:
@@ -101,34 +102,15 @@ static inline void esp_exceptions(esp_err_t e) {
101102

102103
#define ESP_EXCEPTIONS(x) do { esp_exceptions(x); } while (0);
103104

104-
enum { PHY_LAN8720, PHY_TLK110 };
105-
106105
typedef struct _wlan_if_obj_t {
107106
mp_obj_base_t base;
108107
int if_id;
109108
} wlan_if_obj_t;
110109

111-
typedef struct _lan_if_obj_t {
112-
mp_obj_base_t base;
113-
int if_id; // MUST BE FIRST
114-
bool initialized;
115-
bool active;
116-
uint8_t mdc_pin;
117-
uint8_t mdio_pin;
118-
int8_t phy_power_pin;
119-
uint8_t phy_addr;
120-
uint8_t phy_type;
121-
eth_phy_check_link_func link_func;
122-
eth_phy_power_enable_func power_func;
123-
} lan_if_obj_t;
124-
125110
const mp_obj_type_t wlan_if_type;
126111
STATIC const wlan_if_obj_t wlan_sta_obj = {{&wlan_if_type}, WIFI_IF_STA};
127112
STATIC const wlan_if_obj_t wlan_ap_obj = {{&wlan_if_type}, WIFI_IF_AP};
128113

129-
const mp_obj_type_t lan_if_type;
130-
STATIC lan_if_obj_t lan_obj = {{&lan_if_type}, ESP_IF_ETH, false, false};
131-
132114
//static wifi_config_t wifi_ap_config = {{{0}}};
133115
static wifi_config_t wifi_sta_config = {{{0}}};
134116

@@ -215,129 +197,6 @@ STATIC mp_obj_t get_wlan(size_t n_args, const mp_obj_t *args) {
215197
}
216198
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(get_wlan_obj, 0, 1, get_wlan);
217199

218-
STATIC void phy_power_enable(bool enable) {
219-
lan_if_obj_t* self = MP_OBJ_TO_PTR(&lan_obj);
220-
221-
if (self->phy_power_pin != -1) {
222-
223-
if (!enable) {
224-
/* Do the PHY-specific power_enable(false) function before powering down */
225-
self->power_func(false);
226-
}
227-
228-
gpio_pad_select_gpio(self->phy_power_pin);
229-
gpio_set_direction(self->phy_power_pin,GPIO_MODE_OUTPUT);
230-
if(enable == true) {
231-
gpio_set_level(self->phy_power_pin, 1);
232-
} else {
233-
gpio_set_level(self->phy_power_pin, 0);
234-
}
235-
236-
// Allow the power up/down to take effect, min 300us
237-
vTaskDelay(1);
238-
239-
if (enable) {
240-
/* Run the PHY-specific power on operations now the PHY has power */
241-
self->power_func(true);
242-
}
243-
}
244-
}
245-
246-
STATIC void init_lan_rmii() {
247-
lan_if_obj_t* self = MP_OBJ_TO_PTR(&lan_obj);
248-
phy_rmii_configure_data_interface_pins();
249-
phy_rmii_smi_configure_pins(self->mdc_pin, self->mdio_pin);
250-
}
251-
252-
STATIC void init_lan() {
253-
lan_if_obj_t* self = MP_OBJ_TO_PTR(&lan_obj);
254-
eth_config_t config;
255-
256-
switch (self->phy_type) {
257-
case PHY_TLK110:
258-
config = phy_tlk110_default_ethernet_config;
259-
break;
260-
case PHY_LAN8720:
261-
config = phy_lan8720_default_ethernet_config;
262-
break;
263-
}
264-
265-
self->link_func = config.phy_check_link;
266-
267-
// Replace default power func with our own
268-
self->power_func = config.phy_power_enable;
269-
config.phy_power_enable = phy_power_enable;
270-
271-
config.phy_addr = self->phy_addr;
272-
config.gpio_config = init_lan_rmii;
273-
config.tcpip_input = tcpip_adapter_eth_input;
274-
275-
if (esp_eth_init(&config) == ESP_OK) {
276-
esp_eth_enable();
277-
self->active = true;
278-
} else {
279-
mp_raise_msg(&mp_type_OSError, "esp_eth_init() failed");
280-
}
281-
}
282-
283-
STATIC mp_obj_t get_lan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
284-
lan_if_obj_t* self = MP_OBJ_TO_PTR(&lan_obj);
285-
286-
if (self->initialized) {
287-
mp_raise_msg(&mp_type_OSError, "ethernet already initialized");
288-
}
289-
290-
enum { ARG_id, ARG_mdc, ARG_mdio, ARG_power, ARG_phy_addr, ARG_phy_type };
291-
292-
uint8_t default_pins[] = {23, 18, 17}; // mdc, mdio, power
293-
294-
static const mp_arg_t allowed_args[] = {
295-
{ MP_QSTR_id, MP_ARG_OBJ, {.u_obj = mp_const_none} },
296-
{ MP_QSTR_mdc, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
297-
{ MP_QSTR_mdio, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
298-
{ MP_QSTR_power, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
299-
{ MP_QSTR_phy_addr, MP_ARG_INT, {.u_int = 0x01} },
300-
{ MP_QSTR_phy_type, MP_ARG_INT, {.u_int = PHY_LAN8720} },
301-
};
302-
303-
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
304-
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
305-
306-
if (args[ARG_id].u_obj != mp_const_none) {
307-
if (mp_obj_get_int(args[ARG_id].u_obj) != 0) {
308-
mp_raise_ValueError("invalid LAN interface identifier");
309-
}
310-
}
311-
312-
for(int i = ARG_mdc; i <= ARG_power; ++i) {
313-
if (args[i].u_obj == mp_const_none && i == ARG_power) {
314-
args[i].u_int = -1;
315-
} else if (args[i].u_obj != MP_OBJ_NULL) {
316-
args[i].u_int = machine_pin_get_id(args[i].u_obj);
317-
} else {
318-
args[i].u_int = default_pins[i - ARG_mdc];
319-
}
320-
}
321-
322-
if (args[ARG_phy_addr].u_int < 0x00 || args[ARG_phy_addr].u_int > 0x1f) {
323-
mp_raise_ValueError("invalid phy address");
324-
}
325-
326-
if (args[ARG_phy_type].u_int != PHY_LAN8720 && args[ARG_phy_type].u_int != PHY_TLK110) {
327-
mp_raise_ValueError("invalid phy type");
328-
}
329-
330-
self->mdc_pin = args[ARG_mdc].u_int;
331-
self->mdio_pin = args[ARG_mdio].u_int;
332-
self->phy_power_pin = args[ARG_power].u_int;
333-
self->phy_addr = args[ARG_phy_addr].u_int;
334-
self->phy_type = args[ARG_phy_type].u_int;
335-
self->initialized = true;
336-
init_lan();
337-
return MP_OBJ_FROM_PTR(&lan_obj);
338-
}
339-
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(get_lan_obj, 0, get_lan);
340-
341200
STATIC mp_obj_t esp_initialize() {
342201
static int initialized = 0;
343202
if (!initialized) {
@@ -382,28 +241,6 @@ STATIC mp_obj_t esp_active(size_t n_args, const mp_obj_t *args) {
382241

383242
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_active_obj, 1, 2, esp_active);
384243

385-
STATIC mp_obj_t lan_active(size_t n_args, const mp_obj_t *args) {
386-
387-
lan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
388-
389-
if (n_args > 1) {
390-
if (mp_obj_is_true(args[1])) {
391-
self->active = (esp_eth_enable() == ESP_OK);
392-
if (!self->active) {
393-
mp_raise_msg(&mp_type_OSError, "ethernet enable failed");
394-
}
395-
} else {
396-
self->active = !(esp_eth_disable() == ESP_OK);
397-
if (self->active) {
398-
mp_raise_msg(&mp_type_OSError, "ethernet disable failed");
399-
}
400-
}
401-
}
402-
return self->active ? mp_const_true : mp_const_false;
403-
}
404-
405-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(lan_active_obj, 1, 2, lan_active);
406-
407244
STATIC mp_obj_t esp_connect(size_t n_args, const mp_obj_t *args) {
408245

409246
mp_uint_t len;
@@ -440,12 +277,6 @@ STATIC mp_obj_t esp_status(mp_obj_t self_in) {
440277

441278
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_status_obj, esp_status);
442279

443-
STATIC mp_obj_t lan_isconnected(mp_obj_t self_in) {
444-
lan_if_obj_t *self = MP_OBJ_TO_PTR(self_in);
445-
return self->link_func() ? mp_const_true : mp_const_false;
446-
}
447-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(lan_isconnected_obj, lan_isconnected);
448-
449280
STATIC mp_obj_t esp_scan(mp_obj_t self_in) {
450281
// check that STA mode is active
451282
wifi_mode_t mode;
@@ -545,7 +376,7 @@ STATIC mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) {
545376
}
546377
}
547378

548-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_ifconfig_obj, 1, 2, esp_ifconfig);
379+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_ifconfig_obj, 1, 2, esp_ifconfig);
549380

550381
STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
551382
if (n_args != 1 && kwargs->used != 0) {
@@ -694,21 +525,6 @@ const mp_obj_type_t wlan_if_type = {
694525
.locals_dict = (mp_obj_t)&wlan_if_locals_dict,
695526
};
696527

697-
STATIC const mp_map_elem_t lan_if_locals_dict_table[] = {
698-
{ MP_OBJ_NEW_QSTR(MP_QSTR_active), (mp_obj_t)&lan_active_obj },
699-
{ MP_OBJ_NEW_QSTR(MP_QSTR_isconnected), (mp_obj_t)&lan_isconnected_obj },
700-
{ MP_OBJ_NEW_QSTR(MP_QSTR_status), (mp_obj_t)&esp_status_obj },
701-
{ MP_OBJ_NEW_QSTR(MP_QSTR_ifconfig), (mp_obj_t)&esp_ifconfig_obj },
702-
};
703-
704-
STATIC MP_DEFINE_CONST_DICT(lan_if_locals_dict, lan_if_locals_dict_table);
705-
706-
const mp_obj_type_t lan_if_type = {
707-
{ &mp_type_type },
708-
.name = MP_QSTR_LAN,
709-
.locals_dict = (mp_obj_t)&lan_if_locals_dict,
710-
};
711-
712528
STATIC mp_obj_t esp_phy_mode(size_t n_args, const mp_obj_t *args) {
713529
return mp_const_none;
714530
}

0 commit comments

Comments
 (0)
0