一. finsh在RT-Thread中被設計成一個獨立的線程,它試圖從外部設備中獲得用戶的輸入,然后對用戶命令進行解析執行。
正確使用finsh需要一個關聯過程:
- rt_hw_board_init()函數調用串口初始化函數rt_hw_usart_init(),此函數初始化串口,並向系統注冊“usart1”設備,接着系統調用rt_console_set_device()函數設置“usart1”作為console輸出。
- rtthread_startup()函數中調用finsh_system_init()初始化finsh組件,並調用finsh_set_device(“usart1”),將“usart1”和finsh關聯起來,這樣usart1的輸入即可被finsh讀入並分析、執行。
上述過程需要正確配置如下項:
#define RT_USING_FINSH //rt_config.h
#define RT_USING_UART1 //rt_config.h
#define RT_CONSOLE_DEVICE_NAME “usart1” //rtconfig.h
二.finsh中自定義命令、函數及變量
使用宏方式輸出,需在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\n", wang_num); } FINSH_FUNCTION_EXPORT(wang, just test) FINSH_VAR_EXPORT(wang_num, finsh_type_int, just var test) //#endif #endif
執行結果: