[应用相关] 使用CMSIS-RTOS建立任务(转载)

[复制链接]
2125|34
 楼主| 木木guainv 发表于 2019-6-18 13:43 | 显示全部楼层 |阅读模式
昨天,将FreeRTOS移植到STM32现有的工程后,今天希望使用RTOS进行工程设计,遇到的第1个问题,就是工程中的函数在FreeRTOS的帮助文档中全部都检索不到。在网上仔细学习后,才发现,ST公司给的FreeRTOS例程,又进行了一层封装,这层就是CMSIS-RTOS。CMSIS-RTOS是keil公司对不同RTOS的一种封装结构,可以使不同的RTOS具有相同的调用接口,以方便今后程序的移植。本文,详细介绍使用CMSIS-RTOS建立任务的方法。


 楼主| 木木guainv 发表于 2019-6-18 13:44 | 显示全部楼层
使用CMSIS-RTOS建立任务需要用到两个API,分别是osThreadDef和GprsTaskHandle,其具体定义如下:

1、osThreadDef


#define osThreadDef( name,
                     priority,
                     instances,
                     stacksz
)       
解释:Define the attributes of a thread functions that can be created by the function osThreadCreate using osThread. The argument instances defines the number of times that osThreadCreate can be called for the same osThreadDef.

参数:name          name of the thread function.
                      priority        initial priority of the thread function.
                      instances    number of possible thread instances.
                      stacksz       stack size (in bytes) requirements for the thread function.


 楼主| 木木guainv 发表于 2019-6-18 13:44 | 显示全部楼层
2、osThreadCreate

osThreadId osThreadCreate( const osThreadDef_t *thread_def,
                           void  *argument
)       
解释:Start a thread function by adding it to the Active Threads list and set it to state READY. The thread function receives the argument pointer as function argument when the function is started. When the priority of the created thread function is higher than the current RUNNING thread, the created thread function starts instantly and becomes the new RUNNING thread.

参数:[in]    thread_def    thread definition referenced with osThread.
                      [in]    argument      pointer that is passed to the thread function as start argument.


 楼主| 木木guainv 发表于 2019-6-18 13:45 | 显示全部楼层

在osThreadDef涉及到的优先级参数,其具体定义如下:

watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3Fpbmd3dWZlaXlhbmcxMjM0Ng==,size_16,color_FFFFFF,t_70.jpg


 楼主| 木木guainv 发表于 2019-6-18 13:45 | 显示全部楼层
有了上述的准备工作后,我们就可以建立自己的任务了,在下面的例子中,我们建立2个任务分别为:RfidTask和GprsTask,具体步骤如下:

1、声明任务ID

osThreadId RfidTaskHandle;
osThreadId GprsTaskHandle;



 楼主| 木木guainv 发表于 2019-6-18 13:45 | 显示全部楼层
2、声明任务的函数原型

void StartRfidTask(void const * argument);
void StartGprsTask(void const * argument);


 楼主| 木木guainv 发表于 2019-6-18 13:45 | 显示全部楼层
3、在main函数中创建任务

osThreadDef(RfidTask, StartRfidTask, osPriorityNormal, 0, 128);
RfidTaskHandle = osThreadCreate(osThread(RfidTask), NULL);
       
osThreadDef(GprsTask, StartGprsTask, osPriorityNormal, 0, 128);
GprsTaskHandle = osThreadCreate(osThread(GprsTask), NULL);

 楼主| 木木guainv 发表于 2019-6-18 13:46 | 显示全部楼层
4、实现RfidTask

void StartRfidTask(void const * argument)
{
  for(;;)
  {
    Target.Iwdg.Refresh();
    Target.HAL.L1.Open();
    osDelay(1000);
    Target.HAL.L1.Shut();
    osDelay(1000);
  }
}


 楼主| 木木guainv 发表于 2019-6-18 13:47 | 显示全部楼层
5、实现GprsTask

void StartGprsTask(void const * argument)
{
  for(;;)
  {
    Target.Iwdg.Refresh();
    Target.HAL.L2.Open();
    osDelay(500);
    Target.HAL.L2.Shut();
    osDelay(500);
  }
}


 楼主| 木木guainv 发表于 2019-6-18 13:47 | 显示全部楼层
6、将此程序编译下载到硬件中后,可看到L1以1秒为间隔闪烁,L2以0.5秒为间隔闪烁。
643757107 发表于 2019-6-18 20:45 | 显示全部楼层
哦,多谢分享。
 楼主| 木木guainv 发表于 2019-7-4 16:53 | 显示全部楼层
客气了  看到就分享了
usysm 发表于 2019-7-4 18:36 | 显示全部楼层
嵌入式实时操作系统正得到越来越广泛的应用   
typeof 发表于 2019-7-4 18:37 | 显示全部楼层
可以更合理、更有效地利用CPU的资源  
yujielun 发表于 2019-7-4 18:37 | 显示全部楼层
FreeRTOS是一个嵌入式系统使用的  
htmlme 发表于 2019-7-4 18:37 | 显示全部楼层
STM32单片机添加FreeRTOS操作系统  
uiint 发表于 2019-7-4 18:38 | 显示全部楼层
Cortex-M7 内核的 STM32F769 ?  
touser 发表于 2019-7-4 18:38 | 显示全部楼层
学习FreeRTOS的各个功能   
myiclife 发表于 2019-7-4 18:39 | 显示全部楼层
ucOS?FREE RTOS哪个好?
uytyu 发表于 2019-7-4 18:39 | 显示全部楼层
推荐 FreeRTOS源码详解   
您需要登录后才可以回帖 登录 | 注册

本版积分规则

190

主题

4344

帖子

5

粉丝
快速回复 在线客服 返回列表 返回顶部