esp-idf/components/freertos/test/integration/tasks/test_freertos_get_state.c
2022-04-21 07:55:34 +00:00

82 lines
2.4 KiB
C

/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "sdkconfig.h"
#include <stdio.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "unity.h"
#include "test_utils.h"
#define TSK_PRIORITY (UNITY_FREERTOS_PRIORITY + 1)
static TaskHandle_t blocked_task_handle;
static TaskHandle_t suspended_task_handle;
static SemaphoreHandle_t done_sem;
void test_task_get_state(void* arg)
{
//Current task should return eRunning
TEST_ASSERT(eTaskGetState(xTaskGetCurrentTaskHandle()) == eRunning);
//Idle task of current core should return eReady
#ifdef CONFIG_FREERTOS_SMP
TaskHandle_t *idle_handle_list = xTaskGetIdleTaskHandle();
TEST_ASSERT_EQUAL(eReady, eTaskGetState(idle_handle_list[xPortGetCoreID()]));
#else
TEST_ASSERT(eTaskGetState(xTaskGetIdleTaskHandle()) == eReady);
#endif
//Blocked Task should return eBlocked
TEST_ASSERT(eTaskGetState(blocked_task_handle) == eBlocked);
//Suspended Task should return eSuspended
TEST_ASSERT(eTaskGetState(suspended_task_handle) == eSuspended);
xSemaphoreGive(done_sem);
vTaskDelete(NULL);
}
void blocked_task(void *arg)
{
uint32_t notify_value;
while(1){
xTaskNotifyWait(0, 0xFFFFFFFF, &notify_value, portMAX_DELAY);
}
}
void suspended_task(void *arg)
{
while(1){
vTaskSuspend(NULL);
}
}
TEST_CASE("Test eTaskGetState", "[freertos]")
{
done_sem = xQueueCreateCountingSemaphore(portNUM_PROCESSORS, 0);
//Create blocked and suspended task
xTaskCreatePinnedToCore(blocked_task, "Blocked task", 1024, NULL, TSK_PRIORITY, &blocked_task_handle, tskNO_AFFINITY);
xTaskCreatePinnedToCore(suspended_task, "Suspended task", 1024, NULL, TSK_PRIORITY, &suspended_task_handle, tskNO_AFFINITY);
//Create testing task
for(int i = 0; i < portNUM_PROCESSORS; i++){
xTaskCreatePinnedToCore(test_task_get_state, "Test task", 1024, NULL, TSK_PRIORITY, NULL, i);
}
//Wait until done
for(int i = 0; i < portNUM_PROCESSORS; i++){
xSemaphoreTake(done_sem, portMAX_DELAY);
}
//Clean up blocked and suspended tasks
vTaskDelete(blocked_task_handle);
blocked_task_handle = NULL;
vTaskDelete(suspended_task_handle);
suspended_task_handle = NULL;
vSemaphoreDelete(done_sem);
vTaskDelay(10); //Give time for idle to actually delete the tasks
}