Add uart test - send and receive struct works
board_control currently sends a scruct with incrementing values board_motorctl receives the struct and logs it also test code for sending, receiving and returning single value
This commit is contained in:
parent
b03baa4687
commit
8ef71082dc
@ -13,6 +13,7 @@ idf_component_register(
|
|||||||
"http.cpp"
|
"http.cpp"
|
||||||
"auto.cpp"
|
"auto.cpp"
|
||||||
"currentsensor.cpp"
|
"currentsensor.cpp"
|
||||||
|
"uart.cpp"
|
||||||
INCLUDE_DIRS
|
INCLUDE_DIRS
|
||||||
"."
|
"."
|
||||||
)
|
)
|
||||||
|
@ -11,8 +11,6 @@ extern "C"
|
|||||||
#include "sdkconfig.h"
|
#include "sdkconfig.h"
|
||||||
#include "esp_spiffs.h"
|
#include "esp_spiffs.h"
|
||||||
|
|
||||||
#include "driver/uart.h"
|
|
||||||
|
|
||||||
|
|
||||||
//custom C files
|
//custom C files
|
||||||
//#include "wifi.h"
|
//#include "wifi.h"
|
||||||
@ -24,6 +22,8 @@ extern "C"
|
|||||||
//#include "button.hpp"
|
//#include "button.hpp"
|
||||||
//#include "http.hpp"
|
//#include "http.hpp"
|
||||||
|
|
||||||
|
#include "uart.hpp"
|
||||||
|
|
||||||
//tag for logging
|
//tag for logging
|
||||||
static const char * TAG = "main";
|
static const char * TAG = "main";
|
||||||
|
|
||||||
@ -137,41 +137,6 @@ static const char * TAG = "main";
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void uart_task(void *arg)
|
|
||||||
{
|
|
||||||
uart_config_t uart1_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,
|
|
||||||
};
|
|
||||||
|
|
||||||
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));
|
|
||||||
|
|
||||||
uint8_t *data = (uint8_t *) malloc(1024);
|
|
||||||
|
|
||||||
//SEND data to motorctl board
|
|
||||||
uint8_t count = 0;
|
|
||||||
ESP_LOGW(TAG, "startloop...");
|
|
||||||
while (1) {
|
|
||||||
vTaskDelay(500 / portTICK_PERIOD_MS);
|
|
||||||
int len = uart_read_bytes(UART_NUM_1, data, (1024 - 1), 20 / portTICK_PERIOD_MS);
|
|
||||||
uart_flush_input(UART_NUM_1);
|
|
||||||
uart_flush(UART_NUM_1);
|
|
||||||
ESP_LOGW(TAG, "received data %d", *data);
|
|
||||||
*data = 99;
|
|
||||||
uart_write_bytes(UART_NUM_1, (const char *) &count, 1);
|
|
||||||
ESP_LOGW(TAG, "sent data %d", count);
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//=================================
|
//=================================
|
||||||
@ -245,7 +210,10 @@ extern "C" void app_main(void) {
|
|||||||
|
|
||||||
|
|
||||||
//TESTING UART
|
//TESTING UART
|
||||||
xTaskCreate(uart_task, "uart_task", 4096, NULL, 10, NULL);
|
//xTaskCreate(uart_task_testing, "uart_task", 4096, NULL, 10, NULL);
|
||||||
|
uart_init();
|
||||||
|
xTaskCreate(task_uartReceive, "task_uartReceive", 4096, NULL, 10, NULL);
|
||||||
|
xTaskCreate(task_uartSend, "task_uartSend", 5*4096, NULL, 10, NULL);
|
||||||
|
|
||||||
while(1){
|
while(1){
|
||||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||||
|
129
board_control/main/uart.cpp
Normal file
129
board_control/main/uart.cpp
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <esp_system.h>
|
||||||
|
#include <esp_event.h>
|
||||||
|
#include <nvs_flash.h>
|
||||||
|
#include "freertos/FreeRTOS.h"
|
||||||
|
#include "freertos/task.h"
|
||||||
|
#include "driver/gpio.h"
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include "sdkconfig.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "freertos/queue.h"
|
||||||
|
#include "driver/uart.h"
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "uart.hpp"
|
||||||
|
|
||||||
|
static const char * TAG = "uart";
|
||||||
|
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void uart_task_testing(void *arg){
|
||||||
|
//repeatedly send 8 bit count and log received 1 byte
|
||||||
|
uint8_t *data = (uint8_t *) malloc(1024);
|
||||||
|
uint8_t count = 0;
|
||||||
|
ESP_LOGW(TAG, "startloop...");
|
||||||
|
while (1) {
|
||||||
|
vTaskDelay(500 / portTICK_PERIOD_MS);
|
||||||
|
int len = uart_read_bytes(UART_NUM_1, data, (1024 - 1), 20 / portTICK_PERIOD_MS);
|
||||||
|
//uart_flush_input(UART_NUM_1);
|
||||||
|
//uart_flush(UART_NUM_1);
|
||||||
|
ESP_LOGW(TAG, "received len=%d data=%d", len, *data);
|
||||||
|
*data = 99; //set to 99 (indicates no new data received)
|
||||||
|
uart_write_bytes(UART_NUM_1, (const char *) &count, 1);
|
||||||
|
ESP_LOGW(TAG, "sent data %d", count);
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void task_uartReceive(void *arg){
|
||||||
|
static const char * TAG = "uart-receive";
|
||||||
|
//repeatedly send 8 bit count and log received 1 byte
|
||||||
|
char *data = (char *) malloc(1024);
|
||||||
|
char count = 0;
|
||||||
|
ESP_LOGW(TAG, "startloop...");
|
||||||
|
while (1) {
|
||||||
|
vTaskDelay(200 / portTICK_PERIOD_MS);
|
||||||
|
int len = uart_read_bytes(UART_NUM_1, data, (1024 - 1), 20 / portTICK_PERIOD_MS);
|
||||||
|
if (len>0) ESP_LOGW(TAG, "received len=%d data=%d", len, *data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void task_uartReceiveQueue(void *arg){
|
||||||
|
static const char * TAG = "uart-receive";
|
||||||
|
while (1) {
|
||||||
|
vTaskDelay(200 / portTICK_PERIOD_MS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// //send incrementing count
|
||||||
|
// void task_uartSend(void *arg){
|
||||||
|
// static const char * TAG = "uart-send";
|
||||||
|
// //repeatedly send 8 bit count and log received 1 byte
|
||||||
|
// char *data = (char *) malloc(1024);
|
||||||
|
// char count = 0;
|
||||||
|
// ESP_LOGW(TAG, "startloop...");
|
||||||
|
// while (1) {
|
||||||
|
// vTaskDelay(200 / portTICK_PERIOD_MS);
|
||||||
|
// uart_write_bytes(UART_NUM_1, (const char *) &count, 1);
|
||||||
|
// ESP_LOGW(TAG, "sent data %d", (int)count);
|
||||||
|
// count++;
|
||||||
|
// }
|
||||||
|
// ESP_LOGE(TAG, "loop exit...");
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint32_t timestamp;
|
||||||
|
int id;
|
||||||
|
float value;
|
||||||
|
} uartDataStruct;
|
||||||
|
|
||||||
|
//send struct
|
||||||
|
void task_uartSend(void *arg){
|
||||||
|
static const char * TAG = "uart-send";
|
||||||
|
uartDataStruct data = {123, 0, 1.1};
|
||||||
|
uint8_t serialData[sizeof(uartDataStruct)];
|
||||||
|
char count = 0;
|
||||||
|
ESP_LOGW(TAG, "startloop...");
|
||||||
|
while (1) {
|
||||||
|
vTaskDelay(500 / portTICK_PERIOD_MS);
|
||||||
|
memcpy(serialData, &data, sizeof(uartDataStruct));
|
||||||
|
uart_write_bytes(UART_NUM_1, (const char *)serialData, sizeof(uartDataStruct));
|
||||||
|
ESP_LOGW(TAG, "sent data struct with len %d", sizeof(uartDataStruct));
|
||||||
|
ESP_LOGW(TAG, "sent DATA: timestamp=%d, id=%d, value=%.1f", data.timestamp, data.id, data.value);
|
||||||
|
|
||||||
|
//change data values
|
||||||
|
data.timestamp = esp_log_timestamp();
|
||||||
|
data.id++;
|
||||||
|
data.value += 0.6;
|
||||||
|
}
|
||||||
|
ESP_LOGE(TAG, "loop exit...");
|
||||||
|
}
|
5
board_control/main/uart.hpp
Normal file
5
board_control/main/uart.hpp
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
|
||||||
|
void uart_init(void);
|
||||||
|
void uart_task_testing(void *arg);
|
||||||
|
void task_uartReceive(void *arg);
|
||||||
|
void task_uartSend(void *arg);
|
@ -10,21 +10,33 @@ extern "C"
|
|||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "sdkconfig.h"
|
#include "sdkconfig.h"
|
||||||
#include "esp_spiffs.h"
|
#include "esp_spiffs.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "driver/ledc.h"
|
#include "driver/ledc.h"
|
||||||
|
#include "driver/uart.h"
|
||||||
|
|
||||||
//custom C files
|
//custom C files
|
||||||
#include "wifi.h"
|
#include "wifi.h"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//=========================
|
||||||
|
//======= UART TEST =======
|
||||||
|
//=========================
|
||||||
|
//only run uart test code at the end
|
||||||
|
//disables other functionality
|
||||||
|
#define UART_TEST_ONLY
|
||||||
|
|
||||||
|
|
||||||
|
//tag for logging
|
||||||
|
static const char * TAG = "main";
|
||||||
|
|
||||||
|
#ifndef UART_TEST_ONLY
|
||||||
//custom C++ files
|
//custom C++ files
|
||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
#include "control.hpp"
|
#include "control.hpp"
|
||||||
#include "button.hpp"
|
#include "button.hpp"
|
||||||
#include "http.hpp"
|
#include "http.hpp"
|
||||||
|
|
||||||
//tag for logging
|
|
||||||
static const char * TAG = "main";
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -146,13 +158,42 @@ void setLoglevels(void){
|
|||||||
esp_log_level_set("automatedArmchair", ESP_LOG_DEBUG);
|
esp_log_level_set("automatedArmchair", ESP_LOG_DEBUG);
|
||||||
//esp_log_level_set("current-sensors", ESP_LOG_INFO);
|
//esp_log_level_set("current-sensors", ESP_LOG_INFO);
|
||||||
}
|
}
|
||||||
|
#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 ============
|
//=========== app_main ============
|
||||||
//=================================
|
//=================================
|
||||||
extern "C" void app_main(void) {
|
extern "C" void app_main(void) {
|
||||||
|
#ifndef UART_TEST_ONLY
|
||||||
//enable 5V volate regulator
|
//enable 5V volate regulator
|
||||||
gpio_pad_select_gpio(GPIO_NUM_17);
|
gpio_pad_select_gpio(GPIO_NUM_17);
|
||||||
gpio_set_direction(GPIO_NUM_17, GPIO_MODE_OUTPUT);
|
gpio_set_direction(GPIO_NUM_17, GPIO_MODE_OUTPUT);
|
||||||
@ -220,11 +261,33 @@ extern "C" void app_main(void) {
|
|||||||
//--- testing force http mode after startup ---
|
//--- testing force http mode after startup ---
|
||||||
//control.changeMode(controlMode_t::HTTP);
|
//control.changeMode(controlMode_t::HTTP);
|
||||||
|
|
||||||
|
|
||||||
//--- main loop ---
|
//--- main loop ---
|
||||||
//does nothing except for testing things
|
//does nothing except for testing things
|
||||||
while(1){
|
while(1){
|
||||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
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);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
//---------------------------------
|
//---------------------------------
|
||||||
//-------- TESTING section --------
|
//-------- TESTING section --------
|
||||||
|
Loading…
x
Reference in New Issue
Block a user