1 Commits

Author SHA1 Message Date
roy01 9c71708dee feat: uart_cmd_process() done 2026-04-20 17:50:46 +08:00
5 changed files with 280 additions and 114 deletions
+54
View File
@@ -0,0 +1,54 @@
/**
* @file cmd_srv.h
* @brief UART command interface via PB5(RX) / PB3(TX)
*/
#ifndef __cmd_srv_H__
#define __cmd_srv_H__
#ifdef __cplusplus
extern "C"
{
#endif
#include "hal_device.h"
//=============================================================================
// Constant Definition
//=============================================================================
/* UART config */
#define UART_TX_PORT GPIOB
#define UART_TX_PIN GPIO_Pin_03
#define UART_TX_AF GPIO_AF_1
#define UART_RX_PORT GPIOB
#define UART_RX_PIN GPIO_Pin_05
#define UART_RX_AF GPIO_AF_1
#define UART_DEVICE UART0
#define UART_DEVICE_BAUD_RATE 115200
/* CMD */
#define UART_CMD_RX_BUF_SIZE 128
#define FW_VERSION "0.0.1"
//=============================================================================
// Global Data Definition
//=============================================================================
extern volatile char g_uart_rx_buf[UART_CMD_RX_BUF_SIZE];
extern volatile uint16_t g_uart_rx_idx;
extern volatile bool g_uart_rx_complete;
//=============================================================================
// Public Function Definition
//=============================================================================
void uart_init(void);
void uart_cmd_process(void);
void uart_cmd_send_str(const char *str);
#ifdef __cplusplus
}
#endif
#endif
-50
View File
@@ -1,50 +0,0 @@
/**
* Copyright (c) 2024 Wei-Lun Hsu. All Rights Reserved.
*/
/** @file isr.h
*
* @author Wei-Lun Hsu
* @version 0.1
* @date 2024/09/16
* @license
* @description
*/
#ifndef __isr_H_wuraIpBA_lTJm_HvJw_sNDo_uEd5JnucTReY__
#define __isr_H_wuraIpBA_lTJm_HvJw_sNDo_uEd5JnucTReY__
#ifdef __cplusplus
extern "C" {
#endif
#include "main.h"
//=============================================================================
// Constant Definition
//=============================================================================
//=============================================================================
// Macro Definition
//=============================================================================
//=============================================================================
// Structure Definition
//=============================================================================
//=============================================================================
// Global Data Definition
//=============================================================================
//=============================================================================
// Private Function Definition
//=============================================================================
//=============================================================================
// Public Function Definition
//=============================================================================
#ifdef __cplusplus
}
#endif
#endif
+197
View File
@@ -0,0 +1,197 @@
/**
* @file cmd_srv.c
* @brief UART command interface via PB5(RX) / PB3(TX)
*/
#include <string.h>
#include "hal_device.h"
#include "cmd_srv.h"
#include "syslog.h"
//=============================================================================
// Constant Definition
//=============================================================================
//=============================================================================
// Global Data Definition
//=============================================================================
volatile char g_uart_rx_buf[UART_CMD_RX_BUF_SIZE];
volatile uint16_t g_uart_rx_idx = 0;
volatile bool g_uart_rx_complete = false;
static bool g_pwm_enabled = false;
static bool g_notify_enabled = false;
//=============================================================================
// Private Function Definition
//=============================================================================
static int cmd_strcmp(const volatile char *buf, const char *cmd)
{
while (*cmd)
{
if (*buf != *cmd)
return 1;
buf++;
cmd++;
}
return (*buf != '\0');
}
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;
}
//=============================================================================
// ISR Handler
//=============================================================================
__INTERRUPT void uart_rx_handler(void)
{
SAVE_IRQ_CSR_CONTEXT();
if (UART_GetITStatus(UART0, UART_FLAG_RXNE))
{
char ch = (char)UART_ReceiveData(UART0);
if (ch == '\r' || ch == '\n')
{
if (g_uart_rx_idx > 0)
{
g_uart_rx_buf[g_uart_rx_idx] = '\0';
g_uart_rx_complete = true;
}
}
else if (g_uart_rx_idx < UART_CMD_RX_BUF_SIZE - 1)
{
g_uart_rx_buf[g_uart_rx_idx++] = ch;
}
}
RESTORE_IRQ_CSR_CONTEXT();
}
//=============================================================================
// Public Function Definition
//=============================================================================
void uart_cmd_send_str(const char *str)
{
while (*str)
{
UART_SendData(UART0, *str++);
UART_WaitTxFifoEmpty(UART0);
}
}
void uart_cmd_process(void)
{
if (!g_uart_rx_complete)
return;
const char *arg = NULL;
if (cmd_strcmp(g_uart_rx_buf, "help") == 0)
{
uart_cmd_send_str("Commands:\r\n");
uart_cmd_send_str(" help - show this help\r\n");
uart_cmd_send_str(" fw_ver - show firmware version\r\n");
uart_cmd_send_str(" pwm_en 1/0 - enable/disable PWM output\r\n");
uart_cmd_send_str(" notify_en 1/0 - enable/disable notify\r\n");
}
else if (cmd_strcmp(g_uart_rx_buf, "fw_ver") == 0)
{
uart_cmd_send_str("FW: " FW_VERSION "\r\n");
}
else if (cmd_match_with_arg(g_uart_rx_buf, "pwm_en", &arg))
{
if (arg[0] == '1')
{
g_pwm_enabled = true;
uart_cmd_send_str("OK pwm on\r\n");
}
else if (arg[0] == '0')
{
g_pwm_enabled = false;
uart_cmd_send_str("OK pwm off\r\n");
}
else
{
uart_cmd_send_str("ERR usage: pwm_en 1/0\r\n");
}
}
else if (cmd_match_with_arg(g_uart_rx_buf, "notify_en", &arg))
{
if (arg[0] == '1')
{
g_notify_enabled = true;
uart_cmd_send_str("OK notify on\r\n");
}
else if (arg[0] == '0')
{
g_notify_enabled = false;
uart_cmd_send_str("OK notify off\r\n");
}
else
{
uart_cmd_send_str("ERR usage: notify_en 1/0\r\n");
}
}
else
{
uart_cmd_send_str("ERR unknown cmd\r\n");
}
/* Reset for next command */
g_uart_rx_idx = 0;
g_uart_rx_complete = false;
}
/* Configure PB3 as UART0 TX, PB5 as UART0 RX */
void uart_init(void)
{
GPIO_InitTypeDef gpio_init = { 0 };
UART_InitTypeDef uart_init = { 0 };
gpio_init.GPIO_Pin = UART_TX_PIN;
gpio_init.GPIO_Mode = GPIO_Mode_AF;
gpio_init.GPIO_AF_Mode = UART_TX_AF;
GPIO_Init(UART_TX_PORT, &gpio_init);
gpio_init.GPIO_Pin = UART_RX_PIN;
gpio_init.GPIO_Mode = GPIO_Mode_AF;
gpio_init.GPIO_AF_Mode = UART_RX_AF;
GPIO_Init(UART_RX_PORT, &gpio_init);
uart_init.BaudRate = UART_DEVICE_BAUD_RATE;
uart_init.WordLength = UART_WordLength_8b;
uart_init.StopBits = UART_StopBits_1;
uart_init.Parity = UART_Parity_No;
uart_init.Mode = UART_Mode_TxRx;
UART_Init(UART_DEVICE, &uart_init);
/* Enable RX not-empty interrupt */
UART_ITConfig(UART_DEVICE, UART_IT_RX_FIFO_NO_EMPTY, ENABLE);
/* Register UART0 IRQ */
sys_irq_attr_t irq_attr = {
.trig_mode = SYS_IRQ_TRIGGER_LEVEL,
.level = SYS_IRQ_LEVEL_H,
.priority = SYS_IRQ_PRIORITY_MIDDEN,
};
sys_register_IRQ(UART0_IRQn, uart_rx_handler, &irq_attr);
/* Start UART */
UART_Start(UART_DEVICE);
sys_enable_girq();
}
-39
View File
@@ -1,39 +0,0 @@
/**
* Copyright (c) 2024 Wei-Lun Hsu. All Rights Reserved.
*/
/** @file isr.c
*
* @author Wei-Lun Hsu
* @version 0.1
* @date 2024/09/16
* @license
* @description
*/
#include "isr.h"
//=============================================================================
// Constant Definition
//=============================================================================
//=============================================================================
// Macro Definition
//=============================================================================
//=============================================================================
// Structure Definition
//=============================================================================
//=============================================================================
// Global Data Definition
//=============================================================================
//=============================================================================
// Private Function Definition
//=============================================================================
//=============================================================================
// Public Function Definition
//=============================================================================
+29 -25
View File
@@ -10,11 +10,10 @@
* @description
*/
#include "main.h"
#include "isr.h"
#include "cmd_srv.h"
//=============================================================================
// Constant Definition
// Constant Definition
//=============================================================================
//=============================================================================
@@ -32,36 +31,41 @@
//=============================================================================
// Private Function Definition
//=============================================================================
uint32_t __FASTCODE fastcode_proc(void)
static void sysclk_init(void)
{
log_color(SLOG_CYAN, "run at fast area: $pc= x%08X\n", (uint32_t)&fastcode_proc);
return 0;
SYSCFG_ClkInitTypeDef SysClkInit = { 0 };
SysClkInit.ClkSource = SYSCFG_ClkSrc_HSI;
SYSCFG_SysClkConfig(&SysClkInit);
sys_config_systick(SYS_TICK_1_MS);
}
/* Test pin: PA14 */
static void test_pin_init(void)
{
GPIO_InitTypeDef gpio_init = { 0 };
gpio_init.GPIO_Pin = GPIO_Pin_14;
gpio_init.GPIO_Mode = GPIO_Mode_OUT;
gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
gpio_init.GPIO_OType = GPIO_OType_PP;
GPIO_WriteBit(GPIOA, GPIO_Pin_14, GPIO_PIN_HIGH);
GPIO_Init(GPIOA, &gpio_init);
}
//=============================================================================
// Public Function Definition
//=============================================================================
int main(void)
{
SYSCFG_ClkInitTypeDef SysClkInit = {0};
sysclk_init();
uart_init();
test_pin_init();
SysClkInit.ClkSource = SYSCFG_ClkSrc_HSI;
SYSCFG_SysClkConfig(&SysClkInit);
uart_cmd_send_str("CMD service started\n");
sys_config_systick(SYS_TICK_1_MS);
syslog_init();
info("This is a app demo project\n");
msg("log debug message: $pc= x%08X\n", __get_pc());
log_color(SLOG_GREEN, "color green\n");
log_color(SLOG_YELLOW, "color yellow\n");
fastcode_proc();
while(1)
{
__NOP();
}
return 0;
while (1)
{
uart_cmd_process();
}
return 0;
}