본문 바로가기

STM32

FreeRTOS Task priority

 

https://www.freertos.org/Documentation/02-Kernel/02-Kernel-features/01-Tasks-and-co-routines/03-Task-priorities

 

Tasks - FreeRTOS™

The concept of task states

freertos.org

 

Task의 priority를 확인해 보았습니다.

CMSIS V2 환경입니다.

 

아래의 3개의 task들을 정의했습니다.

osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
  .name = "defaultTask",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
osThreadId_t readCanTaskHandle;
const osThreadAttr_t readCanTask_attributes = {
  .name = "readCanTask",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal2,
};
osThreadId_t writeCanTaskHandle;
const osThreadAttr_t writeCanTask_attributes = {
  .name = "writeCANTask",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal1,
};

  /* creation of defaultTask */
  defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

  /* creation of readCanTask */
  readCanTaskHandle = osThreadNew(StartReadCanTask, NULL, &readCanTask_attributes);

  /* creation of writeCanTask */
  writeCanTaskHandle = osThreadNew(StartWriteCanTask, NULL, &writeCanTask_attributes);

 

defaultTask는 osPriorityNormal (24)이고 3초마다 출력합니다.

writeCANTask는 osPriorityNormal1 (25)이고 1.5초마다 출력합니다.

readCanTask는 osPriorityNormal2 (26)이고 1초마다 출력합니다.

 

void StartDefaultTask(void *argument)
{
  const TickType_t xDelay = 3000 / portTICK_PERIOD_MS;
  for(;;)
  {
    printf("DefaultTask\n\r");
    vTaskDelay(xDelay);
  }
}


void StartWriteCanTask(void *argument)
{
  const TickType_t xDelay = 1500 / portTICK_PERIOD_MS;
  /* Infinite loop */
  for(;;)
  {
    printf("WriteCanTask\n\r");
    vTaskDelay(xDelay);
  }
}

void StartReadCanTask(void *argument)
{
  const TickType_t xDelay = 1000 / portTICK_PERIOD_MS;
  for(;;)
  {
    printf("ReadCanTask\n\r");
    vTaskDelay(xDelay);
  }
}

 

결과

 

 

 

ReadCanTask와 다른 task가 동시에 실행될 때 priority에 의해 ReadCanTask가 먼저 실행되는 것을 확인했습니다.

 

Low priority numbers denote low priority tasks. The idle task has priority zero (tskIDLE_PRIORITY).