From ce8b1c5a9db51843e5937ac319f6c244f964e9b9 Mon Sep 17 00:00:00 2001 From: Sudeep Mohanty Date: Thu, 18 Dec 2025 15:50:48 +0100 Subject: [PATCH] fix(freertos): Added stability fixes to tick hook test --- .../freertos/misc/test_idf_additions.c | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/components/freertos/test_apps/freertos/misc/test_idf_additions.c b/components/freertos/test_apps/freertos/misc/test_idf_additions.c index 5c5c3d69ad..99e67eb97b 100644 --- a/components/freertos/test_apps/freertos/misc/test_idf_additions.c +++ b/components/freertos/test_apps/freertos/misc/test_idf_additions.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -282,7 +282,7 @@ Expected: --------------------------------------------------------------------------------------------------------------------- */ #define TEST_DELAY_MS 200 -static uint32_t tick_hook_count[portNUM_PROCESSORS]; +static volatile uint32_t tick_hook_count[portNUM_PROCESSORS]; static void IRAM_ATTR tick_hook(void) { @@ -296,10 +296,21 @@ static void suspend_task(void *arg) /* Fetch the current core ID */ BaseType_t xCoreID = portGET_CORE_ID(); + /* Warm up the cache by running the scheduler suspension/resumption once. + * This reduces the execution time variance caused by cache misses during + * the actual test on targets like the esp32p4. + */ + vTaskSuspendAll(); + xTaskResumeAll(); + vTaskDelay(pdMS_TO_TICKS(10)); + /* Register tick hook */ - memset(tick_hook_count, 0, sizeof(tick_hook_count)); + tick_hook_count[xCoreID] = 0; esp_register_freertos_tick_hook_for_cpu(tick_hook, xCoreID); + /* Read the tick hook count before suspending */ + uint32_t initial_count = tick_hook_count[xCoreID]; + /* Suspend scheduler */ vTaskSuspendAll(); @@ -312,30 +323,39 @@ static void suspend_task(void *arg) /* Delay for a further TEST_DELAY_MS milliseconds after scheduler resumption */ vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MS)); + /* Read the final tick hook count */ + uint32_t final_count = tick_hook_count[xCoreID]; + /* De-register tick hook */ esp_deregister_freertos_tick_hook_for_cpu(tick_hook, xCoreID); /* Verify that the tick hook callback count equals the scheduler suspension time + the delay time. * We add a variation of 2 ticks to account for delays encountered during test setup and teardown. */ - printf("Core%d tick_hook_count = %"PRIu32"\n", xCoreID, tick_hook_count[xCoreID]); - TEST_ASSERT_INT_WITHIN(portTICK_PERIOD_MS * 2, TEST_DELAY_MS * 2, tick_hook_count[xCoreID]); + printf("Core%d initial_count = %"PRIu32"\n", xCoreID, initial_count); + printf("Core%d final_count = %"PRIu32"\n", xCoreID, final_count); + TEST_ASSERT_INT_WITHIN(portTICK_PERIOD_MS * 2, TEST_DELAY_MS * 2, final_count - initial_count); /* Signal main task of test completion */ xTaskNotifyGive(main_task_hdl); - /* Cleanup */ - vTaskDelete(NULL); + vTaskSuspend(NULL); } TEST_CASE("IDF additions: IDF tick hooks during scheduler suspension", "[freertos]") { /* Run test for each core */ + TaskHandle_t suspend_task_handle[portNUM_PROCESSORS]; for (int x = 0; x < portNUM_PROCESSORS; x++) { - xTaskCreatePinnedToCore(&suspend_task, "suspend_task", 8192, (void *)xTaskGetCurrentTaskHandle(), UNITY_FREERTOS_PRIORITY, NULL, x); + xTaskCreatePinnedToCore(&suspend_task, "suspend_task", 8192, (void *)xTaskGetCurrentTaskHandle(), UNITY_FREERTOS_PRIORITY, &suspend_task_handle[x], x); /* Wait for test completion */ ulTaskNotifyTake(pdTRUE, portMAX_DELAY); + + /* Cleanup */ + vTaskSuspend(suspend_task_handle[x]); + vTaskDelay(pdMS_TO_TICKS(10)); + vTaskDelete(suspend_task_handle[x]); } }