源码:
nr_micro_shell: shell for MCU. 单片机命令行交互。
参考连接
nr_micro_shell在STM32+FreeRTOS平台的移植和使用_nr micro shell-优快云博客
裸机移植:
【嵌入式】开源shell命令行的移植和使用(1)——nr_micro_shell_嵌入式 shell 接收命令-优快云博客
移植:
1. 移植的话就是将除了nr_micro_shell_thread.c外的所有文件添加到工程中(thread_demo.c是我自己添加的处理shell线程).
2. 注释掉nr_micro_shell_config.h中的以下两个文件
3. 修改结束符标识, 以及输出前缀
修改历史数据保存条数
#define NR_SHELL_MAX_CMD_HISTORY_NUM 3
使用export机制
#define NR_SHELL_USING_EXPORT_CMD
对接自己的打印函数
#define shell_printf(fmt, args...) printf(fmt, ##args);
个人修改部分:
在nr_micro_shell_command.c中增加清屏函数
void shell_clear_cmd(char argc, char *argv)
{
shell_printf("\x1b[2J\x1b[H");
}
增加shell命令处理线程
#include "FreeRTOS.h"
#include "nr_micro_shell.h"
#include "semphr.h"
#include "stm32f4xx.h"
static SemaphoreHandle_t BinarySemaphore;
static BaseType_t xHigherPriorityTaskWoken;
static char ch;
/* 串口回调 */
void uart_handler()
{
ch = USART_ReceiveData(USART1);
xSemaphoreGiveFromISR(BinarySemaphore, &xHigherPriorityTaskWoken);
}
static char nr_shell_getchar(void)
{
xSemaphoreTake(BinarySemaphore, portMAX_DELAY);
return ch;
}
void thread_shell(void *pvParameters)
{
shell_init();
BinarySemaphore = xSemaphoreCreateBinary();
while(1)
{
shell(nr_shell_getchar());
}
}
使用二值信号量, 上下键切换历史命令的时候异常, 我换了如下方法来做.
#include "FreeRTOS.h"
#include "task.h"
#include "nr_micro_shell.h"
#include "stm32f4xx.h"
#include "que.h"
/* 创建接收队列 */
creat_que(rx_que, 10);
/* 串口回调 */
void uart_handler()
{
InQue(rx_que, USART_ReceiveData(USART1));
}
void thread_shell(void *pvParameters)
{
unsigned char ch;
shell_init();
while(1)
{
while(!QueIsEmpty(rx_que))
{
OutQue(rx_que, &ch);
shell(ch);
}
vTaskDelay(50);
}
}
/*
1. 本来是用二值信号量来做的, 但是发现在使用上下键切换历史命令的时候会出现异常.
2. 使用队列当然也是可以的, 但是可能会使得任务会隔50ms执行一次.
*/
解决查询历史命令时会丢失一个字符
如下图, 修改这四个位置.这几个打印上一行, 他这个开始位置定位有误, 具体我也不是太懂, 下面是关于这几个特殊打印的连接
Linux开发_控制shell光标_linux shell 脚本字符控制光标-优快云博客
移植demo:
https://download.youkuaiyun.com/download/qq_38591801/89321413