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