From 479162e47b5175b23361b8db1cf77eb734a9502c Mon Sep 17 00:00:00 2001 From: simon Date: Sun, 26 Apr 2026 11:43:33 +0200 Subject: [PATCH] Added Uart --- main/CMakeLists.txt | 2 +- main/hw_test.c | 4 ++-- main/uart.c | 39 +++++++++++++++++++++++++++++++++++++++ main/uart.h | 2 ++ 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 main/uart.c create mode 100644 main/uart.h diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 6d3621f..d32ddea 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,2 +1,2 @@ -idf_component_register(SRCS "hw_test.c" "bosch456.c" +idf_component_register(SRCS "hw_test.c" "bosch456.c" "uart.c" INCLUDE_DIRS ".") diff --git a/main/hw_test.c b/main/hw_test.c index 5f754d2..3fdc752 100644 --- a/main/hw_test.c +++ b/main/hw_test.c @@ -6,6 +6,7 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "led_strip.h" +#include "uart.h" #include #include "bosch456.h" @@ -13,8 +14,6 @@ #define TASTER_GPIO 12 #define V_LIPO_1 1 #define V_LIPO_2 12 -#define TERMAINL_RX 2 -#define TERMINAL_TX 3 #define DIP_MASTER 4 #define I2C_SCL 5 #define I2C_SDA 6 @@ -180,4 +179,5 @@ void app_main(void) { } init_bma456(bus_handle); + init_uart(); } diff --git a/main/uart.c b/main/uart.c new file mode 100644 index 0000000..23815f6 --- /dev/null +++ b/main/uart.c @@ -0,0 +1,39 @@ +#include "driver/uart.h" +#include "esp_log.h" +#include "hal/uart_types.h" +#include "uart.h" + +#define MASTER_UART UART_NUM_1 +#define BUF_SIZE (256) +#define TERMAINL_RX 2 +#define TERMINAL_TX 3 + +void uart_read_task(void *param) { + uint8_t *data = (uint8_t *)malloc(BUF_SIZE); + int len = 0; + + while (1) { + len = uart_read_bytes(MASTER_UART, data, BUF_SIZE, pdMS_TO_TICKS(20)); + if (len > 0) { + for (int i = 0; i < len; i++) { + ESP_LOGI("UART", "UART: %c", data[i]); + } + } + } +} + +void init_uart() { + uart_config_t uart_config = {// .baud_rate = 115200, // 921600, 115200 + .baud_rate = 921600, + .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, TERMINAL_TX, TERMAINL_RX, UART_PIN_NO_CHANGE, + UART_PIN_NO_CHANGE); + + xTaskCreate(uart_read_task, "Read Uart", 4096, NULL, 1, NULL); +} diff --git a/main/uart.h b/main/uart.h new file mode 100644 index 0000000..b35b3c8 --- /dev/null +++ b/main/uart.h @@ -0,0 +1,2 @@ + +void init_uart();