1、利用keil MDK - RTE創建rt-thread(3.1.3)工程
2、添加main.c文件。
3、點擊 <Option for target> ,選擇ARM Compiter 為 <... version 5>。
4、Use MicroLIB 和 C99 Mode 視情況選擇。
5、如果要使用shell,需要實現兩個函數:void rt_hw_console_output(const char *str) 和 char rt_hw_console_getchar(void)。
實現如下,需要注意兩點:
a、RT-Thread的 rt_kprintf 函數默認以換行符 '\n' 結尾,實現 rt_hw_console_output 時不要忘了在末尾追加回車符 '\r'。
b、實現 rt_hw_console_getchar 時,返回值要為int類型,否則shell中上下左右鍵會不正常,是因為finsh中調用函數 finsh_getchar 的返回是int,而 finsh_getchar 是直接返回的 rt_hw_console_getchar。
具體代碼如下:
1 void rt_hw_console_output(const char *str) 2 { 3 uint16_t i = 0; 4 while(1) 5 { 6 if(*str == '\n') 7 { 8 comSendChar(COM1, '\r'); 9 } 10 comSendChar(COM1, *str); 11 if(*(++str) == '\0' || ++i == RT_CONSOLEBUF_SIZE) 12 { 13 break; 14 } 15 } 16 } 17
18 int rt_hw_console_getchar(void) 19 { 20 uint8_t ucData; 21
22 if(comGetChar(COM1, &ucData) == 1) 23 { 24 return ucData; 25 } 26 return (-1); 27 }
6、利用RT-Thread的組件初始化宏 INIT_BOARD_EXPORT(fn) 初始化串口,使用此宏可以在 rt_kprintf 函數被調用前初始化,否則會卡死。
7、如果要使用 finsh 中的歷史記錄功能,需要在 rtconfig.h 中添加定義:#define FINSH_USING_HISTORY (感覺是RT-Thread忘記了)。
8、編譯燒錄下載運行,看到打印啟動logo后說明運行正常,OK, Enjoy your RT-Thread 吧。