feat: implement UART command processing and versioning

This commit is contained in:
roy01
2026-04-22 11:28:47 +08:00
parent 19bdd31ece
commit 4ab09cde9e
6 changed files with 167 additions and 7 deletions
+1 -1
View File
@@ -26,7 +26,7 @@ extern "C"
//=============================================================================
// Macro Definition
//=============================================================================
#define FW_VERSION "0.0.1"
//=============================================================================
// Structure Definition
//=============================================================================
+30
View File
@@ -0,0 +1,30 @@
/**
* Copyright (c) 2026 Wisetop. All Rights Reserved.
*/
/** @file main.c
*
* @version 0.0.1
* @date 2026/04/22
* @license
* @description
*/
#ifndef __UART_CMD_SRV_H__
#define __UART_CMD_SRV_H__
#ifdef __cplusplus
extern "C"
{
#endif
#include "uart_drv.h"
#include "main.h"
void uart_cmd_process(void);
#ifdef __cplusplus
}
#endif
#endif
+9 -1
View File
@@ -6,11 +6,19 @@ extern "C"
{
#endif
#include "syslog.h"
#include "main.h"
#define UART_RX_BUF_SIZE 128
extern volatile char g_uart_rx_buf[UART_RX_BUF_SIZE];
extern volatile uint16_t g_uart_rx_idx;
extern volatile bool g_uart_rx_complete;
#define uart_send_msg(str, ...) printf(str, ##__VA_ARGS__);
void uart_init(void);
void uart_clear_rx_frame(void);
bool uart_is_frame_ready(void);
#ifdef __cplusplus
}
+2 -2
View File
@@ -10,7 +10,7 @@
*/
#include "main.h"
#include "uart_drv.h"
#include "uart_cmd_srv.h"
//=============================================================================
// Constant Definition
//=============================================================================
@@ -50,7 +50,7 @@ int main(void)
while (1)
{
__NOP();
uart_cmd_process();
}
return 0;
}
+114
View File
@@ -0,0 +1,114 @@
#include "uart_cmd_srv.h"
#include <stdlib.h>
#include <string.h>
static bool cmd_match_with_arg(const volatile char *buf, const char *prefix, const char **arg_out)
{
size_t len = strlen(prefix);
for (size_t i = 0; i < len; i++)
{
if (buf[i] != prefix[i])
return false;
}
if (buf[len] == ' ')
{
*arg_out = (const char *)&buf[len + 1];
return true;
}
return false;
}
static int32_t cmd_parse_int_list(const char *arg, int32_t *out, int32_t max_count)
{
int32_t count = 0;
char *end = NULL;
const char *p = arg;
while (*p != '\0' && count < max_count)
{
while (*p == ' ')
p++;
if (*p == '\0')
break;
out[count] = strtol(p, &end, 10);
if (end == p)
return -1;
count++;
p = end;
}
while (*p == ' ')
p++;
if (*p != '\0')
return -1;
return count;
}
void uart_cmd_process(void)
{
if (!uart_is_frame_ready())
return;
const char *arg = NULL;
char *cmd = (char *)g_uart_rx_buf;
if (strcmp(cmd, "help") == 0)
{
uart_send_msg("Commands:\r\n");
uart_send_msg(" help - show this help\r\n");
uart_send_msg(" fw_ver - show firmware version\r\n");
uart_send_msg(" pwm_en 1/0 - enable/disable PWM output\r\n");
uart_send_msg(" val n1 n2 ... - parse integer values\r\n");
}
else if (strcmp(cmd, "fw_ver") == 0)
{
uart_send_msg("FW: " FW_VERSION "\r\n");
}
else if (cmd_match_with_arg(cmd, "pwm_en", &arg))
{
if (arg[0] == '1')
{
uart_send_msg("OK pwm on\r\n");
}
else if (arg[0] == '0')
{
uart_send_msg("OK pwm off\r\n");
}
else
{
uart_send_msg("ERR usage: pwm_en 1/0\r\n");
}
}
else if (cmd_match_with_arg(cmd, "val", &arg))
{
int32_t values[8] = { 0 };
int32_t parsed = cmd_parse_int_list(arg, values, 8);
if (parsed <= 0)
{
uart_send_msg("ERR usage: val n1 n2 ...\r\n");
}
else
{
for (int32_t i = 0; i < parsed; i++)
{
char buf[32];
snprintf(buf, sizeof(buf), "%d\r\n", values[i]);
uart_send_msg(buf);
}
}
}
else
{
uart_send_msg("ERR unknown cmd\r\n");
}
/* Reset for next command */
uart_clear_rx_frame();
}
+11 -3
View File
@@ -1,5 +1,4 @@
#include "uart_drv.h"
#include "hal_device.h"
/* UART config */
#define UART_DEVICE UART0
@@ -13,8 +12,6 @@
#define UART_RX_PIN GPIO_Pin_05
#define UART_RX_AF GPIO_AF_1
#define UART_RX_BUF_SIZE 128
volatile char g_uart_rx_buf[UART_RX_BUF_SIZE];
volatile uint16_t g_uart_rx_idx = 0;
volatile bool g_uart_rx_complete = false;
@@ -53,6 +50,17 @@ __INTERRUPT void uart_rx_handler(void)
// }
// }
void uart_clear_rx_frame(void)
{
g_uart_rx_idx = 0;
g_uart_rx_complete = false;
}
bool uart_is_frame_ready(void)
{
return g_uart_rx_complete;
}
/* Configure PB3 as UART0 TX, PB5 as UART0 RX */
void uart_init(void)
{