xTaskCreate()関数分析
task. h
BaseType_t xTaskCreate( TaskFunction_t pvTaskCode,
const char * const pcName,
configSTACK_DEPTH_TYPE usStackDepth,
void *pvParameters,
UBaseType_t uxPriority,
TaskHandle_t *pxCreatedTask
);
まとめ
新しいタスクインスタンスを作成します。
各タスクは、タスクの状態(タスク制御ブロック、またはTCB)を保存するためにRAMを必要とし、タスクによってそのスタックとして使用されます。タスクがxTaskCreate()を使用して作成された場合、FreeRTOSヒープからRAMを自動的に割り当てる必要があります。xTaskCreateStatic()を使用してタスクを作成する場合、アプリケーション作成者はRAMを提供します。これにより、2つの追加の関数パラメーターが生成されますが、コンパイル時に静的RAMを割り当てることができます。
新しく作成されたタスクは、最初は準備完了状態ですが、優先度の高いタスクを実行できない場合は、すぐに実行中のタスクになります。
タスクは、スケジューラーの開始前と開始後に作成できます。
パラメータ
パラメータ | 機能 |
---|---|
pvTaskCode | タスク機能 |
pcName | タスク名 |
usStackDepth | タスクスタックサイズ |
pvParameters | タスクパラメータ |
uxPriority | タスクの優先度 |
pxCreatedTask | タスクハンドル |
戻り値
戻り値 | 意味 |
---|---|
pdPass | タスクが正常に作成されたことを示します。 |
errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY | FreeRTOSにタスクデータ構造とスタックを割り当てるのに十分なヒープメモリがないため、タスクを作成できないことを示します。プロジェクトにheap_1.c、heap_2.c、またはheap_4.cが含まれている場合、FreeRTOSConfigのconfigTOTAL_HEAP_SIZEが使用可能なヒープの総量を定義します。vApplicationMallocFailedHook()コールバック関数(または「フック」)を使用してメモリの割り当ての失敗をキャッチし、xPortGetFreeHeapSize()API関数を使用して空きヒープメモリの残量をクエリします。heap_3.cがプロジェクトに含まれている場合、リンカー構成は合計ヒープサイズを定義します。 |
ルーチンを使用する
/* Task to be created. */
void vTaskCode( void * pvParameters )
{
/* The parameter value is expected to be 1 as 1 is passed in the
pvParameters value in the call to xTaskCreate() below. configASSERT( ( ( uint32_t ) pvParameters ) == 1 );
for( ;; )
{
/* Task code goes here. */
}
}
/* Function that creates a task. */
void vOtherFunction( void )
{
BaseType_t xReturned;
TaskHandle_t xHandle = NULL;
/* Create the task, storing the handle. */
xReturned = xTaskCreate(
vTaskCode, /* Function that implements the task. */
"NAME", /* Text name for the task. */
STACK_SIZE, /* Stack size in words, not bytes. */
( void * ) 1, /* Parameter passed into the task. */
tskIDLE_PRIORITY,/* Priority at which the task is created. */
&xHandle ); /* Used to pass out the created task's handle. */
if( xReturned == pdPASS )
{
/* The task was created. Use the task's handle to delete the task. */vTaskDelete( xHandle );
}
}
解説
FreeRTOSConfig.hでは、configSUPPORT_DYNAMIC_ALLOCATIONを1(動的メモリアプリケーションをサポート)に設定するか、この関数を使用可能にするために単に未定義にする必要があります。