Fix UART1 GPIO mapping and simplify command logging.
Use TX=GPIO3 and RX=GPIO2 at 921600 to match the terminal adapter wiring; log received message id and which handler command is triggered. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
43a85ce697
commit
a1629fb3db
@ -22,7 +22,7 @@ init_uart(cmd_queue);
|
||||
|
||||
## UART frame format
|
||||
|
||||
Packets on UART1 (921600 baud, pins TX=2 / RX=3):
|
||||
Packets on **UART1** (921600 baud, **TX=GPIO3**, **RX=GPIO2** — USB adapter on `/dev/ttyUSB0`):
|
||||
|
||||
| Field | Value |
|
||||
|-----------|--------------------------------------------|
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
#include "esp_log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/idf_additions.h"
|
||||
#include "uart_messages.pb.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
@ -13,9 +14,39 @@ static QueueHandle_t cmd_queue;
|
||||
static msg_binding_t handlers[MAX_HANDLERS];
|
||||
static int handler_count;
|
||||
|
||||
static const char *message_type_name(uint16_t id) {
|
||||
switch ((alox_MessageType)id) {
|
||||
case alox_MessageType_ACK:
|
||||
return "ACK";
|
||||
case alox_MessageType_ECHO:
|
||||
return "ECHO";
|
||||
case alox_MessageType_VERSION:
|
||||
return "VERSION";
|
||||
case alox_MessageType_CLIENT_INFO:
|
||||
return "CLIENT_INFO";
|
||||
case alox_MessageType_CLIENT_INPUT:
|
||||
return "CLIENT_INPUT";
|
||||
case alox_MessageType_OTA_START:
|
||||
return "OTA_START";
|
||||
case alox_MessageType_OTA_PAYLOAD:
|
||||
return "OTA_PAYLOAD";
|
||||
case alox_MessageType_OTA_END:
|
||||
return "OTA_END";
|
||||
case alox_MessageType_OTA_STATUS:
|
||||
return "OTA_STATUS";
|
||||
case alox_MessageType_OTA_START_ESPNOW:
|
||||
return "OTA_START_ESPNOW";
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
void init_cmdHandler(QueueHandle_t queue) {
|
||||
cmd_queue = queue;
|
||||
xTaskCreate(vCmdDispatcherTask, "cmd_dispatch", 4096, NULL, 5, NULL);
|
||||
if (xTaskCreate(vCmdDispatcherTask, "cmd_dispatch", 4096, NULL, 5, NULL) !=
|
||||
pdPASS) {
|
||||
ESP_LOGE(TAG, "failed to create cmd_dispatch task");
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t msg_register_handler(uint16_t id, msg_callback_t cb) {
|
||||
@ -59,20 +90,24 @@ esp_err_t msg_post(uint16_t id, const uint8_t *data, size_t len) {
|
||||
}
|
||||
|
||||
void vCmdDispatcherTask(void *param) {
|
||||
(void)param;
|
||||
generic_msg_t msg;
|
||||
|
||||
while (1) {
|
||||
if (xQueueReceive(cmd_queue, &msg, portMAX_DELAY) == pdPASS) {
|
||||
bool handled = false;
|
||||
for (int i = 0; i < handler_count; i++) {
|
||||
if (handlers[i].msg_id == msg.msg_id) {
|
||||
ESP_LOGI(TAG, "trigger command %s (0x%02x)", message_type_name(msg.msg_id),
|
||||
(unsigned)msg.msg_id);
|
||||
handlers[i].callback(msg.payload, msg.len);
|
||||
handled = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!handled) {
|
||||
ESP_LOGW(TAG, "no handler for msg_id 0x%04x (%u bytes)", msg.msg_id,
|
||||
(unsigned)msg.len);
|
||||
ESP_LOGW(TAG, "no handler for %s (0x%02x)", message_type_name(msg.msg_id),
|
||||
(unsigned)msg.msg_id);
|
||||
}
|
||||
free(msg.payload);
|
||||
}
|
||||
|
||||
@ -39,20 +39,13 @@ static void handle_version(const uint8_t *data, size_t len) {
|
||||
response.payload.version_response.git_hash.funcs.encode = encode_git_hash;
|
||||
response.payload.version_response.git_hash.arg = (void *)POWERPOD_GIT_HASH;
|
||||
|
||||
esp_err_t err = uart_send_uart_message(&response);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "failed to send version response: %s", esp_err_to_name(err));
|
||||
return;
|
||||
if (uart_send_uart_message(&response) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "failed to send response");
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "version=%u git=%s", (unsigned)POWERPOD_FW_VERSION,
|
||||
POWERPOD_GIT_HASH);
|
||||
}
|
||||
|
||||
void cmd_version_register(void) {
|
||||
esp_err_t err =
|
||||
msg_register_handler(alox_MessageType_VERSION, handle_version);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "register failed: %s", esp_err_to_name(err));
|
||||
if (msg_register_handler(alox_MessageType_VERSION, handle_version) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "register failed");
|
||||
}
|
||||
}
|
||||
|
||||
60
main/uart.c
60
main/uart.c
@ -1,12 +1,9 @@
|
||||
#include "cmd_handler.h"
|
||||
#include "driver/uart.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_log_buffer.h"
|
||||
#include "freertos/idf_additions.h"
|
||||
#include "hal/uart_types.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "portmacro.h"
|
||||
#include "uart.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
@ -44,49 +41,69 @@ static bool uart_enqueue_packet(const uart_packet_t *packet) {
|
||||
}
|
||||
|
||||
void init_uart(QueueHandle_t cmd_queue) {
|
||||
esp_err_t err;
|
||||
|
||||
uart_cmd_queue = cmd_queue;
|
||||
uart_config_t uart_config = {// .baud_rate = 115200, // 921600, 115200
|
||||
.baud_rate = 921600,
|
||||
uart_config_t uart_config = {
|
||||
.baud_rate = UART_BAUD_RATE,
|
||||
.data_bits = UART_DATA_8_BITS,
|
||||
.parity = UART_PARITY_DISABLE,
|
||||
.stop_bits = UART_STOP_BITS_1,
|
||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE};
|
||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
||||
};
|
||||
|
||||
uart_driver_install(UART_NUM, UART_BUF_SIZE * 2, 0, 0, NULL, 0);
|
||||
uart_param_config(UART_NUM, &uart_config);
|
||||
uart_set_pin(UART_NUM, UART_TXD_PIN, UART_RXD_PIN, UART_PIN_NO_CHANGE,
|
||||
err = uart_driver_install(UART_NUM, UART_BUF_SIZE * 2, 0, 0, NULL, 0);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "uart_driver_install failed: %s", esp_err_to_name(err));
|
||||
return;
|
||||
}
|
||||
err = uart_param_config(UART_NUM, &uart_config);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "uart_param_config failed: %s", esp_err_to_name(err));
|
||||
return;
|
||||
}
|
||||
err = uart_set_pin(UART_NUM, UART_TXD_PIN, UART_RXD_PIN, UART_PIN_NO_CHANGE,
|
||||
UART_PIN_NO_CHANGE);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "uart_set_pin failed: %s", esp_err_to_name(err));
|
||||
return;
|
||||
}
|
||||
|
||||
xTaskCreate(uart_read_task, "Read Uart", 4096, NULL, 1, NULL);
|
||||
if (xTaskCreate(uart_read_task, "uart_rx", 4096, NULL, 1, NULL) != pdPASS) {
|
||||
ESP_LOGE(TAG, "failed to create uart_read_task");
|
||||
}
|
||||
}
|
||||
|
||||
void uart_read_task(void *param) {
|
||||
// Send all Input from Uart to the Message Handler for Parsing
|
||||
(void)param;
|
||||
|
||||
uint8_t *data = (uint8_t *)malloc(UART_BUF_SIZE);
|
||||
if (data == NULL) {
|
||||
ESP_LOGE(TAG, "out of memory");
|
||||
vTaskDelete(NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
int len = 0;
|
||||
uart_packet_t packet = {.state = STATE_START};
|
||||
TickType_t last_byte_time = xTaskGetTickCount();
|
||||
const TickType_t timeout_ticks = pdMS_TO_TICKS(50);
|
||||
|
||||
while (1) {
|
||||
len = 0;
|
||||
len = uart_read_bytes(UART_NUM, data, UART_BUF_SIZE, pdMS_TO_TICKS(20));
|
||||
if (len > 0) {
|
||||
for (int i = 0; i < len; ++i) {
|
||||
if (parse_uart_byte(data[i], &packet)) {
|
||||
ESP_LOGI(TAG, "packet received, len=%d, cmd=0x%02x", packet.len,
|
||||
packet.len > 0 ? packet.payload[0] : 0);
|
||||
ESP_LOGI(TAG, "received message cmd=0x%02x len=%u",
|
||||
packet.len > 0 ? packet.payload[0] : 0, (unsigned)packet.len);
|
||||
uart_enqueue_packet(&packet);
|
||||
}
|
||||
}
|
||||
last_byte_time = xTaskGetTickCount();
|
||||
} else { // reset message receive when there comes nothing new
|
||||
if (packet.state != STATE_START) {
|
||||
} else if (packet.state != STATE_START) {
|
||||
TickType_t now = xTaskGetTickCount();
|
||||
if ((now - last_byte_time) > timeout_ticks) {
|
||||
packet.state = STATE_START;
|
||||
ESP_LOGW("UART", "Parser Timeout - Resetting State");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -104,7 +121,6 @@ bool parse_uart_byte(uint8_t byte, uart_packet_t *p) {
|
||||
|
||||
case STATE_LEN:
|
||||
if (byte > MAX_PAYLOAD_SIZE || byte == 0) {
|
||||
// corrupt length or fake start
|
||||
p->state = STATE_START;
|
||||
} else {
|
||||
p->len = byte;
|
||||
@ -124,7 +140,6 @@ bool parse_uart_byte(uint8_t byte, uart_packet_t *p) {
|
||||
if (byte == p->checksum) {
|
||||
p->state = STATE_STOP;
|
||||
} else {
|
||||
// CRC wrong: reset
|
||||
p->state = STATE_START;
|
||||
}
|
||||
break;
|
||||
@ -158,8 +173,7 @@ esp_err_t uart_send_framed(const uint8_t *payload, size_t len) {
|
||||
frame[pos++] = checksum;
|
||||
frame[pos++] = STOP_MARKER;
|
||||
|
||||
int written =
|
||||
uart_write_bytes(UART_NUM, frame, pos);
|
||||
int written = uart_write_bytes(UART_NUM, frame, pos);
|
||||
if (written < 0 || (size_t)written != pos) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
10
main/uart.h
10
main/uart.h
@ -6,11 +6,13 @@
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#define UART_NUM UART_NUM_1
|
||||
#define UART_BUF_SIZE 256
|
||||
#define UART_TXD_PIN 2
|
||||
#define UART_RXD_PIN 3
|
||||
|
||||
#define UART_NUM UART_NUM_1
|
||||
#define UART_BAUD_RATE 921600
|
||||
#define UART_TXD_PIN 3
|
||||
#define UART_RXD_PIN 2
|
||||
|
||||
#define UART_BUF_SIZE 256
|
||||
#define START_MARKER 0xAA
|
||||
#define STOP_MARKER 0xCC
|
||||
#define MAX_BUF_SIZE 256
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user