blob: e1a8c957857c499bca21b11eee79824ebbf02269 [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
Damien Tournoude982c1d2023-01-12 21:04:07 -080033#if MICROPY_PY_NETWORK_LAN
Tobias Eydam48437ce2021-04-18 21:38:36 +020034
35#include "esp_eth.h"
36#include "esp_eth_mac.h"
Glenn Moloney288a0362024-07-08 17:33:53 +100037#include "esp_mac.h"
Tobias Eydam48437ce2021-04-18 21:38:36 +020038#include "esp_event.h"
39#include "esp_log.h"
40#include "esp_netif.h"
Damien Tournoude982c1d2023-01-12 21:04:07 -080041#if CONFIG_ETH_USE_SPI_ETHERNET
42#include "driver/spi_master.h"
43#endif
Eric Poulsen29dd6a72017-09-27 14:59:49 -070044
45#include "modnetwork.h"
robert-hhbbbd4842023-10-20 21:34:46 +020046#include "extmod/modnetwork.h"
Eric Poulsen29dd6a72017-09-27 14:59:49 -070047
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;
robert-hhcdfc6c12024-03-03 12:54:33 +010053 int8_t phy_reset_pin;
Eric Poulsen29dd6a72017-09-27 14:59:49 -070054 int8_t phy_power_pin;
Damien Tournoude982c1d2023-01-12 21:04:07 -080055 int8_t phy_cs_pin;
56 int8_t phy_int_pin;
Eric Poulsen29dd6a72017-09-27 14:59:49 -070057 uint8_t phy_addr;
58 uint8_t phy_type;
Tobias Eydam48437ce2021-04-18 21:38:36 +020059 esp_eth_phy_t *phy;
Tobias Eydam48437ce2021-04-18 21:38:36 +020060 esp_eth_handle_t eth_handle;
Eric Poulsen29dd6a72017-09-27 14:59:49 -070061} lan_if_obj_t;
62
63const mp_obj_type_t lan_if_type;
Angus Grattondecf8e62024-02-27 15:32:29 +110064static lan_if_obj_t lan_obj = {{{&lan_if_type}, ESP_IF_ETH, NULL}, false, false};
65static uint8_t eth_status = 0;
Eric Poulsen29dd6a72017-09-27 14:59:49 -070066
Tobias Eydam48437ce2021-04-18 21:38:36 +020067static void eth_event_handler(void *arg, esp_event_base_t event_base,
68 int32_t event_id, void *event_data) {
69 switch (event_id) {
70 case ETHERNET_EVENT_CONNECTED:
71 eth_status = ETH_CONNECTED;
72 ESP_LOGI("ethernet", "Ethernet Link Up");
73 break;
74 case ETHERNET_EVENT_DISCONNECTED:
75 eth_status = ETH_DISCONNECTED;
76 ESP_LOGI("ethernet", "Ethernet Link Down");
77 break;
78 case ETHERNET_EVENT_START:
79 eth_status = ETH_STARTED;
80 ESP_LOGI("ethernet", "Ethernet Started");
81 break;
82 case ETHERNET_EVENT_STOP:
83 eth_status = ETH_STOPPED;
84 ESP_LOGI("ethernet", "Ethernet Stopped");
85 break;
86 case IP_EVENT_ETH_GOT_IP:
87 eth_status = ETH_GOT_IP;
88 ESP_LOGI("ethernet", "Ethernet Got IP");
89 break;
90 default:
91 break;
Eric Poulsen29dd6a72017-09-27 14:59:49 -070092 }
93}
94
Glenn Moloney288a0362024-07-08 17:33:53 +100095static void set_mac_address(lan_if_obj_t *self, uint8_t *mac, size_t len) {
96 if (len != 6) {
97 mp_raise_ValueError(MP_ERROR_TEXT("invalid buffer length"));
98 }
99 if (((mac[0] & 0x01) != 0) ||
100 (esp_eth_ioctl(self->eth_handle, ETH_CMD_S_MAC_ADDR, mac) != ESP_OK) ||
101 (esp_netif_set_mac(self->base.netif, mac) != ESP_OK)) {
102 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("failed setting MAC address"));
103 }
104}
105
Angus Grattondecf8e62024-02-27 15:32:29 +1100106static 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 +1100107 lan_if_obj_t *self = &lan_obj;
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700108
109 if (self->initialized) {
110 return MP_OBJ_FROM_PTR(&lan_obj);
111 }
112
robert-hhcdfc6c12024-03-03 12:54:33 +0100113 enum { ARG_id, ARG_mdc, ARG_mdio, ARG_reset, ARG_power, ARG_phy_addr, ARG_phy_type,
Damien George67097d82023-01-18 13:47:10 +1100114 ARG_spi, ARG_cs, ARG_int, ARG_ref_clk_mode, ARG_ref_clk };
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700115 static const mp_arg_t allowed_args[] = {
116 { MP_QSTR_id, MP_ARG_OBJ, {.u_obj = mp_const_none} },
Damien Tournoude982c1d2023-01-12 21:04:07 -0800117 { MP_QSTR_mdc, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
118 { MP_QSTR_mdio, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
robert-hhcdfc6c12024-03-03 12:54:33 +0100119 { MP_QSTR_reset, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
Petr Kracík5801a002019-02-17 14:02:02 +0100120 { MP_QSTR_power, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700121 { MP_QSTR_phy_addr, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
122 { MP_QSTR_phy_type, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
Damien George67097d82023-01-18 13:47:10 +1100123 { MP_QSTR_spi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
124 { MP_QSTR_cs, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
125 { MP_QSTR_int, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
robert-hh4b520032022-12-24 16:52:55 +0100126 { MP_QSTR_ref_clk_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
127 { MP_QSTR_ref_clk, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700128 };
129
130 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
131 mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
132
133 if (args[ARG_id].u_obj != mp_const_none) {
134 if (mp_obj_get_int(args[ARG_id].u_obj) != 0) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100135 mp_raise_ValueError(MP_ERROR_TEXT("invalid LAN interface identifier"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700136 }
137 }
138
Damien Tournoude982c1d2023-01-12 21:04:07 -0800139 #define GET_PIN(XXX) args[XXX].u_obj == mp_const_none ? -1 : machine_pin_get_id(args[XXX].u_obj);
140
141 self->mdc_pin = GET_PIN(ARG_mdc);
142 self->mdio_pin = GET_PIN(ARG_mdio);
robert-hhcdfc6c12024-03-03 12:54:33 +0100143 self->phy_reset_pin = GET_PIN(ARG_reset);
Damien Tournoude982c1d2023-01-12 21:04:07 -0800144 self->phy_power_pin = GET_PIN(ARG_power);
145 self->phy_cs_pin = GET_PIN(ARG_cs);
146 self->phy_int_pin = GET_PIN(ARG_int);
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700147
148 if (args[ARG_phy_addr].u_int < 0x00 || args[ARG_phy_addr].u_int > 0x1f) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100149 mp_raise_ValueError(MP_ERROR_TEXT("invalid phy address"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700150 }
Tobias Eydam48437ce2021-04-18 21:38:36 +0200151 self->phy_addr = args[ARG_phy_addr].u_int;
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700152
robert-hhefb4bd32022-12-24 09:37:02 +0100153 if (args[ARG_phy_type].u_int != PHY_LAN8710 &&
154 args[ARG_phy_type].u_int != PHY_LAN8720 &&
Tobias Eydam48437ce2021-04-18 21:38:36 +0200155 args[ARG_phy_type].u_int != PHY_IP101 &&
156 args[ARG_phy_type].u_int != PHY_RTL8201 &&
Tobias Eydam48437ce2021-04-18 21:38:36 +0200157 args[ARG_phy_type].u_int != PHY_KSZ8041 &&
ma-lalonde30db33d2022-05-30 01:16:51 -0400158 args[ARG_phy_type].u_int != PHY_KSZ8081 &&
Damien Tournoude982c1d2023-01-12 21:04:07 -0800159 #if CONFIG_ETH_USE_SPI_ETHERNET
160 #if CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL
161 args[ARG_phy_type].u_int != PHY_KSZ8851SNL &&
162 #endif
163 #if CONFIG_ETH_SPI_ETHERNET_DM9051
164 args[ARG_phy_type].u_int != PHY_DM9051 &&
165 #endif
166 #if CONFIG_ETH_SPI_ETHERNET_W5500
167 args[ARG_phy_type].u_int != PHY_W5500 &&
168 #endif
169 #endif
Tobias Eydam48437ce2021-04-18 21:38:36 +0200170 args[ARG_phy_type].u_int != PHY_DP83848) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100171 mp_raise_ValueError(MP_ERROR_TEXT("invalid phy type"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700172 }
173
Tobias Eydam48437ce2021-04-18 21:38:36 +0200174 eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
Damien Georgee4650122023-05-09 09:52:54 +1000175 #if CONFIG_IDF_TARGET_ESP32
176 eth_esp32_emac_config_t esp32_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
177 #endif
178
Damien Tournoude982c1d2023-01-12 21:04:07 -0800179 esp_eth_mac_t *mac = NULL;
180
Damien Georgee4650122023-05-09 09:52:54 +1000181 #if CONFIG_IDF_TARGET_ESP32
182 // Dynamic ref_clk configuration.
robert-hh4b520032022-12-24 16:52:55 +0100183 if (args[ARG_ref_clk_mode].u_int != -1) {
184 // Map the GPIO_MODE constants to EMAC_CLK constants.
Damien Georgee4650122023-05-09 09:52:54 +1000185 esp32_config.clock_config.rmii.clock_mode =
robert-hh4b520032022-12-24 16:52:55 +0100186 args[ARG_ref_clk_mode].u_int == GPIO_MODE_INPUT ? EMAC_CLK_EXT_IN : EMAC_CLK_OUT;
187 }
188 if (args[ARG_ref_clk].u_obj != mp_const_none) {
Damien Georgee4650122023-05-09 09:52:54 +1000189 esp32_config.clock_config.rmii.clock_gpio = machine_pin_get_id(args[ARG_ref_clk].u_obj);
robert-hh4b520032022-12-24 16:52:55 +0100190 }
191 #endif
Petr Kracík7d8c71c2019-02-17 11:46:23 +0100192
Tobias Eydam48437ce2021-04-18 21:38:36 +0200193 eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
194 phy_config.phy_addr = self->phy_addr;
robert-hhcdfc6c12024-03-03 12:54:33 +0100195 phy_config.reset_gpio_num = self->phy_reset_pin;
Tobias Eydam48437ce2021-04-18 21:38:36 +0200196 self->phy = NULL;
robert-hhcdfc6c12024-03-03 12:54:33 +0100197 // Switch on the power before PHY is reset
198 if (self->phy_power_pin >= 0) {
199 mp_hal_pin_output(self->phy_power_pin);
200 mp_hal_pin_write(self->phy_power_pin, 1);
201 // let the power settle
202 mp_hal_delay_ms(100);
203 }
Damien Tournoude982c1d2023-01-12 21:04:07 -0800204 #if CONFIG_ETH_USE_SPI_ETHERNET
robert-hhc4e63ac2023-10-17 21:48:18 +0200205 spi_device_interface_config_t devcfg = {
206 .mode = 0,
207 .clock_speed_hz = MICROPY_PY_NETWORK_LAN_SPI_CLOCK_SPEED_MZ * 1000 * 1000,
208 .queue_size = 20,
209 .spics_io_num = self->phy_cs_pin,
210 .command_bits = 0, // Can both be set to 0, as the respective
211 .address_bits = 0, // driver fills in proper default values.
212 };
Damien Tournoude982c1d2023-01-12 21:04:07 -0800213 #endif
214
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700215 switch (args[ARG_phy_type].u_int) {
Damien Tournoude982c1d2023-01-12 21:04:07 -0800216 #if CONFIG_IDF_TARGET_ESP32
robert-hhefb4bd32022-12-24 09:37:02 +0100217 case PHY_LAN8710:
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700218 case PHY_LAN8720:
Damien Georgee4650122023-05-09 09:52:54 +1000219 self->phy = esp_eth_phy_new_lan87xx(&phy_config);
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700220 break;
Kenneth Ryerson76fefad2020-04-20 09:51:11 -0500221 case PHY_IP101:
Tobias Eydam48437ce2021-04-18 21:38:36 +0200222 self->phy = esp_eth_phy_new_ip101(&phy_config);
Kenneth Ryerson76fefad2020-04-20 09:51:11 -0500223 break;
Tobias Eydam48437ce2021-04-18 21:38:36 +0200224 case PHY_RTL8201:
225 self->phy = esp_eth_phy_new_rtl8201(&phy_config);
226 break;
227 case PHY_DP83848:
228 self->phy = esp_eth_phy_new_dp83848(&phy_config);
229 break;
230 case PHY_KSZ8041:
ma-lalonde30db33d2022-05-30 01:16:51 -0400231 case PHY_KSZ8081:
Damien Georgee4650122023-05-09 09:52:54 +1000232 self->phy = esp_eth_phy_new_ksz80xx(&phy_config);
ma-lalonde30db33d2022-05-30 01:16:51 -0400233 break;
234 #endif
Damien Tournoude982c1d2023-01-12 21:04:07 -0800235 #if CONFIG_ETH_USE_SPI_ETHERNET
236 #if CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL
237 case PHY_KSZ8851SNL: {
robert-hhc4e63ac2023-10-17 21:48:18 +0200238 spi_host_device_t host = machine_hw_spi_get_host(args[ARG_spi].u_obj);
239 eth_ksz8851snl_config_t chip_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(host, &devcfg);
Damien Tournoude982c1d2023-01-12 21:04:07 -0800240 chip_config.int_gpio_num = self->phy_int_pin;
241 mac = esp_eth_mac_new_ksz8851snl(&chip_config, &mac_config);
242 self->phy = esp_eth_phy_new_ksz8851snl(&phy_config);
243 break;
244 }
245 #endif
246 #if CONFIG_ETH_SPI_ETHERNET_DM9051
247 case PHY_DM9051: {
robert-hhc4e63ac2023-10-17 21:48:18 +0200248 spi_host_device_t host = machine_hw_spi_get_host(args[ARG_spi].u_obj);
249 eth_dm9051_config_t chip_config = ETH_DM9051_DEFAULT_CONFIG(host, &devcfg);
Damien Tournoude982c1d2023-01-12 21:04:07 -0800250 chip_config.int_gpio_num = self->phy_int_pin;
251 mac = esp_eth_mac_new_dm9051(&chip_config, &mac_config);
252 self->phy = esp_eth_phy_new_dm9051(&phy_config);
253 break;
254 }
255 #endif
256 #if CONFIG_ETH_SPI_ETHERNET_W5500
257 case PHY_W5500: {
robert-hhc4e63ac2023-10-17 21:48:18 +0200258 spi_host_device_t host = machine_hw_spi_get_host(args[ARG_spi].u_obj);
259 eth_w5500_config_t chip_config = ETH_W5500_DEFAULT_CONFIG(host, &devcfg);
Damien Tournoude982c1d2023-01-12 21:04:07 -0800260 chip_config.int_gpio_num = self->phy_int_pin;
261 mac = esp_eth_mac_new_w5500(&chip_config, &mac_config);
262 self->phy = esp_eth_phy_new_w5500(&phy_config);
263 break;
264 }
265 #endif
266 #endif
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700267 }
268
Damien Tournoude982c1d2023-01-12 21:04:07 -0800269 #if CONFIG_IDF_TARGET_ESP32
270 if (!IS_SPI_PHY(args[ARG_phy_type].u_int)) {
271 if (self->mdc_pin == -1 || self->mdio_pin == -1) {
272 mp_raise_ValueError(MP_ERROR_TEXT("mdc and mdio must be specified"));
273 }
Damien Georgee4650122023-05-09 09:52:54 +1000274 esp32_config.smi_mdc_gpio_num = self->mdc_pin;
275 esp32_config.smi_mdio_gpio_num = self->mdio_pin;
276 mac = esp_eth_mac_new_esp32(&esp32_config, &mac_config);
Damien Tournoude982c1d2023-01-12 21:04:07 -0800277 }
278 #endif
279
Tobias Eydam48437ce2021-04-18 21:38:36 +0200280 if (esp_netif_init() != ESP_OK) {
281 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_netif_init failed"));
Petr Kracík7d8c71c2019-02-17 11:46:23 +0100282 }
283
Tobias Eydam48437ce2021-04-18 21:38:36 +0200284 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
Damien Georgee4650122023-05-09 09:52:54 +1000285 self->base.netif = esp_netif_new(&cfg);
Tobias Eydam48437ce2021-04-18 21:38:36 +0200286
287 if (esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL) != ESP_OK) {
288 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_event_handler_register failed"));
289 }
290
291 if (esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL) != ESP_OK) {
292 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_event_handler_register failed"));
293 }
294
295 esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, self->phy);
296
297 esp_err_t esp_err = esp_eth_driver_install(&config, &self->eth_handle);
298 if (esp_err == ESP_OK) {
Glenn Moloneydbced752023-07-01 13:54:44 +1000299 self->base.active = false;
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700300 self->initialized = true;
301 } else {
Tobias Eydam48437ce2021-04-18 21:38:36 +0200302 if (esp_err == ESP_ERR_INVALID_ARG) {
303 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_eth_driver_install failed with invalid argument"));
304 } else if (esp_err == ESP_ERR_NO_MEM) {
305 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_eth_driver_install failed with no memory for driver"));
306 } else {
307 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_eth_driver_install failed"));
308 }
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700309 }
Tobias Eydam48437ce2021-04-18 21:38:36 +0200310
Damien Georgee4650122023-05-09 09:52:54 +1000311 if (esp_netif_attach(self->base.netif, esp_eth_new_netif_glue(self->eth_handle)) != ESP_OK) {
Tobias Eydam48437ce2021-04-18 21:38:36 +0200312 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_netif_attach failed"));
313 }
314
Glenn Moloney288a0362024-07-08 17:33:53 +1000315 // If MAC address is unset, set it to the address reserved for the ESP32 ETH interface
316 uint8_t mac_addr[6];
317 esp_eth_ioctl(self->eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
318 if ((mac_addr[0] | mac_addr[1] | mac_addr[2] | mac_addr[3] | mac_addr[4] | mac_addr[5]) == 0) {
319 esp_read_mac(mac_addr, ESP_MAC_ETH); // Get ESP32 MAC address for ETH iface
320 set_mac_address(self, mac_addr, sizeof(mac_addr));
321 }
322
Tobias Eydam48437ce2021-04-18 21:38:36 +0200323 eth_status = ETH_INITIALIZED;
324
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700325 return MP_OBJ_FROM_PTR(&lan_obj);
326}
Jim Mussaredeb51ca42023-02-01 14:20:45 +1100327MP_DEFINE_CONST_FUN_OBJ_KW(esp_network_get_lan_obj, 0, get_lan);
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700328
Angus Grattondecf8e62024-02-27 15:32:29 +1100329static mp_obj_t lan_active(size_t n_args, const mp_obj_t *args) {
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700330 lan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
331
332 if (n_args > 1) {
Glenn Moloney868d3112024-06-30 22:01:32 +1000333 bool make_active = mp_obj_is_true(args[1]);
334 if (make_active && !self->base.active) {
robert-hhbbbd4842023-10-20 21:34:46 +0200335 esp_netif_set_hostname(self->base.netif, mod_network_hostname_data);
Glenn Moloneydbced752023-07-01 13:54:44 +1000336 self->base.active = (esp_eth_start(self->eth_handle) == ESP_OK);
337 if (!self->base.active) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100338 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("ethernet enable failed"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700339 }
Glenn Moloney868d3112024-06-30 22:01:32 +1000340 } else if (!make_active && self->base.active) {
Glenn Moloneydbced752023-07-01 13:54:44 +1000341 self->base.active = !(esp_eth_stop(self->eth_handle) == ESP_OK);
342 if (self->base.active) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100343 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("ethernet disable failed"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700344 }
345 }
346 }
Tobias Eydam48437ce2021-04-18 21:38:36 +0200347
Glenn Moloneydbced752023-07-01 13:54:44 +1000348 return mp_obj_new_bool(self->base.active);
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700349}
Angus Grattondecf8e62024-02-27 15:32:29 +1100350static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(lan_active_obj, 1, 2, lan_active);
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700351
Angus Grattondecf8e62024-02-27 15:32:29 +1100352static mp_obj_t lan_status(mp_obj_t self_in) {
Tobias Eydam48437ce2021-04-18 21:38:36 +0200353 return MP_OBJ_NEW_SMALL_INT(eth_status);
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700354}
Angus Grattondecf8e62024-02-27 15:32:29 +1100355static MP_DEFINE_CONST_FUN_OBJ_1(lan_status_obj, lan_status);
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700356
Angus Grattondecf8e62024-02-27 15:32:29 +1100357static mp_obj_t lan_isconnected(mp_obj_t self_in) {
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700358 lan_if_obj_t *self = MP_OBJ_TO_PTR(self_in);
robert-hh9c244132023-10-19 15:05:59 +0200359 return mp_obj_new_bool(self->base.active && (eth_status == ETH_GOT_IP));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700360}
Angus Grattondecf8e62024-02-27 15:32:29 +1100361static MP_DEFINE_CONST_FUN_OBJ_1(lan_isconnected_obj, lan_isconnected);
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700362
Angus Grattondecf8e62024-02-27 15:32:29 +1100363static mp_obj_t lan_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
Tobias Eydam48437ce2021-04-18 21:38:36 +0200364 if (n_args != 1 && kwargs->used != 0) {
365 mp_raise_TypeError(MP_ERROR_TEXT("either pos or kw args are allowed"));
366 }
367 lan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
Tobias Eydam48437ce2021-04-18 21:38:36 +0200368
369 if (kwargs->used != 0) {
370
371 for (size_t i = 0; i < kwargs->alloc; i++) {
372 if (mp_map_slot_is_filled(kwargs, i)) {
Damien George5adb1fa2021-12-10 23:28:46 +1100373 switch (mp_obj_str_get_qstr(kwargs->table[i].key)) {
374 case MP_QSTR_mac: {
Tobias Eydam48437ce2021-04-18 21:38:36 +0200375 mp_buffer_info_t bufinfo;
376 mp_get_buffer_raise(kwargs->table[i].value, &bufinfo, MP_BUFFER_READ);
Glenn Moloney288a0362024-07-08 17:33:53 +1000377 set_mac_address(self, bufinfo.buf, bufinfo.len);
Tobias Eydam48437ce2021-04-18 21:38:36 +0200378 break;
379 }
380 default:
381 break;
382 }
383 }
384 }
385 return mp_const_none;
386 }
387
388 if (n_args != 2) {
389 mp_raise_TypeError(MP_ERROR_TEXT("can query only one param"));
390 }
391
392 mp_obj_t val = mp_const_none;
393
Damien George5adb1fa2021-12-10 23:28:46 +1100394 switch (mp_obj_str_get_qstr(args[1])) {
395 case MP_QSTR_mac: {
Tobias Eydam48437ce2021-04-18 21:38:36 +0200396 uint8_t mac[6];
397 esp_eth_ioctl(self->eth_handle, ETH_CMD_G_MAC_ADDR, mac);
398 return mp_obj_new_bytes(mac, sizeof(mac));
399 }
Daniël van de Giessenba8aad32023-07-04 15:36:37 +0200400 case MP_QSTR_ifname: {
401 val = esp_ifname(self->base.netif);
402 break;
403 }
Tobias Eydam48437ce2021-04-18 21:38:36 +0200404 default:
405 mp_raise_ValueError(MP_ERROR_TEXT("unknown config param"));
406 }
407
408 return val;
409}
Angus Grattondecf8e62024-02-27 15:32:29 +1100410static MP_DEFINE_CONST_FUN_OBJ_KW(lan_config_obj, 1, lan_config);
Tobias Eydam48437ce2021-04-18 21:38:36 +0200411
Angus Grattondecf8e62024-02-27 15:32:29 +1100412static const mp_rom_map_elem_t lan_if_locals_dict_table[] = {
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700413 { MP_ROM_QSTR(MP_QSTR_active), MP_ROM_PTR(&lan_active_obj) },
414 { MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&lan_isconnected_obj) },
415 { MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&lan_status_obj) },
Tobias Eydam48437ce2021-04-18 21:38:36 +0200416 { MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&lan_config_obj) },
Jim Mussaredeb51ca42023-02-01 14:20:45 +1100417 { MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&esp_network_ifconfig_obj) },
Felix Dörre1f23ab12024-03-27 21:53:34 +0000418 { MP_ROM_QSTR(MP_QSTR_ipconfig), MP_ROM_PTR(&esp_nic_ipconfig_obj) },
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700419};
420
Angus Grattondecf8e62024-02-27 15:32:29 +1100421static MP_DEFINE_CONST_DICT(lan_if_locals_dict, lan_if_locals_dict_table);
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700422
Jim Mussared662b9762021-07-14 14:38:38 +1000423MP_DEFINE_CONST_OBJ_TYPE(
424 lan_if_type,
425 MP_QSTR_LAN,
426 MP_TYPE_FLAG_NONE,
Jim Mussared9dce8272022-06-24 16:27:46 +1000427 locals_dict, &lan_if_locals_dict
Jim Mussared662b9762021-07-14 14:38:38 +1000428 );
Jim Mussared96008ff2019-09-13 23:04:13 +1000429
Tobias Eydam48437ce2021-04-18 21:38:36 +0200430#endif