314 lines
7.3 KiB
C
314 lines
7.3 KiB
C
/**
|
|
* @file app_cli.c
|
|
* @brief CLI framework: an independent task receives command lines from the
|
|
* USART1 RX interrupt queue, parses and executes them, and answers
|
|
* through USART1 DMA TX.
|
|
*
|
|
* Line terminators: "\r\n", "\n\r", "\r" and "\n" are all accepted.
|
|
* Editing: backspace (0x08 / 0x7F) erases the last character.
|
|
*/
|
|
#include "app_cli.h"
|
|
#include "bsp_usart.h"
|
|
#include "bsp_led.h"
|
|
#include "app_tasks.h"
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdarg.h>
|
|
|
|
#include "FreeRTOS.h"
|
|
#include "task.h"
|
|
|
|
#define CLI_TASK_STACK 256 /* in words (printf needs more stack) */
|
|
#define CLI_TASK_PRIO 3
|
|
#define CLI_LINE_BUF_SIZE 64
|
|
#define CLI_TX_BUF_SIZE 96
|
|
#define CLI_MAX_ARGS 8
|
|
#define CLI_PROMPT "n32> "
|
|
|
|
typedef void (*CliCmdHandler_t)(int argc, char* argv[]);
|
|
|
|
typedef struct
|
|
{
|
|
const char* name;
|
|
const char* help;
|
|
CliCmdHandler_t handler;
|
|
} CliCmd_t;
|
|
|
|
static char s_txBuf[CLI_TX_BUF_SIZE];
|
|
|
|
/**
|
|
* @brief Send a string through USART1 DMA TX.
|
|
* @note Strings must contain explicit "\r\n" (no auto CR like fputc).
|
|
*/
|
|
static void cli_write(const char* str)
|
|
{
|
|
bsp_usart_write_dma((const uint8_t*)str, (uint16_t)strlen(str));
|
|
}
|
|
|
|
/**
|
|
* @brief Formatted CLI output through USART1 DMA TX.
|
|
*/
|
|
static void cli_printf(const char* fmt, ...)
|
|
{
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
vsnprintf(s_txBuf, CLI_TX_BUF_SIZE, fmt, args);
|
|
va_end(args);
|
|
s_txBuf[CLI_TX_BUF_SIZE - 1] = '\0';
|
|
cli_write(s_txBuf);
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Built-in commands */
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
static void CmdHelp(int argc, char* argv[]);
|
|
|
|
/**
|
|
* @brief "version": show firmware and clock info.
|
|
*/
|
|
static void CmdVersion(int argc, char* argv[])
|
|
{
|
|
(void)argc;
|
|
(void)argv;
|
|
cli_write("N32WB031 FreeRTOS + UART(DMA) + LED + CLI demo\r\n");
|
|
cli_printf("SystemCoreClock = %lu Hz\r\n", (unsigned long)SystemCoreClock);
|
|
}
|
|
|
|
/**
|
|
* @brief "log [on|off]": show or set the active log printing switch.
|
|
*/
|
|
static void CmdLog(int argc, char* argv[])
|
|
{
|
|
if (argc < 2)
|
|
{
|
|
cli_printf("log is %s\r\n", AppTasks_GetLogEnabled() ? "on" : "off");
|
|
return;
|
|
}
|
|
|
|
if (strcmp(argv[1], "on") == 0)
|
|
{
|
|
AppTasks_SetLogEnabled(1);
|
|
}
|
|
else if (strcmp(argv[1], "off") == 0)
|
|
{
|
|
AppTasks_SetLogEnabled(0);
|
|
}
|
|
else
|
|
{
|
|
cli_write("usage: log [on|off]\r\n");
|
|
return;
|
|
}
|
|
|
|
cli_printf("log set to %s\r\n", AppTasks_GetLogEnabled() ? "on" : "off");
|
|
}
|
|
|
|
/**
|
|
* @brief "led <1|2> <on|off|toggle>": control a board LED.
|
|
*/
|
|
static void CmdLed(int argc, char* argv[])
|
|
{
|
|
GPIO_Module* port;
|
|
uint16_t pin;
|
|
|
|
if (argc < 3)
|
|
{
|
|
cli_write("usage: led <1|2> <on|off|toggle>\r\n");
|
|
return;
|
|
}
|
|
|
|
if (argv[1][0] == '1' && argv[1][1] == '\0')
|
|
{
|
|
port = LED1_PORT;
|
|
pin = LED1_PIN;
|
|
}
|
|
else if (argv[1][0] == '2' && argv[1][1] == '\0')
|
|
{
|
|
port = LED2_PORT;
|
|
pin = LED2_PIN;
|
|
}
|
|
else
|
|
{
|
|
cli_write("usage: led <1|2> <on|off|toggle>\r\n");
|
|
return;
|
|
}
|
|
|
|
if (strcmp(argv[2], "on") == 0)
|
|
{
|
|
bsp_led_on(port, pin);
|
|
}
|
|
else if (strcmp(argv[2], "off") == 0)
|
|
{
|
|
bsp_led_off(port, pin);
|
|
}
|
|
else if (strcmp(argv[2], "toggle") == 0)
|
|
{
|
|
bsp_led_toggle(port, pin);
|
|
}
|
|
else
|
|
{
|
|
cli_write("usage: led <1|2> <on|off|toggle>\r\n");
|
|
return;
|
|
}
|
|
|
|
cli_printf("led %s %s done\r\n", argv[1], argv[2]);
|
|
}
|
|
|
|
/* Command table; add new commands here */
|
|
static const CliCmd_t s_cmds[] = {
|
|
{"help", "list all commands", CmdHelp},
|
|
{"version", "show firmware/clock info", CmdVersion},
|
|
{"log", "log [on|off]: active printing switch (default off)", CmdLog},
|
|
{"led", "led <1|2> <on|off|toggle>: control board LED", CmdLed},
|
|
};
|
|
|
|
#define CLI_CMD_COUNT (sizeof(s_cmds) / sizeof(s_cmds[0]))
|
|
|
|
/**
|
|
* @brief "help": list all registered commands.
|
|
*/
|
|
static void CmdHelp(int argc, char* argv[])
|
|
{
|
|
uint32_t i;
|
|
(void)argc;
|
|
(void)argv;
|
|
|
|
cli_write("commands:\r\n");
|
|
for (i = 0; i < CLI_CMD_COUNT; i++)
|
|
{
|
|
cli_printf(" %-8s %s\r\n", s_cmds[i].name, s_cmds[i].help);
|
|
}
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Line parsing / dispatch */
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
/**
|
|
* @brief Tokenize a received line in place and run the matching command.
|
|
*/
|
|
static void cli_dispatch(char* line)
|
|
{
|
|
char* argv[CLI_MAX_ARGS];
|
|
int argc = 0;
|
|
char* p = line;
|
|
uint32_t i;
|
|
|
|
/* Split on spaces/tabs */
|
|
while (*p != '\0' && argc < CLI_MAX_ARGS)
|
|
{
|
|
while (*p == ' ' || *p == '\t')
|
|
{
|
|
p++;
|
|
}
|
|
if (*p == '\0')
|
|
{
|
|
break;
|
|
}
|
|
argv[argc++] = p;
|
|
while (*p != '\0' && *p != ' ' && *p != '\t')
|
|
{
|
|
p++;
|
|
}
|
|
if (*p != '\0')
|
|
{
|
|
*p++ = '\0';
|
|
}
|
|
}
|
|
|
|
if (argc == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
for (i = 0; i < CLI_CMD_COUNT; i++)
|
|
{
|
|
if (strcmp(argv[0], s_cmds[i].name) == 0)
|
|
{
|
|
s_cmds[i].handler(argc, argv);
|
|
return;
|
|
}
|
|
}
|
|
|
|
cli_printf("unknown command: %s (try 'help')\r\n", argv[0]);
|
|
}
|
|
|
|
/**
|
|
* @brief CLI task: receive bytes, edit the line, dispatch on terminator.
|
|
*/
|
|
static void CliTask(void* argument)
|
|
{
|
|
static char line[CLI_LINE_BUF_SIZE];
|
|
uint16_t len = 0;
|
|
uint8_t swallow = 0; /* complementary terminator to swallow once */
|
|
uint8_t ch;
|
|
(void)argument;
|
|
|
|
cli_write("\r\nCLI ready. Type 'help' for commands.\r\n" CLI_PROMPT);
|
|
|
|
for (;;)
|
|
{
|
|
if (bsp_usart_read_byte(&ch, BSP_USART_WAIT_FOREVER) == 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
/* Swallow the second half of a "\r\n" / "\n\r" pair */
|
|
if (swallow != 0)
|
|
{
|
|
uint8_t exp = swallow;
|
|
swallow = 0;
|
|
if (ch == exp)
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
|
|
if (ch == '\r' || ch == '\n')
|
|
{
|
|
/* Any of "\r", "\n", "\r\n", "\n\r" terminates the line */
|
|
swallow = (ch == '\r') ? (uint8_t)'\n' : (uint8_t)'\r';
|
|
|
|
bsp_usart_tx_lock();
|
|
cli_write("\r\n");
|
|
line[len] = '\0';
|
|
cli_dispatch(line);
|
|
cli_write(CLI_PROMPT);
|
|
bsp_usart_tx_unlock();
|
|
|
|
len = 0;
|
|
}
|
|
else if (ch == '\b' || ch == 0x7F)
|
|
{
|
|
if (len > 0)
|
|
{
|
|
len--;
|
|
cli_write("\b \b");
|
|
}
|
|
}
|
|
else if (ch >= 0x20 && ch < 0x7F)
|
|
{
|
|
if (len < CLI_LINE_BUF_SIZE - 1)
|
|
{
|
|
line[len++] = (char)ch;
|
|
bsp_usart_write_dma(&ch, 1); /* echo */
|
|
}
|
|
else
|
|
{
|
|
cli_write("\a"); /* line full: bell */
|
|
}
|
|
}
|
|
/* other control characters are ignored */
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Create the CLI task.
|
|
*/
|
|
void AppCli_Init(void)
|
|
{
|
|
xTaskCreate(CliTask, "CLI", CLI_TASK_STACK, NULL, CLI_TASK_PRIO, NULL);
|
|
}
|