From 02ad9b7596eb4bd968e32f581e2747951d7283d8 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 4 Mar 2025 16:43:30 +0530 Subject: [PATCH] First commit --- .clang-format | 9 ++++ .clangd | 3 ++ .cproject | 16 +++++++ .project | 20 ++++++++ .settings/org.eclipse.core.resources.prefs | 2 + CMakeLists.txt | 6 +++ main/CMakeLists.txt | 2 + main/uart_async_rxtxtasks_main.c | 56 ++++++++++++++++++++++ 8 files changed, 114 insertions(+) create mode 100644 .clang-format create mode 100644 .clangd create mode 100644 .cproject create mode 100644 .project create mode 100644 .settings/org.eclipse.core.resources.prefs create mode 100644 CMakeLists.txt create mode 100644 main/CMakeLists.txt create mode 100644 main/uart_async_rxtxtasks_main.c diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..0c7b3aa --- /dev/null +++ b/.clang-format @@ -0,0 +1,9 @@ +# We'll use defaults from the LLVM style, but with some modifications so that it's close to the CDT K&R style. +BasedOnStyle: LLVM +UseTab: Always +IndentWidth: 4 +TabWidth: 4 +PackConstructorInitializers: NextLineOnly +BreakConstructorInitializers: AfterColon +IndentAccessModifiers: false +AccessModifierOffset: -4 diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..22e5ea0 --- /dev/null +++ b/.clangd @@ -0,0 +1,3 @@ +CompileFlags: + CompilationDatabase: build + Remove: [-m*, -f*] diff --git a/.cproject b/.cproject new file mode 100644 index 0000000..63b192f --- /dev/null +++ b/.cproject @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.project b/.project new file mode 100644 index 0000000..334c2b7 --- /dev/null +++ b/.project @@ -0,0 +1,20 @@ + + + uart_async_rxtxtasks + + + + + + org.eclipse.cdt.core.cBuilder + clean,full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + com.espressif.idf.core.idfNature + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..211ab31 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,6 @@ +# The following lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.16) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(uart_async_rxtxtasks) diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt new file mode 100644 index 0000000..30e774e --- /dev/null +++ b/main/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "uart_async_rxtxtasks_main.c" + INCLUDE_DIRS ".") diff --git a/main/uart_async_rxtxtasks_main.c b/main/uart_async_rxtxtasks_main.c new file mode 100644 index 0000000..0abdc6c --- /dev/null +++ b/main/uart_async_rxtxtasks_main.c @@ -0,0 +1,56 @@ +#include +#include "driver/uart.h" +#include "driver/gpio.h" +#include "esp_log.h" +#include "string.h" + +#define UART_NUM UART_NUM_1 +#define TXD_PIN GPIO_NUM_17 +#define RXD_PIN GPIO_NUM_16 +#define BUF_SIZE 1024 + +static const char *TAG = "UART"; + +void uart_init(void) { + uart_config_t uart_config = { + .baud_rate = 4800, + .data_bits = UART_DATA_8_BITS, + .parity = UART_PARITY_DISABLE, + .stop_bits = UART_STOP_BITS_1, + .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, + }; + uart_param_config(UART_NUM, &uart_config); + uart_set_pin(UART_NUM, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); + uart_driver_install(UART_NUM, BUF_SIZE * 2, 0, 0, NULL, 0); +} + +void uart_send_command(const uint8_t *command, size_t length) { + uart_write_bytes(UART_NUM, (const char *)command, length); +} + +int uart_receive_response(uint8_t *data, size_t length) { + return uart_read_bytes(UART_NUM, data, length, pdMS_TO_TICKS(1000)); +} + +void request_current_value(void) { + uint8_t command[] = {0xA5, 0x05,0x4C, 0x4C, 0x42}; // Example command to request current value + uart_send_command(command, sizeof(command)); + vTaskDelay(pdMS_TO_TICKS(100)); + + uint8_t response[31]; + int len = uart_receive_response(response, sizeof(response)); + if (len > 0) { + ESP_LOGI(TAG, "Received %d bytes", len); + for (int i = 0; i < len; i++) { + ESP_LOGI(TAG, "0x%02X", response[i]); + } + } +} + +void app_main(void) { + uart_init(); + while (1) { + request_current_value(); + vTaskDelay(pdMS_TO_TICKS(2000)); + } +} \ No newline at end of file