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