FreeRTOS10例程學習(一)
阿新 • • 發佈:2018-12-31
官網下載source-code-for-book-examples壓縮包,解壓後得到VS工程,開啟source-code-for-book-examples\Win32-simulator-MSVC\RTOSDemo.sln後有Examples001-025。
Examples001,任務建立
包含標頭檔案
/* FreeRTOS.org includes. */
#include "FreeRTOS.h"
#include "task.h"
/* Demo includes. */
#include "supporting_functions.h"
supporting_functions.c包含了支援所有示例所使用的一些函式,一共有三類:
/*
* 1) IO functions: vPrintString() and vPrintStringAndNumber().
* 2) RTOS hook functions: vApplicationMallocFailedHook(), vApplicationIdleHook()
* vApplicationIdleHook(), vApplicationStackOverflowHook() and
* vApplicationTickHook().
* 3) configASSERT() implementation: vAssertCalled()
*/
1)IO函式,為了允許最大的可移植性,例程不依賴於任何晶片的IO口,將其輸出到控制檯。然而直接輸出到控制檯是執行緒不安全的,所以將所有輸出封裝到函式中。
2)這些函式可以由使用者進行定義,以獲得執行中應用發生事件的通知。
3)FreeRTOS原始碼使用assert()函式來捕獲使用者和其他錯誤。configASSERT()在FreeRTOSConfig.h中定義,呼叫vAssertCalled(),它是在這個.c檔案中實現的。
在main.c中,預定義了一個變數
#define mainDELAY_LOOP_COUNT (0xffffff)
該變數作為一個粗糙的延時,用於任務執行時的睡眠。
定義了兩個任務函式:
void vTask1( void *pvParameters );
void vTask2( void *pvParameters );
void vTask1( void *pvParameters )
{
const char *pcTaskName = "Task 1 is running\r\n";
volatile uint32_t ul;
/* As per most tasks, this task is implemented in an infinite loop. */
for( ;; )
{
/* Print out the name of this task. */
vPrintString( pcTaskName );
/* Delay for a period. */
for( ul = 0; ul < mainDELAY_LOOP_COUNT; ul++ )
{
/* This loop is just a very crude delay implementation. There is
nothing to do in here. Later exercises will replace this crude
loop with a proper delay/sleep function. */
}
}
}
void vTask2( void *pvParameters )
{
const char *pcTaskName = "Task 2 is running\r\n";
volatile uint32_t ul;
for( ;; )
{
vPrintString( pcTaskName );
for( ul = 0; ul < mainDELAY_LOOP_COUNT; ul++ )
{
}
}
}
main():
int main( void )
{
/* Create one of the two tasks. */
xTaskCreate( vTask1, /* Pointer to the function that implements the task. */
"Task 1", /* Text name for the task. This is to facilitate debugging only. */
1000, /* Stack depth - most small microcontrollers will use much less stack than this. */
NULL, /* We are not using the task parameter. */
1, /* This task will run at priority 1. */
NULL ); /* We are not using the task handle. */
/* Create the other task in exactly the same way. */
xTaskCreate( vTask2, "Task 2", 1000, NULL, 1, NULL );
/* Start the scheduler to start the tasks executing. */
vTaskStartScheduler();
/* The following line should never be reached because vTaskStartScheduler()
will only return if there was not enough FreeRTOS heap memory available to
create the Idle and (if configured) Timer tasks. Heap management, and
techniques for trapping heap exhaustion, are described in the book text. */
for( ;; );
return 0;
}
建立兩個任務後執行vTaskStartScheduler()啟動任務啟動排程器,此時兩個任務開始執行。