Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 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> |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 7 | * Copyright (c) 2021 "Tobias Eydam" <tobiaseydam@hotmail.com> |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 8 | * |
| 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 Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 33 | #include "esp_idf_version.h" |
| 34 | |
| 35 | // LAN only for ESP32 (not ESP32S2) and only for ESP-IDF v4.1 and higher |
Damien Tournoud | e982c1d | 2023-01-12 21:04:07 -0800 | [diff] [blame] | 36 | #if MICROPY_PY_NETWORK_LAN |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 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" |
Damien Tournoud | e982c1d | 2023-01-12 21:04:07 -0800 | [diff] [blame] | 43 | #if CONFIG_ETH_USE_SPI_ETHERNET |
| 44 | #include "driver/spi_master.h" |
| 45 | #endif |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 46 | |
| 47 | #include "modnetwork.h" |
| 48 | |
| 49 | typedef struct _lan_if_obj_t { |
| 50 | mp_obj_base_t base; |
| 51 | int if_id; // MUST BE FIRST to match wlan_if_obj_t |
| 52 | bool initialized; |
| 53 | bool active; |
Damien Tournoud | e982c1d | 2023-01-12 21:04:07 -0800 | [diff] [blame] | 54 | int8_t mdc_pin; |
| 55 | int8_t mdio_pin; |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 56 | int8_t phy_power_pin; |
Damien Tournoud | e982c1d | 2023-01-12 21:04:07 -0800 | [diff] [blame] | 57 | int8_t phy_cs_pin; |
| 58 | int8_t phy_int_pin; |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 59 | uint8_t phy_addr; |
| 60 | uint8_t phy_type; |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 61 | esp_eth_phy_t *phy; |
| 62 | esp_netif_t *eth_netif; |
| 63 | esp_eth_handle_t eth_handle; |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 64 | } lan_if_obj_t; |
| 65 | |
| 66 | const mp_obj_type_t lan_if_type; |
| 67 | STATIC lan_if_obj_t lan_obj = {{&lan_if_type}, ESP_IF_ETH, false, false}; |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 68 | STATIC uint8_t eth_status = 0; |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 69 | |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 70 | static void eth_event_handler(void *arg, esp_event_base_t event_base, |
| 71 | int32_t event_id, void *event_data) { |
| 72 | switch (event_id) { |
| 73 | case ETHERNET_EVENT_CONNECTED: |
| 74 | eth_status = ETH_CONNECTED; |
| 75 | ESP_LOGI("ethernet", "Ethernet Link Up"); |
| 76 | break; |
| 77 | case ETHERNET_EVENT_DISCONNECTED: |
| 78 | eth_status = ETH_DISCONNECTED; |
| 79 | ESP_LOGI("ethernet", "Ethernet Link Down"); |
| 80 | break; |
| 81 | case ETHERNET_EVENT_START: |
| 82 | eth_status = ETH_STARTED; |
| 83 | ESP_LOGI("ethernet", "Ethernet Started"); |
| 84 | break; |
| 85 | case ETHERNET_EVENT_STOP: |
| 86 | eth_status = ETH_STOPPED; |
| 87 | ESP_LOGI("ethernet", "Ethernet Stopped"); |
| 88 | break; |
| 89 | case IP_EVENT_ETH_GOT_IP: |
| 90 | eth_status = ETH_GOT_IP; |
| 91 | ESP_LOGI("ethernet", "Ethernet Got IP"); |
| 92 | break; |
| 93 | default: |
| 94 | break; |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 98 | STATIC mp_obj_t get_lan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 99 | lan_if_obj_t *self = &lan_obj; |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 100 | |
| 101 | if (self->initialized) { |
| 102 | return MP_OBJ_FROM_PTR(&lan_obj); |
| 103 | } |
| 104 | |
robert-hh | 4b52003 | 2022-12-24 16:52:55 +0100 | [diff] [blame] | 105 | enum { ARG_id, ARG_mdc, ARG_mdio, ARG_power, ARG_phy_addr, ARG_phy_type, |
Damien Tournoud | e982c1d | 2023-01-12 21:04:07 -0800 | [diff] [blame] | 106 | ARG_ref_clk_mode, ARG_ref_clk, ARG_spi, ARG_cs, ARG_int }; |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 107 | static const mp_arg_t allowed_args[] = { |
| 108 | { MP_QSTR_id, MP_ARG_OBJ, {.u_obj = mp_const_none} }, |
Damien Tournoud | e982c1d | 2023-01-12 21:04:07 -0800 | [diff] [blame] | 109 | { MP_QSTR_mdc, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, |
| 110 | { MP_QSTR_mdio, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, |
Petr KracĂk | 5801a00 | 2019-02-17 14:02:02 +0100 | [diff] [blame] | 111 | { MP_QSTR_power, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 112 | { MP_QSTR_phy_addr, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT }, |
| 113 | { MP_QSTR_phy_type, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT }, |
robert-hh | 4b52003 | 2022-12-24 16:52:55 +0100 | [diff] [blame] | 114 | #if ESP_IDF_VERSION_MINOR >= 4 |
Damien Tournoud | e982c1d | 2023-01-12 21:04:07 -0800 | [diff] [blame] | 115 | // Dynamic ref_clk configuration available at v4.4 |
robert-hh | 4b52003 | 2022-12-24 16:52:55 +0100 | [diff] [blame] | 116 | { MP_QSTR_ref_clk_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, |
| 117 | { MP_QSTR_ref_clk, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, |
| 118 | #endif |
Damien Tournoud | e982c1d | 2023-01-12 21:04:07 -0800 | [diff] [blame] | 119 | { MP_QSTR_spi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, |
| 120 | { MP_QSTR_cs, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, |
| 121 | { MP_QSTR_int, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 125 | mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 126 | |
| 127 | if (args[ARG_id].u_obj != mp_const_none) { |
| 128 | if (mp_obj_get_int(args[ARG_id].u_obj) != 0) { |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame] | 129 | mp_raise_ValueError(MP_ERROR_TEXT("invalid LAN interface identifier")); |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Damien Tournoud | e982c1d | 2023-01-12 21:04:07 -0800 | [diff] [blame] | 133 | #define GET_PIN(XXX) args[XXX].u_obj == mp_const_none ? -1 : machine_pin_get_id(args[XXX].u_obj); |
| 134 | |
| 135 | self->mdc_pin = GET_PIN(ARG_mdc); |
| 136 | self->mdio_pin = GET_PIN(ARG_mdio); |
| 137 | self->phy_power_pin = GET_PIN(ARG_power); |
| 138 | self->phy_cs_pin = GET_PIN(ARG_cs); |
| 139 | self->phy_int_pin = GET_PIN(ARG_int); |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 140 | |
| 141 | if (args[ARG_phy_addr].u_int < 0x00 || args[ARG_phy_addr].u_int > 0x1f) { |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame] | 142 | mp_raise_ValueError(MP_ERROR_TEXT("invalid phy address")); |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 143 | } |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 144 | self->phy_addr = args[ARG_phy_addr].u_int; |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 145 | |
robert-hh | efb4bd3 | 2022-12-24 09:37:02 +0100 | [diff] [blame] | 146 | if (args[ARG_phy_type].u_int != PHY_LAN8710 && |
| 147 | args[ARG_phy_type].u_int != PHY_LAN8720 && |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 148 | args[ARG_phy_type].u_int != PHY_IP101 && |
| 149 | args[ARG_phy_type].u_int != PHY_RTL8201 && |
| 150 | #if ESP_IDF_VERSION_MINOR >= 3 // KSZ8041 is new in ESP-IDF v4.3 |
| 151 | args[ARG_phy_type].u_int != PHY_KSZ8041 && |
| 152 | #endif |
Damien Tournoud | e982c1d | 2023-01-12 21:04:07 -0800 | [diff] [blame] | 153 | #if CONFIG_ETH_USE_SPI_ETHERNET |
| 154 | #if CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL |
| 155 | args[ARG_phy_type].u_int != PHY_KSZ8851SNL && |
| 156 | #endif |
| 157 | #if CONFIG_ETH_SPI_ETHERNET_DM9051 |
| 158 | args[ARG_phy_type].u_int != PHY_DM9051 && |
| 159 | #endif |
| 160 | #if CONFIG_ETH_SPI_ETHERNET_W5500 |
| 161 | args[ARG_phy_type].u_int != PHY_W5500 && |
| 162 | #endif |
| 163 | #endif |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 164 | args[ARG_phy_type].u_int != PHY_DP83848) { |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame] | 165 | mp_raise_ValueError(MP_ERROR_TEXT("invalid phy type")); |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 168 | eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); |
Damien Tournoud | e982c1d | 2023-01-12 21:04:07 -0800 | [diff] [blame] | 169 | esp_eth_mac_t *mac = NULL; |
| 170 | |
robert-hh | 4b52003 | 2022-12-24 16:52:55 +0100 | [diff] [blame] | 171 | // Dynamic ref_clk configuration available at v4.4 |
| 172 | #if ESP_IDF_VERSION_MINOR >= 4 |
| 173 | if (args[ARG_ref_clk_mode].u_int != -1) { |
| 174 | // Map the GPIO_MODE constants to EMAC_CLK constants. |
| 175 | mac_config.clock_config.rmii.clock_mode = |
| 176 | args[ARG_ref_clk_mode].u_int == GPIO_MODE_INPUT ? EMAC_CLK_EXT_IN : EMAC_CLK_OUT; |
| 177 | } |
| 178 | if (args[ARG_ref_clk].u_obj != mp_const_none) { |
| 179 | mac_config.clock_config.rmii.clock_gpio = machine_pin_get_id(args[ARG_ref_clk].u_obj); |
| 180 | } |
| 181 | #endif |
Petr KracĂk | 7d8c71c | 2019-02-17 11:46:23 +0100 | [diff] [blame] | 182 | |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 183 | eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); |
| 184 | phy_config.phy_addr = self->phy_addr; |
| 185 | phy_config.reset_gpio_num = self->phy_power_pin; |
| 186 | self->phy = NULL; |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 187 | |
Damien Tournoud | e982c1d | 2023-01-12 21:04:07 -0800 | [diff] [blame] | 188 | #if CONFIG_ETH_USE_SPI_ETHERNET |
| 189 | spi_device_handle_t spi_handle = NULL; |
| 190 | if (IS_SPI_PHY(args[ARG_phy_type].u_int)) { |
| 191 | spi_device_interface_config_t devcfg = { |
| 192 | .mode = 0, |
| 193 | .clock_speed_hz = MICROPY_PY_NETWORK_LAN_SPI_CLOCK_SPEED_MZ * 1000 * 1000, |
| 194 | .queue_size = 20, |
| 195 | .spics_io_num = self->phy_cs_pin, |
| 196 | }; |
| 197 | switch (args[ARG_phy_type].u_int) { |
| 198 | #if CONFIG_ETH_SPI_ETHERNET_DM9051 |
| 199 | case PHY_DM9051: { |
| 200 | devcfg.command_bits = 1; |
| 201 | devcfg.address_bits = 7; |
| 202 | break; |
| 203 | } |
| 204 | #endif |
| 205 | #if CONFIG_ETH_SPI_ETHERNET_W5500 |
| 206 | case PHY_W5500: { |
| 207 | devcfg.command_bits = 16; |
| 208 | devcfg.address_bits = 8; |
| 209 | break; |
| 210 | } |
| 211 | #endif |
| 212 | } |
| 213 | spi_host_device_t host = machine_hw_spi_get_host(args[ARG_spi].u_obj); |
| 214 | if (spi_bus_add_device(host, &devcfg, &spi_handle) != ESP_OK) { |
| 215 | mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("spi_bus_add_device failed")); |
| 216 | } |
| 217 | } |
| 218 | #endif |
| 219 | |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 220 | switch (args[ARG_phy_type].u_int) { |
Damien Tournoud | e982c1d | 2023-01-12 21:04:07 -0800 | [diff] [blame] | 221 | #if CONFIG_IDF_TARGET_ESP32 |
robert-hh | efb4bd3 | 2022-12-24 09:37:02 +0100 | [diff] [blame] | 222 | case PHY_LAN8710: |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 223 | case PHY_LAN8720: |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 224 | self->phy = esp_eth_phy_new_lan8720(&phy_config); |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 225 | break; |
Kenneth Ryerson | 76fefad | 2020-04-20 09:51:11 -0500 | [diff] [blame] | 226 | case PHY_IP101: |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 227 | self->phy = esp_eth_phy_new_ip101(&phy_config); |
Kenneth Ryerson | 76fefad | 2020-04-20 09:51:11 -0500 | [diff] [blame] | 228 | break; |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 229 | case PHY_RTL8201: |
| 230 | self->phy = esp_eth_phy_new_rtl8201(&phy_config); |
| 231 | break; |
| 232 | case PHY_DP83848: |
| 233 | self->phy = esp_eth_phy_new_dp83848(&phy_config); |
| 234 | break; |
Damien Tournoud | e982c1d | 2023-01-12 21:04:07 -0800 | [diff] [blame] | 235 | #if ESP_IDF_VERSION_MINOR >= 3 // KSZ8041 is new in ESP-IDF v4.3 |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 236 | case PHY_KSZ8041: |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 237 | self->phy = esp_eth_phy_new_ksz8041(&phy_config); |
| 238 | break; |
Damien Tournoud | e982c1d | 2023-01-12 21:04:07 -0800 | [diff] [blame] | 239 | #endif |
| 240 | #endif |
| 241 | #if CONFIG_ETH_USE_SPI_ETHERNET |
| 242 | #if CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL |
| 243 | case PHY_KSZ8851SNL: { |
| 244 | eth_ksz8851snl_config_t chip_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(spi_handle); |
| 245 | chip_config.int_gpio_num = self->phy_int_pin; |
| 246 | mac = esp_eth_mac_new_ksz8851snl(&chip_config, &mac_config); |
| 247 | self->phy = esp_eth_phy_new_ksz8851snl(&phy_config); |
| 248 | break; |
| 249 | } |
| 250 | #endif |
| 251 | #if CONFIG_ETH_SPI_ETHERNET_DM9051 |
| 252 | case PHY_DM9051: { |
| 253 | eth_dm9051_config_t chip_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle); |
| 254 | chip_config.int_gpio_num = self->phy_int_pin; |
| 255 | mac = esp_eth_mac_new_dm9051(&chip_config, &mac_config); |
| 256 | self->phy = esp_eth_phy_new_dm9051(&phy_config); |
| 257 | break; |
| 258 | } |
| 259 | #endif |
| 260 | #if CONFIG_ETH_SPI_ETHERNET_W5500 |
| 261 | case PHY_W5500: { |
| 262 | eth_w5500_config_t chip_config = ETH_W5500_DEFAULT_CONFIG(spi_handle); |
| 263 | chip_config.int_gpio_num = self->phy_int_pin; |
| 264 | mac = esp_eth_mac_new_w5500(&chip_config, &mac_config); |
| 265 | self->phy = esp_eth_phy_new_w5500(&phy_config); |
| 266 | break; |
| 267 | } |
| 268 | #endif |
| 269 | #endif |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Damien Tournoud | e982c1d | 2023-01-12 21:04:07 -0800 | [diff] [blame] | 272 | #if CONFIG_IDF_TARGET_ESP32 |
| 273 | if (!IS_SPI_PHY(args[ARG_phy_type].u_int)) { |
| 274 | if (self->mdc_pin == -1 || self->mdio_pin == -1) { |
| 275 | mp_raise_ValueError(MP_ERROR_TEXT("mdc and mdio must be specified")); |
| 276 | } |
| 277 | mac_config.smi_mdc_gpio_num = self->mdc_pin; |
| 278 | mac_config.smi_mdio_gpio_num = self->mdio_pin; |
| 279 | mac = esp_eth_mac_new_esp32(&mac_config); |
| 280 | } |
| 281 | #endif |
| 282 | |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 283 | if (esp_netif_init() != ESP_OK) { |
| 284 | mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_netif_init failed")); |
Petr KracĂk | 7d8c71c | 2019-02-17 11:46:23 +0100 | [diff] [blame] | 285 | } |
| 286 | |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 287 | esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH(); |
| 288 | self->eth_netif = esp_netif_new(&cfg); |
| 289 | |
| 290 | if (esp_eth_set_default_handlers(self->eth_netif) != ESP_OK) { |
| 291 | mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_eth_set_default_handlers failed (invalid parameter)")); |
| 292 | } |
| 293 | |
| 294 | if (esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, NULL) != ESP_OK) { |
| 295 | mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_event_handler_register failed")); |
| 296 | } |
| 297 | |
| 298 | if (esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, NULL) != ESP_OK) { |
| 299 | mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_event_handler_register failed")); |
| 300 | } |
| 301 | |
| 302 | esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, self->phy); |
| 303 | |
| 304 | esp_err_t esp_err = esp_eth_driver_install(&config, &self->eth_handle); |
| 305 | if (esp_err == ESP_OK) { |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 306 | self->active = false; |
| 307 | self->initialized = true; |
| 308 | } else { |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 309 | if (esp_err == ESP_ERR_INVALID_ARG) { |
| 310 | mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_eth_driver_install failed with invalid argument")); |
| 311 | } else if (esp_err == ESP_ERR_NO_MEM) { |
| 312 | mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_eth_driver_install failed with no memory for driver")); |
| 313 | } else { |
| 314 | mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_eth_driver_install failed")); |
| 315 | } |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 316 | } |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 317 | |
| 318 | if (esp_netif_attach(self->eth_netif, esp_eth_new_netif_glue(self->eth_handle)) != ESP_OK) { |
| 319 | mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_netif_attach failed")); |
| 320 | } |
| 321 | |
| 322 | eth_status = ETH_INITIALIZED; |
| 323 | |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 324 | return MP_OBJ_FROM_PTR(&lan_obj); |
| 325 | } |
| 326 | MP_DEFINE_CONST_FUN_OBJ_KW(get_lan_obj, 0, get_lan); |
| 327 | |
| 328 | STATIC mp_obj_t lan_active(size_t n_args, const mp_obj_t *args) { |
| 329 | lan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]); |
| 330 | |
| 331 | if (n_args > 1) { |
| 332 | if (mp_obj_is_true(args[1])) { |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 333 | self->active = (esp_eth_start(self->eth_handle) == ESP_OK); |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 334 | if (!self->active) { |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame] | 335 | mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("ethernet enable failed")); |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 336 | } |
| 337 | } else { |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 338 | self->active = !(esp_eth_stop(self->eth_handle) == ESP_OK); |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 339 | if (self->active) { |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame] | 340 | mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("ethernet disable failed")); |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | } |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 344 | |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 345 | return mp_obj_new_bool(self->active); |
| 346 | } |
| 347 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(lan_active_obj, 1, 2, lan_active); |
| 348 | |
| 349 | STATIC mp_obj_t lan_status(mp_obj_t self_in) { |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 350 | return MP_OBJ_NEW_SMALL_INT(eth_status); |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 351 | } |
| 352 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(lan_status_obj, lan_status); |
| 353 | |
| 354 | STATIC mp_obj_t lan_isconnected(mp_obj_t self_in) { |
| 355 | lan_if_obj_t *self = MP_OBJ_TO_PTR(self_in); |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 356 | return self->active ? mp_obj_new_bool(self->phy->get_link(self->phy) == ETH_LINK_UP) : mp_const_false; |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 357 | } |
| 358 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(lan_isconnected_obj, lan_isconnected); |
| 359 | |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 360 | STATIC mp_obj_t lan_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { |
| 361 | if (n_args != 1 && kwargs->used != 0) { |
| 362 | mp_raise_TypeError(MP_ERROR_TEXT("either pos or kw args are allowed")); |
| 363 | } |
| 364 | lan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]); |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 365 | |
| 366 | if (kwargs->used != 0) { |
| 367 | |
| 368 | for (size_t i = 0; i < kwargs->alloc; i++) { |
| 369 | if (mp_map_slot_is_filled(kwargs, i)) { |
Damien George | 5adb1fa | 2021-12-10 23:28:46 +1100 | [diff] [blame] | 370 | switch (mp_obj_str_get_qstr(kwargs->table[i].key)) { |
| 371 | case MP_QSTR_mac: { |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 372 | mp_buffer_info_t bufinfo; |
| 373 | mp_get_buffer_raise(kwargs->table[i].value, &bufinfo, MP_BUFFER_READ); |
| 374 | if (bufinfo.len != 6) { |
| 375 | mp_raise_ValueError(MP_ERROR_TEXT("invalid buffer length")); |
| 376 | } |
Damien Tournoud | c7301b8 | 2023-01-12 21:04:07 -0800 | [diff] [blame^] | 377 | if ( |
| 378 | (esp_eth_ioctl(self->eth_handle, ETH_CMD_S_MAC_ADDR, bufinfo.buf) != ESP_OK) || |
| 379 | (esp_netif_set_mac(self->eth_netif, bufinfo.buf) != ESP_OK) |
| 380 | ) { |
| 381 | mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("failed setting MAC address")); |
| 382 | } |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 383 | break; |
| 384 | } |
| 385 | default: |
| 386 | break; |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | return mp_const_none; |
| 391 | } |
| 392 | |
| 393 | if (n_args != 2) { |
| 394 | mp_raise_TypeError(MP_ERROR_TEXT("can query only one param")); |
| 395 | } |
| 396 | |
| 397 | mp_obj_t val = mp_const_none; |
| 398 | |
Damien George | 5adb1fa | 2021-12-10 23:28:46 +1100 | [diff] [blame] | 399 | switch (mp_obj_str_get_qstr(args[1])) { |
| 400 | case MP_QSTR_mac: { |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 401 | uint8_t mac[6]; |
| 402 | esp_eth_ioctl(self->eth_handle, ETH_CMD_G_MAC_ADDR, mac); |
| 403 | return mp_obj_new_bytes(mac, sizeof(mac)); |
| 404 | } |
| 405 | default: |
| 406 | mp_raise_ValueError(MP_ERROR_TEXT("unknown config param")); |
| 407 | } |
| 408 | |
| 409 | return val; |
| 410 | } |
| 411 | STATIC MP_DEFINE_CONST_FUN_OBJ_KW(lan_config_obj, 1, lan_config); |
| 412 | |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 413 | STATIC const mp_rom_map_elem_t lan_if_locals_dict_table[] = { |
| 414 | { MP_ROM_QSTR(MP_QSTR_active), MP_ROM_PTR(&lan_active_obj) }, |
| 415 | { MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&lan_isconnected_obj) }, |
| 416 | { MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&lan_status_obj) }, |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 417 | { MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&lan_config_obj) }, |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 418 | { MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&esp_ifconfig_obj) }, |
| 419 | }; |
| 420 | |
| 421 | STATIC MP_DEFINE_CONST_DICT(lan_if_locals_dict, lan_if_locals_dict_table); |
| 422 | |
Jim Mussared | 662b976 | 2021-07-14 14:38:38 +1000 | [diff] [blame] | 423 | MP_DEFINE_CONST_OBJ_TYPE( |
| 424 | lan_if_type, |
| 425 | MP_QSTR_LAN, |
| 426 | MP_TYPE_FLAG_NONE, |
Jim Mussared | 9dce827 | 2022-06-24 16:27:46 +1000 | [diff] [blame] | 427 | locals_dict, &lan_if_locals_dict |
Jim Mussared | 662b976 | 2021-07-14 14:38:38 +1000 | [diff] [blame] | 428 | ); |
Jim Mussared | 96008ff | 2019-09-13 23:04:13 +1000 | [diff] [blame] | 429 | |
Tobias Eydam | 48437ce | 2021-04-18 21:38:36 +0200 | [diff] [blame] | 430 | #endif |