blob: fc50e13c48edc7e2a20d794568557f39f2183d7a [file] [log] [blame]
Eric Poulsen29dd6a72017-09-27 14:59:49 -07001/*
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>
Tobias Eydam48437ce2021-04-18 21:38:36 +02007 * Copyright (c) 2021 "Tobias Eydam" <tobiaseydam@hotmail.com>
Eric Poulsen29dd6a72017-09-27 14:59:49 -07008 *
9 * Based on the ESP IDF example code which is Public Domain / CC0
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 */
29
30#include "py/runtime.h"
31#include "py/mphal.h"
32
Tobias Eydam48437ce2021-04-18 21:38:36 +020033#include "esp_idf_version.h"
34
35// LAN only for ESP32 (not ESP32S2) and only for ESP-IDF v4.1 and higher
36#if (ESP_IDF_VERSION_MAJOR == 4) && (ESP_IDF_VERSION_MINOR >= 1) && (CONFIG_IDF_TARGET_ESP32)
37
38#include "esp_eth.h"
39#include "esp_eth_mac.h"
40#include "esp_event.h"
41#include "esp_log.h"
42#include "esp_netif.h"
Eric Poulsen29dd6a72017-09-27 14:59:49 -070043
44#include "modnetwork.h"
45
46typedef struct _lan_if_obj_t {
47 mp_obj_base_t base;
48 int if_id; // MUST BE FIRST to match wlan_if_obj_t
49 bool initialized;
50 bool active;
51 uint8_t mdc_pin;
52 uint8_t mdio_pin;
53 int8_t phy_power_pin;
54 uint8_t phy_addr;
55 uint8_t phy_type;
Tobias Eydam48437ce2021-04-18 21:38:36 +020056 esp_eth_phy_t *phy;
57 esp_netif_t *eth_netif;
58 esp_eth_handle_t eth_handle;
Eric Poulsen29dd6a72017-09-27 14:59:49 -070059} lan_if_obj_t;
60
61const mp_obj_type_t lan_if_type;
62STATIC lan_if_obj_t lan_obj = {{&lan_if_type}, ESP_IF_ETH, false, false};
Tobias Eydam48437ce2021-04-18 21:38:36 +020063STATIC uint8_t eth_status = 0;
Eric Poulsen29dd6a72017-09-27 14:59:49 -070064
Tobias Eydam48437ce2021-04-18 21:38:36 +020065static void eth_event_handler(void *arg, esp_event_base_t event_base,
66 int32_t event_id, void *event_data) {
67 switch (event_id) {
68 case ETHERNET_EVENT_CONNECTED:
69 eth_status = ETH_CONNECTED;
70 ESP_LOGI("ethernet", "Ethernet Link Up");
71 break;
72 case ETHERNET_EVENT_DISCONNECTED:
73 eth_status = ETH_DISCONNECTED;
74 ESP_LOGI("ethernet", "Ethernet Link Down");
75 break;
76 case ETHERNET_EVENT_START:
77 eth_status = ETH_STARTED;
78 ESP_LOGI("ethernet", "Ethernet Started");
79 break;
80 case ETHERNET_EVENT_STOP:
81 eth_status = ETH_STOPPED;
82 ESP_LOGI("ethernet", "Ethernet Stopped");
83 break;
84 case IP_EVENT_ETH_GOT_IP:
85 eth_status = ETH_GOT_IP;
86 ESP_LOGI("ethernet", "Ethernet Got IP");
87 break;
88 default:
89 break;
Eric Poulsen29dd6a72017-09-27 14:59:49 -070090 }
91}
92
Eric Poulsen29dd6a72017-09-27 14:59:49 -070093STATIC mp_obj_t get_lan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
Damien George69661f32020-02-27 15:36:53 +110094 lan_if_obj_t *self = &lan_obj;
Eric Poulsen29dd6a72017-09-27 14:59:49 -070095
96 if (self->initialized) {
97 return MP_OBJ_FROM_PTR(&lan_obj);
98 }
99
Tobias Eydam48437ce2021-04-18 21:38:36 +0200100 enum { ARG_id, ARG_mdc, ARG_mdio, ARG_power, ARG_phy_addr, ARG_phy_type };
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700101 static const mp_arg_t allowed_args[] = {
102 { MP_QSTR_id, MP_ARG_OBJ, {.u_obj = mp_const_none} },
103 { MP_QSTR_mdc, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
104 { MP_QSTR_mdio, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
Petr KracĂ­k5801a002019-02-17 14:02:02 +0100105 { MP_QSTR_power, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700106 { MP_QSTR_phy_addr, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
107 { MP_QSTR_phy_type, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
108 };
109
110 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
111 mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
112
113 if (args[ARG_id].u_obj != mp_const_none) {
114 if (mp_obj_get_int(args[ARG_id].u_obj) != 0) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100115 mp_raise_ValueError(MP_ERROR_TEXT("invalid LAN interface identifier"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700116 }
117 }
118
119 self->mdc_pin = machine_pin_get_id(args[ARG_mdc].u_obj);
120 self->mdio_pin = machine_pin_get_id(args[ARG_mdio].u_obj);
121 self->phy_power_pin = args[ARG_power].u_obj == mp_const_none ? -1 : machine_pin_get_id(args[ARG_power].u_obj);
122
123 if (args[ARG_phy_addr].u_int < 0x00 || args[ARG_phy_addr].u_int > 0x1f) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100124 mp_raise_ValueError(MP_ERROR_TEXT("invalid phy address"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700125 }
Tobias Eydam48437ce2021-04-18 21:38:36 +0200126 self->phy_addr = args[ARG_phy_addr].u_int;
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700127
Kenneth Ryerson76fefad2020-04-20 09:51:11 -0500128 if (args[ARG_phy_type].u_int != PHY_LAN8720 &&
Tobias Eydam48437ce2021-04-18 21:38:36 +0200129 args[ARG_phy_type].u_int != PHY_IP101 &&
130 args[ARG_phy_type].u_int != PHY_RTL8201 &&
131 #if ESP_IDF_VERSION_MINOR >= 3 // KSZ8041 is new in ESP-IDF v4.3
132 args[ARG_phy_type].u_int != PHY_KSZ8041 &&
133 #endif
134 args[ARG_phy_type].u_int != PHY_DP83848) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100135 mp_raise_ValueError(MP_ERROR_TEXT("invalid phy type"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700136 }
137
Tobias Eydam48437ce2021-04-18 21:38:36 +0200138 eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
139 mac_config.smi_mdc_gpio_num = self->mdc_pin;
140 mac_config.smi_mdio_gpio_num = self->mdio_pin;
141 esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
Petr KracĂ­k7d8c71c2019-02-17 11:46:23 +0100142
Tobias Eydam48437ce2021-04-18 21:38:36 +0200143 eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
144 phy_config.phy_addr = self->phy_addr;
145 phy_config.reset_gpio_num = self->phy_power_pin;
146 self->phy = NULL;
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700147
148 switch (args[ARG_phy_type].u_int) {
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700149 case PHY_LAN8720:
Tobias Eydam48437ce2021-04-18 21:38:36 +0200150 self->phy = esp_eth_phy_new_lan8720(&phy_config);
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700151 break;
Kenneth Ryerson76fefad2020-04-20 09:51:11 -0500152 case PHY_IP101:
Tobias Eydam48437ce2021-04-18 21:38:36 +0200153 self->phy = esp_eth_phy_new_ip101(&phy_config);
Kenneth Ryerson76fefad2020-04-20 09:51:11 -0500154 break;
Tobias Eydam48437ce2021-04-18 21:38:36 +0200155 case PHY_RTL8201:
156 self->phy = esp_eth_phy_new_rtl8201(&phy_config);
157 break;
158 case PHY_DP83848:
159 self->phy = esp_eth_phy_new_dp83848(&phy_config);
160 break;
161 case PHY_KSZ8041:
162 #if ESP_IDF_VERSION_MINOR >= 3 // KSZ8041 is new in ESP-IDF v4.3
163 self->phy = esp_eth_phy_new_ksz8041(&phy_config);
164 break;
165 #endif
166 default:
167 mp_raise_ValueError(MP_ERROR_TEXT("unknown phy"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700168 }
169
Tobias Eydam48437ce2021-04-18 21:38:36 +0200170 if (esp_netif_init() != ESP_OK) {
171 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_netif_init failed"));
Petr KracĂ­k7d8c71c2019-02-17 11:46:23 +0100172 }
173
Tobias Eydam48437ce2021-04-18 21:38:36 +0200174 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
175 self->eth_netif = esp_netif_new(&cfg);
176
177 if (esp_eth_set_default_handlers(self->eth_netif) != ESP_OK) {
178 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_eth_set_default_handlers failed (invalid parameter)"));
179 }
180
181 if (esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL) != ESP_OK) {
182 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_event_handler_register failed"));
183 }
184
185 if (esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL) != ESP_OK) {
186 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_event_handler_register failed"));
187 }
188
189 esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, self->phy);
190
191 esp_err_t esp_err = esp_eth_driver_install(&config, &self->eth_handle);
192 if (esp_err == ESP_OK) {
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700193 self->active = false;
194 self->initialized = true;
195 } else {
Tobias Eydam48437ce2021-04-18 21:38:36 +0200196 if (esp_err == ESP_ERR_INVALID_ARG) {
197 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_eth_driver_install failed with invalid argument"));
198 } else if (esp_err == ESP_ERR_NO_MEM) {
199 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_eth_driver_install failed with no memory for driver"));
200 } else {
201 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_eth_driver_install failed"));
202 }
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700203 }
Tobias Eydam48437ce2021-04-18 21:38:36 +0200204
205 if (esp_netif_attach(self->eth_netif, esp_eth_new_netif_glue(self->eth_handle)) != ESP_OK) {
206 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_netif_attach failed"));
207 }
208
209 eth_status = ETH_INITIALIZED;
210
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700211 return MP_OBJ_FROM_PTR(&lan_obj);
212}
213MP_DEFINE_CONST_FUN_OBJ_KW(get_lan_obj, 0, get_lan);
214
215STATIC mp_obj_t lan_active(size_t n_args, const mp_obj_t *args) {
216 lan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
217
218 if (n_args > 1) {
219 if (mp_obj_is_true(args[1])) {
Tobias Eydam48437ce2021-04-18 21:38:36 +0200220 self->active = (esp_eth_start(self->eth_handle) == ESP_OK);
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700221 if (!self->active) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100222 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("ethernet enable failed"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700223 }
224 } else {
Tobias Eydam48437ce2021-04-18 21:38:36 +0200225 self->active = !(esp_eth_stop(self->eth_handle) == ESP_OK);
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700226 if (self->active) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100227 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("ethernet disable failed"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700228 }
229 }
230 }
Tobias Eydam48437ce2021-04-18 21:38:36 +0200231
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700232 return mp_obj_new_bool(self->active);
233}
234STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(lan_active_obj, 1, 2, lan_active);
235
236STATIC mp_obj_t lan_status(mp_obj_t self_in) {
Tobias Eydam48437ce2021-04-18 21:38:36 +0200237 return MP_OBJ_NEW_SMALL_INT(eth_status);
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700238}
239STATIC MP_DEFINE_CONST_FUN_OBJ_1(lan_status_obj, lan_status);
240
241STATIC mp_obj_t lan_isconnected(mp_obj_t self_in) {
242 lan_if_obj_t *self = MP_OBJ_TO_PTR(self_in);
Tobias Eydam48437ce2021-04-18 21:38:36 +0200243 return self->active ? mp_obj_new_bool(self->phy->get_link(self->phy) == ETH_LINK_UP) : mp_const_false;
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700244}
245STATIC MP_DEFINE_CONST_FUN_OBJ_1(lan_isconnected_obj, lan_isconnected);
246
Tobias Eydam48437ce2021-04-18 21:38:36 +0200247STATIC mp_obj_t lan_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
248 if (n_args != 1 && kwargs->used != 0) {
249 mp_raise_TypeError(MP_ERROR_TEXT("either pos or kw args are allowed"));
250 }
251 lan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
Tobias Eydam48437ce2021-04-18 21:38:36 +0200252
253 if (kwargs->used != 0) {
254
255 for (size_t i = 0; i < kwargs->alloc; i++) {
256 if (mp_map_slot_is_filled(kwargs, i)) {
Damien George5adb1fa2021-12-10 23:28:46 +1100257 switch (mp_obj_str_get_qstr(kwargs->table[i].key)) {
258 case MP_QSTR_mac: {
Tobias Eydam48437ce2021-04-18 21:38:36 +0200259 mp_buffer_info_t bufinfo;
260 mp_get_buffer_raise(kwargs->table[i].value, &bufinfo, MP_BUFFER_READ);
261 if (bufinfo.len != 6) {
262 mp_raise_ValueError(MP_ERROR_TEXT("invalid buffer length"));
263 }
264 esp_eth_ioctl(self->eth_handle, ETH_CMD_S_MAC_ADDR, bufinfo.buf);
265 break;
266 }
267 default:
268 break;
269 }
270 }
271 }
272 return mp_const_none;
273 }
274
275 if (n_args != 2) {
276 mp_raise_TypeError(MP_ERROR_TEXT("can query only one param"));
277 }
278
279 mp_obj_t val = mp_const_none;
280
Damien George5adb1fa2021-12-10 23:28:46 +1100281 switch (mp_obj_str_get_qstr(args[1])) {
282 case MP_QSTR_mac: {
Tobias Eydam48437ce2021-04-18 21:38:36 +0200283 uint8_t mac[6];
284 esp_eth_ioctl(self->eth_handle, ETH_CMD_G_MAC_ADDR, mac);
285 return mp_obj_new_bytes(mac, sizeof(mac));
286 }
287 default:
288 mp_raise_ValueError(MP_ERROR_TEXT("unknown config param"));
289 }
290
291 return val;
292}
293STATIC MP_DEFINE_CONST_FUN_OBJ_KW(lan_config_obj, 1, lan_config);
294
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700295STATIC const mp_rom_map_elem_t lan_if_locals_dict_table[] = {
296 { MP_ROM_QSTR(MP_QSTR_active), MP_ROM_PTR(&lan_active_obj) },
297 { MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&lan_isconnected_obj) },
298 { MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&lan_status_obj) },
Tobias Eydam48437ce2021-04-18 21:38:36 +0200299 { MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&lan_config_obj) },
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700300 { MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&esp_ifconfig_obj) },
301};
302
303STATIC MP_DEFINE_CONST_DICT(lan_if_locals_dict, lan_if_locals_dict_table);
304
Jim Mussared662b9762021-07-14 14:38:38 +1000305MP_DEFINE_CONST_OBJ_TYPE(
306 lan_if_type,
307 MP_QSTR_LAN,
308 MP_TYPE_FLAG_NONE,
309 MP_TYPE_NULL_MAKE_NEW,
310 locals_dict, (mp_obj_dict_t *)&lan_if_locals_dict
311 );
Jim Mussared96008ff2019-09-13 23:04:13 +1000312
Tobias Eydam48437ce2021-04-18 21:38:36 +0200313#endif