Skip to content

Commit 98e19e2

Browse files
committed
Merge branch 'dev-ethernet' into dev-x
2 parents 0fd2b70 + a5203c5 commit 98e19e2

File tree

4 files changed

+267
-190
lines changed

4 files changed

+267
-190
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: 7 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* The MIT License (MIT)
88
*
99
* Copyright (c) 2016, 2017 Nick Moore @mnemote
10+
* Copyright (c) 2017 "Eric Poulsen" <eric@zyxod.com>
1011
*
1112
* Based on esp8266/modnetwork.c which is Copyright (c) 2015 Paul Sokolovsky
1213
* And the ESP IDF example code which is Public Domain / CC0
@@ -34,6 +35,7 @@
3435
#include <stdint.h>
3536
#include <string.h>
3637

38+
3739
#include "py/nlr.h"
3840
#include "py/objlist.h"
3941
#include "py/runtime.h"
@@ -47,12 +49,12 @@
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"
52+
53+
#include "modnetwork.h"
5354

5455
#define MODNETWORK_INCLUDE_CONSTANTS (1)
5556

57+
5658
NORETURN void _esp_exceptions(esp_err_t e) {
5759
switch (e) {
5860
case ESP_ERR_WIFI_NOT_INIT:
@@ -101,34 +103,15 @@ static inline void esp_exceptions(esp_err_t e) {
101103

102104
#define ESP_EXCEPTIONS(x) do { esp_exceptions(x); } while (0);
103105

104-
enum { PHY_LAN8720, PHY_TLK110 };
105-
106106
typedef struct _wlan_if_obj_t {
107107
mp_obj_base_t base;
108108
int if_id;
109109
} wlan_if_obj_t;
110110

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-
125111
const mp_obj_type_t wlan_if_type;
126112
STATIC const wlan_if_obj_t wlan_sta_obj = {{&wlan_if_type}, WIFI_IF_STA};
127113
STATIC const wlan_if_obj_t wlan_ap_obj = {{&wlan_if_type}, WIFI_IF_AP};
128114

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-
132115
//static wifi_config_t wifi_ap_config = {{{0}}};
133116
static wifi_config_t wifi_sta_config = {{{0}}};
134117

@@ -206,7 +189,6 @@ STATIC void require_if(mp_obj_t wlan_if, int if_no) {
206189
STATIC mp_obj_t get_wlan(size_t n_args, const mp_obj_t *args) {
207190
static int initialized = 0;
208191
if (!initialized) {
209-
ESP_LOGD("modnetwork", "esp_event_loop_init done");
210192
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
211193
ESP_LOGD("modnetwork", "Initializing WiFi");
212194
ESP_EXCEPTIONS( esp_wifi_init(&cfg) );
@@ -229,136 +211,14 @@ STATIC mp_obj_t get_wlan(size_t n_args, const mp_obj_t *args) {
229211
}
230212
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(get_wlan_obj, 0, 1, get_wlan);
231213

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

388248
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_active_obj, 1, 2, esp_active);
389249

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

414252
mp_uint_t len;
@@ -445,12 +283,6 @@ STATIC mp_obj_t esp_status(mp_obj_t self_in) {
445283

446284
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_status_obj, esp_status);
447285

448-
STATIC mp_obj_t lan_isconnected(mp_obj_t self_in) {
449-
lan_if_obj_t *self = MP_OBJ_TO_PTR(self_in);
450-
return self->link_func() ? mp_const_true : mp_const_false;
451-
}
452-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(lan_isconnected_obj, lan_isconnected);
453-
454286
STATIC mp_obj_t esp_scan(mp_obj_t self_in) {
455287
// check that STA mode is active
456288
wifi_mode_t mode;
@@ -550,7 +382,7 @@ STATIC mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) {
550382
}
551383
}
552384

553-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_ifconfig_obj, 1, 2, esp_ifconfig);
385+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_ifconfig_obj, 1, 2, esp_ifconfig);
554386

555387
STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
556388
if (n_args != 1 && kwargs->used != 0) {
@@ -699,21 +531,6 @@ const mp_obj_type_t wlan_if_type = {
699531
.locals_dict = (mp_obj_t)&wlan_if_locals_dict,
700532
};
701533

702-
STATIC const mp_map_elem_t lan_if_locals_dict_table[] = {
703-
{ MP_OBJ_NEW_QSTR(MP_QSTR_active), (mp_obj_t)&lan_active_obj },
704-
{ MP_OBJ_NEW_QSTR(MP_QSTR_isconnected), (mp_obj_t)&lan_isconnected_obj },
705-
{ MP_OBJ_NEW_QSTR(MP_QSTR_status), (mp_obj_t)&esp_status_obj },
706-
{ MP_OBJ_NEW_QSTR(MP_QSTR_ifconfig), (mp_obj_t)&esp_ifconfig_obj },
707-
};
708-
709-
STATIC MP_DEFINE_CONST_DICT(lan_if_locals_dict, lan_if_locals_dict_table);
710-
711-
const mp_obj_type_t lan_if_type = {
712-
{ &mp_type_type },
713-
.name = MP_QSTR_LAN,
714-
.locals_dict = (mp_obj_t)&lan_if_locals_dict,
715-
};
716-
717534
STATIC mp_obj_t esp_phy_mode(size_t n_args, const mp_obj_t *args) {
718535
return mp_const_none;
719536
}

ports/esp32/modnetwork.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 "Eric Poulsen" <eric@zyxod.com>
7+
*
8+
* Based on the ESP IDF example code which is Public Domain / CC0
9+
*
10+
* Permission is hereby granted, free of charge, to any person obtaining a copy
11+
* of this software and associated documentation files (the "Software"), to deal
12+
* in the Software without restriction, including without limitation the rights
13+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
* copies of the Software, and to permit persons to whom the Software is
15+
* furnished to do so, subject to the following conditions:
16+
*
17+
* The above copyright notice and this permission notice shall be included in
18+
* all copies or substantial portions of the Software.
19+
*
20+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26+
* THE SOFTWARE.
27+
*/
28+
29+
#ifndef MICROPY_INCLUDED_ESP32_MODNETWORK_H
30+
#define MICROPY_INCLUDED_ESP32_MODNETWORK_H
31+
32+
enum { PHY_LAN8720, PHY_TLK110 };
33+
MP_DECLARE_CONST_FUN_OBJ_KW(get_lan_obj);
34+
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(esp_ifconfig_obj);
35+
36+
#endif

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy