STM32F0开发笔记14: 使用CMSIS-RTOS建立任务

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qingwufeiyang12346/article/details/84330525

昨天,将FreeRTOS移植到STM32现有的工程后,今天希望使用RTOS进行工程设计,遇到的第1个问题,就是工程中的函数在FreeRTOS的帮助文档中全部都检索不到。在网上仔细学习后,才发现,ST公司给的FreeRTOS例程,又进行了一层封装,这层就是CMSIS-RTOS。CMSIS-RTOS是keil公司对不同RTOS的一种封装结构,可以使不同的RTOS具有相同的调用接口,以方便今后程序的移植。本文,详细介绍使用CMSIS-RTOS建立任务的方法。

 

使用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.

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.

 

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

扫描二维码关注公众号,回复: 4195714 查看本文章

 

有了上述的准备工作后,我们就可以建立自己的任务了,在下面的例子中,我们建立2个任务分别为:RfidTask和GprsTask,具体步骤如下:

1、声明任务ID

osThreadId RfidTaskHandle;
osThreadId GprsTaskHandle;

2、声明任务的函数原型

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

3、在main函数中创建任务

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

4、实现RfidTask

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

5、实现GprsTask

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

6、将此程序编译下载到硬件中后,可看到L1以1秒为间隔闪烁,L2以0.5秒为间隔闪烁。

 

原创性文章,转载请注明出处
CSDN:http://blog.csdn.net/qingwufeiyang12346

 

 

 

 

 

 

 

 

 

猜你喜欢

转载自blog.csdn.net/qingwufeiyang12346/article/details/84330525