Compare commits

...

2 Commits

Author SHA1 Message Date
erki
3720ec76d2 Add a simple hello test 2023-06-26 15:29:21 +03:00
erki
3c29475b82 Install larger RX buffer, and implement error handling via flushing 2023-06-26 14:41:40 +03:00
5 changed files with 56 additions and 9 deletions

View File

@ -3,6 +3,7 @@
#include <algorithm>
#include <cstring>
#include <variant>
#include <cstdio>
#include "esp_event.h"
#include "esp_mac.h"
@ -26,7 +27,7 @@ std::variant<MacAddress, std::monostate> s_peer;
bool s_isBroadcastAddress(const std::uint8_t* mac)
{
constexpr MacAddress broadcast_mac{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
constexpr MacAddress broadcast_mac{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
return std::memcmp(mac, broadcast_mac.begin(), ESP_NOW_ETH_ALEN) == 0;
}
@ -35,6 +36,11 @@ bool s_isBroadcastAddress(const esp_now_recv_info_t* sender)
return s_isBroadcastAddress(sender->src_addr);
}
constexpr MacAddress s_getBroadcastAddress()
{
return MacAddress{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
}
esp_err_t s_addPeer(const MacAddress& peer_address)
{
esp_now_peer_info_t broadcast_peer;
@ -96,7 +102,7 @@ QueueHandle_t setupEspNow()
ESP_ERROR_CHECK(esp_now_register_send_cb(s_cbEspNowSendComplete));
ESP_ERROR_CHECK(esp_now_register_recv_cb(s_cbEspNowReceiveComplete));
const auto broadcast_mac = MacAddress{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
const auto broadcast_mac = s_getBroadcastAddress();
s_peer = broadcast_mac;
ESP_ERROR_CHECK(s_addPeer(broadcast_mac));
@ -104,6 +110,19 @@ QueueHandle_t setupEspNow()
return s_esp_now_queue;
}
void sendHello()
{
MacAddress mac;
ESP_ERROR_CHECK(esp_read_mac(mac.data(), ESP_MAC_WIFI_STA));
char buffer[100];
const std::size_t length = std::sprintf(buffer, "Hello from %02X:%02X:%02X:%02X:%02X:%02X.\n\r",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
const auto broadcast_mac = s_getBroadcastAddress();
ESP_ERROR_CHECK(esp_now_send(broadcast_mac.data(), (uint8_t*)buffer, length));
}
const std::array<std::uint8_t, 128>& getRxBuffer()
{
return s_rx_buffer;

View File

@ -26,6 +26,7 @@ using MacAddress = std::array<std::uint8_t, 6>;
void setupWifi();
[[nodiscard]] QueueHandle_t setupEspNow();
void sendHello();
void sendData(const std::array<std::uint8_t, 128>& buffer, const std::size_t length);
const std::array<std::uint8_t, 128>& getRxBuffer();

View File

@ -28,11 +28,16 @@ QueueHandle_t setupSerial(const uart_port_t uart)
.source_clk = UART_SCLK_DEFAULT,
};
ESP_ERROR_CHECK(uart_driver_install(uart, 132, 128 * 2, 4, &s_uart_queue, 0));
ESP_ERROR_CHECK(uart_driver_install(uart, 128 * 2, 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;
}
void discardRxBuffer(const uart_port_t uart)
{
ESP_ERROR_CHECK(uart_flush(uart));
}
}

View File

@ -13,4 +13,6 @@ namespace Serial
[[nodiscard]] QueueHandle_t setupSerial(const uart_port_t uart);
void discardRxBuffer(const uart_port_t uart);
}

View File

@ -1,6 +1,7 @@
#include <array>
#include <cstdint>
#include <stdio.h>
#include <algorithm>
#include "app_networking.hpp"
#include "app_serial.hpp"
@ -26,13 +27,15 @@ QueueSetHandle_t s_queue_set = nullptr;
QueueHandle_t s_esp_now_queue = nullptr;
QueueHandle_t s_uart_queue = nullptr;
void s_sendUartData(const std::size_t len)
void s_sendUartData(std::size_t len)
{
len = std::clamp<std::size_t>(len, 1, 128);
ESP_LOGD(LOG_TAG, "Sending %u bytes via UART.", len);
std::array<std::uint8_t, 128> tx_buffer;
uart_read_bytes(CONFIG_ESPTNL_UART, tx_buffer.data(), len, 0);
xTimerStop(s_timer, 0);
Networking::sendData(tx_buffer, len);
}
@ -54,7 +57,7 @@ std::size_t s_uartBytesWaiting()
if (queue_select == s_esp_now_queue)
{
Networking::EspNowEvent event;
xQueueReceive(queue_select, &event, 0);
xQueueReceive(s_esp_now_queue, &event, 0);
ESP_LOGD(LOG_TAG, "ESP-NOW event: %u, rx-length: %ull.", event.type, event.rx_length);
@ -81,7 +84,6 @@ std::size_t s_uartBytesWaiting()
if (s_uartBytesWaiting() >= 128)
{
s_sendUartData(bytes_waiting);
xTimerStop(s_timer, 0);
}
}
}
@ -94,11 +96,26 @@ std::size_t s_uartBytesWaiting()
if (event.type == UART_DATA)
{
if (event.size >= 128)
s_sendUartData(event.size);
const std::size_t recv_size = s_uartBytesWaiting();
if (recv_size >= 128)
s_sendUartData(recv_size);
else
xTimerReset(s_timer, 0);
}
else if (event.type == UART_BUFFER_FULL)
{
const std::size_t recv_size = s_uartBytesWaiting();
s_sendUartData(recv_size);
}
else if (event.type == UART_FIFO_OVF)
{
ESP_LOGE(LOG_TAG, "UART FIFO overflow. Flushing.");
Serial::discardRxBuffer(CONFIG_ESPTNL_UART);
}
}
else
{
ESP_LOGW(LOG_TAG, "Unknown event type.");
}
}
}
@ -134,6 +151,9 @@ extern "C" void app_main(void)
xQueueAddToSet(s_esp_now_queue, s_queue_set);
xQueueAddToSet(s_uart_queue, s_queue_set);
ESP_LOGI(LOG_TAG, "Setup completed.");
ESP_LOGI(LOG_TAG, "Setup completed. Sending hello.");
Networking::sendHello();
xTaskCreate(s_mainTask, "main_task", 2048, nullptr, 4, &s_main_task);
}