Add uart templates, send and receive tasks with templates work

both boards compile and send/receive example data using new templates in
common uart code
common/uart_common.hpp
This commit is contained in:
jonny_jr9
2023-08-29 11:11:29 +02:00
parent 76e8bac113
commit 3722b0af74
15 changed files with 854 additions and 305 deletions

View File

@@ -13,11 +13,13 @@ extern "C"
#include <string.h>
#include "driver/ledc.h"
#include "driver/uart.h"
//custom C files
//custom C files
#include "wifi.h"
}
//custom C++ files
#include "config.hpp"
#include "uart.hpp"
//=========================
//======= UART TEST =======
@@ -30,13 +32,10 @@ extern "C"
//tag for logging
static const char * TAG = "main";
#ifndef UART_TEST_ONLY
//custom C++ files
#include "config.hpp"
#include "control.hpp"
//====================================
//========== motorctl task ===========
//====================================
@@ -109,33 +108,6 @@ void setLoglevels(void){
#endif
#ifdef UART_TEST_ONLY
void uart_init(void){
uart_config_t uart1_config = {
.baud_rate = 115198,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_EVEN,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
};
ESP_LOGW(TAG, "config...");
ESP_ERROR_CHECK(uart_param_config(UART_NUM_1, &uart1_config));
ESP_LOGW(TAG, "setpins...");
ESP_ERROR_CHECK(uart_set_pin(UART_NUM_1, 23, 22, 0, 0));
ESP_LOGW(TAG, "init...");
ESP_ERROR_CHECK(uart_driver_install(UART_NUM_1, 1024, 1024, 10, NULL, 0));
}
//struct for testing uart
typedef struct {
uint32_t timestamp;
int id;
float value;
} uartDataStruct;
#endif
//=================================
//=========== app_main ============
//=================================
@@ -170,36 +142,22 @@ extern "C" void app_main(void) {
//beep at startup
buzzer.beep(3, 70, 50);
//--- main loop ---
//does nothing except for testing things
while(1){
vTaskDelay(5000 / portTICK_PERIOD_MS);
}
#endif
#ifdef UART_TEST_ONLY
uart_init();
uint8_t receivedData[sizeof(uartDataStruct)];
uartDataStruct data;
ESP_LOGW(TAG, "startloop...");
while(1){
vTaskDelay(500 / portTICK_PERIOD_MS);
int len = uart_read_bytes(UART_NUM_1, receivedData, sizeof(uartDataStruct), 20 / portTICK_PERIOD_MS);
uart_flush_input(UART_NUM_1);
if (len > 0){
memcpy(&data, receivedData, sizeof(uartDataStruct));
//uart_write_bytes(UART_NUM_1, (const char *) data, 1);
//ESP_LOGW(TAG, "sent data back %d", *data);
ESP_LOGW(TAG, "received len=%d DATA: timestamp=%d, id=%d, value=%.1f", len, data.timestamp, data.id, data.value);
}
xTaskCreate(task_uartReceive, "task_uartReceive", 4096, NULL, 10, NULL);
xTaskCreate(task_uartSend, "task_uartSend", 4096, NULL, 10, NULL);
#endif
//--- main loop ---
//does nothing except for testing things
while(1){
vTaskDelay(5000 / portTICK_PERIOD_MS);
//--- test controlledMotor --- (ramp)
// //brake for 1 s
// motorLeft.setTarget(motorstate_t::BRAKE);
@@ -210,5 +168,6 @@ extern "C" void app_main(void) {
// //command 100% - forward
// motorLeft.setTarget(motorstate_t::FWD, 100);
// vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}

View File

@@ -7,9 +7,6 @@
//Note fade functionality provided by LEDC would be very useful but unfortunately is not usable here because:
//"Due to hardware limitations, there is no way to stop a fade before it reaches its target duty."
//definition of string array to be able to convert state enum to readable string
const char* motorstateStr[4] = {"IDLE", "FWD", "REV", "BRAKE"};
//tag for logging
static const char * TAG = "motordriver";

View File

@@ -0,0 +1,44 @@
#include "uart.hpp"
//===== uart board MOTORCTL =====
static const char * TAG = "uart";
//==============================
//====== task_uartReceive ======
//==============================
void task_uartReceive(void *arg){
//receive data from uart, detect associated struct and copy/handle the data
//TODO use queue instead of check interval?
uartData_test_t testData;
uint8_t receivedData[1024-1];
while(1){
//note: check has to be more frequent than pause time between sending
vTaskDelay(200 / portTICK_PERIOD_MS);
int len = uart_read_bytes(UART_NUM_1, receivedData, sizeof(uartData_test_t), 20 / portTICK_PERIOD_MS);
uart_flush_input(UART_NUM_1);
if (len < 1) continue;
switch (len){
case sizeof(uartData_test_t):
testData = serialData2Struct<uartData_test_t>(receivedData);
ESP_LOGW(TAG, "received uartDataStruct len=%d DATA: timestamp=%d, id=%d, value=%.1f", len, testData.timestamp, testData.id, testData.value);
break;
//TODO add other received structs here
default:
ESP_LOGW(TAG, "received data len=%d cant be associated with configures struct", len);
break;
}
}
}
//=============================
//======= task_uartSend =======
//=============================
//TODO copy send task from board_control/uart.cpp
void task_uartSend(void *arg){
while (1) {
vTaskDelay(500 / portTICK_PERIOD_MS);
}
}

View File

@@ -0,0 +1,9 @@
#pragma once
#include "uart_common.hpp"
//===== uart board MOTORCTL =====
void task_uartReceive(void *arg);
void task_uartSend(void *arg);