blob: 7a4ad49e01cebb7c8135d794d3ccd9804a2698f3 [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>
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 Mussared96008ff2019-09-13 23:04:13 +100029#if !MICROPY_ESP_IDF_4
Eric Poulsen29dd6a72017-09-27 14:59:49 -070030#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 Ryerson76fefad2020-04-20 09:51:11 -050036#include "eth_phy/phy_ip101.h"
Eric Poulsen29dd6a72017-09-27 14:59:49 -070037#include "tcpip_adapter.h"
38
39#include "modnetwork.h"
40
41typedef 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
55const mp_obj_type_t lan_if_type;
56STATIC lan_if_obj_t lan_obj = {{&lan_if_type}, ESP_IF_ETH, false, false};
57
58STATIC void phy_power_enable(bool enable) {
Damien George69661f32020-02-27 15:36:53 +110059 lan_if_obj_t *self = &lan_obj;
Eric Poulsen29dd6a72017-09-27 14:59:49 -070060
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
86STATIC void init_lan_rmii() {
Damien George69661f32020-02-27 15:36:53 +110087 lan_if_obj_t *self = &lan_obj;
Eric Poulsen29dd6a72017-09-27 14:59:49 -070088 phy_rmii_configure_data_interface_pins();
89 phy_rmii_smi_configure_pins(self->mdc_pin, self->mdio_pin);
90}
91
92STATIC 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 +110093 lan_if_obj_t *self = &lan_obj;
Eric Poulsen29dd6a72017-09-27 14:59:49 -070094
95 if (self->initialized) {
96 return MP_OBJ_FROM_PTR(&lan_obj);
97 }
98
Petr Kracík7d8c71c2019-02-17 11:46:23 +010099 enum { ARG_id, ARG_mdc, ARG_mdio, ARG_power, ARG_phy_addr, ARG_phy_type, ARG_clock_mode };
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700100 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ík5801a002019-02-17 14:02:02 +0100104 { MP_QSTR_power, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700105 { 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ík7d8c71c2019-02-17 11:46:23 +0100107 { MP_QSTR_clock_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700108 };
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 Mussareddef76fe2020-03-02 22:35:22 +1100115 mp_raise_ValueError(MP_ERROR_TEXT("invalid LAN interface identifier"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700116 }
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 Mussareddef76fe2020-03-02 22:35:22 +1100124 mp_raise_ValueError(MP_ERROR_TEXT("invalid phy address"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700125 }
126
Kenneth Ryerson76fefad2020-04-20 09:51:11 -0500127 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 Mussareddef76fe2020-03-02 22:35:22 +1100130 mp_raise_ValueError(MP_ERROR_TEXT("invalid phy type"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700131 }
132
Petr Kracík7d8c71c2019-02-17 11:46:23 +0100133 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)
stijn84fa3312020-04-16 09:13:57 +0200136 // args[ARG_clock_mode].u_int != ETH_CLOCK_GPIO0_OUT &&
Petr Kracík7d8c71c2019-02-17 11:46:23 +0100137 args[ARG_clock_mode].u_int != ETH_CLOCK_GPIO16_OUT &&
138 args[ARG_clock_mode].u_int != ETH_CLOCK_GPIO17_OUT) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100139 mp_raise_ValueError(MP_ERROR_TEXT("invalid clock mode"));
Petr Kracík7d8c71c2019-02-17 11:46:23 +0100140 }
141
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700142 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 Ryerson76fefad2020-04-20 09:51:11 -0500151 case PHY_IP101:
152 config = phy_ip101_default_ethernet_config;
153 break;
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700154 }
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ík7d8c71c2019-02-17 11:46:23 +0100166 if (args[ARG_clock_mode].u_int != -1) {
167 config.clock_mode = args[ARG_clock_mode].u_int;
168 }
169
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700170 if (esp_eth_init(&config) == ESP_OK) {
171 self->active = false;
172 self->initialized = true;
173 } else {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100174 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_eth_init() failed"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700175 }
176 return MP_OBJ_FROM_PTR(&lan_obj);
177}
178MP_DEFINE_CONST_FUN_OBJ_KW(get_lan_obj, 0, get_lan);
179
180STATIC 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 Mussareddef76fe2020-03-02 22:35:22 +1100187 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("ethernet enable failed"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700188 }
189 } else {
190 self->active = !(esp_eth_disable() == ESP_OK);
191 if (self->active) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100192 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("ethernet disable failed"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700193 }
194 }
195 }
196 return mp_obj_new_bool(self->active);
197}
198STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(lan_active_obj, 1, 2, lan_active);
199
200STATIC mp_obj_t lan_status(mp_obj_t self_in) {
201 return mp_const_none;
202}
203STATIC MP_DEFINE_CONST_FUN_OBJ_1(lan_status_obj, lan_status);
204
205STATIC 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}
209STATIC MP_DEFINE_CONST_FUN_OBJ_1(lan_isconnected_obj, lan_isconnected);
210
211STATIC 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 Poulsen5635b962019-08-27 10:11:03 -0700215 { MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&esp_config_obj) },
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700216 { MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&esp_ifconfig_obj) },
217};
218
219STATIC MP_DEFINE_CONST_DICT(lan_if_locals_dict, lan_if_locals_dict_table);
220
221const mp_obj_type_t lan_if_type = {
222 { &mp_type_type },
223 .name = MP_QSTR_LAN,
Damien George69661f32020-02-27 15:36:53 +1100224 .locals_dict = (mp_obj_dict_t *)&lan_if_locals_dict,
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700225};
Jim Mussared96008ff2019-09-13 23:04:13 +1000226
227#endif // !MICROPY_ESP_IDF_4