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> |
| 7 | * |
| 8 | * Based on the ESP IDF example code which is Public Domain / CC0 |
| 9 | * |
| 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | * of this software and associated documentation files (the "Software"), to deal |
| 12 | * in the Software without restriction, including without limitation the rights |
| 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | * copies of the Software, and to permit persons to whom the Software is |
| 15 | * furnished to do so, subject to the following conditions: |
| 16 | * |
| 17 | * The above copyright notice and this permission notice shall be included in |
| 18 | * all copies or substantial portions of the Software. |
| 19 | * |
| 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 26 | * THE SOFTWARE. |
| 27 | */ |
| 28 | |
Jim Mussared | 96008ff | 2019-09-13 23:04:13 +1000 | [diff] [blame] | 29 | #if !MICROPY_ESP_IDF_4 |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 30 | #include "py/runtime.h" |
| 31 | #include "py/mphal.h" |
| 32 | |
| 33 | #include "eth_phy/phy.h" |
| 34 | #include "eth_phy/phy_tlk110.h" |
| 35 | #include "eth_phy/phy_lan8720.h" |
Kenneth Ryerson | 76fefad | 2020-04-20 09:51:11 -0500 | [diff] [blame] | 36 | #include "eth_phy/phy_ip101.h" |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 37 | #include "tcpip_adapter.h" |
| 38 | |
| 39 | #include "modnetwork.h" |
| 40 | |
| 41 | typedef struct _lan_if_obj_t { |
| 42 | mp_obj_base_t base; |
| 43 | int if_id; // MUST BE FIRST to match wlan_if_obj_t |
| 44 | bool initialized; |
| 45 | bool active; |
| 46 | uint8_t mdc_pin; |
| 47 | uint8_t mdio_pin; |
| 48 | int8_t phy_power_pin; |
| 49 | uint8_t phy_addr; |
| 50 | uint8_t phy_type; |
| 51 | eth_phy_check_link_func link_func; |
| 52 | eth_phy_power_enable_func power_func; |
| 53 | } lan_if_obj_t; |
| 54 | |
| 55 | const mp_obj_type_t lan_if_type; |
| 56 | STATIC lan_if_obj_t lan_obj = {{&lan_if_type}, ESP_IF_ETH, false, false}; |
| 57 | |
| 58 | STATIC void phy_power_enable(bool enable) { |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 59 | lan_if_obj_t *self = &lan_obj; |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 60 | |
| 61 | if (self->phy_power_pin != -1) { |
| 62 | |
| 63 | if (!enable) { |
| 64 | // Do the PHY-specific power_enable(false) function before powering down |
| 65 | self->power_func(false); |
| 66 | } |
| 67 | |
| 68 | gpio_pad_select_gpio(self->phy_power_pin); |
| 69 | gpio_set_direction(self->phy_power_pin, GPIO_MODE_OUTPUT); |
| 70 | if (enable) { |
| 71 | gpio_set_level(self->phy_power_pin, 1); |
| 72 | } else { |
| 73 | gpio_set_level(self->phy_power_pin, 0); |
| 74 | } |
| 75 | |
| 76 | // Allow the power up/down to take effect, min 300us |
| 77 | vTaskDelay(1); |
| 78 | |
| 79 | if (enable) { |
| 80 | // Run the PHY-specific power on operations now the PHY has power |
| 81 | self->power_func(true); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | STATIC void init_lan_rmii() { |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 87 | lan_if_obj_t *self = &lan_obj; |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 88 | phy_rmii_configure_data_interface_pins(); |
| 89 | phy_rmii_smi_configure_pins(self->mdc_pin, self->mdio_pin); |
| 90 | } |
| 91 | |
| 92 | 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] | 93 | lan_if_obj_t *self = &lan_obj; |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 94 | |
| 95 | if (self->initialized) { |
| 96 | return MP_OBJ_FROM_PTR(&lan_obj); |
| 97 | } |
| 98 | |
Petr Kracík | 7d8c71c | 2019-02-17 11:46:23 +0100 | [diff] [blame] | 99 | enum { ARG_id, ARG_mdc, ARG_mdio, ARG_power, ARG_phy_addr, ARG_phy_type, ARG_clock_mode }; |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 100 | static const mp_arg_t allowed_args[] = { |
| 101 | { MP_QSTR_id, MP_ARG_OBJ, {.u_obj = mp_const_none} }, |
| 102 | { MP_QSTR_mdc, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ }, |
| 103 | { MP_QSTR_mdio, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ }, |
Petr Kracík | 5801a00 | 2019-02-17 14:02:02 +0100 | [diff] [blame] | 104 | { 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] | 105 | { MP_QSTR_phy_addr, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT }, |
| 106 | { MP_QSTR_phy_type, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT }, |
Petr Kracík | 7d8c71c | 2019-02-17 11:46:23 +0100 | [diff] [blame] | 107 | { MP_QSTR_clock_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 111 | mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 112 | |
| 113 | if (args[ARG_id].u_obj != mp_const_none) { |
| 114 | if (mp_obj_get_int(args[ARG_id].u_obj) != 0) { |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame] | 115 | mp_raise_ValueError(MP_ERROR_TEXT("invalid LAN interface identifier")); |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | self->mdc_pin = machine_pin_get_id(args[ARG_mdc].u_obj); |
| 120 | self->mdio_pin = machine_pin_get_id(args[ARG_mdio].u_obj); |
| 121 | self->phy_power_pin = args[ARG_power].u_obj == mp_const_none ? -1 : machine_pin_get_id(args[ARG_power].u_obj); |
| 122 | |
| 123 | if (args[ARG_phy_addr].u_int < 0x00 || args[ARG_phy_addr].u_int > 0x1f) { |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame] | 124 | mp_raise_ValueError(MP_ERROR_TEXT("invalid phy address")); |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Kenneth Ryerson | 76fefad | 2020-04-20 09:51:11 -0500 | [diff] [blame] | 127 | if (args[ARG_phy_type].u_int != PHY_LAN8720 && |
| 128 | args[ARG_phy_type].u_int != PHY_TLK110 && |
| 129 | args[ARG_phy_type].u_int != PHY_IP101) { |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame] | 130 | mp_raise_ValueError(MP_ERROR_TEXT("invalid phy type")); |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Petr Kracík | 7d8c71c | 2019-02-17 11:46:23 +0100 | [diff] [blame] | 133 | if (args[ARG_clock_mode].u_int != -1 && |
| 134 | args[ARG_clock_mode].u_int != ETH_CLOCK_GPIO0_IN && |
| 135 | // Disabled due ESP-IDF (see modnetwork.c note) |
stijn | 84fa331 | 2020-04-16 09:13:57 +0200 | [diff] [blame] | 136 | // args[ARG_clock_mode].u_int != ETH_CLOCK_GPIO0_OUT && |
Petr Kracík | 7d8c71c | 2019-02-17 11:46:23 +0100 | [diff] [blame] | 137 | args[ARG_clock_mode].u_int != ETH_CLOCK_GPIO16_OUT && |
| 138 | args[ARG_clock_mode].u_int != ETH_CLOCK_GPIO17_OUT) { |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame] | 139 | mp_raise_ValueError(MP_ERROR_TEXT("invalid clock mode")); |
Petr Kracík | 7d8c71c | 2019-02-17 11:46:23 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 142 | eth_config_t config; |
| 143 | |
| 144 | switch (args[ARG_phy_type].u_int) { |
| 145 | case PHY_TLK110: |
| 146 | config = phy_tlk110_default_ethernet_config; |
| 147 | break; |
| 148 | case PHY_LAN8720: |
| 149 | config = phy_lan8720_default_ethernet_config; |
| 150 | break; |
Kenneth Ryerson | 76fefad | 2020-04-20 09:51:11 -0500 | [diff] [blame] | 151 | case PHY_IP101: |
| 152 | config = phy_ip101_default_ethernet_config; |
| 153 | break; |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | self->link_func = config.phy_check_link; |
| 157 | |
| 158 | // Replace default power func with our own |
| 159 | self->power_func = config.phy_power_enable; |
| 160 | config.phy_power_enable = phy_power_enable; |
| 161 | |
| 162 | config.phy_addr = args[ARG_phy_addr].u_int; |
| 163 | config.gpio_config = init_lan_rmii; |
| 164 | config.tcpip_input = tcpip_adapter_eth_input; |
| 165 | |
Petr Kracík | 7d8c71c | 2019-02-17 11:46:23 +0100 | [diff] [blame] | 166 | if (args[ARG_clock_mode].u_int != -1) { |
| 167 | config.clock_mode = args[ARG_clock_mode].u_int; |
| 168 | } |
| 169 | |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 170 | if (esp_eth_init(&config) == ESP_OK) { |
| 171 | self->active = false; |
| 172 | self->initialized = true; |
| 173 | } else { |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame] | 174 | mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_eth_init() failed")); |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 175 | } |
| 176 | return MP_OBJ_FROM_PTR(&lan_obj); |
| 177 | } |
| 178 | MP_DEFINE_CONST_FUN_OBJ_KW(get_lan_obj, 0, get_lan); |
| 179 | |
| 180 | STATIC mp_obj_t lan_active(size_t n_args, const mp_obj_t *args) { |
| 181 | lan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]); |
| 182 | |
| 183 | if (n_args > 1) { |
| 184 | if (mp_obj_is_true(args[1])) { |
| 185 | self->active = (esp_eth_enable() == ESP_OK); |
| 186 | if (!self->active) { |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame] | 187 | mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("ethernet enable failed")); |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 188 | } |
| 189 | } else { |
| 190 | self->active = !(esp_eth_disable() == ESP_OK); |
| 191 | if (self->active) { |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame] | 192 | mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("ethernet disable failed")); |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | } |
| 196 | return mp_obj_new_bool(self->active); |
| 197 | } |
| 198 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(lan_active_obj, 1, 2, lan_active); |
| 199 | |
| 200 | STATIC mp_obj_t lan_status(mp_obj_t self_in) { |
| 201 | return mp_const_none; |
| 202 | } |
| 203 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(lan_status_obj, lan_status); |
| 204 | |
| 205 | STATIC mp_obj_t lan_isconnected(mp_obj_t self_in) { |
| 206 | lan_if_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 207 | return self->active ? mp_obj_new_bool(self->link_func()) : mp_const_false; |
| 208 | } |
| 209 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(lan_isconnected_obj, lan_isconnected); |
| 210 | |
| 211 | STATIC const mp_rom_map_elem_t lan_if_locals_dict_table[] = { |
| 212 | { MP_ROM_QSTR(MP_QSTR_active), MP_ROM_PTR(&lan_active_obj) }, |
| 213 | { MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&lan_isconnected_obj) }, |
| 214 | { MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&lan_status_obj) }, |
Eric Poulsen | 5635b96 | 2019-08-27 10:11:03 -0700 | [diff] [blame] | 215 | { MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&esp_config_obj) }, |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 216 | { MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&esp_ifconfig_obj) }, |
| 217 | }; |
| 218 | |
| 219 | STATIC MP_DEFINE_CONST_DICT(lan_if_locals_dict, lan_if_locals_dict_table); |
| 220 | |
| 221 | const mp_obj_type_t lan_if_type = { |
| 222 | { &mp_type_type }, |
| 223 | .name = MP_QSTR_LAN, |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 224 | .locals_dict = (mp_obj_dict_t *)&lan_if_locals_dict, |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 225 | }; |
Jim Mussared | 96008ff | 2019-09-13 23:04:13 +1000 | [diff] [blame] | 226 | |
| 227 | #endif // !MICROPY_ESP_IDF_4 |