Skip to content

Commit

Permalink
fix(eppp_link): Support for UART transport
Browse files Browse the repository at this point in the history
  • Loading branch information
david-cermak committed Dec 21, 2023
1 parent 2984e71 commit c775915
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
2 changes: 1 addition & 1 deletion components/eppp_link/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
idf_component_register(SRCS "eppp_link.c"
INCLUDE_DIRS "include"
PRIV_REQUIRES esp_netif esp_driver_spi esp_driver_gpio esp_timer)
PRIV_REQUIRES esp_netif esp_driver_spi esp_driver_gpio esp_timer driver)
64 changes: 63 additions & 1 deletion components/eppp_link/eppp_link.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "sdkconfig.h"
#include "esp_log.h"
#include "esp_netif.h"
#include "esp_check.h"
#include "esp_event.h"
#include "esp_netif_ppp.h"
#include "eppp_link_types.h"
Expand All @@ -17,6 +18,8 @@
#include "driver/spi_slave.h"
#include "driver/gpio.h"
#include "esp_timer.h"
#elif CONFIG_EPPP_LINK_DEVICE_UART
#include "driver/uart.h"
#endif

static const int GOT_IPV4 = BIT0;
Expand Down Expand Up @@ -44,7 +47,11 @@ enum eppp_type {

struct eppp_handle {
QueueHandle_t out_queue;
#if CONFIG_EPPP_LINK_DEVICE_SPI
QueueHandle_t ready_semaphore;
#elif CONFIG_EPPP_LINK_DEVICE_UART
QueueHandle_t uart_event_queue;
#endif
esp_netif_t *netif;
enum eppp_type role;
};
Expand Down Expand Up @@ -76,6 +83,8 @@ static esp_err_t transmit(void *h, void *buffer, size_t len)
ESP_LOGE(TAG, "Failed to queue packet to slave!");
}
} while (remaining > 0);
#elif CONFIG_EPPP_LINK_DEVICE_UART
uart_write_bytes(UART_NUM_1, buffer, len);
#endif
return ESP_OK;
}
Expand All @@ -101,6 +110,7 @@ static esp_netif_t *netif_init(enum eppp_type role)
return NULL;
}
h->role = role;
#if CONFIG_EPPP_LINK_DEVICE_SPI
if (role == EPPP_CLIENT) {
h->ready_semaphore = xSemaphoreCreateBinary();
if (!h->ready_semaphore) {
Expand All @@ -110,6 +120,7 @@ static esp_netif_t *netif_init(enum eppp_type role)
return NULL;
}
}
#endif

esp_netif_driver_ifconfig_t driver_cfg = {
.handle = h,
Expand Down Expand Up @@ -470,7 +481,56 @@ _Noreturn static void ppp_task(void *args)
}
}
}
#endif // CONFIG_EPPP_LINK_DEVICE_SPI
#elif CONFIG_EPPP_LINK_DEVICE_UART
#define BUF_SIZE (1024)
#define UART_TX_CLIENT_TO_SERVER 10
#define UART_TX_SERVER_TO_CLIENT 11
#define UART_BAUDRATE 4000000
#define UART_QUEUE_SIZE 16

static esp_err_t init_uart(struct eppp_handle *h)
{
uart_config_t uart_config = {};
uart_config.baud_rate = UART_BAUDRATE;
uart_config.data_bits = UART_DATA_8_BITS;
uart_config.parity = UART_PARITY_DISABLE;
uart_config.stop_bits = UART_STOP_BITS_1;
uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
uart_config.source_clk = UART_SCLK_DEFAULT;

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");
ESP_RETURN_ON_ERROR(uart_param_config(UART_NUM_1, &uart_config), TAG, "Failed to set params");
int tx_io_num = h->role == EPPP_CLIENT ? UART_TX_CLIENT_TO_SERVER : UART_TX_SERVER_TO_CLIENT;
int rx_io_num = h->role == EPPP_CLIENT ? UART_TX_SERVER_TO_CLIENT : UART_TX_CLIENT_TO_SERVER;
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");
ESP_RETURN_ON_ERROR(uart_set_rx_timeout(UART_NUM_1, 1), TAG, "Failed to set UART Rx timeout");
return ESP_OK;
}

_Noreturn static void ppp_task(void *args)
{
static uint8_t buffer[BUF_SIZE] = {};

esp_netif_t *netif = args;
struct eppp_handle *h = esp_netif_get_io_driver(netif);
uart_event_t event;
while (1) {
xQueueReceive(h->uart_event_queue, &event, pdMS_TO_TICKS(pdMS_TO_TICKS(100)));
if (event.type == UART_DATA) {
size_t len;
uart_get_buffered_data_len(UART_NUM_1, &len);
if (len) {
len = uart_read_bytes(UART_NUM_1, buffer, BUF_SIZE, 0);
ESP_LOG_BUFFER_HEXDUMP("ppp_uart_recv", buffer, len, ESP_LOG_VERBOSE);
esp_netif_receive(netif, buffer, len, NULL);
}
} else {
ESP_LOGW(TAG, "Received UART event: %d", event.type);
}
}

}
#endif // CONFIG_EPPP_LINK_DEVICE_SPI / UART


static esp_netif_t *default_setup(enum eppp_type role)
Expand Down Expand Up @@ -498,6 +558,8 @@ static esp_netif_t *default_setup(enum eppp_type role)
} else {
init_slave(&s_spi_device, netif);
}
#elif CONFIG_EPPP_LINK_DEVICE_UART
init_uart(esp_netif_get_io_driver(netif));
#endif

netif_start(netif);
Expand Down

0 comments on commit c775915

Please sign in to comment.