network_lan.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #include "py/runtime.h"
  29. #include "py/mphal.h"
  30. #include "eth_phy/phy.h"
  31. #include "eth_phy/phy_tlk110.h"
  32. #include "eth_phy/phy_lan8720.h"
  33. #include "tcpip_adapter.h"
  34. #include "modnetwork.h"
  35. typedef struct _lan_if_obj_t {
  36. mp_obj_base_t base;
  37. int if_id; // MUST BE FIRST to match wlan_if_obj_t
  38. bool initialized;
  39. bool active;
  40. uint8_t mdc_pin;
  41. uint8_t mdio_pin;
  42. int8_t phy_power_pin;
  43. uint8_t phy_addr;
  44. uint8_t phy_type;
  45. eth_phy_check_link_func link_func;
  46. eth_phy_power_enable_func power_func;
  47. } lan_if_obj_t;
  48. const mp_obj_type_t lan_if_type;
  49. STATIC lan_if_obj_t lan_obj = {{&lan_if_type}, ESP_IF_ETH, false, false};
  50. STATIC void phy_power_enable(bool enable) {
  51. lan_if_obj_t* self = &lan_obj;
  52. if (self->phy_power_pin != -1) {
  53. if (!enable) {
  54. // Do the PHY-specific power_enable(false) function before powering down
  55. self->power_func(false);
  56. }
  57. gpio_pad_select_gpio(self->phy_power_pin);
  58. gpio_set_direction(self->phy_power_pin, GPIO_MODE_OUTPUT);
  59. if (enable) {
  60. gpio_set_level(self->phy_power_pin, 1);
  61. } else {
  62. gpio_set_level(self->phy_power_pin, 0);
  63. }
  64. // Allow the power up/down to take effect, min 300us
  65. vTaskDelay(1);
  66. if (enable) {
  67. // Run the PHY-specific power on operations now the PHY has power
  68. self->power_func(true);
  69. }
  70. }
  71. }
  72. STATIC void init_lan_rmii() {
  73. lan_if_obj_t* self = &lan_obj;
  74. phy_rmii_configure_data_interface_pins();
  75. phy_rmii_smi_configure_pins(self->mdc_pin, self->mdio_pin);
  76. }
  77. STATIC mp_obj_t get_lan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  78. lan_if_obj_t* self = &lan_obj;
  79. if (self->initialized) {
  80. return MP_OBJ_FROM_PTR(&lan_obj);
  81. }
  82. enum { ARG_id, ARG_mdc, ARG_mdio, ARG_power, ARG_phy_addr, ARG_phy_type };
  83. static const mp_arg_t allowed_args[] = {
  84. { MP_QSTR_id, MP_ARG_OBJ, {.u_obj = mp_const_none} },
  85. { MP_QSTR_mdc, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
  86. { MP_QSTR_mdio, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
  87. { MP_QSTR_power, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
  88. { MP_QSTR_phy_addr, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
  89. { MP_QSTR_phy_type, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
  90. };
  91. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  92. mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  93. if (args[ARG_id].u_obj != mp_const_none) {
  94. if (mp_obj_get_int(args[ARG_id].u_obj) != 0) {
  95. mp_raise_ValueError("invalid LAN interface identifier");
  96. }
  97. }
  98. self->mdc_pin = machine_pin_get_id(args[ARG_mdc].u_obj);
  99. self->mdio_pin = machine_pin_get_id(args[ARG_mdio].u_obj);
  100. self->phy_power_pin = args[ARG_power].u_obj == mp_const_none ? -1 : machine_pin_get_id(args[ARG_power].u_obj);
  101. if (args[ARG_phy_addr].u_int < 0x00 || args[ARG_phy_addr].u_int > 0x1f) {
  102. mp_raise_ValueError("invalid phy address");
  103. }
  104. if (args[ARG_phy_type].u_int != PHY_LAN8720 && args[ARG_phy_type].u_int != PHY_TLK110) {
  105. mp_raise_ValueError("invalid phy type");
  106. }
  107. eth_config_t config;
  108. switch (args[ARG_phy_type].u_int) {
  109. case PHY_TLK110:
  110. config = phy_tlk110_default_ethernet_config;
  111. break;
  112. case PHY_LAN8720:
  113. config = phy_lan8720_default_ethernet_config;
  114. break;
  115. }
  116. self->link_func = config.phy_check_link;
  117. // Replace default power func with our own
  118. self->power_func = config.phy_power_enable;
  119. config.phy_power_enable = phy_power_enable;
  120. config.phy_addr = args[ARG_phy_addr].u_int;
  121. config.gpio_config = init_lan_rmii;
  122. config.tcpip_input = tcpip_adapter_eth_input;
  123. if (esp_eth_init(&config) == ESP_OK) {
  124. self->active = false;
  125. self->initialized = true;
  126. } else {
  127. mp_raise_msg(&mp_type_OSError, "esp_eth_init() failed");
  128. }
  129. return MP_OBJ_FROM_PTR(&lan_obj);
  130. }
  131. MP_DEFINE_CONST_FUN_OBJ_KW(get_lan_obj, 0, get_lan);
  132. STATIC mp_obj_t lan_active(size_t n_args, const mp_obj_t *args) {
  133. lan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
  134. if (n_args > 1) {
  135. if (mp_obj_is_true(args[1])) {
  136. self->active = (esp_eth_enable() == ESP_OK);
  137. if (!self->active) {
  138. mp_raise_msg(&mp_type_OSError, "ethernet enable failed");
  139. }
  140. } else {
  141. self->active = !(esp_eth_disable() == ESP_OK);
  142. if (self->active) {
  143. mp_raise_msg(&mp_type_OSError, "ethernet disable failed");
  144. }
  145. }
  146. }
  147. return mp_obj_new_bool(self->active);
  148. }
  149. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(lan_active_obj, 1, 2, lan_active);
  150. STATIC mp_obj_t lan_status(mp_obj_t self_in) {
  151. return mp_const_none;
  152. }
  153. STATIC MP_DEFINE_CONST_FUN_OBJ_1(lan_status_obj, lan_status);
  154. STATIC mp_obj_t lan_isconnected(mp_obj_t self_in) {
  155. lan_if_obj_t *self = MP_OBJ_TO_PTR(self_in);
  156. return self->active ? mp_obj_new_bool(self->link_func()) : mp_const_false;
  157. }
  158. STATIC MP_DEFINE_CONST_FUN_OBJ_1(lan_isconnected_obj, lan_isconnected);
  159. STATIC const mp_rom_map_elem_t lan_if_locals_dict_table[] = {
  160. { MP_ROM_QSTR(MP_QSTR_active), MP_ROM_PTR(&lan_active_obj) },
  161. { MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&lan_isconnected_obj) },
  162. { MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&lan_status_obj) },
  163. { MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&esp_ifconfig_obj) },
  164. };
  165. STATIC MP_DEFINE_CONST_DICT(lan_if_locals_dict, lan_if_locals_dict_table);
  166. const mp_obj_type_t lan_if_type = {
  167. { &mp_type_type },
  168. .name = MP_QSTR_LAN,
  169. .locals_dict = (mp_obj_dict_t*)&lan_if_locals_dict,
  170. };