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 吧。