blob: 15a5110b1ee67f462bef1cab84cc7f36288cdbde [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
Damien Tournoude982c1d2023-01-12 21:04:07 -080035#if MICROPY_PY_NETWORK_LAN
Tobias Eydam48437ce2021-04-18 21:38:36 +020036
37#include "esp_eth.h"
38#include "esp_eth_mac.h"
39#include "esp_event.h"
40#include "esp_log.h"
41#include "esp_netif.h"
Damien Tournoude982c1d2023-01-12 21:04:07 -080042#if CONFIG_ETH_USE_SPI_ETHERNET
43#include "driver/spi_master.h"
44#endif
Eric Poulsen29dd6a72017-09-27 14:59:49 -070045
46#include "modnetwork.h"
47
48typedef struct _lan_if_obj_t {
Damien Georgee4650122023-05-09 09:52:54 +100049 base_if_obj_t base;
Eric Poulsen29dd6a72017-09-27 14:59:49 -070050 bool initialized;
Damien Tournoude982c1d2023-01-12 21:04:07 -080051 int8_t mdc_pin;
52 int8_t mdio_pin;
Eric Poulsen29dd6a72017-09-27 14:59:49 -070053 int8_t phy_power_pin;
Damien Tournoude982c1d2023-01-12 21:04:07 -080054 int8_t phy_cs_pin;
55 int8_t phy_int_pin;
Eric Poulsen29dd6a72017-09-27 14:59:49 -070056 uint8_t phy_addr;
57 uint8_t phy_type;
Tobias Eydam48437ce2021-04-18 21:38:36 +020058 esp_eth_phy_t *phy;
Tobias Eydam48437ce2021-04-18 21:38:36 +020059 esp_eth_handle_t eth_handle;
Eric Poulsen29dd6a72017-09-27 14:59:49 -070060} lan_if_obj_t;
61
62const mp_obj_type_t lan_if_type;
Damien Georgee4650122023-05-09 09:52:54 +100063STATIC lan_if_obj_t lan_obj = {{{&lan_if_type}, ESP_IF_ETH, NULL}, false, false};
Tobias Eydam48437ce2021-04-18 21:38:36 +020064STATIC uint8_t eth_status = 0;
Eric Poulsen29dd6a72017-09-27 14:59:49 -070065
Tobias Eydam48437ce2021-04-18 21:38:36 +020066static void eth_event_handler(void *arg, esp_event_base_t event_base,
67 int32_t event_id, void *event_data) {
68 switch (event_id) {
69 case ETHERNET_EVENT_CONNECTED:
70 eth_status = ETH_CONNECTED;
71 ESP_LOGI("ethernet", "Ethernet Link Up");
72 break;
73 case ETHERNET_EVENT_DISCONNECTED:
74 eth_status = ETH_DISCONNECTED;
75 ESP_LOGI("ethernet", "Ethernet Link Down");
76 break;
77 case ETHERNET_EVENT_START:
78 eth_status = ETH_STARTED;
79 ESP_LOGI("ethernet", "Ethernet Started");
80 break;
81 case ETHERNET_EVENT_STOP:
82 eth_status = ETH_STOPPED;
83 ESP_LOGI("ethernet", "Ethernet Stopped");
84 break;
85 case IP_EVENT_ETH_GOT_IP:
86 eth_status = ETH_GOT_IP;
87 ESP_LOGI("ethernet", "Ethernet Got IP");
88 break;
89 default:
90 break;
Eric Poulsen29dd6a72017-09-27 14:59:49 -070091 }
92}
93
Eric Poulsen29dd6a72017-09-27 14:59:49 -070094STATIC 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 +110095 lan_if_obj_t *self = &lan_obj;
Eric Poulsen29dd6a72017-09-27 14:59:49 -070096
97 if (self->initialized) {
98 return MP_OBJ_FROM_PTR(&lan_obj);
99 }
100
robert-hh4b520032022-12-24 16:52:55 +0100101 enum { ARG_id, ARG_mdc, ARG_mdio, ARG_power, ARG_phy_addr, ARG_phy_type,
Damien George67097d82023-01-18 13:47:10 +1100102 ARG_spi, ARG_cs, ARG_int, ARG_ref_clk_mode, ARG_ref_clk };
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700103 static const mp_arg_t allowed_args[] = {
104 { MP_QSTR_id, MP_ARG_OBJ, {.u_obj = mp_const_none} },
Damien Tournoude982c1d2023-01-12 21:04:07 -0800105 { MP_QSTR_mdc, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
106 { MP_QSTR_mdio, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
Petr Kracík5801a002019-02-17 14:02:02 +0100107 { MP_QSTR_power, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700108 { MP_QSTR_phy_addr, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
109 { MP_QSTR_phy_type, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
Damien George67097d82023-01-18 13:47:10 +1100110 { MP_QSTR_spi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
111 { MP_QSTR_cs, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
112 { MP_QSTR_int, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
robert-hh4b520032022-12-24 16:52:55 +0100113 { MP_QSTR_ref_clk_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
114 { MP_QSTR_ref_clk, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700115 };
116
117 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
118 mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
119
120 if (args[ARG_id].u_obj != mp_const_none) {
121 if (mp_obj_get_int(args[ARG_id].u_obj) != 0) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100122 mp_raise_ValueError(MP_ERROR_TEXT("invalid LAN interface identifier"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700123 }
124 }
125
Damien Tournoude982c1d2023-01-12 21:04:07 -0800126 #define GET_PIN(XXX) args[XXX].u_obj == mp_const_none ? -1 : machine_pin_get_id(args[XXX].u_obj);
127
128 self->mdc_pin = GET_PIN(ARG_mdc);
129 self->mdio_pin = GET_PIN(ARG_mdio);
130 self->phy_power_pin = GET_PIN(ARG_power);
131 self->phy_cs_pin = GET_PIN(ARG_cs);
132 self->phy_int_pin = GET_PIN(ARG_int);
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700133
134 if (args[ARG_phy_addr].u_int < 0x00 || args[ARG_phy_addr].u_int > 0x1f) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100135 mp_raise_ValueError(MP_ERROR_TEXT("invalid phy address"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700136 }
Tobias Eydam48437ce2021-04-18 21:38:36 +0200137 self->phy_addr = args[ARG_phy_addr].u_int;
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700138
robert-hhefb4bd32022-12-24 09:37:02 +0100139 if (args[ARG_phy_type].u_int != PHY_LAN8710 &&
140 args[ARG_phy_type].u_int != PHY_LAN8720 &&
Tobias Eydam48437ce2021-04-18 21:38:36 +0200141 args[ARG_phy_type].u_int != PHY_IP101 &&
142 args[ARG_phy_type].u_int != PHY_RTL8201 &&
Tobias Eydam48437ce2021-04-18 21:38:36 +0200143 args[ARG_phy_type].u_int != PHY_KSZ8041 &&
ma-lalonde30db33d2022-05-30 01:16:51 -0400144 args[ARG_phy_type].u_int != PHY_KSZ8081 &&
Damien Tournoude982c1d2023-01-12 21:04:07 -0800145 #if CONFIG_ETH_USE_SPI_ETHERNET
146 #if CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL
147 args[ARG_phy_type].u_int != PHY_KSZ8851SNL &&
148 #endif
149 #if CONFIG_ETH_SPI_ETHERNET_DM9051
150 args[ARG_phy_type].u_int != PHY_DM9051 &&
151 #endif
152 #if CONFIG_ETH_SPI_ETHERNET_W5500
153 args[ARG_phy_type].u_int != PHY_W5500 &&
154 #endif
155 #endif
Tobias Eydam48437ce2021-04-18 21:38:36 +0200156 args[ARG_phy_type].u_int != PHY_DP83848) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100157 mp_raise_ValueError(MP_ERROR_TEXT("invalid phy type"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700158 }
159
Tobias Eydam48437ce2021-04-18 21:38:36 +0200160 eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
Damien Georgee4650122023-05-09 09:52:54 +1000161 #if CONFIG_IDF_TARGET_ESP32
162 eth_esp32_emac_config_t esp32_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
163 #endif
164
Damien Tournoude982c1d2023-01-12 21:04:07 -0800165 esp_eth_mac_t *mac = NULL;
166
Damien Georgee4650122023-05-09 09:52:54 +1000167 #if CONFIG_IDF_TARGET_ESP32
168 // Dynamic ref_clk configuration.
robert-hh4b520032022-12-24 16:52:55 +0100169 if (args[ARG_ref_clk_mode].u_int != -1) {
170 // Map the GPIO_MODE constants to EMAC_CLK constants.
Damien Georgee4650122023-05-09 09:52:54 +1000171 esp32_config.clock_config.rmii.clock_mode =
robert-hh4b520032022-12-24 16:52:55 +0100172 args[ARG_ref_clk_mode].u_int == GPIO_MODE_INPUT ? EMAC_CLK_EXT_IN : EMAC_CLK_OUT;
173 }
174 if (args[ARG_ref_clk].u_obj != mp_const_none) {
Damien Georgee4650122023-05-09 09:52:54 +1000175 esp32_config.clock_config.rmii.clock_gpio = machine_pin_get_id(args[ARG_ref_clk].u_obj);
robert-hh4b520032022-12-24 16:52:55 +0100176 }
177 #endif
Petr Kracík7d8c71c2019-02-17 11:46:23 +0100178
Tobias Eydam48437ce2021-04-18 21:38:36 +0200179 eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
180 phy_config.phy_addr = self->phy_addr;
181 phy_config.reset_gpio_num = self->phy_power_pin;
182 self->phy = NULL;
Damien Tournoude982c1d2023-01-12 21:04:07 -0800183 #if CONFIG_ETH_USE_SPI_ETHERNET
robert-hhc4e63ac2023-10-17 21:48:18 +0200184 spi_device_interface_config_t devcfg = {
185 .mode = 0,
186 .clock_speed_hz = MICROPY_PY_NETWORK_LAN_SPI_CLOCK_SPEED_MZ * 1000 * 1000,
187 .queue_size = 20,
188 .spics_io_num = self->phy_cs_pin,
189 .command_bits = 0, // Can both be set to 0, as the respective
190 .address_bits = 0, // driver fills in proper default values.
191 };
Damien Tournoude982c1d2023-01-12 21:04:07 -0800192 #endif
193
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700194 switch (args[ARG_phy_type].u_int) {
Damien Tournoude982c1d2023-01-12 21:04:07 -0800195 #if CONFIG_IDF_TARGET_ESP32
robert-hhefb4bd32022-12-24 09:37:02 +0100196 case PHY_LAN8710:
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700197 case PHY_LAN8720:
Damien Georgee4650122023-05-09 09:52:54 +1000198 self->phy = esp_eth_phy_new_lan87xx(&phy_config);
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700199 break;
Kenneth Ryerson76fefad2020-04-20 09:51:11 -0500200 case PHY_IP101:
Tobias Eydam48437ce2021-04-18 21:38:36 +0200201 self->phy = esp_eth_phy_new_ip101(&phy_config);
Kenneth Ryerson76fefad2020-04-20 09:51:11 -0500202 break;
Tobias Eydam48437ce2021-04-18 21:38:36 +0200203 case PHY_RTL8201:
204 self->phy = esp_eth_phy_new_rtl8201(&phy_config);
205 break;
206 case PHY_DP83848:
207 self->phy = esp_eth_phy_new_dp83848(&phy_config);
208 break;
209 case PHY_KSZ8041:
ma-lalonde30db33d2022-05-30 01:16:51 -0400210 case PHY_KSZ8081:
Damien Georgee4650122023-05-09 09:52:54 +1000211 self->phy = esp_eth_phy_new_ksz80xx(&phy_config);
ma-lalonde30db33d2022-05-30 01:16:51 -0400212 break;
213 #endif
Damien Tournoude982c1d2023-01-12 21:04:07 -0800214 #if CONFIG_ETH_USE_SPI_ETHERNET
215 #if CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL
216 case PHY_KSZ8851SNL: {
robert-hhc4e63ac2023-10-17 21:48:18 +0200217 spi_host_device_t host = machine_hw_spi_get_host(args[ARG_spi].u_obj);
218 eth_ksz8851snl_config_t chip_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(host, &devcfg);
Damien Tournoude982c1d2023-01-12 21:04:07 -0800219 chip_config.int_gpio_num = self->phy_int_pin;
220 mac = esp_eth_mac_new_ksz8851snl(&chip_config, &mac_config);
221 self->phy = esp_eth_phy_new_ksz8851snl(&phy_config);
222 break;
223 }
224 #endif
225 #if CONFIG_ETH_SPI_ETHERNET_DM9051
226 case PHY_DM9051: {
robert-hhc4e63ac2023-10-17 21:48:18 +0200227 spi_host_device_t host = machine_hw_spi_get_host(args[ARG_spi].u_obj);
228 eth_dm9051_config_t chip_config = ETH_DM9051_DEFAULT_CONFIG(host, &devcfg);
Damien Tournoude982c1d2023-01-12 21:04:07 -0800229 chip_config.int_gpio_num = self->phy_int_pin;
230 mac = esp_eth_mac_new_dm9051(&chip_config, &mac_config);
231 self->phy = esp_eth_phy_new_dm9051(&phy_config);
232 break;
233 }
234 #endif
235 #if CONFIG_ETH_SPI_ETHERNET_W5500
236 case PHY_W5500: {
robert-hhc4e63ac2023-10-17 21:48:18 +0200237 spi_host_device_t host = machine_hw_spi_get_host(args[ARG_spi].u_obj);
238 eth_w5500_config_t chip_config = ETH_W5500_DEFAULT_CONFIG(host, &devcfg);
Damien Tournoude982c1d2023-01-12 21:04:07 -0800239 chip_config.int_gpio_num = self->phy_int_pin;
240 mac = esp_eth_mac_new_w5500(&chip_config, &mac_config);
241 self->phy = esp_eth_phy_new_w5500(&phy_config);
242 break;
243 }
244 #endif
245 #endif
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700246 }
247
Damien Tournoude982c1d2023-01-12 21:04:07 -0800248 #if CONFIG_IDF_TARGET_ESP32
249 if (!IS_SPI_PHY(args[ARG_phy_type].u_int)) {
250 if (self->mdc_pin == -1 || self->mdio_pin == -1) {
251 mp_raise_ValueError(MP_ERROR_TEXT("mdc and mdio must be specified"));
252 }
Damien Georgee4650122023-05-09 09:52:54 +1000253 esp32_config.smi_mdc_gpio_num = self->mdc_pin;
254 esp32_config.smi_mdio_gpio_num = self->mdio_pin;
255 mac = esp_eth_mac_new_esp32(&esp32_config, &mac_config);
Damien Tournoude982c1d2023-01-12 21:04:07 -0800256 }
257 #endif
258
Tobias Eydam48437ce2021-04-18 21:38:36 +0200259 if (esp_netif_init() != ESP_OK) {
260 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_netif_init failed"));
Petr Kracík7d8c71c2019-02-17 11:46:23 +0100261 }
262
Tobias Eydam48437ce2021-04-18 21:38:36 +0200263 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
Damien Georgee4650122023-05-09 09:52:54 +1000264 self->base.netif = esp_netif_new(&cfg);
Tobias Eydam48437ce2021-04-18 21:38:36 +0200265
266 if (esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL) != ESP_OK) {
267 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_event_handler_register failed"));
268 }
269
270 if (esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL) != ESP_OK) {
271 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_event_handler_register failed"));
272 }
273
274 esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, self->phy);
275
276 esp_err_t esp_err = esp_eth_driver_install(&config, &self->eth_handle);
277 if (esp_err == ESP_OK) {
Glenn Moloneydbced752023-07-01 13:54:44 +1000278 self->base.active = false;
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700279 self->initialized = true;
280 } else {
Tobias Eydam48437ce2021-04-18 21:38:36 +0200281 if (esp_err == ESP_ERR_INVALID_ARG) {
282 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_eth_driver_install failed with invalid argument"));
283 } else if (esp_err == ESP_ERR_NO_MEM) {
284 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_eth_driver_install failed with no memory for driver"));
285 } else {
286 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_eth_driver_install failed"));
287 }
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700288 }
Tobias Eydam48437ce2021-04-18 21:38:36 +0200289
Damien Georgee4650122023-05-09 09:52:54 +1000290 if (esp_netif_attach(self->base.netif, esp_eth_new_netif_glue(self->eth_handle)) != ESP_OK) {
Tobias Eydam48437ce2021-04-18 21:38:36 +0200291 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_netif_attach failed"));
292 }
293
294 eth_status = ETH_INITIALIZED;
295
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700296 return MP_OBJ_FROM_PTR(&lan_obj);
297}
Jim Mussaredeb51ca42023-02-01 14:20:45 +1100298MP_DEFINE_CONST_FUN_OBJ_KW(esp_network_get_lan_obj, 0, get_lan);
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700299
300STATIC mp_obj_t lan_active(size_t n_args, const mp_obj_t *args) {
301 lan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
302
303 if (n_args > 1) {
304 if (mp_obj_is_true(args[1])) {
Glenn Moloneydbced752023-07-01 13:54:44 +1000305 self->base.active = (esp_eth_start(self->eth_handle) == ESP_OK);
306 if (!self->base.active) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100307 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("ethernet enable failed"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700308 }
309 } else {
Glenn Moloneydbced752023-07-01 13:54:44 +1000310 self->base.active = !(esp_eth_stop(self->eth_handle) == ESP_OK);
311 if (self->base.active) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100312 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("ethernet disable failed"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700313 }
314 }
315 }
Tobias Eydam48437ce2021-04-18 21:38:36 +0200316
Glenn Moloneydbced752023-07-01 13:54:44 +1000317 return mp_obj_new_bool(self->base.active);
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700318}
319STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(lan_active_obj, 1, 2, lan_active);
320
321STATIC mp_obj_t lan_status(mp_obj_t self_in) {
Tobias Eydam48437ce2021-04-18 21:38:36 +0200322 return MP_OBJ_NEW_SMALL_INT(eth_status);
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700323}
324STATIC MP_DEFINE_CONST_FUN_OBJ_1(lan_status_obj, lan_status);
325
326STATIC mp_obj_t lan_isconnected(mp_obj_t self_in) {
327 lan_if_obj_t *self = MP_OBJ_TO_PTR(self_in);
robert-hh9c244132023-10-19 15:05:59 +0200328 return mp_obj_new_bool(self->base.active && (eth_status == ETH_GOT_IP));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700329}
330STATIC MP_DEFINE_CONST_FUN_OBJ_1(lan_isconnected_obj, lan_isconnected);
331
Tobias Eydam48437ce2021-04-18 21:38:36 +0200332STATIC mp_obj_t lan_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
333 if (n_args != 1 && kwargs->used != 0) {
334 mp_raise_TypeError(MP_ERROR_TEXT("either pos or kw args are allowed"));
335 }
336 lan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
Tobias Eydam48437ce2021-04-18 21:38:36 +0200337
338 if (kwargs->used != 0) {
339
340 for (size_t i = 0; i < kwargs->alloc; i++) {
341 if (mp_map_slot_is_filled(kwargs, i)) {
Damien George5adb1fa2021-12-10 23:28:46 +1100342 switch (mp_obj_str_get_qstr(kwargs->table[i].key)) {
343 case MP_QSTR_mac: {
Tobias Eydam48437ce2021-04-18 21:38:36 +0200344 mp_buffer_info_t bufinfo;
345 mp_get_buffer_raise(kwargs->table[i].value, &bufinfo, MP_BUFFER_READ);
346 if (bufinfo.len != 6) {
347 mp_raise_ValueError(MP_ERROR_TEXT("invalid buffer length"));
348 }
Damien Tournoudc7301b82023-01-12 21:04:07 -0800349 if (
350 (esp_eth_ioctl(self->eth_handle, ETH_CMD_S_MAC_ADDR, bufinfo.buf) != ESP_OK) ||
Damien Georgee4650122023-05-09 09:52:54 +1000351 (esp_netif_set_mac(self->base.netif, bufinfo.buf) != ESP_OK)
Damien Tournoudc7301b82023-01-12 21:04:07 -0800352 ) {
353 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("failed setting MAC address"));
354 }
Tobias Eydam48437ce2021-04-18 21:38:36 +0200355 break;
356 }
357 default:
358 break;
359 }
360 }
361 }
362 return mp_const_none;
363 }
364
365 if (n_args != 2) {
366 mp_raise_TypeError(MP_ERROR_TEXT("can query only one param"));
367 }
368
369 mp_obj_t val = mp_const_none;
370
Damien George5adb1fa2021-12-10 23:28:46 +1100371 switch (mp_obj_str_get_qstr(args[1])) {
372 case MP_QSTR_mac: {
Tobias Eydam48437ce2021-04-18 21:38:36 +0200373 uint8_t mac[6];
374 esp_eth_ioctl(self->eth_handle, ETH_CMD_G_MAC_ADDR, mac);
375 return mp_obj_new_bytes(mac, sizeof(mac));
376 }
Daniël van de Giessenba8aad32023-07-04 15:36:37 +0200377 case MP_QSTR_ifname: {
378 val = esp_ifname(self->base.netif);
379 break;
380 }
Tobias Eydam48437ce2021-04-18 21:38:36 +0200381 default:
382 mp_raise_ValueError(MP_ERROR_TEXT("unknown config param"));
383 }
384
385 return val;
386}
387STATIC MP_DEFINE_CONST_FUN_OBJ_KW(lan_config_obj, 1, lan_config);
388
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700389STATIC const mp_rom_map_elem_t lan_if_locals_dict_table[] = {
390 { MP_ROM_QSTR(MP_QSTR_active), MP_ROM_PTR(&lan_active_obj) },
391 { MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&lan_isconnected_obj) },
392 { MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&lan_status_obj) },
Tobias Eydam48437ce2021-04-18 21:38:36 +0200393 { MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&lan_config_obj) },
Jim Mussaredeb51ca42023-02-01 14:20:45 +1100394 { MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&esp_network_ifconfig_obj) },
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700395};
396
397STATIC MP_DEFINE_CONST_DICT(lan_if_locals_dict, lan_if_locals_dict_table);
398
Jim Mussared662b9762021-07-14 14:38:38 +1000399MP_DEFINE_CONST_OBJ_TYPE(
400 lan_if_type,
401 MP_QSTR_LAN,
402 MP_TYPE_FLAG_NONE,
Jim Mussared9dce8272022-06-24 16:27:46 +1000403 locals_dict, &lan_if_locals_dict
Jim Mussared662b9762021-07-14 14:38:38 +1000404 );
Jim Mussared96008ff2019-09-13 23:04:13 +1000405
Tobias Eydam48437ce2021-04-18 21:38:36 +0200406#endif