Skip to content

Commit 37a6655

Browse files
committed
fix(eppp): Cleanup the Ethernet transport
1 parent 325296c commit 37a6655

File tree

6 files changed

+98
-31
lines changed

6 files changed

+98
-31
lines changed

components/eppp_link/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,5 @@ idf_component_register(SRCS eppp_link.c ${transport_src} ${netif_src}
2929
PRIV_REQUIRES esp_netif esp_timer esp_eth ${driver_deps})
3030

3131
if(CONFIG_EPPP_LINK_DEVICE_ETH)
32-
idf_component_get_property(ethernet_init espressif__ethernet_init COMPONENT_LIB)
33-
target_link_libraries(${COMPONENT_LIB} PRIVATE ${ethernet_init})
32+
idf_component_optional_requires(PRIVATE ethernet_init espressif__ethernet_init)
3433
endif()

components/eppp_link/README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# ESP PPP Link component (eppp_link)
22

33
The component provides a general purpose connectivity engine between two microcontrollers, one acting as PPP server (slave), the other one as PPP client (host).
4-
This component could be used for extending network using physical serial connection. Applications could vary from providing PRC engine for multiprocessor solutions to serial connection to POSIX machine. This uses a standard PPP protocol to negotiate IP addresses and networking, so standard PPP toolset could be used, e.g. a `pppd` service on linux. Typical application is a WiFi connectivity provider for chips that do not have WiFi
4+
This component could be used for extending network using physical serial connection. Applications could vary from providing PRC engine for multiprocessor solutions to serial connection to POSIX machine. This uses a standard PPP protocol (if enabled) to negotiate IP addresses and networking, so standard PPP toolset could be used, e.g. a `pppd` service on linux. Typical application is a WiFi connectivity provider for chips that do not have WiFi.
5+
Uses simplified TUN network interface by default to enable faster data transfer on non-UART transports.
56

67
## Typical application
78

@@ -19,6 +20,23 @@ brings in the WiFi connectivity from the "SLAVE" microcontroller.
1920
+----------------+ +----------------+
2021
```
2122

23+
## Configuration
24+
25+
### Choose the transport layer
26+
27+
* `CONFIG_EPPP_LINK_UART` -- Use UART transport layer
28+
* `CONFIG_EPPP_LINK_SPI` -- Use SPI transport layer
29+
* `CONFIG_EPPP_LINK_SDIO` -- Use SDIO transport layer
30+
* `CONFIG_EPPP_LINK_ETHERNET` -- Use Ethernet transport
31+
- Note: Ethernet creates it's own task, so calling `eppp_perform()` would not work
32+
- Note: Add dependency to ethernet_init component to use other Ethernet drivers
33+
- Note: You can override functions `eppp_transport_ethernet_deinit()` and `eppp_transport_ethernet_init()` to use your own Ethernet driver
34+
35+
### Choose the network interface
36+
37+
Use PPP netif for UART; Keep the default (TUN) for others
38+
39+
2240
## API
2341

2442
### Client

components/eppp_link/eppp_eth.c

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@
1111
#include "esp_check.h"
1212
#include "esp_event.h"
1313
#include "esp_mac.h"
14+
#include "esp_idf_version.h"
1415
#include "eppp_link.h"
1516
#include "eppp_transport.h"
1617
#include "esp_eth_driver.h"
17-
#include "ethernet_init.h"
1818
#include "esp_eth_spec.h"
1919
#include "eppp_transport_eth.h"
20+
// Use Ethernet Init component if available
21+
// (otherwise use just simple init/deinit with generic MAC/PHY)
22+
#if __has_include("ethernet_init.h")
23+
#define USE_ETHERNET_INIT_COMPONENT
24+
#include "ethernet_init.h"
25+
#endif
2026

2127
typedef struct header {
2228
uint8_t dst[ETH_ADDR_LEN];
@@ -30,6 +36,52 @@ static esp_eth_handle_t *s_eth_handles = NULL;
3036
static uint8_t s_their_mac[ETH_ADDR_LEN];
3137
static uint8_t s_our_mac[ETH_ADDR_LEN];
3238

39+
#ifndef USE_ETHERNET_INIT_COMPONENT
40+
static esp_err_t simple_init_deinit(bool init, struct eppp_config_ethernet_s *config, esp_eth_handle_t *handle_array[])
41+
{
42+
static esp_eth_handle_t handles[1] = { NULL };
43+
static esp_eth_mac_t *mac = NULL;
44+
static esp_eth_phy_t *phy = NULL;
45+
esp_err_t ret = ESP_OK;
46+
if (!init) {
47+
if (handles[0] != NULL) {
48+
esp_eth_driver_uninstall(handles[0]);
49+
handles[0] = NULL;
50+
}
51+
if (mac != NULL) {
52+
mac->del(mac);
53+
mac = NULL;
54+
}
55+
if (phy != NULL) {
56+
phy->del(phy);
57+
phy = NULL;
58+
}
59+
return ESP_OK;
60+
}
61+
62+
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
63+
eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
64+
esp32_emac_config.smi_gpio.mdc_num = config->mdc_io;
65+
esp32_emac_config.smi_gpio.mdio_num = config->mdio_io;
66+
mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
67+
eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
68+
phy_config.phy_addr = config->phy_addr;
69+
phy_config.reset_gpio_num = config->rst_io;
70+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 4, 0)
71+
phy = esp_eth_phy_new_generic(&phy_config);
72+
#else
73+
phy = esp_eth_phy_new_ip101(&phy_config);
74+
#endif
75+
esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
76+
ESP_GOTO_ON_ERROR(esp_eth_driver_install(&eth_config, &handles[0]), err, TAG, "Ethernet driver install failed");
77+
*handle_array = handles;
78+
return ESP_OK;
79+
err:
80+
simple_init_deinit(false, config, handle_array); // call recursively to deinit and free
81+
return ret;
82+
}
83+
#endif // USE_ETHERNET_INIT_COMPONENT
84+
3385
static void event_handler(void *arg, esp_event_base_t event_base,
3486
int32_t event_id, void *event_data)
3587
{
@@ -65,39 +117,25 @@ static esp_err_t receive(esp_eth_handle_t h, uint8_t *buffer, uint32_t len, void
65117
return ESP_FAIL;
66118
}
67119

68-
__attribute__((weak)) esp_err_t eppp_transport_ethernet_init(esp_eth_handle_t *handle_array[])
120+
__attribute__((weak)) esp_err_t eppp_transport_ethernet_init(struct eppp_config_ethernet_s *config, esp_eth_handle_t *handle_array[])
69121
{
122+
#ifdef USE_ETHERNET_INIT_COMPONENT
70123
uint8_t eth_port_cnt = 0;
71124
ESP_RETURN_ON_ERROR(ethernet_init_all(handle_array, &eth_port_cnt), TAG, "Failed to init common eth drivers");
72125
ESP_RETURN_ON_FALSE(eth_port_cnt == 1, ESP_ERR_INVALID_ARG, TAG, "multiple Ethernet devices detected, please init only one");
73126
return ESP_OK;
127+
#else
128+
return simple_init_deinit(true, config, handle_array);
129+
#endif
74130
}
75131

76-
__attribute__((weak)) void eppp_transport_ethernet_deinit(esp_eth_handle_t *handle_array)
132+
__attribute__((weak)) void eppp_transport_ethernet_deinit(esp_eth_handle_t *handle_array[])
77133
{
134+
#ifdef USE_ETHERNET_INIT_COMPONENT
78135
ethernet_deinit_all(s_eth_handles);
79-
}
80-
81-
82-
esp_err_t eppp_transport_init(eppp_config_t *config, esp_netif_t *esp_netif)
83-
{
84-
ESP_RETURN_ON_ERROR(eppp_transport_ethernet_init(&s_eth_handles), TAG, "Failed to initialize Ethernet driver");
85-
ESP_RETURN_ON_ERROR(esp_eth_update_input_path(s_eth_handles[0], receive, esp_netif), TAG, "Failed to set Ethernet Rx callback");
86-
sscanf(CONFIG_EPPP_LINK_ETHERNET_OUR_ADDRESS, "%2" PRIu8 ":%2" PRIu8 ":%2" PRIi8 ":%2" PRIu8 ":%2" PRIu8 ":%2" PRIu8,
87-
&s_our_mac[0], &s_our_mac[1], &s_our_mac[2], &s_our_mac[3], &s_our_mac[4], &s_our_mac[5]);
88-
89-
sscanf(CONFIG_EPPP_LINK_ETHERNET_THEIR_ADDRESS, "%2" PRIu8 ":%2" PRIu8 ":%2" PRIi8 ":%2" PRIu8 ":%2" PRIu8 ":%2" PRIu8,
90-
&s_their_mac[0], &s_their_mac[1], &s_their_mac[2], &s_their_mac[3], &s_their_mac[4], &s_their_mac[5]);
91-
esp_eth_ioctl(s_eth_handles[0], ETH_CMD_S_MAC_ADDR, s_our_mac);
92-
ESP_RETURN_ON_ERROR(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, event_handler, NULL), TAG, "Failed to register Ethernet handlers");
93-
ESP_RETURN_ON_ERROR(esp_eth_start(s_eth_handles[0]), TAG, "Failed to start Ethernet driver");
94-
return ESP_OK;
95-
}
96-
97-
void eppp_transport_deinit(void)
98-
{
99-
esp_eth_stop(s_eth_handles[0]);
100-
eppp_transport_ethernet_deinit(s_eth_handles);
136+
#else
137+
simple_init_deinit(false, NULL, handle_array);
138+
#endif
101139
}
102140

103141
esp_err_t eppp_transport_tx(void *h, void *buffer, size_t len)
@@ -121,7 +159,6 @@ esp_err_t eppp_transport_tx(void *h, void *buffer, size_t len)
121159

122160
static esp_err_t start_driver(esp_netif_t *esp_netif)
123161
{
124-
ESP_RETURN_ON_ERROR(eppp_transport_ethernet_init(&s_eth_handles), TAG, "Failed to initialize Ethernet driver");
125162
ESP_RETURN_ON_ERROR(esp_eth_update_input_path(s_eth_handles[0], receive, esp_netif), TAG, "Failed to set Ethernet Rx callback");
126163
sscanf(CONFIG_EPPP_LINK_ETHERNET_OUR_ADDRESS, "%2" PRIu8 ":%2" PRIu8 ":%2" PRIi8 ":%2" PRIu8 ":%2" PRIu8 ":%2" PRIu8,
127164
&s_our_mac[0], &s_our_mac[1], &s_our_mac[2], &s_our_mac[3], &s_our_mac[4], &s_our_mac[5]);
@@ -153,14 +190,18 @@ static esp_err_t post_attach(esp_netif_t *esp_netif, void *args)
153190

154191
eppp_transport_handle_t eppp_eth_init(struct eppp_config_ethernet_s *config)
155192
{
193+
__attribute__((unused)) esp_err_t ret = ESP_OK;
156194
eppp_transport_handle_t h = calloc(1, sizeof(struct eppp_handle));
157195
ESP_RETURN_ON_FALSE(h, NULL, TAG, "Failed to allocate eppp_handle");
196+
ESP_GOTO_ON_ERROR(eppp_transport_ethernet_init(config, &s_eth_handles), err, TAG, "Failed to init Ethernet transport");
158197
h->base.post_attach = post_attach;
159198
return h;
199+
err:
200+
return NULL;
160201
}
161202

162203
void eppp_eth_deinit(eppp_transport_handle_t h)
163204
{
164205
esp_eth_stop(s_eth_handles[0]);
165-
eppp_transport_ethernet_deinit(s_eth_handles);
206+
eppp_transport_ethernet_deinit(&s_eth_handles);
166207
}

components/eppp_link/examples/host/main/app_main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ void app_main(void)
117117
config.uart.tx_io = 17;
118118
config.uart.rx_io = 18;
119119
// config.uart.baud = CONFIG_EXAMPLE_UART_BAUDRATE;
120+
#elif CONFIG_EPPP_LINK_DEVICE_ETH
121+
config.transport = EPPP_TRANSPORT_ETHERNET;
120122
#else
121123
config.transport = EPPP_TRANSPORT_SDIO;
122124
config.sdio.is_host = true;

components/eppp_link/idf_component.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ description: The component provides a general purpose PPP connectivity, typicall
44
dependencies:
55
idf: '>=5.2'
66
espressif/esp_serial_slave_link: "^1.1.0"
7-
espressif/ethernet_init: '>=0.0.7'

components/eppp_link/include/eppp_link.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545

4646
#define EPPP_DEFAULT_ETH_CONFIG() \
4747
.ethernet = { \
48+
.mdc_io = 23, \
49+
.mdio_io = 18, \
50+
.phy_addr = 1, \
51+
.rst_io= 5, \
4852
}, \
4953

5054
#if CONFIG_EPPP_LINK_DEVICE_SPI
@@ -142,6 +146,10 @@ typedef struct eppp_config_t {
142146
} sdio;
143147

144148
struct eppp_config_ethernet_s {
149+
int mdc_io;
150+
int mdio_io;
151+
int phy_addr;
152+
int rst_io;
145153
} ethernet;
146154

147155
};

0 commit comments

Comments
 (0)