[FreeRTOS] Task

 

# Task 생성

BaseType_t xTaskCreate( 
 TaskFunction_t pxTaskCode, // Task 함수
 const char * const pcName, // Task 함수 이름
 const configSTACK_DEPTH_TYPE usStackDepth, // 스택 크기(word) / ex) 128 * 4bytes(word) = 512 bytes / 
 void * const pvParameters, // Task 전달 파라미터
 UBaseType_t uxPriority, // Task 우선순위
 TaskHandle_t * const pxCreatedTask // Task 핸들
)


# Task 생성 Example 1

// main.c


#define TASK_1_PRIO    1

// declare task function and handle
static void Task1(void const *pvParameters);
TaskHandle_t xHandle1;


static void Task1(void const *pvParameters)
{
    const char *pcTaskName = "Task1";

    (void)pvParameters /* To avoid gcc/g++ warnings */

    printf("%s is running\n", pcTaskName);

    while(1)
    {

    }
}

int main(void)
{
    HAL_Init();
    SystemClock_Config();

    /* Task Create */
    xTaskCreate((TaskFunction_t)Task1, "Task1", 128, NULL, TASK_1_PRIO, &xHandle1);

    /* Start scheduler */
    osKernelStart();

    return 0;
}


# Task 생성 Example 2

// main.c

#define TASK_2_PRIO    2

// declare task function and handle
static void Task2(const struct stParam *pParam);
TaskHandle_t xHandle2;

// struct for parameter passing to task
struct stParam {
	char *msg;
	int i;
} parameters;

int main(void)
{
    struct stParam *pParam;

    HAL_Init();
    SystemClock_Config();

    // Set parameter passing to task
    pParam = &parameters;
    pPram->i = 1;

    /* Task Create */
    xTaskCreate((TaskFunction_t)Task2, "Task2", 128, (void*)pParam, TASK_2_PRIO, &xHandle2);

    /* Start scheduler */
    osKernelStart();

    return 0;
}


static void Task2(const struct stParam *pParam)
{
    const char *pcTaskName = "Task2";
    
    printf("%s is running\n", pcTaskName);
    
    printf("Task2 parameter = %d\n", pParam->i);

    while(1)
    {

    }
}


# Task 일시정지

void vTaskSuspend( 
 TaskHandle_t xTaskToSuspend // 일시 정지 시킬 Task 핸들
)

# Task 다시 시작하기

void vTaskResume(
 TaskHandle_t xTaskToResume // 준비 상태로 전환시킬 Task 핸들
)

# Task 제거

void vTaskDelete(
 TaskHandle_t xTaskToDelete // 제거할 Task 핸들
)

/*
 # IDLE Task는 제거 불가능
 # 파라미터 Task 핸들에 NULL을 전달하여 자기 자신 제거 가능
   => xTaskDelete(NULL)
 */

# Task 우선 순위 변경

void vTaskPrioritySet(
 TaskHandle_t xTask, // 변경 대상 Task 핸들
 UBaseType_t uxNewPriority // 새로 설정할 우선 순위
)

/*
 # Task 핸들에 NULL을 사용 하여 자기자신의 Task 우선순위 변경
  -> vTaskPrioritySet(NULL, priority);
 # IDLE Task의 우선순위 변경은 불가능 함 
*/

# Task 정보 얻어 오기

void vTaskGetTaskInfo(
 TaskHandle_t xTask, // 정보 얻어 올 Task 핸들
 TaskStatus_t *pxTaskStatus, // 정보를 가져 올 TaskStatus_t 타입의 포인터
 BaseType_t xGetFreeStackSpace, // 스택 상위 워터마크 작업 유무
 eTaskState eState 
)

/*
 # xGetFreeStackSpace
  - 스택 상위 워터마크 : Task에 대해 존재하는 최소 스택 공간이므로 0에 가까울수록 Task가 Overflow에 가까워 짐
  - 스택 상위 워터마크 계산에는 비교적 오랜 시간이 걸리므로 시스템이 일시적으로 응답하지 않을 수 있음
   => 따라서 이 작업을 건너 뛸 수 있도록 xGetFreeStackSpace 파라미터가 제공 됨.
   => pdTrue / pdFALSE
 
 # eState
  - 작업 상태를 얻는 것은 단순 할당 만큼 빠르지 않음. 
  - 따라서 TaskStatus_t 구조체에서 상태정보를 생략 할 수 있음.
  - 상태정보를 얻으려면 eState를 eInvalid 로 설정 해야 함.
*/


# TaskStatus_t 구조체

typedef struct xTASK_STATUS
{
 /* The handle of the task to which the rest of the information in the structure 
 relates. */
 TaskHandle_t xHandle; 
 
 /* A pointer to the task's name. This value will be invalid if the task was deleted 
 since the structure was populated! */
 const signed char *pcTaskName; 
 
 /* A number unique to the task. */
 UBaseType_t xTaskNumber; 
 
 /* The state in which the task existed when the structure was populated. */
 eTaskState eCurrentState; 
 
 /* The priority at which the task was running (may be inherited) when the structure 
 was populated. */
 UBaseType_t uxCurrentPriority; 
 
 /* The priority to which the task will return if the task's current priority has been 
 inherited to avoid unbounded priority inversion when obtaining a mutex. Only valid 
 if configUSE_MUTEXES is defined as 1 in FreeRTOSConfig.h. */
 UBaseType_t uxBasePriority; 
 /* The total run time allocated to the task so far, as defined by the run time stats 
 clock. Only valid when configGENERATE_RUN_TIME_STATS is defined as 1 in 
 FreeRTOSConfig.h. */
 unsigned long ulRunTimeCounter;
 /* Points to the lowest address of the task's stack area. */
 StackType_t *pxStackBase; 
 
 /* The minimum amount of stack space that has remained for the task since the task was 
 created. The closer this value is to zero the closer the task has come to overflowing 
 its stack. */
 unsigned short usStackHighWaterMark;
 
} TaskStatus_t;




이 블로그의 인기 게시물