blob: 5aaabfa21e26a157c69426303f7e3594dce103ca [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"
36#include "tcpip_adapter.h"
37
38#include "modnetwork.h"
39
40typedef 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
54const mp_obj_type_t lan_if_type;
55STATIC lan_if_obj_t lan_obj = {{&lan_if_type}, ESP_IF_ETH, false, false};
56
57STATIC void phy_power_enable(bool enable) {
Damien George69661f32020-02-27 15:36:53 +110058 lan_if_obj_t *self = &lan_obj;
Eric Poulsen29dd6a72017-09-27 14:59:49 -070059
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
85STATIC void init_lan_rmii() {
Damien George69661f32020-02-27 15:36:53 +110086 lan_if_obj_t *self = &lan_obj;
Eric Poulsen29dd6a72017-09-27 14:59:49 -070087 phy_rmii_configure_data_interface_pins();
88 phy_rmii_smi_configure_pins(self->mdc_pin, self->mdio_pin);
89}
90
91STATIC 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 +110092 lan_if_obj_t *self = &lan_obj;
Eric Poulsen29dd6a72017-09-27 14:59:49 -070093
94 if (self->initialized) {
95 return MP_OBJ_FROM_PTR(&lan_obj);
96 }
97
Petr Kracík7d8c71c2019-02-17 11:46:23 +010098 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 -070099 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ík5801a002019-02-17 14:02:02 +0100103 { MP_QSTR_power, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700104 { 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ík7d8c71c2019-02-17 11:46:23 +0100106 { MP_QSTR_clock_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700107 };
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 Mussareddef76fe2020-03-02 22:35:22 +1100114 mp_raise_ValueError(MP_ERROR_TEXT("invalid LAN interface identifier"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700115 }
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 Mussareddef76fe2020-03-02 22:35:22 +1100123 mp_raise_ValueError(MP_ERROR_TEXT("invalid phy address"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700124 }
125
126 if (args[ARG_phy_type].u_int != PHY_LAN8720 && args[ARG_phy_type].u_int != PHY_TLK110) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100127 mp_raise_ValueError(MP_ERROR_TEXT("invalid phy type"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700128 }
129
Petr Kracík7d8c71c2019-02-17 11:46:23 +0100130 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 Mussareddef76fe2020-03-02 22:35:22 +1100136 mp_raise_ValueError(MP_ERROR_TEXT("invalid clock mode"));
Petr Kracík7d8c71c2019-02-17 11:46:23 +0100137 }
138
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700139 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ík7d8c71c2019-02-17 11:46:23 +0100160 if (args[ARG_clock_mode].u_int != -1) {
161 config.clock_mode = args[ARG_clock_mode].u_int;
162 }
163
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700164 if (esp_eth_init(&config) == ESP_OK) {
165 self->active = false;
166 self->initialized = true;
167 } else {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100168 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_eth_init() failed"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700169 }
170 return MP_OBJ_FROM_PTR(&lan_obj);
171}
172MP_DEFINE_CONST_FUN_OBJ_KW(get_lan_obj, 0, get_lan);
173
174STATIC 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 Mussareddef76fe2020-03-02 22:35:22 +1100181 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("ethernet enable failed"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700182 }
183 } else {
184 self->active = !(esp_eth_disable() == ESP_OK);
185 if (self->active) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100186 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("ethernet disable failed"));
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700187 }
188 }
189 }
190 return mp_obj_new_bool(self->active);
191}
192STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(lan_active_obj, 1, 2, lan_active);
193
194STATIC mp_obj_t lan_status(mp_obj_t self_in) {
195 return mp_const_none;
196}
197STATIC MP_DEFINE_CONST_FUN_OBJ_1(lan_status_obj, lan_status);
198
199STATIC 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}
203STATIC MP_DEFINE_CONST_FUN_OBJ_1(lan_isconnected_obj, lan_isconnected);
204
205STATIC 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 Poulsen5635b962019-08-27 10:11:03 -0700209 { MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&esp_config_obj) },
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700210 { MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&esp_ifconfig_obj) },
211};
212
213STATIC MP_DEFINE_CONST_DICT(lan_if_locals_dict, lan_if_locals_dict_table);
214
215const mp_obj_type_t lan_if_type = {
216 { &mp_type_type },
217 .name = MP_QSTR_LAN,
Damien George69661f32020-02-27 15:36:53 +1100218 .locals_dict = (mp_obj_dict_t *)&lan_if_locals_dict,
Eric Poulsen29dd6a72017-09-27 14:59:49 -0700219};
Jim Mussared96008ff2019-09-13 23:04:13 +1000220
221#endif // !MICROPY_ESP_IDF_4