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" |
| 36 | #include "tcpip_adapter.h" |
| 37 | |
| 38 | #include "modnetwork.h" |
| 39 | |
| 40 | typedef struct _lan_if_obj_t { |
| 41 | mp_obj_base_t base; |
| 42 | int if_id; // MUST BE FIRST to match wlan_if_obj_t |
| 43 | bool initialized; |
| 44 | bool active; |
| 45 | uint8_t mdc_pin; |
| 46 | uint8_t mdio_pin; |
| 47 | int8_t phy_power_pin; |
| 48 | uint8_t phy_addr; |
| 49 | uint8_t phy_type; |
| 50 | eth_phy_check_link_func link_func; |
| 51 | eth_phy_power_enable_func power_func; |
| 52 | } lan_if_obj_t; |
| 53 | |
| 54 | const mp_obj_type_t lan_if_type; |
| 55 | STATIC lan_if_obj_t lan_obj = {{&lan_if_type}, ESP_IF_ETH, false, false}; |
| 56 | |
| 57 | STATIC void phy_power_enable(bool enable) { |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 58 | lan_if_obj_t *self = &lan_obj; |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 59 | |
| 60 | if (self->phy_power_pin != -1) { |
| 61 | |
| 62 | if (!enable) { |
| 63 | // Do the PHY-specific power_enable(false) function before powering down |
| 64 | self->power_func(false); |
| 65 | } |
| 66 | |
| 67 | gpio_pad_select_gpio(self->phy_power_pin); |
| 68 | gpio_set_direction(self->phy_power_pin, GPIO_MODE_OUTPUT); |
| 69 | if (enable) { |
| 70 | gpio_set_level(self->phy_power_pin, 1); |
| 71 | } else { |
| 72 | gpio_set_level(self->phy_power_pin, 0); |
| 73 | } |
| 74 | |
| 75 | // Allow the power up/down to take effect, min 300us |
| 76 | vTaskDelay(1); |
| 77 | |
| 78 | if (enable) { |
| 79 | // Run the PHY-specific power on operations now the PHY has power |
| 80 | self->power_func(true); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | STATIC void init_lan_rmii() { |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 86 | lan_if_obj_t *self = &lan_obj; |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 87 | phy_rmii_configure_data_interface_pins(); |
| 88 | phy_rmii_smi_configure_pins(self->mdc_pin, self->mdio_pin); |
| 89 | } |
| 90 | |
| 91 | 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] | 92 | lan_if_obj_t *self = &lan_obj; |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 93 | |
| 94 | if (self->initialized) { |
| 95 | return MP_OBJ_FROM_PTR(&lan_obj); |
| 96 | } |
| 97 | |
Petr Kracík | 7d8c71c | 2019-02-17 11:46:23 +0100 | [diff] [blame] | 98 | 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] | 99 | static const mp_arg_t allowed_args[] = { |
| 100 | { MP_QSTR_id, MP_ARG_OBJ, {.u_obj = mp_const_none} }, |
| 101 | { MP_QSTR_mdc, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ }, |
| 102 | { 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] | 103 | { 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] | 104 | { MP_QSTR_phy_addr, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT }, |
| 105 | { 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] | 106 | { 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] | 107 | }; |
| 108 | |
| 109 | mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 110 | mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 111 | |
| 112 | if (args[ARG_id].u_obj != mp_const_none) { |
| 113 | if (mp_obj_get_int(args[ARG_id].u_obj) != 0) { |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame^] | 114 | mp_raise_ValueError(MP_ERROR_TEXT("invalid LAN interface identifier")); |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | self->mdc_pin = machine_pin_get_id(args[ARG_mdc].u_obj); |
| 119 | self->mdio_pin = machine_pin_get_id(args[ARG_mdio].u_obj); |
| 120 | self->phy_power_pin = args[ARG_power].u_obj == mp_const_none ? -1 : machine_pin_get_id(args[ARG_power].u_obj); |
| 121 | |
| 122 | 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^] | 123 | mp_raise_ValueError(MP_ERROR_TEXT("invalid phy address")); |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | if (args[ARG_phy_type].u_int != PHY_LAN8720 && args[ARG_phy_type].u_int != PHY_TLK110) { |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame^] | 127 | mp_raise_ValueError(MP_ERROR_TEXT("invalid phy type")); |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Petr Kracík | 7d8c71c | 2019-02-17 11:46:23 +0100 | [diff] [blame] | 130 | if (args[ARG_clock_mode].u_int != -1 && |
| 131 | args[ARG_clock_mode].u_int != ETH_CLOCK_GPIO0_IN && |
| 132 | // Disabled due ESP-IDF (see modnetwork.c note) |
| 133 | //args[ARG_clock_mode].u_int != ETH_CLOCK_GPIO0_OUT && |
| 134 | args[ARG_clock_mode].u_int != ETH_CLOCK_GPIO16_OUT && |
| 135 | args[ARG_clock_mode].u_int != ETH_CLOCK_GPIO17_OUT) { |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame^] | 136 | mp_raise_ValueError(MP_ERROR_TEXT("invalid clock mode")); |
Petr Kracík | 7d8c71c | 2019-02-17 11:46:23 +0100 | [diff] [blame] | 137 | } |
| 138 | |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 139 | eth_config_t config; |
| 140 | |
| 141 | switch (args[ARG_phy_type].u_int) { |
| 142 | case PHY_TLK110: |
| 143 | config = phy_tlk110_default_ethernet_config; |
| 144 | break; |
| 145 | case PHY_LAN8720: |
| 146 | config = phy_lan8720_default_ethernet_config; |
| 147 | break; |
| 148 | } |
| 149 | |
| 150 | self->link_func = config.phy_check_link; |
| 151 | |
| 152 | // Replace default power func with our own |
| 153 | self->power_func = config.phy_power_enable; |
| 154 | config.phy_power_enable = phy_power_enable; |
| 155 | |
| 156 | config.phy_addr = args[ARG_phy_addr].u_int; |
| 157 | config.gpio_config = init_lan_rmii; |
| 158 | config.tcpip_input = tcpip_adapter_eth_input; |
| 159 | |
Petr Kracík | 7d8c71c | 2019-02-17 11:46:23 +0100 | [diff] [blame] | 160 | if (args[ARG_clock_mode].u_int != -1) { |
| 161 | config.clock_mode = args[ARG_clock_mode].u_int; |
| 162 | } |
| 163 | |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 164 | if (esp_eth_init(&config) == ESP_OK) { |
| 165 | self->active = false; |
| 166 | self->initialized = true; |
| 167 | } else { |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame^] | 168 | 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] | 169 | } |
| 170 | return MP_OBJ_FROM_PTR(&lan_obj); |
| 171 | } |
| 172 | MP_DEFINE_CONST_FUN_OBJ_KW(get_lan_obj, 0, get_lan); |
| 173 | |
| 174 | STATIC mp_obj_t lan_active(size_t n_args, const mp_obj_t *args) { |
| 175 | lan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]); |
| 176 | |
| 177 | if (n_args > 1) { |
| 178 | if (mp_obj_is_true(args[1])) { |
| 179 | self->active = (esp_eth_enable() == ESP_OK); |
| 180 | if (!self->active) { |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame^] | 181 | mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("ethernet enable failed")); |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 182 | } |
| 183 | } else { |
| 184 | self->active = !(esp_eth_disable() == ESP_OK); |
| 185 | if (self->active) { |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame^] | 186 | mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("ethernet disable failed")); |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | } |
| 190 | return mp_obj_new_bool(self->active); |
| 191 | } |
| 192 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(lan_active_obj, 1, 2, lan_active); |
| 193 | |
| 194 | STATIC mp_obj_t lan_status(mp_obj_t self_in) { |
| 195 | return mp_const_none; |
| 196 | } |
| 197 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(lan_status_obj, lan_status); |
| 198 | |
| 199 | STATIC mp_obj_t lan_isconnected(mp_obj_t self_in) { |
| 200 | lan_if_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 201 | return self->active ? mp_obj_new_bool(self->link_func()) : mp_const_false; |
| 202 | } |
| 203 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(lan_isconnected_obj, lan_isconnected); |
| 204 | |
| 205 | STATIC const mp_rom_map_elem_t lan_if_locals_dict_table[] = { |
| 206 | { MP_ROM_QSTR(MP_QSTR_active), MP_ROM_PTR(&lan_active_obj) }, |
| 207 | { MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&lan_isconnected_obj) }, |
| 208 | { MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&lan_status_obj) }, |
Eric Poulsen | 5635b96 | 2019-08-27 10:11:03 -0700 | [diff] [blame] | 209 | { MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&esp_config_obj) }, |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 210 | { MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&esp_ifconfig_obj) }, |
| 211 | }; |
| 212 | |
| 213 | STATIC MP_DEFINE_CONST_DICT(lan_if_locals_dict, lan_if_locals_dict_table); |
| 214 | |
| 215 | const mp_obj_type_t lan_if_type = { |
| 216 | { &mp_type_type }, |
| 217 | .name = MP_QSTR_LAN, |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 218 | .locals_dict = (mp_obj_dict_t *)&lan_if_locals_dict, |
Eric Poulsen | 29dd6a7 | 2017-09-27 14:59:49 -0700 | [diff] [blame] | 219 | }; |
Jim Mussared | 96008ff | 2019-09-13 23:04:13 +1000 | [diff] [blame] | 220 | |
| 221 | #endif // !MICROPY_ESP_IDF_4 |