Skip to content

Commit

Permalink
feat: add commands to get and set uart config
Browse files Browse the repository at this point in the history
  • Loading branch information
DeimosHall committed Dec 20, 2024
1 parent 35825a7 commit 61e3008
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 14 deletions.
12 changes: 10 additions & 2 deletions firmware/components/uart_bridge/include/uart_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,23 @@
*/
#pragma once

#include "driver/uart.h"
#include "esp_err.h"

typedef struct {
int buffer_size;
uart_config_t uart_config;
} uart_bridge_config_t;

/**
* @brief Initialize the UART bridge.
*
* @param baud_rate The baud rate to use.
* @param uart_config The UART configuration.
* @param buffer_size The size of the UART buffer.
*
* @return ESP_OK on success, ESP_FAIL otherwise.
*/
esp_err_t uart_bridge_begin(int baud_rate, int buffer_size);
esp_err_t uart_bridge_begin(uart_config_t uart_config, int buffer_size);

/**
* @brief Read data from the UART bridge.
Expand Down Expand Up @@ -55,6 +61,8 @@ void uart_bridge_set_logs_to_uart();
*/
void uart_bridge_set_logs_to_usb();

uart_bridge_config_t uart_bridge_get_config();

/**
* @brief End the UART bridge.
*
Expand Down
31 changes: 20 additions & 11 deletions firmware/components/uart_bridge/uart_bridge.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
#include "uart_bridge.h"
#include <string.h>
#include "driver/uart.h"
#include "esp_log.h"

static const char* TAG = "uart_bridge";

esp_err_t uart_bridge_begin(int baud_rate, int buffer_size) {
uart_bridge_config_t uart_bridge_config;

esp_err_t uart_bridge_begin(uart_config_t uart_config, int buffer_size) {
#if !defined(CONFIG_UART_BRIDGE_DEBUG)
esp_log_level_set(TAG, ESP_LOG_NONE);
#endif

uart_config_t uart_config = {
.baud_rate = baud_rate,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
};
uart_bridge_config.buffer_size = buffer_size;
uart_bridge_config.uart_config = uart_config;

// uart_config_t uart_config = {
// .baud_rate = baud_rate,
// .data_bits = UART_DATA_8_BITS,
// .parity = UART_PARITY_DISABLE,
// .stop_bits = UART_STOP_BITS_1,
// .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
// };

esp_err_t err = uart_param_config(UART_NUM_0, &uart_config);
if (err != ESP_OK) {
Expand Down Expand Up @@ -63,21 +68,25 @@ esp_err_t uart_bridge_write(const char* buffer, int buffer_size) {
return ESP_OK;
}

int custom_esp_log(const char* format, va_list args) {
int custom_log(const char* format, va_list args) {
char buffer[256];
vsnprintf(buffer, sizeof(buffer), format, args);
uart_bridge_write(buffer, strlen(buffer));
return 0;
}

void uart_bridge_set_logs_to_uart() {
esp_log_set_vprintf(custom_esp_log);
esp_log_set_vprintf(custom_log);
}

void uart_bridge_set_logs_to_usb() {
esp_log_set_vprintf(&vprintf);
}

uart_bridge_config_t uart_bridge_get_config() {
return uart_bridge_config;
}

esp_err_t uart_bridge_end() {
esp_err_t err = uart_driver_delete(UART_NUM_0);
if (err != ESP_OK) {
Expand Down
11 changes: 10 additions & 1 deletion firmware/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ void app_main() {
#if !defined(CONFIG_MAIN_DEBUG)
esp_log_level_set(TAG, ESP_LOG_NONE);
#endif
uart_bridge_begin(BAUD_RATE, UART_BUFFER_SIZE);

uart_config_t uart_config = {
.baud_rate = BAUD_RATE,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
};

uart_bridge_begin(uart_config, UART_BUFFER_SIZE);
preferences_begin();

bool stealth_mode = preferences_get_bool("stealth_mode", false);
Expand Down
182 changes: 182 additions & 0 deletions firmware/main/modules/cmd_control/cmd_uart_bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ static struct {
struct arg_end* end;
} uart_bridge_args;

static struct {
struct arg_int* buffer_size;
struct arg_int* baud_rate;
struct arg_int* data_bits;
struct arg_int* parity;
struct arg_int* stop_bits;
struct arg_int* flow_ctrl;
struct arg_end* end;
} uart_bridge_config_args;

// Structure to hold parameters for uart_bridge_task
typedef struct {
const char* buffer_size;
Expand Down Expand Up @@ -101,6 +111,124 @@ static void uart_bridge(int argc, char** argv) {
ESP_LOGI(TAG, "uart_bridge_task started");
}

uint8_t print_uart_bridge_config() {
uart_bridge_config_t config = uart_bridge_get_config();
printf("UART bridge configuration:\n");
printf("\tBuffer size: %d\n", config.buffer_size);
printf("\tBaud rate: %d\n", config.uart_config.baud_rate);
printf("\tData bits: %d", config.uart_config.data_bits);
switch (config.uart_config.data_bits) {
case UART_DATA_5_BITS:
printf(" (5 bits)\n");
break;
case UART_DATA_6_BITS:
printf(" (6 bits)\n");
break;
case UART_DATA_7_BITS:
printf(" (7 bits)\n");
break;
case UART_DATA_8_BITS:
printf(" (8 bits)\n");
break;
default:
printf(" (invalid)\n");
break;
}
printf("\tParity: %d", config.uart_config.parity);
switch (config.uart_config.parity) {
case UART_PARITY_DISABLE:
printf(" (disable)\n");
break;
case UART_PARITY_ODD:
printf(" (odd)\n");
break;
case UART_PARITY_EVEN:
printf(" (even)\n");
break;
default:
printf(" (invalid)\n");
break;
}
printf("\tStop bits: %d", config.uart_config.stop_bits);
switch (config.uart_config.stop_bits) {
case UART_STOP_BITS_1:
printf(" (1 bit)\n");
break;
case UART_STOP_BITS_1_5:
printf(" (1.5 bits)\n");
break;
case UART_STOP_BITS_2:
printf(" (2 bits)\n");
break;
default:
printf(" (invalid)\n");
break;
}
printf("\tFlow control: %d", config.uart_config.flow_ctrl);
switch (config.uart_config.flow_ctrl) {
case UART_HW_FLOWCTRL_DISABLE:
printf(" (disable)\n");
break;
case UART_HW_FLOWCTRL_RTS:
printf(" (enable RX hardware flow control (rts))\n");
break;
case UART_HW_FLOWCTRL_CTS:
printf(" (enable TX hardware flow control (cts))\n");
break;
case UART_HW_FLOWCTRL_CTS_RTS:
printf(" (enable hardware flow control)\n");
break;
default:
printf(" (invalid)\n");
break;
}
return 0;
}

uint8_t uart_bridge_set_config(int argc, char** argv) {
int nerrors = arg_parse(argc, argv, (void**) &uart_bridge_config_args);
if (nerrors != 0) {
arg_print_errors(stderr, uart_bridge_config_args.end, argv[0]);
return 1;
}

assert(uart_bridge_config_args.buffer_size->count == 1);
assert(uart_bridge_config_args.baud_rate->count == 1);
assert(uart_bridge_config_args.data_bits->count == 1);
assert(uart_bridge_config_args.parity->count == 1);
assert(uart_bridge_config_args.stop_bits->count == 1);
assert(uart_bridge_config_args.flow_ctrl->count == 1);

const int buffer_size = *uart_bridge_config_args.buffer_size->ival;
const int baud_rate = *uart_bridge_config_args.baud_rate->ival;
const int data_bits = *uart_bridge_config_args.data_bits->ival;
const int parity = *uart_bridge_config_args.parity->ival;
const int stop_bits = *uart_bridge_config_args.stop_bits->ival;
const int flow_ctrl = *uart_bridge_config_args.flow_ctrl->ival;

uart_config_t uart_config = {
.baud_rate = baud_rate,
.data_bits = data_bits,
.parity = parity,
.stop_bits = stop_bits,
.flow_ctrl = flow_ctrl,
};

uart_bridge_config_t config = {
.buffer_size = buffer_size,
.uart_config = uart_config,
};

esp_err_t err = uart_bridge_begin(uart_config, buffer_size);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to set UART bridge configuration (error code: %d)",
err);
return 1;
}

return 0;
}

void cmd_control_register_uart_bridge_commands() {
#if !defined(CONFIG_CMD_UART_BRIDGE_DEBUG)
esp_log_level_set(TAG, ESP_LOG_NONE);
Expand Down Expand Up @@ -137,6 +265,60 @@ void cmd_control_register_uart_bridge_commands() {
.func = &uart_bridge,
.argtable = &uart_bridge_args};

esp_console_cmd_t uart_bridge_print_config_cmd = {
.command = "uart_bridge_get_config",
.help = "Print the UART bridge configuration",
.hint = NULL,
.func = &print_uart_bridge_config,
.argtable = NULL};

uart_bridge_config_args.buffer_size =
arg_int1(NULL, NULL, "<buffer_size>",
"Size in bytes of the buffer to read data into\n"
"\tExample: 1024");
uart_bridge_config_args.baud_rate =
arg_int1(NULL, NULL, "<baud_rate>",
"Baud rate\n"
"\tFree to choose, but common values are:\n"
"\t9600, 115200, 230400, 460800, 921600");
uart_bridge_config_args.data_bits = arg_int1(NULL, NULL, "<data_bits>",
"Data bits\n"
"\tOptions:\n"
"\t0: 5 bits\n"
"\t1: 6 bits\n"
"\t2: 7 bits\n"
"\t3: 8 bits");
uart_bridge_config_args.parity = arg_int1(NULL, NULL, "<parity>",
"Parity\n"
"\tOptions:\n"
"\t0: disable\n"
"\t2: odd\n"
"\t3: even");
uart_bridge_config_args.stop_bits = arg_int1(NULL, NULL, "<stop_bits>",
"Stop bits\n"
"\tOptions:\n"
"\t1: 1 bit\n"
"\t2: 1.5 bits\n"
"\t3: 2 bits");
uart_bridge_config_args.flow_ctrl =
arg_int1(NULL, NULL, "<flow_ctrl>",
"Flow control\n"
"\tOptions:\n"
"\t0: disable\n"
"\t1: enable RX hardware flow control (rts)\n"
"\t2: enable TX hardware flow control (cts)\n"
"\t3: enable hardware flow control");
uart_bridge_config_args.end = arg_end(6);

esp_console_cmd_t uart_bridge_set_config_cmd = {
.command = "uart_bridge_set_config",
.help = "Set the UART bridge configuration",
.hint = NULL,
.func = &uart_bridge_set_config,
.argtable = &uart_bridge_config_args};

ESP_ERROR_CHECK(esp_console_cmd_register(&uart_bridge_print_cmd));
ESP_ERROR_CHECK(esp_console_cmd_register(&uart_bridge_uart_bridge_cmd));
ESP_ERROR_CHECK(esp_console_cmd_register(&uart_bridge_print_config_cmd));
ESP_ERROR_CHECK(esp_console_cmd_register(&uart_bridge_set_config_cmd));
}

0 comments on commit 61e3008

Please sign in to comment.