From d26390ea754477101957357e3bb196c69802b3d4 Mon Sep 17 00:00:00 2001 From: simon Date: Sat, 22 Mar 2025 13:09:19 +0100 Subject: [PATCH] Refactored Uart in own module --- main/CMakeLists.txt | 2 +- main/main.c | 33 ++++----------------------------- main/main.h | 6 ------ main/uart_handler.c | 38 ++++++++++++++++++++++++++++++++++++++ main/uart_handler.h | 13 +++++++++++++ 5 files changed, 56 insertions(+), 36 deletions(-) create mode 100644 main/uart_handler.c create mode 100644 main/uart_handler.h diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 760124d..9d501da 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,3 +1,3 @@ -idf_component_register(SRCS "main.c" +idf_component_register(SRCS "main.c" "uart_handler.c" INCLUDE_DIRS ".") diff --git a/main/main.c b/main/main.c index f27e699..b98f1d2 100644 --- a/main/main.c +++ b/main/main.c @@ -9,6 +9,10 @@ #include "nvs_flash.h" #include "main.h" +#include "uart_handler.h" + +static const char *TAG = "ALOX - MAIN"; + // return any inactive client field for new usage int getNextFreeClientId() { for (int i = 0; i < numClients; i++) { @@ -282,35 +286,6 @@ void client_monitor_task(void *pvParameters) { } } -void uart_read_task(void *param) { - uint8_t *data = (uint8_t *)malloc(BUF_SIZE); - int len = 0; - while (1) { - len = 0; - len = - uart_read_bytes(MASTER_UART, data, BUF_SIZE, (20 / portTICK_PERIOD_MS)); - if (len > 0) { - data[len] = '\0'; - ESP_LOGI(TAG, "GOT UART DATA %s", data); - uart_write_bytes(MASTER_UART, data, len); - } - } -} - -void init_uart() { - uart_config_t uart_config = {.baud_rate = 115200, - .data_bits = UART_DATA_8_BITS, - .parity = UART_PARITY_DISABLE, - .stop_bits = UART_STOP_BITS_1, - .flow_ctrl = UART_HW_FLOWCTRL_DISABLE}; - - uart_driver_install(MASTER_UART, BUF_SIZE * 2, 0, 0, NULL, 0); - uart_param_config(MASTER_UART, &uart_config); - uart_set_pin(MASTER_UART, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, - UART_PIN_NO_CHANGE); - xTaskCreate(uart_read_task, "Read Uart", 4096, NULL, 1, NULL); -} - void app_main(void) { esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || diff --git a/main/main.h b/main/main.h index 5294a12..b0fbd64 100644 --- a/main/main.h +++ b/main/main.h @@ -15,13 +15,8 @@ #include #define MASTER_MODE_PIN GPIO_NUM_23 // Jumper-Erkennungspin -#define MASTER_UART UART_NUM_2 #define BROADCAST_INTERVAL_MS 500 -#define BUF_SIZE (1024) -#define TXD_PIN (GPIO_NUM_17) -#define RXD_PIN (GPIO_NUM_16) - #define CLIENT_TIMEOUT_MS 5000 // 5 Sekunden Timeout #define CHECK_INTERVAL_MS 1000 // Jede Sekunde überprüfen @@ -71,7 +66,6 @@ static_assert(sizeof(BaseMessage) <= 255, "BaseMessage darf nicht größer als 255 sein"); QueueHandle_t messageQueue; // Warteschlange für empfangene Nachrichten -const char *TAG = "ALOX"; typedef struct { uint8_t macAddr[ESP_NOW_ETH_ALEN]; diff --git a/main/uart_handler.c b/main/uart_handler.c new file mode 100644 index 0000000..2759ad3 --- /dev/null +++ b/main/uart_handler.c @@ -0,0 +1,38 @@ +#include "driver/uart.h" +#include "driver/gpio.h" +#include "esp_log.h" +#include "hal/uart_types.h" +#include "nvs_flash.h" + +#include "uart_handler.h" + +static const char *TAG = "ALOX - UART"; + +void init_uart() { + uart_config_t uart_config = {.baud_rate = 115200, + .data_bits = UART_DATA_8_BITS, + .parity = UART_PARITY_DISABLE, + .stop_bits = UART_STOP_BITS_1, + .flow_ctrl = UART_HW_FLOWCTRL_DISABLE}; + + uart_driver_install(MASTER_UART, BUF_SIZE * 2, 0, 0, NULL, 0); + uart_param_config(MASTER_UART, &uart_config); + uart_set_pin(MASTER_UART, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, + UART_PIN_NO_CHANGE); + xTaskCreate(uart_read_task, "Read Uart", 4096, NULL, 1, NULL); +} + +void uart_read_task(void *param) { + uint8_t *data = (uint8_t *)malloc(BUF_SIZE); + int len = 0; + while (1) { + len = 0; + len = + uart_read_bytes(MASTER_UART, data, BUF_SIZE, (20 / portTICK_PERIOD_MS)); + if (len > 0) { + data[len] = '\0'; + ESP_LOGI(TAG, "GOT UART DATA %s", data); + uart_write_bytes(MASTER_UART, data, len); + } + } +} diff --git a/main/uart_handler.h b/main/uart_handler.h new file mode 100644 index 0000000..f0f1fb9 --- /dev/null +++ b/main/uart_handler.h @@ -0,0 +1,13 @@ +#ifndef UART_HANDLER_H +#define UART_HANDLER_H + +#define MASTER_UART UART_NUM_2 +#define TXD_PIN (GPIO_NUM_17) +#define RXD_PIN (GPIO_NUM_16) + +#define BUF_SIZE (1024) + +void init_uart(); +void uart_read_task(void *param); + +#endif