finsh初步
一. finsh在RT-Thread中被設(shè)計(jì)成一個(gè)獨(dú)立的線程,它試圖從外部設(shè)備中獲得用戶的輸入,然后對(duì)用戶命令進(jìn)行解析執(zhí)行。
正確使用finsh需要一個(gè)關(guān)聯(lián)過(guò)程:
rt_hw_board_init()函數(shù)調(diào)用串口初始化函數(shù)rt_hw_usart_init(),此函數(shù)初始化串口,并向系統(tǒng)注冊(cè)“usart1”設(shè)備,接著系統(tǒng)調(diào)用rt_console_set_device()函數(shù)設(shè)置“usart1”作為console輸出。
rtthread_startup()函數(shù)中調(diào)用finsh_system_init()初始化finsh組件,并調(diào)用finsh_set_device(“usart1”),將“usart1”和finsh關(guān)聯(lián)起來(lái),這樣usart1的輸入即可被finsh讀入并分析、執(zhí)行。
上述過(guò)程需要正確配置如下項(xiàng):
#define RT_USING_FINSH //rt_config.h
#define RT_USING_UART1 //rt_config.h
#define RT_CONSOLE_DEVICE_NAME “usart1” //rtconfig.h
二.finsh中自定義命令、函數(shù)及變量
使用宏方式輸出,需在rtconfig.h中定義FINSH_USING_SYMTAB
FINSH_FUNCTION_EXPORT()
FINSH_FUNCTION_EXPORT_ALIAS()
FINSH_VAR_EXPORT()
#include <rtthread.h>
#ifdef RT_USING_FINSH
//#ifdef FINSH_USING_SYMTAB
#include <finsh.h> // must include
static int wang_num = 1;
void wang(int num)
{
if(num < 10000)
wang_num = wang_num++ * 2;
rt_kprintf("Param:%d
", wang_num);
}
FINSH_FUNCTION_EXPORT(wang, just test)
FINSH_VAR_EXPORT(wang_num, finsh_type_int, just var test)
//#endif
#endif
執(zhí)行結(jié)果:
總結(jié)
- 上一篇: ansible命令应用示例
- 下一篇: 100NED