当前位置:网站首页>STM32上μC/Shell移植与应用
STM32上μC/Shell移植与应用
2022-04-23 03:56:00 【Chenxr32】
本着“停课不停学”的原则,在家也不能闲着,最近在进一步探索μC/OS时发现了μC/Shell,果断决定尝试一下。
Micrium的产品开源了,GitHub上可以下载代码。https://github.com/weston-embedded
下面先介绍μC/Shell的文件结构,再介绍移植方法和实际应用。
μC/Shell文件结构
\Cfg\Template\shell_cfg.h
μC/Shell的配置文件,设置指令表大小、指令参数格式、最大指令长度等参数。
\Cmd\General\sh_shell.c, sh_shell.h
我没看明白这两个文件是干什么的,移植时也没用上。
\Source\shell.c
μC/Shell的所有函数接口都在这个文件里,包括Shell初始化、添加指令、删除指令、执行指令等函数接口。
\Source\shell.h
μC/Shell的头文件,包括一些函数、函数指针、结构体、变量的声明和宏定义。
\Terminal\ 终端文件夹,提供的接口方便单片机与PC终端交互。
\Terminal\Cfg\Template\terminal_cfg.h
终端的配置文件。
\Terminal\Mode\VT100\terminal_mode.c
包括读写终端指令的函数。
\Terminal\OS\uCOS-III\terminal_os.c
配合uCOS-III使用的文件,创建终端任务。还有一个可以用于uCOS-II的文件。
\Terminal\Serial\Template\terminal_serial.c
输入输出接口文件,移植时需要修改文件里的函数以配合单片机硬件接口。
\Terminal\Source\terminal.c, terminal.h
包括终端初始化、终端任务函数等。
μC/Shell移植
集成开发环境:keil5。单片机:STM32F405,事先已移植好μC/OS-III操作系统。
1、在工程目录下创建UCShell文件夹,将μC/Shell的文件复制到UCShell文件夹内,文件结构可以自己调整一下。
2、在Keil中建立UCShell和UCShell_terminal分组,将相应文件添加入工程,我用的uCOS-III的terminal_os.c文件,没有添加\Cmd\General\下的文件。配置头文件路径。
3、修改terminal_serial.c文件。因为我自己写的外设配置文件是用C++写的,为了方便调用C++函数接口,我将terminal_serial.c改为了terminal_serial.cpp,在terminal.h文件里加extern "C"。
/*
TerminalSerial_Init 函数用于配置单片机串口,我在其他文件里配置了串口。
*/
CPU_BOOLEAN TerminalSerial_Init (void)
{
return (DEF_OK);//原版文件里是DEF_FAIL,一定要改成DEF_OK。
}
/*
TerminalSerial_Exit 关闭串口函数,没必要改。
*/
void TerminalSerial_Exit (void)
{
}
/*
TerminalSerial_Wr 串口发送字符串,添加串口发送函数。
*/
CPU_INT16S TerminalSerial_Wr (void *pbuf,
CPU_SIZE_T buf_len)
{
board.USART6Send((unsigned char*)pbuf,buf_len);//USART6发送字符串。
return (-1);
}
/*
TerminalSerial_RdByte 从串口读取一字节。
我是从缓冲队列中取字节,缓冲队列中的字节是在串口中断时送入。
当缓冲队列空时,返回值一要是0x00。
*/
CPU_INT08U TerminalSerial_RdByte (void)
{
unsigned char byte = 0x00;
if(!shell_rx_queue.EmptyCheck())
{
byte = shell_rx_queue.GetData();
}
return byte;
}
/*
TerminalSerial_WrByte 串口发送一字节,添加串口发送函数。
*/
void TerminalSerial_WrByte (CPU_INT08U c)
{
board.USART6Send((unsigned char*)&c,1);
}
至此,μC/Shell移植完成,接下来需要用实践检验移植是否成功。
μC/Shell应用
新建myshell.cpp和myshell.h文件,写一个命令行控制LED亮灭的代码验证移植是否成功。
//myshell.c
#include "myshell.h"
#include "shell.h"
#include "terminal.h"
#include "stm32f4xx.h"
//函数原型,函数的参数必须是这种形式,否则不能正常使用。
CPU_INT16S LedCmd(CPU_INT16U argc,
CPU_CHAR *argv[],
SHELL_OUT_FNCT out_fnct,
SHELL_CMD_PARAM *pcmd_param);
//指令表,"LED"是指令,LedCmd是指令回调函数。
static SHELL_CMD ShellCmdTbl[] =
{
{"LED", LedCmd},
{0, 0}
};
//Shell初始化,在μC/OS系统启动任务中调用。
void ShellInit(void)
{
SHELL_ERR err;
Shell_Init(); //初始化Shell
Terminal_Init(); //初始化终端
Shell_CmdTblAdd((CPU_CHAR*)"LED",ShellCmdTbl,&err);//添加一项指令
}
/****************************************************
//LED指令回调函数,参数必须是这种形式。
//argc 记录参数个数,只有指令没有参数时为1;
//argv 字符串指针数组,记录指令及参数,argv[0]是指令LED,argv[1]是参数ON或OFF,如果参数更多,参数会存在argv[2]...
//out_fnct 函数指针,函数响应时指向terminal.c中的Terminal_OutFnct函数;
//pcmd_param 目前还不知道这个参数干什么用的。
*****************************************************/
CPU_INT16S LedCmd(CPU_INT16U argc,
CPU_CHAR *argv[],
SHELL_OUT_FNCT out_fnct,
SHELL_CMD_PARAM *pcmd_param)
{
CPU_INT16S output;
CPU_INT16S ret_val;
if(argc == 2)
{
if(!Str_Cmp(argv[1],"ON"))
{
GPIOB->BSRRL = GPIO_Pin_15;//亮灯
}
else if(!Str_Cmp(argv[1],"OFF"))
{
GPIOB->BSRRH = GPIO_Pin_15;//关灯
}
output = out_fnct((CPU_CHAR*)"OK",(CPU_INT16U)Str_Len("OK"),pcmd_param->pout_opt);
}
else if(argc > 2)
{
output = out_fnct((CPU_CHAR*)"error: too many arguments",(CPU_INT16U)Str_Len("error: too many arguments"),pcmd_param->pout_opt);
}
else
{
output = out_fnct((CPU_CHAR*)"error: missing argument",(CPU_INT16U)Str_Len("error: missing argument"),pcmd_param->pout_opt);
}
ret_val = output;
return ret_val;
}
代码写好后可以试一下,在SecureCRT上输入几条指令看一下现象。(LED亮灭我就不拍照了)
实践证明,移植成功,μC/Shell可以正常使用。
μC/Shell的详细说明请看μC/Shell官方文档 。
μC/Shell模板下载:https://download.csdn.net/download/QDchenxr/12262695
版权声明
本文为[Chenxr32]所创,转载请带上原文链接,感谢
https://blog.csdn.net/QDchenxr/article/details/105025236
边栏推荐
- ROS series (I): rapid installation of ROS
- php导出Excel表格
- OpenCV----YOLACT实例分割模型推理
- RuntimeError: output with shape [4, 1, 512, 512] doesn‘t match the broadcast shape[4, 4, 512, 512]
- VS Studio 修改C语言scanf等报错
- 【测绘程序设计】坐标反算神器V1.0(附C/C#/VB源程序)
- PolarMask is not in the models registry
- ROS series (IV): ROS communication mechanism series (5): Service Communication Practice
- 【ICCV 2019】MAP-VAE:Multi-Angle Point Cloud-VAE: Unsupervised Feature Learning for 3D Point Clouds..
- What if win10 doesn't have a local group policy?
猜你喜欢
Idea debug debugging tutorial
Common auxiliary classes
Common net HP UNIX system FTP server listfiles returns null solution.
使用大华设备开发行AI人流量统计出现时间不正确的原因分析
对象和类的概念
【BIM入门实战】Revit建筑墙体:构造、包络、叠层图文详解
创下国产手机在海外市场销量最高纪录的小米,重新关注国内市场
Xiaomi, which has set the highest sales record of domestic mobile phones in overseas markets, paid renewed attention to the domestic market
STM32 advanced timer com event
Machine translation baseline
随机推荐
中国移动日赚2.85亿很高?其实是5G难带来更多利润,那么钱去哪里了?
Add the compiled and installed Mysql to the path environment variable
51 single chip microcomputer: D / a digital to analog conversion experiment
Retrieval question answering system baseline
Basic introduction to spot gold
Picture synthesis video
Idea debug debugging tutorial
How to introduce opencv into cmake project
SQL topic exercise summary
VS Studio 修改C語言scanf等報錯
php导出Excel表格
STM32 advanced timer com event
Network principle | connection management mechanism in TCP / IP important protocol and core mechanism
[AI vision · quick review of NLP natural language processing papers today, issue 29] Mon, 14 Feb 2022
Nel ASA:挪威Herøya设施正式启用
ROS series (IV): ROS communication mechanism series (2): Service Communication
[latex] differences in the way scores are written
ERROR: Could not find a version that satisfies the requirement win32gui
Process seven state transition diagram
[AI vision · quick review of NLP natural language processing papers today, issue 30] Thu, 14 APR 2022