移植rt-thread nano系统到开发板。
一、配置rt-thread nano
移植参考的教程:https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-nano/nano-port-keil/an0039-nano-port-keil
1.1、nano pack 安装
1.2、添加rt-thread nano到MDK软件
添加后,工程文件增加的有关RTOS的文件
二、修改代码
rt-thread nano增加到工程后,修改和开发板有关的代码。
2.1、添加systick配置和中断函数
在工程其他文件中定义的systick中断函数屏蔽到,在board.c文件中增加
2.2、修改内存堆
根据芯片RAM的大小来指定大小,这里我选择2K
2.3、测试代码
- #include "hardware_config.h"
- #include "led.h"
- //#include "usart.h"
- //#include <stdio.h>
- #include <rtthread.h>
- int main(void)
- {
- u8 key = 0;
- Hardware_init(); /* Ó²¼þ³õʼ»¯*/
- init_led();
-
- while (1)
- {
- led1_on();
- led2_off();
- rt_thread_mdelay(100);
- //led3_on();
- led1_off();
- led2_on();
- rt_thread_mdelay(100);
- //led3_off();
- }
- }
驱动LED指示灯,实现和上篇功能一样的LED灯交替点亮。
三、在rt-thread nano 上添加控制台
3.1、在rtconfig.h中,使能uart console
3.2、初始化串口和串口输出函数
四、创建任务
4.1、创建两个任务
- #include "hardware_config.h"
- #include <rtthread.h>
- #include <rthw.h>
- #include "stdio.h"
- #include "string.h"
- #include "led.h"
- static rt_thread_t thread1;
- static rt_thread_t thread2;
- void thread1_entry(void *parameter)
- {
- //init_ledd();
- while(1)
- {
- //leddisp();
- rt_kprintf("thread1 run \n");
- rt_thread_mdelay(100);
-
- }
- }
- void thread2_entry(void *parameter)
- {
- while(1)
- {
- rt_kprintf("thread2 run \n");
- rt_thread_mdelay(200);
-
- }
- }
- void TaskInit(void)
- {
- thread1 = rt_thread_create("thread1",
- thread1_entry,
- RT_NULL,
- 256,
- 2,
- 10);
- if(thread1 != RT_NULL)
- {
- rt_thread_startup(thread1);
- }
- else
- {
- rt_kprintf("create thread1 fail\n\n");
- }
- thread2 = rt_thread_create("thread2",
- thread2_entry,
- RT_NULL,
- 256,
- 2,
- 10);
- if(thread2 != RT_NULL)
- {
- rt_thread_startup(thread2);
- }
- else
- {
- rt_kprintf("create thread2 fail\n\n");
- }
- }
4.2、主程序 - #include "hardware_config.h"
- #include <rtthread.h>
- #include "task.h"
- #include "led.h"
- int main(void)
- {
- u8 key = 0;
- Hardware_init(); /* Ó²¼þ³õʼ»¯*/
- init_led();
- TaskInit();
-
-
- while (1)
- {
- //rt_kprintf("https://www.linkosemi.com\r\n");
- led1_on();
- led2_off();
- rt_thread_mdelay(100);
- //led3_on();
- led1_off();
- led2_on();
- rt_thread_mdelay(100);
- //led3_off();
- }
- }
五、程序运行
程序运行,串口输出:
|