Skip to content

Commit 2b78d7e

Browse files
committed
ESP32: Update to ESP-IDF 4.2
cesanta/mongoose-os#565
1 parent 76d87db commit 2b78d7e

File tree

3 files changed

+126
-77
lines changed

3 files changed

+126
-77
lines changed

.clang-format

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
BasedOnStyle: Google
2+
AllowShortFunctionsOnASingleLine: false
3+
SpaceAfterCStyleCast: true
4+
PointerBindsToType: false
5+
DerivePointerBinding: false
6+
IncludeBlocks: Preserve

mos_esp32.yml

+7-9
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ sources:
22
- src/esp32
33

44
cdefs:
5-
# Select the PHY model. Only one at a time, not both.
5+
# Select the PHY model. Only one at a time.
6+
MGOS_ETH_PHY_IP101: 0
7+
MGOS_ETH_PHY_RTL8201: 0
68
MGOS_ETH_PHY_LAN87x0: 1
7-
# MGOS_ETH_PHY_TLK110: 1
9+
MGOS_ETH_PHY_DP83848: 0
810

911
config_schema:
1012
- ["eth.clk_mode", "i", 0, {title: "50 MHz clock source: 0 in <- GPIO0, 1 out -> GPIO0, 2 out -> GPIO16, 3 out -> GPIO17 (inverted)"}]
@@ -13,10 +15,6 @@ config_schema:
1315
- ["eth.phy_pwr_gpio", "i", -1, {title: "GPIO to use for PHY PWR control signal"}]
1416

1517
build_vars:
16-
ESP_IDF_SDKCONFIG_OPTS: >
17-
${build_vars.ESP_IDF_SDKCONFIG_OPTS}
18-
CONFIG_PPP_SUPPORT=y
19-
CONFIG_PPP_PAP_SUPPORT=y
20-
CONFIG_PPP_CHAP_SUPPORT=y
21-
CONFIG_PPP_MSCHAP_SUPPORT=y
22-
CONFIG_PPP_MPPE_SUPPORT=y
18+
ESP_IDF_EXTRA_COMPONENTS: >
19+
${build_vars.ESP_IDF_EXTRA_COMPONENTS}
20+
esp_eth

src/esp32/esp32_eth.c

+113-68
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,9 @@
1818
#include <stdbool.h>
1919

2020
#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"
3324

3425
#include "lwip/ip_addr.h"
3526

@@ -42,97 +33,151 @@
4233
#include "mgos_sys_config.h"
4334
#include "mgos_system.h"
4435

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
5751

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);
6181
}
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;
6885
}
6986

7087
bool mgos_ethernet_init(void) {
7188
bool res = false;
7289

7390
if (!mgos_sys_config_get_eth_enable()) {
7491
res = true;
75-
goto clean;
92+
goto out;
7693
}
7794

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());
83101

84-
eth_config_t config = DEFAULT_ETHERNET_PHY_CONFIG;
102+
/* Create PHY */
103+
eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
85104
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);
86110

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);
92112

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(&eth_config, &eth_handle);
115+
if (ret != ESP_OK) {
116+
LOG(LL_ERROR, ("Ethernet %s failed: %d", "driver init", ret));
117+
return false;
95118
}
96119

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(&eth_if_cfg);
122+
123+
ret = esp_netif_attach(eth_if, esp_eth_new_netif_glue(eth_handle));
98124
if (ret != ESP_OK) {
99-
LOG(LL_ERROR, ("Ethernet init failed: %d", ret));
125+
LOG(LL_ERROR, ("Ethernet %s failed: %d", "if attach", ret));
100126
return false;
101127
}
102128

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);
106146

107147
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" : "")));
111151
if (!is_dhcp) {
112152
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));
116156
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;
121161
}
122162
}
123163

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;
125171

126-
clean:
172+
out:
127173
return res;
128174
}
129175

130176
bool mgos_eth_dev_get_ip_info(int if_instance,
131177
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) {
136181
return false;
137182
}
138183
ip_info->ip.sin_addr.s_addr = info.ip.addr;

0 commit comments

Comments
 (0)