0%

terminal for microcontrol

前言

此前的嵌入式项目开发过程中,因为硬件资源紧张,硬件工程师在进行原理图设计的时候没有使用芯片的串口,导致软件开发和调试极为困难。后来考虑使用USB CDC虚拟串口搭配命令行控制台进行调试,在gayhub上发现了这个开源的命令行控制项目 Terminal

Terminal简介

Terminal 支持跨平台、可选配置、日志命令、指令中断、回车和删除等常用按键配置,最大耗费系统内存不到10KB。

代码的目录结构如下,

  • lib 中包含自定义的字符串和队列操作。
  • module 中包含对input解析和log的相关操作。
  • terminal.hterminal.c 实现了命令行控制台的基本功能,包括初始化、命令添加、命令执行、命令解析等API,供用户直接使用。
1
2
3
4
5
6
7
8
9
terminal # ls 
LICENSE README.md def_config examples lib module terminal.c terminal.h
terminal # ls def_config
terminal_config.h
terminal # ls lib
cli_queue.c cli_queue.h cli_string.c cli_string.h
terminal #
terminal # ls module
cli_input.c cli_input.h cli_log.c cli_log.h cli_time.c cli_time.h

移植

参考 examples/Coocox_stm32f4的工程代码和README.md,对当前工程添加自定义配置文件 terminal_config.h

  • 使能 TERM_TX_RX_EN 开启输入输出
1
#define TERM_TX_RX_EN  (1)    // Terminal Printf (without this don,t work)
  • 复写 CLI_PrintfCLI_PutChar 输出函数,实现输出重定向
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#if (TERM_TX_RX_EN == 1)
#include <stdio.h>
#define COM_Printf(...) printf(__VA_ARGS__)
#define CLI_Printf COM_Printf
#if (ECHO_EN == 1)
#define CLI_PutChar putchar
#else // ECHO_EN != 1
#define CLI_PutChar
#endif // ECHO_EN == 1

#else // TERM_TX_RX_EN != 1
#define CLI_Printf
#define CLI_PutChar
#endif // TERM_TX_RX_EN == 1
  • 设置基本参数
    1
    2
    3
    4
    5
    #define TERM_SIZE_TASK        (20)    // Max number of commands
    #define TERM_CMD_BUF_SIZE (60) // Max number of character buffer string command
    #define TERM_CMD_LOG_SIZE (10) // Max number of loging command
    #define TERM_ARGS_BUF_SIZE (10) // Max number of arguments in one command
    #define TERM_ARG_SIZE (15) // Max number character of one arguments

另外可以使用 CLI_AddCmd 接口添加自定义命令,CLI_GetArg接口获取参数,参考my_test_commands目录下的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
static uint8_t _t1_cmd();
static uint8_t _t2_cmd();

void MyTestCmds_Init()
{
CLI_AddCmd("t1", _t1_cmd, 1, TMC_PrintStartTime | TMC_PrintStopTime, "t1 - description command");
CLI_AddCmd("t2", _t2_cmd, 0, TMC_PrintDiffTime, "t2 - description command");
}

// ***************** implementation commands ****************

uint8_t _t1_cmd()
{
uint32_t a = 0x01;
uint32_t b = 0x10;
uint32_t c = 7;

// be sure arguments
c = CLI_GetArgDec(0);

// optional arguments
CLI_GetArgHexByFlag("-a", &a);
CLI_GetArgHexByFlag("-b", &b);

CLI_Printf("\r\na: 0x%08X\r\nb: 0x%08X\r\nc: %d", (int) a, (int) b, (int) c);

return TE_OK;
}

uint8_t _t2_cmd()
{
CLI_Printf("\r\nPress ESC");
while(1)
{
CLI_CheckAbort();
}

return TE_OK;
}

Demo

已经移植好的固件,在PC上枚举出虚拟串口,使用minicom打开对应串口设备,能够看到如下的相关打印信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
➜  ~ minicom -D /dev/tty.usbmodem3079385933364

Welcome to minicom 2.7.1

OPTIONS:
Compiled on Aug 20 2018, 10:22:42.
Port /dev/tty.usbmodem3079385933364, 11:38:42

Press Meta-Z for help on special keys

****************************************************
| |
| Terminal v1.4 |
| sw ver.: Sep 10 2018 22:13:54 |
| |
****************************************************

Count base command: 4
Max command: 20

>>
>>
>> help
Count command: 4
[] - mandatory argument
<> - optional argument
| - choice between arguments
~ - reset cpu
settime - set current time
settime [h] [m] [s]
gettime - print current time