1. Shell 命令行调试¶
本 demo 将演示如何使用 shell 通过串口进行命令行调试。
1.1. 准备工具¶
pc控制端使用串口终端软件:xshell 或者 mobaxterm
连接介质:usb转串口 or 网络 or usb
1.3. 软件实现¶
1.3.1. shell 移植到串口¶
软件代码见
examples/shell
1 2 | #define BSP_UART_CLOCK_SOURCE ROOT_CLOCK_SOURCE_PLL_96M
#define BSP_UART_CLOCK_DIV 0
|
配置
UART
设备时钟源,见bsp/board/bl706_iot/clock_config.h
1 2 | #define CONFIG_GPIO14_FUNC GPIO_FUN_UART0_TX
#define CONFIG_GPIO15_FUNC GPIO_FUN_UART0_RX
|
配置
UART
设备复用引脚,见bsp/board/bl706_iot/pinmux_config.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #define BSP_USING_UART0
#if defined(BSP_USING_UART0)
#ifndef UART0_CONFIG
#define UART0_CONFIG \
{ \
.id = 0, \
.baudrate = 2000000,\
.databits = UART_DATA_LEN_8, \
.stopbits = UART_STOP_ONE, \
.parity = UART_PAR_NONE, \
.fifo_threshold = 1, \
}
#endif
#endif
|
使能
BSP_USING_UART0
并配置UART
设备配置,见bsp/board/bl706_iot/peripheral_config.h
1 | bflb_platform_init();
|
在
bflb_platform_init
函数中,我们已经注册并且打开了一个调试用的串口设备,给用户实现一个MSG
的基本功能用作打印输出报文。具体实现如下
1 2 3 4 5 6 7 8 9 | uart_register(board_get_debug_uart_index(), "debug_log");
struct device *uart = device_find("debug_log");
if (uart)
{
device_open(uart, DEVICE_OFLAG_STREAM_TX | DEVICE_OFLAG_INT_RX);
device_set_callback(uart, NULL);
device_control(uart, DEVICE_CTRL_CLR_INT, (void *)(UART_RX_FIFO_IT));
}
|
首先调用
uart_register
函数注册UART
设备,当前注册UART0
然后通过
find
函数找到设备对应的句柄,保存于uart
句柄中最后使用
device_open
以轮询发送和中断接收来打开uart
设备,默认关闭中断并且不注册接收中断回调函数
1 2 3 4 5 | struct device *uart = device_find("debug_log");
if (uart) {
device_set_callback(uart, shell_irq_callback);
device_control(uart, DEVICE_CTRL_SET_INT, (void *)(UART_RX_FIFO_IT));
}
|
通过
device_set_callback
函数,为UART0
注册接收中断服务函数。通过device_control
函数打开UART_RX_FIFO_IT
中断
1 2 3 4 5 6 7 8 | void shell_irq_callback(struct device *dev, void *args, uint32_t size, uint32_t state)
{
uint8_t data;
if (state == UART_EVENT_RX_FIFO) {
data = *(uint8_t *)args;
shell_handler(data);
}
}
|
中断回调函数中,判断
state
是否是UART_EVENT_RX_FIFO
,是的话就将接收的字节传入shell_handler
函数。
1 | shell_init();
|
调用
shell_init
初始化 shell 组件。
1.3.2. SHELL 命令注册¶
以下两种注册方式任选
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | void hellowd()
{
MSG("hello World\r\n");
}
int echo(int argc, char *argv[])
{
MSG("%dparameter(s)\r\n", argc);
for (uint8_t i = 1; i < argc; i++) {
MSG("%s\r\n", argv[i]);
}
return 0;
}
SHELL_CMD_EXPORT(hellowd, hellowd test)
SHELL_CMD_EXPORT(echo, echo test)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | void hellowd()
{
MSG("hello World\r\n");
}
int cmd_echo(int argc, char *argv[])
{
MSG("%dparameter(s)\r\n", argc);
for (uint8_t i = 1; i < argc; i++) {
MSG("%s\r\n", argv[i]);
}
return 0;
}
SHELL_CMD_EXPORT_ALIAS(hellowd, hellwd,hellowd test)
SHELL_CMD_EXPORT_ALIAS(cmd_echo, echo,echo test)
|
1.4. 编译和烧录¶
CDK 编译
打开项目中提供的工程文件:shell.cdkproj
参照 Windows 下使用 CDK (类 MDK Keil)开发指南 的步骤编译下载即可
命令行编译
1 2 | $ cd <sdk_path>/bl_mcu_sdk
$ make BOARD=bl706_iot APP=shell
|
烧录