Make UART configurable, move config validation
This commit is contained in:
parent
fd0c853f36
commit
b30701bb3c
@ -1,4 +1,5 @@
|
||||
idf_component_register(SRCS "esp_tunnel.cpp"
|
||||
"app_networking.cpp"
|
||||
"app_serial.cpp"
|
||||
"app_config.cpp"
|
||||
INCLUDE_DIRS ".")
|
||||
|
||||
@ -12,7 +12,6 @@ menu "EspTunnel Configuration"
|
||||
help
|
||||
ESPNOW local master key. Must be 16 bytes.
|
||||
|
||||
|
||||
config ESPTNL_CHANNEL
|
||||
int "Channel"
|
||||
default 1
|
||||
@ -20,4 +19,10 @@ menu "EspTunnel Configuration"
|
||||
help
|
||||
The channel for sending and receiving ESPNOW data.
|
||||
|
||||
config ESPTNL_UART
|
||||
int "UART interface"
|
||||
default 0
|
||||
help
|
||||
The UART interface to use for RX and TX.
|
||||
|
||||
endmenu
|
||||
24
main/app_config.cpp
Normal file
24
main/app_config.cpp
Normal file
@ -0,0 +1,24 @@
|
||||
//
|
||||
// Created by erki on 11/06/23.
|
||||
//
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "esp_now.h"
|
||||
#include "driver/uart.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
template<std::size_t N>
|
||||
constexpr auto STR_LEN(const char (&s)[N])
|
||||
{
|
||||
return N;
|
||||
}
|
||||
|
||||
static_assert(STR_LEN(CONFIG_ESPTNL_PMK) == ESP_NOW_KEY_LEN + 1, "CONFIG_ESPTNL_PMK must be of length 16 bytes + 1 null terminator.");
|
||||
static_assert(STR_LEN(CONFIG_ESPTNL_LMK) == ESP_NOW_KEY_LEN + 1, "CONFIG_ESPTNL_LMK must be of length 16 bytes + 1 null terminator.");
|
||||
|
||||
static_assert(CONFIG_ESPTNL_UART < UART_NUM_MAX && CONFIG_ESPTNL_UART >= 0, "CONFIG_ESPTNL_UART must be within range [0, UART_NUM_MAX[.");
|
||||
|
||||
}
|
||||
@ -22,15 +22,6 @@ static const char* TAG = "Networking";
|
||||
static_assert(std::is_standard_layout_v<EspNowEvent> && std::is_trivial_v<EspNowEvent>,
|
||||
"EspNowEvent is not compatible with a FreeRTOS queue.");
|
||||
|
||||
template<std::size_t N>
|
||||
constexpr auto STR_LEN(const char (&s)[N])
|
||||
{
|
||||
return N;
|
||||
}
|
||||
|
||||
static_assert(STR_LEN(CONFIG_ESPTNL_PMK) == ESP_NOW_KEY_LEN + 1, "CONFIG_ESPTNL_PMK must be of length 16 bytes + 1 null terminator.");
|
||||
static_assert(STR_LEN(CONFIG_ESPTNL_LMK) == ESP_NOW_KEY_LEN + 1, "CONFIG_ESPTNL_LMK must be of length 16 bytes + 1 null terminator.");
|
||||
|
||||
QueueHandle_t s_esp_now_queue = nullptr;
|
||||
std::array<std::uint8_t, 128> s_rx_buffer;
|
||||
std::variant<MacAddress, std::monostate> s_peer;
|
||||
|
||||
@ -16,7 +16,7 @@ QueueHandle_t s_uart_queue = nullptr;
|
||||
namespace Serial
|
||||
{
|
||||
|
||||
QueueHandle_t setupSerial()
|
||||
QueueHandle_t setupSerial(const uart_port_t uart)
|
||||
{
|
||||
uart_config_t config = {
|
||||
.baud_rate = 115200,
|
||||
@ -27,9 +27,9 @@ QueueHandle_t setupSerial()
|
||||
.source_clk = UART_SCLK_DEFAULT,
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(uart_driver_install(UART_NUM_0, 132, 128 * 2, 4, &s_uart_queue, 0));
|
||||
ESP_ERROR_CHECK(uart_param_config(UART_NUM_0, &config));
|
||||
ESP_ERROR_CHECK(uart_set_pin(UART_NUM_0, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
|
||||
ESP_ERROR_CHECK(uart_driver_install(uart, 132, 128 * 2, 4, &s_uart_queue, 0));
|
||||
ESP_ERROR_CHECK(uart_param_config(uart, &config));
|
||||
ESP_ERROR_CHECK(uart_set_pin(uart, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
|
||||
|
||||
return s_uart_queue;
|
||||
}
|
||||
|
||||
@ -6,10 +6,11 @@
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "driver/uart.h"
|
||||
|
||||
namespace Serial
|
||||
{
|
||||
|
||||
[[nodiscard]] QueueHandle_t setupSerial();
|
||||
[[nodiscard]] QueueHandle_t setupSerial(const uart_port_t uart);
|
||||
|
||||
}
|
||||
|
||||
@ -30,7 +30,7 @@ void s_sendUartData(const std::size_t len)
|
||||
ESP_LOGD(TAG, "Sending %u bytes via UART.", len);
|
||||
|
||||
std::array<std::uint8_t, 128> tx_buffer;
|
||||
uart_read_bytes(UART_NUM_0, tx_buffer.data(), len, 0);
|
||||
uart_read_bytes(CONFIG_ESPTNL_UART, tx_buffer.data(), len, 0);
|
||||
|
||||
Networking::sendData(tx_buffer, len);
|
||||
}
|
||||
@ -38,7 +38,7 @@ void s_sendUartData(const std::size_t len)
|
||||
std::size_t s_uartBytesWaiting()
|
||||
{
|
||||
std::size_t len = 0;
|
||||
uart_get_buffered_data_len(UART_NUM_0, &len);
|
||||
uart_get_buffered_data_len(CONFIG_ESPTNL_UART, &len);
|
||||
return std::min<std::size_t>(len, 128);
|
||||
}
|
||||
|
||||
@ -60,12 +60,12 @@ std::size_t s_uartBytesWaiting()
|
||||
if (event.type == Networking::EspNowEvent::MSG_RECEIVED)
|
||||
{
|
||||
std::size_t uart_free = 0;
|
||||
uart_get_tx_buffer_free_size(UART_NUM_0, &uart_free);
|
||||
uart_get_tx_buffer_free_size(CONFIG_ESPTNL_UART, &uart_free);
|
||||
if (uart_free >= event.rx_length)
|
||||
{
|
||||
ESP_LOGD(TAG, "ESP-NOW event: wrote %ull bytes to UART RX.", uart_free);
|
||||
const auto& esp_rx_buffer = Networking::getRxBuffer();
|
||||
uart_write_bytes(UART_NUM_0, esp_rx_buffer.data(), event.rx_length);
|
||||
uart_write_bytes(CONFIG_ESPTNL_UART, esp_rx_buffer.data(), event.rx_length);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -124,7 +124,7 @@ extern "C" void app_main(void)
|
||||
|
||||
Networking::setupWifi();
|
||||
s_esp_now_queue = Networking::setupEspNow();
|
||||
s_uart_queue = Serial::setupSerial();
|
||||
s_uart_queue = Serial::setupSerial(CONFIG_ESPTNL_UART);
|
||||
|
||||
s_timer = xTimerCreate("autosend_timer", pdMS_TO_TICKS(100), false, nullptr, s_cbListenTimeout);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user