STM32F0開發筆記14: 使用CMSIS-RTOS建立任務
昨天,將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涉及到的優先順序引數,其具體定義如下:
有了上述的準備工作後,我們就可以建立自己的任務了,在下面的例子中,我們建立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。