Skip to content

Commit ad27414

Browse files
committed
feat(eppp): Added support for UART transport
1 parent a761039 commit ad27414

17 files changed

+706
-4
lines changed

components/eppp_link/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
idf_component_register(SRCS "eppp_link.c"
22
INCLUDE_DIRS "include"
3-
PRIV_REQUIRES esp_netif esp_driver_spi esp_driver_gpio esp_timer)
3+
PRIV_REQUIRES esp_netif esp_driver_spi esp_driver_gpio esp_timer driver)

components/eppp_link/eppp_link.c

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "sdkconfig.h"
99
#include "esp_log.h"
1010
#include "esp_netif.h"
11+
#include "esp_check.h"
1112
#include "esp_event.h"
1213
#include "esp_netif_ppp.h"
1314
#include "eppp_link_types.h"
@@ -17,6 +18,8 @@
1718
#include "driver/spi_slave.h"
1819
#include "driver/gpio.h"
1920
#include "esp_timer.h"
21+
#elif CONFIG_EPPP_LINK_DEVICE_UART
22+
#include "driver/uart.h"
2023
#endif
2124

2225
static const int GOT_IPV4 = BIT0;
@@ -44,7 +47,11 @@ enum eppp_type {
4447

4548
struct eppp_handle {
4649
QueueHandle_t out_queue;
50+
#if CONFIG_EPPP_LINK_DEVICE_SPI
4751
QueueHandle_t ready_semaphore;
52+
#elif CONFIG_EPPP_LINK_DEVICE_UART
53+
QueueHandle_t uart_event_queue;
54+
#endif
4855
esp_netif_t *netif;
4956
enum eppp_type role;
5057
};
@@ -57,7 +64,7 @@ struct packet {
5764

5865
static esp_err_t transmit(void *h, void *buffer, size_t len)
5966
{
60-
#if CONFIG_EXAMPLE_CONNECT_PPP_DEVICE_SPI
67+
#if CONFIG_EPPP_LINK_DEVICE_SPI
6168
#define MAX_PAYLOAD 1600
6269
struct eppp_handle *handle = h;
6370
struct packet buf = { };
@@ -76,6 +83,8 @@ static esp_err_t transmit(void *h, void *buffer, size_t len)
7683
ESP_LOGE(TAG, "Failed to queue packet to slave!");
7784
}
7885
} while (remaining > 0);
86+
#elif CONFIG_EPPP_LINK_DEVICE_UART
87+
uart_write_bytes(UART_NUM_1, buffer, len);
7988
#endif
8089
return ESP_OK;
8190
}
@@ -101,6 +110,7 @@ static esp_netif_t *netif_init(enum eppp_type role)
101110
return NULL;
102111
}
103112
h->role = role;
113+
#if CONFIG_EPPP_LINK_DEVICE_SPI
104114
if (role == EPPP_CLIENT) {
105115
h->ready_semaphore = xSemaphoreCreateBinary();
106116
if (!h->ready_semaphore) {
@@ -110,6 +120,7 @@ static esp_netif_t *netif_init(enum eppp_type role)
110120
return NULL;
111121
}
112122
}
123+
#endif
113124

114125
esp_netif_driver_ifconfig_t driver_cfg = {
115126
.handle = h,
@@ -119,7 +130,7 @@ static esp_netif_t *netif_init(enum eppp_type role)
119130

120131
esp_netif_inherent_config_t base_netif_cfg = ESP_NETIF_INHERENT_DEFAULT_PPP();
121132
char if_key[] = "EPPP0"; // netif key needs to be unique
122-
if_key[sizeof(if_key) - 1] += s_eppp_netif_count++;
133+
if_key[sizeof(if_key) - 2 /* 2 = two chars before the terminator */ ] += s_eppp_netif_count++;
123134
base_netif_cfg.if_key = if_key;
124135
if (role == EPPP_CLIENT) {
125136
base_netif_cfg.if_desc = "pppos_client";
@@ -470,7 +481,56 @@ _Noreturn static void ppp_task(void *args)
470481
}
471482
}
472483
}
473-
#endif // CONFIG_EPPP_LINK_DEVICE_SPI
484+
#elif CONFIG_EPPP_LINK_DEVICE_UART
485+
#define BUF_SIZE (1024)
486+
#define UART_TX_CLIENT_TO_SERVER 10
487+
#define UART_TX_SERVER_TO_CLIENT 11
488+
#define UART_BAUDRATE 4000000
489+
#define UART_QUEUE_SIZE 16
490+
491+
static esp_err_t init_uart(struct eppp_handle *h)
492+
{
493+
uart_config_t uart_config = {};
494+
uart_config.baud_rate = UART_BAUDRATE;
495+
uart_config.data_bits = UART_DATA_8_BITS;
496+
uart_config.parity = UART_PARITY_DISABLE;
497+
uart_config.stop_bits = UART_STOP_BITS_1;
498+
uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
499+
uart_config.source_clk = UART_SCLK_DEFAULT;
500+
501+
ESP_RETURN_ON_ERROR(uart_driver_install(UART_NUM_1, BUF_SIZE, 0, UART_QUEUE_SIZE, &h->uart_event_queue, 0), TAG, "Failed to install UART");
502+
ESP_RETURN_ON_ERROR(uart_param_config(UART_NUM_1, &uart_config), TAG, "Failed to set params");
503+
int tx_io_num = h->role == EPPP_CLIENT ? UART_TX_CLIENT_TO_SERVER : UART_TX_SERVER_TO_CLIENT;
504+
int rx_io_num = h->role == EPPP_CLIENT ? UART_TX_SERVER_TO_CLIENT : UART_TX_CLIENT_TO_SERVER;
505+
ESP_RETURN_ON_ERROR(uart_set_pin(UART_NUM_1, tx_io_num, rx_io_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE), TAG, "Failed to set UART pins");
506+
ESP_RETURN_ON_ERROR(uart_set_rx_timeout(UART_NUM_1, 1), TAG, "Failed to set UART Rx timeout");
507+
return ESP_OK;
508+
}
509+
510+
_Noreturn static void ppp_task(void *args)
511+
{
512+
static uint8_t buffer[BUF_SIZE] = {};
513+
514+
esp_netif_t *netif = args;
515+
struct eppp_handle *h = esp_netif_get_io_driver(netif);
516+
uart_event_t event;
517+
while (1) {
518+
xQueueReceive(h->uart_event_queue, &event, pdMS_TO_TICKS(pdMS_TO_TICKS(100)));
519+
if (event.type == UART_DATA) {
520+
size_t len;
521+
uart_get_buffered_data_len(UART_NUM_1, &len);
522+
if (len) {
523+
len = uart_read_bytes(UART_NUM_1, buffer, BUF_SIZE, 0);
524+
ESP_LOG_BUFFER_HEXDUMP("ppp_uart_recv", buffer, len, ESP_LOG_VERBOSE);
525+
esp_netif_receive(netif, buffer, len, NULL);
526+
}
527+
} else {
528+
ESP_LOGW(TAG, "Received UART event: %d", event.type);
529+
}
530+
}
531+
532+
}
533+
#endif // CONFIG_EPPP_LINK_DEVICE_SPI / UART
474534

475535

476536
static esp_netif_t *default_setup(enum eppp_type role)
@@ -498,6 +558,8 @@ static esp_netif_t *default_setup(enum eppp_type role)
498558
} else {
499559
init_slave(&s_spi_device, netif);
500560
}
561+
#elif CONFIG_EPPP_LINK_DEVICE_UART
562+
init_uart(esp_netif_get_io_driver(netif));
501563
#endif
502564

503565
netif_start(netif);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# The following four lines of boilerplate have to be in your project's CMakeLists
2+
# in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.16)
4+
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/iperf)
5+
6+
7+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
8+
project(pppos_host)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
# Client side demo of ESP-PPP-Link
3+
4+
This is a basic demo of using esp-mqtt library, but connects to the internet using a PPPoS client. To run this example, you would need a PPP server that provides connectivity to the MQTT broker used in this example (by default a public broker accessible on the internet).
5+
6+
If configured, this example could also run a ping session and an iperf console.
7+
8+
9+
The PPP server could be a Linux computer with `pppd` service or an ESP32 acting like a connection gateway with PPPoS server (see the "slave" project).
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
idf_component_register(SRCS app_main.c register_iperf.c
2+
INCLUDE_DIRS ".")
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
menu "Example Configuration"
2+
3+
config EXAMPLE_GLOBAL_DNS
4+
hex "Set global DNS server"
5+
range 0 0xFFFFFFFF
6+
default 0x08080808
7+
help
8+
Global DNS server address.
9+
10+
config EXAMPLE_MQTT
11+
bool "Run mqtt example"
12+
default y
13+
help
14+
Run MQTT client after startup.
15+
16+
config EXAMPLE_BROKER_URL
17+
string "Broker URL"
18+
depends on EXAMPLE_MQTT
19+
default "mqtt://mqtt.eclipseprojects.io"
20+
help
21+
URL of the broker to connect to.
22+
23+
config EXAMPLE_ICMP_PING
24+
bool "Run ping example"
25+
default y
26+
help
27+
Ping configured address after startup.
28+
29+
config EXAMPLE_PING_ADDR
30+
hex "Ping IPv4 address"
31+
depends on EXAMPLE_ICMP_PING
32+
range 0 0xFFFFFFFF
33+
default 0x08080808
34+
help
35+
Address to send ping requests.
36+
37+
config EXAMPLE_IPERF
38+
bool "Run iperf"
39+
default y
40+
help
41+
Init and run iperf console.
42+
43+
endmenu

0 commit comments

Comments
 (0)