|
18 | 18 | #include <stdbool.h>
|
19 | 19 |
|
20 | 20 | #include "esp_eth.h"
|
21 |
| -#if defined(MGOS_ETH_PHY_LAN87x0) |
22 |
| -#include "eth_phy/phy_lan8720.h" |
23 |
| -#define DEFAULT_ETHERNET_PHY_CONFIG phy_lan8720_default_ethernet_config |
24 |
| -#define PHY_MODEL "LAN87x0" |
25 |
| -#elif defined(MGOS_ETH_PHY_TLK110) |
26 |
| -#include "eth_phy/phy_tlk110.h" |
27 |
| -#define DEFAULT_ETHERNET_PHY_CONFIG phy_tlk110_default_ethernet_config |
28 |
| -#define PHY_MODEL "TLK110" |
29 |
| -#else |
30 |
| -#error Unknown/unspecified PHY model |
31 |
| -#endif |
32 |
| -#include "tcpip_adapter.h" |
| 21 | +#include "esp_eth_phy.h" |
| 22 | +#include "esp_event.h" |
| 23 | +#include "esp_netif.h" |
33 | 24 |
|
34 | 25 | #include "lwip/ip_addr.h"
|
35 | 26 |
|
|
42 | 33 | #include "mgos_sys_config.h"
|
43 | 34 | #include "mgos_system.h"
|
44 | 35 |
|
45 |
| -static void phy_device_power_enable_via_gpio(bool enable) { |
46 |
| - if (!enable) { |
47 |
| - /* Do the PHY-specific power_enable(false) function before powering down */ |
48 |
| - DEFAULT_ETHERNET_PHY_CONFIG.phy_power_enable(false); |
49 |
| - } |
50 |
| - |
51 |
| - const int pin = mgos_sys_config_get_eth_phy_pwr_gpio(); |
52 |
| - mgos_gpio_set_mode(pin, MGOS_GPIO_MODE_OUTPUT); |
53 |
| - mgos_gpio_write(pin, enable); |
54 |
| - |
55 |
| - /* Allow the power up/down to take effect, min 300us. */ |
56 |
| - mgos_msleep(1); |
| 36 | +#if MGOS_ETH_PHY_IP101 |
| 37 | +#define PHY_MODEL "IP101" |
| 38 | +#define PHY_CREATE_FUNC esp_eth_phy_new_ip101 |
| 39 | +#elif MGOS_ETH_PHY_RTL8201 |
| 40 | +#define PHY_MODEL "RTL8201" |
| 41 | +#define PHY_CREATE_FUNC esp_eth_phy_new_ip101 |
| 42 | +#elif MGOS_ETH_PHY_LAN87x0 |
| 43 | +#define PHY_MODEL "LAN87x0" |
| 44 | +#define PHY_CREATE_FUNC esp_eth_phy_new_lan8720 |
| 45 | +#elif MGOS_ETH_PHY_DP83848 |
| 46 | +#define PHY_MODEL "DP83848" |
| 47 | +#define PHY_CREATE_FUNC esp_eth_phy_new_dp83848 |
| 48 | +#else |
| 49 | +#error Unknown/unspecified PHY model |
| 50 | +#endif |
57 | 51 |
|
58 |
| - if (enable) { |
59 |
| - /* Run the PHY-specific power on operations now the PHY has power */ |
60 |
| - DEFAULT_ETHERNET_PHY_CONFIG.phy_power_enable(true); |
| 52 | +static void esp32_eth_event_handler(void *ctx, esp_event_base_t ev_base, |
| 53 | + int32_t ev_id, void *ev_data) { |
| 54 | + esp_netif_t *eth_if = (esp_netif_t *) ctx; |
| 55 | + if (ev_base == ETH_EVENT) { |
| 56 | + switch (ev_id) { |
| 57 | + case ETHERNET_EVENT_START: { |
| 58 | + esp_netif_action_start(eth_if, ev_base, ev_id, ev_data); |
| 59 | + break; |
| 60 | + } |
| 61 | + case ETHERNET_EVENT_STOP: { |
| 62 | + esp_netif_action_stop(eth_if, ev_base, ev_id, ev_data); |
| 63 | + break; |
| 64 | + } |
| 65 | + case ETHERNET_EVENT_CONNECTED: { |
| 66 | + esp_netif_action_connected(eth_if, ev_base, ev_id, ev_data); |
| 67 | + mgos_net_dev_event_cb(MGOS_NET_IF_TYPE_ETHERNET, 0, |
| 68 | + MGOS_NET_EV_CONNECTED); |
| 69 | + break; |
| 70 | + } |
| 71 | + case ETHERNET_EVENT_DISCONNECTED: { |
| 72 | + mgos_net_dev_event_cb(MGOS_NET_IF_TYPE_ETHERNET, 0, |
| 73 | + MGOS_NET_EV_DISCONNECTED); |
| 74 | + esp_netif_action_disconnected(eth_if, ev_base, ev_id, ev_data); |
| 75 | + break; |
| 76 | + } |
| 77 | + } |
| 78 | + } else { |
| 79 | + mgos_net_dev_event_cb(MGOS_NET_IF_TYPE_ETHERNET, 0, |
| 80 | + MGOS_NET_EV_IP_ACQUIRED); |
61 | 81 | }
|
62 |
| -} |
63 |
| - |
64 |
| -static void eth_config_pins(void) { |
65 |
| - phy_rmii_configure_data_interface_pins(); |
66 |
| - phy_rmii_smi_configure_pins(mgos_sys_config_get_eth_mdc_gpio(), |
67 |
| - mgos_sys_config_get_eth_mdio_gpio()); |
| 82 | + (void) ctx; |
| 83 | + (void) ev_base; |
| 84 | + (void) ev_data; |
68 | 85 | }
|
69 | 86 |
|
70 | 87 | bool mgos_ethernet_init(void) {
|
71 | 88 | bool res = false;
|
72 | 89 |
|
73 | 90 | if (!mgos_sys_config_get_eth_enable()) {
|
74 | 91 | res = true;
|
75 |
| - goto clean; |
| 92 | + goto out; |
76 | 93 | }
|
77 | 94 |
|
78 |
| - tcpip_adapter_ip_info_t static_ip; |
79 |
| - if (!mgos_eth_get_static_ip_config(&static_ip.ip, &static_ip.netmask, |
80 |
| - &static_ip.gw)) { |
81 |
| - goto clean; |
82 |
| - } |
| 95 | + /* Create MAC */ |
| 96 | + eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); |
| 97 | + mac_config.smi_mdc_gpio_num = mgos_sys_config_get_eth_mdc_gpio(); |
| 98 | + mac_config.smi_mdio_gpio_num = mgos_sys_config_get_eth_mdio_gpio(); |
| 99 | + esp_eth_mac_t *mac = esp_eth_mac_new_esp32_clock_mode( |
| 100 | + &mac_config, (emac_clock_mode_t) mgos_sys_config_get_eth_clk_mode()); |
83 | 101 |
|
84 |
| - eth_config_t config = DEFAULT_ETHERNET_PHY_CONFIG; |
| 102 | + /* Create PHY */ |
| 103 | + eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); |
85 | 104 | const char *phy_model = PHY_MODEL;
|
| 105 | + phy_config.phy_addr = mgos_sys_config_get_eth_phy_addr(); |
| 106 | + if (mgos_sys_config_get_eth_phy_pwr_gpio() != -1) { |
| 107 | + phy_config.reset_gpio_num = mgos_sys_config_get_eth_phy_pwr_gpio(); |
| 108 | + } |
| 109 | + esp_eth_phy_t *phy = PHY_CREATE_FUNC(&phy_config); |
86 | 110 |
|
87 |
| - /* Set the PHY address in the example configuration */ |
88 |
| - config.phy_addr = mgos_sys_config_get_eth_phy_addr(); |
89 |
| - config.clock_mode = mgos_sys_config_get_eth_clk_mode(); |
90 |
| - config.gpio_config = eth_config_pins; |
91 |
| - config.tcpip_input = tcpip_adapter_eth_input; |
| 111 | + esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy); |
92 | 112 |
|
93 |
| - if (mgos_sys_config_get_eth_phy_pwr_gpio() != -1) { |
94 |
| - config.phy_power_enable = phy_device_power_enable_via_gpio; |
| 113 | + esp_eth_handle_t eth_handle = NULL; |
| 114 | + esp_err_t ret = esp_eth_driver_install(ð_config, ð_handle); |
| 115 | + if (ret != ESP_OK) { |
| 116 | + LOG(LL_ERROR, ("Ethernet %s failed: %d", "driver init", ret)); |
| 117 | + return false; |
95 | 118 | }
|
96 | 119 |
|
97 |
| - esp_err_t ret = esp_eth_init(&config); |
| 120 | + esp_netif_config_t eth_if_cfg = ESP_NETIF_DEFAULT_ETH(); |
| 121 | + esp_netif_t *eth_if = esp_netif_new(ð_if_cfg); |
| 122 | + |
| 123 | + ret = esp_netif_attach(eth_if, esp_eth_new_netif_glue(eth_handle)); |
98 | 124 | if (ret != ESP_OK) {
|
99 |
| - LOG(LL_ERROR, ("Ethernet init failed: %d", ret)); |
| 125 | + LOG(LL_ERROR, ("Ethernet %s failed: %d", "if attach", ret)); |
100 | 126 | return false;
|
101 | 127 | }
|
102 | 128 |
|
103 |
| - uint8_t mac[6]; |
104 |
| - esp_eth_get_mac(mac); |
105 |
| - bool is_dhcp = ip4_addr_isany_val(static_ip.ip); |
| 129 | + esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, |
| 130 | + esp32_eth_event_handler, eth_if); |
| 131 | + esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, |
| 132 | + esp32_eth_event_handler, eth_if); |
| 133 | + |
| 134 | + uint8_t mac_addr[6]; |
| 135 | + mac->get_addr(mac, mac_addr); |
| 136 | + |
| 137 | + esp_netif_ip_info_t static_ip = {0}; |
| 138 | + if (!mgos_eth_get_static_ip_config((ip4_addr_t *) &static_ip.ip, |
| 139 | + (ip4_addr_t *) &static_ip.netmask, |
| 140 | + (ip4_addr_t *) &static_ip.gw)) { |
| 141 | + goto out; |
| 142 | + } |
| 143 | + |
| 144 | + bool is_dhcp = |
| 145 | + (static_ip.ip.addr == IPADDR_ANY || static_ip.netmask.addr == IPADDR_ANY); |
106 | 146 |
|
107 | 147 | LOG(LL_INFO,
|
108 |
| - ("ETH: MAC %02x:%02x:%02x:%02x:%02x:%02x; PHY: %s @ %d%s", mac[0], mac[1], |
109 |
| - mac[2], mac[3], mac[4], mac[5], phy_model, |
110 |
| - mgos_sys_config_get_eth_phy_addr(), (is_dhcp ? "; IP: DHCP" : ""))); |
| 148 | + ("ETH: MAC %02x:%02x:%02x:%02x:%02x:%02x; PHY: %s @ %d%s", mac_addr[0], |
| 149 | + mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5], |
| 150 | + phy_model, phy_config.phy_addr, (is_dhcp ? "; IP: DHCP" : ""))); |
111 | 151 | if (!is_dhcp) {
|
112 | 152 | char ips[16], nms[16], gws[16];
|
113 |
| - ip4addr_ntoa_r(&static_ip.ip, ips, sizeof(ips)); |
114 |
| - ip4addr_ntoa_r(&static_ip.netmask, nms, sizeof(nms)); |
115 |
| - ip4addr_ntoa_r(&static_ip.gw, gws, sizeof(gws)); |
| 153 | + ip4addr_ntoa_r((ip4_addr_t *) &static_ip.ip, ips, sizeof(ips)); |
| 154 | + ip4addr_ntoa_r((ip4_addr_t *) &static_ip.netmask, nms, sizeof(nms)); |
| 155 | + ip4addr_ntoa_r((ip4_addr_t *) &static_ip.gw, gws, sizeof(gws)); |
116 | 156 | LOG(LL_INFO, ("ETH: IP %s/%s, GW %s", ips, nms, gws));
|
117 |
| - tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH); |
118 |
| - if (tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_ETH, &static_ip) != ESP_OK) { |
119 |
| - LOG(LL_ERROR, ("ETH: Failed to set ip info")); |
120 |
| - goto clean; |
| 157 | + esp_netif_dhcpc_stop(eth_if); |
| 158 | + if ((ret = esp_netif_set_ip_info(eth_if, &static_ip)) != ESP_OK) { |
| 159 | + LOG(LL_ERROR, ("ETH: Failed to set ip info: %d", ret)); |
| 160 | + goto out; |
121 | 161 | }
|
122 | 162 | }
|
123 | 163 |
|
124 |
| - res = (esp_eth_enable() == ESP_OK); |
| 164 | + ret = esp_eth_start(eth_handle); |
| 165 | + if (ret != ESP_OK) { |
| 166 | + LOG(LL_ERROR, ("Ethernet %s failed: %d", "start", ret)); |
| 167 | + return false; |
| 168 | + } |
| 169 | + |
| 170 | + res = true; |
125 | 171 |
|
126 |
| -clean: |
| 172 | +out: |
127 | 173 | return res;
|
128 | 174 | }
|
129 | 175 |
|
130 | 176 | bool mgos_eth_dev_get_ip_info(int if_instance,
|
131 | 177 | struct mgos_net_ip_info *ip_info) {
|
132 |
| - tcpip_adapter_ip_info_t info; |
133 |
| - if (if_instance != 0 || |
134 |
| - tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &info) != ESP_OK || |
135 |
| - info.ip.addr == 0) { |
| 178 | + esp_netif_ip_info_t info; |
| 179 | + esp_netif_t *netif = esp_netif_get_handle_from_ifkey("ETH_DEF"); |
| 180 | + if ((esp_netif_get_ip_info(netif, &info) != ESP_OK) || info.ip.addr == 0) { |
136 | 181 | return false;
|
137 | 182 | }
|
138 | 183 | ip_info->ip.sin_addr.s_addr = info.ip.addr;
|
|
0 commit comments