Refactored Uart in own module

This commit is contained in:
simon 2025-03-22 13:09:19 +01:00
parent c7b86f96d8
commit d26390ea75
5 changed files with 56 additions and 36 deletions

View File

@ -1,3 +1,3 @@
idf_component_register(SRCS "main.c" idf_component_register(SRCS "main.c" "uart_handler.c"
INCLUDE_DIRS ".") INCLUDE_DIRS ".")

View File

@ -9,6 +9,10 @@
#include "nvs_flash.h" #include "nvs_flash.h"
#include "main.h" #include "main.h"
#include "uart_handler.h"
static const char *TAG = "ALOX - MAIN";
// return any inactive client field for new usage // return any inactive client field for new usage
int getNextFreeClientId() { int getNextFreeClientId() {
for (int i = 0; i < numClients; i++) { 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) { void app_main(void) {
esp_err_t ret = nvs_flash_init(); esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||

View File

@ -15,13 +15,8 @@
#include <string.h> #include <string.h>
#define MASTER_MODE_PIN GPIO_NUM_23 // Jumper-Erkennungspin #define MASTER_MODE_PIN GPIO_NUM_23 // Jumper-Erkennungspin
#define MASTER_UART UART_NUM_2
#define BROADCAST_INTERVAL_MS 500 #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 CLIENT_TIMEOUT_MS 5000 // 5 Sekunden Timeout
#define CHECK_INTERVAL_MS 1000 // Jede Sekunde überprüfen #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"); "BaseMessage darf nicht größer als 255 sein");
QueueHandle_t messageQueue; // Warteschlange für empfangene Nachrichten QueueHandle_t messageQueue; // Warteschlange für empfangene Nachrichten
const char *TAG = "ALOX";
typedef struct { typedef struct {
uint8_t macAddr[ESP_NOW_ETH_ALEN]; uint8_t macAddr[ESP_NOW_ETH_ALEN];

38
main/uart_handler.c Normal file
View File

@ -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);
}
}
}

13
main/uart_handler.h Normal file
View File

@ -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