Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 45ef0b6e6d |
Vendored
+1
-15
@@ -16,13 +16,6 @@
|
||||
"postDebugTask": "Stop OpenOCD (Ubuntu)",
|
||||
"overrideGDBServerStartedRegex": "Listening on port",
|
||||
},
|
||||
{
|
||||
"name": "Ubuntu Flash Only (No GDB)",
|
||||
"type": "node-terminal",
|
||||
"request": "launch",
|
||||
"cwd": "${workspaceFolder}",
|
||||
"command": "${workspaceFolder}/openocd/linux/2025.10/bin/openocd -s openocd/linux/2025.10/scripts -f openocd/linux/2025.10/scripts/target/pec930.cfg -c \"program ./build/Debug/pec930_motor.elf verify reset exit\""
|
||||
},
|
||||
{
|
||||
"name": "Windows RISC-V Debug (OpenOCD)",
|
||||
"type": "cortex-debug",
|
||||
@@ -37,14 +30,7 @@
|
||||
"preLaunchTask": "Start OpenOCD (Windows)",
|
||||
"postDebugTask": "Stop OpenOCD (Windows)",
|
||||
"overrideGDBServerStartedRegex": "Listening on port",
|
||||
},
|
||||
{
|
||||
"name": "Windows Flash Only (No GDB)",
|
||||
"type": "node-terminal",
|
||||
"request": "launch",
|
||||
"cwd": "${workspaceFolder}",
|
||||
"command": "${workspaceFolder}\\openocd\\win\\2025.10\\bin\\openocd.exe -s openocd\\win\\2025.10\\scripts -f openocd\\win\\2025.10\\scripts\\target\\pec930.cfg -c \"program .\\build\\Debug\\pec930_motor.elf verify reset exit\""
|
||||
},
|
||||
}
|
||||
|
||||
]
|
||||
}
|
||||
@@ -54,7 +54,6 @@ set(HAL_SOURCES
|
||||
|
||||
set(COMMON_SOURCES
|
||||
${PEC930_SDK_DIR}/Common/printf.c
|
||||
${PEC930_SDK_DIR}/Common/syslog.c
|
||||
)
|
||||
|
||||
file(GLOB_RECURSE APP_SOURCES "app/src/*.c")
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
/**
|
||||
* @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
|
||||
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* 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
|
||||
+51
-51
@@ -1,51 +1,51 @@
|
||||
/**
|
||||
* Copyright (c) 2024 Wei-Lun Hsu. All Rights Reserved.
|
||||
*/
|
||||
/** @file main.h
|
||||
*
|
||||
* @author Wei-Lun Hsu
|
||||
* @version 0.1
|
||||
* @date 2024/08/29
|
||||
* @license
|
||||
* @description
|
||||
*/
|
||||
|
||||
#ifndef __main_H_wxBnwdPw_lq7D_H4I5_sHPp_uXof15pFLp4V__
|
||||
#define __main_H_wxBnwdPw_lq7D_H4I5_sHPp_uXof15pFLp4V__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
#include "hal_device.h"
|
||||
#include "syslog.h"
|
||||
//=============================================================================
|
||||
// Constant Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Macro Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Structure Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Global Data Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Private Function Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Public Function Definition
|
||||
//=============================================================================
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
/**
|
||||
* Copyright (c) 2024 Wei-Lun Hsu. All Rights Reserved.
|
||||
*/
|
||||
/** @file main.h
|
||||
*
|
||||
* @author Wei-Lun Hsu
|
||||
* @version 0.1
|
||||
* @date 2024/08/29
|
||||
* @license
|
||||
* @description
|
||||
*/
|
||||
|
||||
#ifndef __main_H_wxBnwdPw_lq7D_H4I5_sHPp_uXof15pFLp4V__
|
||||
#define __main_H_wxBnwdPw_lq7D_H4I5_sHPp_uXof15pFLp4V__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
#include "hal_device.h"
|
||||
#include "printf.h"
|
||||
//=============================================================================
|
||||
// Constant Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Macro Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Structure Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Global Data Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Private Function Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Public Function Definition
|
||||
//=============================================================================
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* @file uart_cmd.h
|
||||
* @brief UART command interface via PB5(RX) / PB3(TX)
|
||||
*/
|
||||
|
||||
#ifndef __uart_cmd_H__
|
||||
#define __uart_cmd_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "hal_device.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
//=============================================================================
|
||||
// Constant Definition
|
||||
//=============================================================================
|
||||
#define UART_CMD_BAUD_RATE 115200
|
||||
#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
|
||||
//=============================================================================
|
||||
|
||||
/**
|
||||
* @brief Initialize UART0 with PB3(TX) PB5(RX), enable RX interrupt
|
||||
*/
|
||||
void uart_cmd_init(void);
|
||||
|
||||
/**
|
||||
* @brief Process received command (call in main loop when g_uart_rx_complete is set)
|
||||
*/
|
||||
void uart_cmd_process(void);
|
||||
|
||||
/**
|
||||
* @brief Send a null-terminated string via UART0 TX
|
||||
*/
|
||||
void uart_cmd_send_str(const char *str);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,197 +0,0 @@
|
||||
/**
|
||||
* @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();
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* 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
|
||||
//=============================================================================
|
||||
|
||||
+62
-71
@@ -1,71 +1,62 @@
|
||||
/**
|
||||
* Copyright (c) 2024 Wei-Lun Hsu. All Rights Reserved.
|
||||
*/
|
||||
/** @file main.c
|
||||
*
|
||||
* @author Wei-Lun Hsu
|
||||
* @version 0.1
|
||||
* @date 2024/08/29
|
||||
* @license
|
||||
* @description
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "cmd_srv.h"
|
||||
//=============================================================================
|
||||
// Constant Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Macro Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Structure Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Global Data Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Private Function Definition
|
||||
//=============================================================================
|
||||
static void sysclk_init(void)
|
||||
{
|
||||
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)
|
||||
{
|
||||
sysclk_init();
|
||||
uart_init();
|
||||
test_pin_init();
|
||||
|
||||
uart_cmd_send_str("CMD service started\n");
|
||||
|
||||
while (1)
|
||||
{
|
||||
uart_cmd_process();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/**
|
||||
* Copyright (c) 2024 Wei-Lun Hsu. All Rights Reserved.
|
||||
*/
|
||||
/** @file main.c
|
||||
*
|
||||
* @author Wei-Lun Hsu
|
||||
* @version 0.1
|
||||
* @date 2024/08/29
|
||||
* @license
|
||||
* @description
|
||||
*/
|
||||
|
||||
|
||||
#include "main.h"
|
||||
#include "isr.h"
|
||||
#include "uart_cmd.h"
|
||||
//=============================================================================
|
||||
// Constant Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Macro Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Structure Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Global Data Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Private Function Definition
|
||||
//=============================================================================
|
||||
static void sysclk_init(void)
|
||||
{
|
||||
SYSCFG_ClkInitTypeDef SysClkInit = { 0 };
|
||||
SysClkInit.ClkSource = SYSCFG_ClkSrc_HSI;
|
||||
SYSCFG_SysClkConfig(&SysClkInit);
|
||||
|
||||
sys_config_systick(SYS_TICK_1_MS);
|
||||
}
|
||||
|
||||
//=============================================================================
|
||||
// Public Function Definition
|
||||
//=============================================================================
|
||||
int main(void)
|
||||
{
|
||||
sysclk_init();
|
||||
|
||||
uart_cmd_init();
|
||||
uart_cmd_send_str("This is demo.\r\n");
|
||||
|
||||
while (1)
|
||||
{
|
||||
__NOP();
|
||||
//uart_cmd_process();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,214 @@
|
||||
/**
|
||||
* @file uart_cmd.c
|
||||
* @brief UART command interface via PB5(RX) / PB3(TX)
|
||||
*/
|
||||
|
||||
#include "uart_cmd.h"
|
||||
#include "printf.h"
|
||||
#include <string.h>
|
||||
|
||||
//=============================================================================
|
||||
// Constant Definition
|
||||
//=============================================================================
|
||||
#define UART_CMD_TX_PORT GPIOB
|
||||
#define UART_CMD_TX_PIN GPIO_Pin_03
|
||||
#define UART_CMD_TX_AF GPIO_AF_1
|
||||
|
||||
#define UART_CMD_RX_PORT GPIOB
|
||||
#define UART_CMD_RX_PIN GPIO_Pin_05
|
||||
#define UART_CMD_RX_AF GPIO_AF_1
|
||||
|
||||
//=============================================================================
|
||||
// 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
|
||||
//=============================================================================
|
||||
|
||||
/* printf library calls _putchar for each character */
|
||||
void _putchar(char character)
|
||||
{
|
||||
UART_SendData(UART0, character);
|
||||
UART_WaitTxFifoEmpty(UART0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
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 uart0_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_init(void)
|
||||
{
|
||||
/* Configure PB3 as UART0 TX */
|
||||
GPIO_InitTypeDef gpio_init = {0};
|
||||
gpio_init.GPIO_Pin = UART_CMD_TX_PIN;
|
||||
gpio_init.GPIO_Mode = GPIO_Mode_AF;
|
||||
gpio_init.GPIO_AF_Mode = UART_CMD_TX_AF;
|
||||
GPIO_Init(UART_CMD_TX_PORT, &gpio_init);
|
||||
|
||||
/* Configure PB5 as UART0 RX */
|
||||
gpio_init.GPIO_Pin = UART_CMD_RX_PIN;
|
||||
gpio_init.GPIO_Mode = GPIO_Mode_AF;
|
||||
gpio_init.GPIO_AF_Mode = UART_CMD_RX_AF;
|
||||
GPIO_Init(UART_CMD_RX_PORT, &gpio_init);
|
||||
|
||||
/* Configure UART0 */
|
||||
UART_InitTypeDef uart_init = {0};
|
||||
uart_init.BaudRate = UART_CMD_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(UART0, &uart_init);
|
||||
|
||||
/* Enable RX not-empty interrupt */
|
||||
UART_ITConfig(UART0, 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, uart0_rx_handler, &irq_attr);
|
||||
|
||||
/* Start UART */
|
||||
UART_Start(UART0);
|
||||
|
||||
sys_enable_girq();
|
||||
}
|
||||
|
||||
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 1/0 - enable/disable PWM output\r\n");
|
||||
uart_cmd_send_str(" notify 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", &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 1/0\r\n");
|
||||
}
|
||||
}
|
||||
else if (cmd_match_with_arg(g_uart_rx_buf, "notify", &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 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;
|
||||
}
|
||||
Reference in New Issue
Block a user