2022-02-25 08:50:58 -05:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2017-06-05 02:08:23 -04:00
|
|
|
/* Tests for FreeRTOS scheduler suspend & resume all tasks */
|
2022-02-25 08:50:58 -05:00
|
|
|
#include "sdkconfig.h"
|
2017-06-05 02:08:23 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "freertos/semphr.h"
|
|
|
|
#include "freertos/queue.h"
|
|
|
|
#include "unity.h"
|
2018-10-25 00:52:32 -04:00
|
|
|
#include "test_utils.h"
|
2022-01-02 03:19:49 -05:00
|
|
|
#include "esp_intr_alloc.h"
|
|
|
|
#include "driver/gptimer.h"
|
|
|
|
#include "esp_private/gptimer.h"
|
2018-12-05 07:22:25 -05:00
|
|
|
#include "sdkconfig.h"
|
2017-06-05 02:08:23 -04:00
|
|
|
|
2020-07-21 01:07:34 -04:00
|
|
|
#include "esp_rom_sys.h"
|
|
|
|
|
2017-06-05 02:08:23 -04:00
|
|
|
static SemaphoreHandle_t isr_semaphore;
|
2017-09-27 20:58:32 -04:00
|
|
|
static volatile unsigned isr_count;
|
2017-06-05 02:08:23 -04:00
|
|
|
|
|
|
|
/* Timer ISR increments an ISR counter, and signals a
|
|
|
|
mutex semaphore to wake up another counter task */
|
2022-01-02 03:19:49 -05:00
|
|
|
static bool on_timer_alarm_cb(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_ctx)
|
2017-06-05 02:08:23 -04:00
|
|
|
{
|
|
|
|
portBASE_TYPE higher_awoken = pdFALSE;
|
|
|
|
isr_count++;
|
|
|
|
xSemaphoreGiveFromISR(isr_semaphore, &higher_awoken);
|
2022-01-02 03:19:49 -05:00
|
|
|
return higher_awoken == pdTRUE;
|
2017-06-05 02:08:23 -04:00
|
|
|
}
|
|
|
|
|
2017-09-27 20:58:32 -04:00
|
|
|
typedef struct {
|
|
|
|
SemaphoreHandle_t trigger_sem;
|
|
|
|
volatile unsigned counter;
|
|
|
|
} counter_config_t;
|
|
|
|
|
|
|
|
static void counter_task_fn(void *vp_config)
|
2017-06-05 02:08:23 -04:00
|
|
|
{
|
2017-09-27 20:58:32 -04:00
|
|
|
counter_config_t *config = (counter_config_t *)vp_config;
|
2017-06-05 02:08:23 -04:00
|
|
|
printf("counter_task running...\n");
|
2022-01-02 03:19:49 -05:00
|
|
|
while (1) {
|
2017-09-27 20:58:32 -04:00
|
|
|
xSemaphoreTake(config->trigger_sem, portMAX_DELAY);
|
|
|
|
config->counter++;
|
2017-06-05 02:08:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* This test verifies that an interrupt can wake up a task while the scheduler is disabled.
|
|
|
|
|
|
|
|
In the FreeRTOS implementation, this exercises the xPendingReadyList for that core.
|
|
|
|
*/
|
2017-09-27 22:08:34 -04:00
|
|
|
TEST_CASE("Scheduler disabled can handle a pending context switch on resume", "[freertos]")
|
2017-06-05 02:08:23 -04:00
|
|
|
{
|
|
|
|
isr_count = 0;
|
|
|
|
TaskHandle_t counter_task;
|
2022-01-02 03:19:49 -05:00
|
|
|
gptimer_handle_t gptimer = NULL;
|
|
|
|
intr_handle_t intr_handle = NULL;
|
|
|
|
isr_semaphore = xSemaphoreCreateBinary();
|
|
|
|
TEST_ASSERT_NOT_NULL(isr_semaphore);
|
2017-06-05 02:08:23 -04:00
|
|
|
|
2017-09-27 20:58:32 -04:00
|
|
|
counter_config_t count_config = {
|
|
|
|
.trigger_sem = isr_semaphore,
|
|
|
|
.counter = 0,
|
|
|
|
};
|
2017-06-05 02:08:23 -04:00
|
|
|
xTaskCreatePinnedToCore(counter_task_fn, "counter", 2048,
|
2017-09-27 20:58:32 -04:00
|
|
|
&count_config, UNITY_FREERTOS_PRIORITY + 1,
|
2017-06-05 02:08:23 -04:00
|
|
|
&counter_task, UNITY_FREERTOS_CPU);
|
|
|
|
|
2022-01-02 03:19:49 -05:00
|
|
|
gptimer_config_t timer_config = {
|
2022-04-13 01:12:30 -04:00
|
|
|
.clk_src = GPTIMER_CLK_SRC_DEFAULT,
|
2022-01-02 03:19:49 -05:00
|
|
|
.direction = GPTIMER_COUNT_UP,
|
|
|
|
.resolution_hz = 1000000, // 1MHz, 1 tick=1us
|
|
|
|
};
|
|
|
|
TEST_ESP_OK(gptimer_new_timer(&timer_config, &gptimer));
|
|
|
|
gptimer_alarm_config_t alarm_config = {
|
|
|
|
.reload_count = 0,
|
|
|
|
.alarm_count = 1000, // alarm period 1ms
|
|
|
|
.flags.auto_reload_on_alarm = true,
|
|
|
|
};
|
|
|
|
gptimer_event_callbacks_t cbs = {
|
|
|
|
.on_alarm = on_timer_alarm_cb,
|
2017-06-05 02:08:23 -04:00
|
|
|
};
|
2022-01-02 03:19:49 -05:00
|
|
|
TEST_ESP_OK(gptimer_register_event_callbacks(gptimer, &cbs, NULL));
|
|
|
|
TEST_ESP_OK(gptimer_set_alarm_action(gptimer, &alarm_config));
|
|
|
|
TEST_ESP_OK(gptimer_start(gptimer));
|
|
|
|
TEST_ESP_OK(gptimer_get_intr_handle(gptimer, &intr_handle));
|
2017-06-05 02:08:23 -04:00
|
|
|
|
2022-01-02 03:19:49 -05:00
|
|
|
vTaskDelay(pdMS_TO_TICKS(20));
|
2017-06-05 02:08:23 -04:00
|
|
|
|
|
|
|
// Check some counts have been triggered via the ISR
|
2017-09-27 20:58:32 -04:00
|
|
|
TEST_ASSERT(count_config.counter > 10);
|
2017-06-05 02:08:23 -04:00
|
|
|
TEST_ASSERT(isr_count > 10);
|
|
|
|
|
|
|
|
for (int i = 0; i < 20; i++) {
|
2022-02-25 08:50:58 -05:00
|
|
|
#ifdef CONFIG_FREERTOS_SMP
|
|
|
|
//Note: Scheduler suspension behavior changed in FreeRTOS SMP
|
|
|
|
vTaskPreemptionDisable(NULL);
|
|
|
|
#else
|
2017-06-05 02:08:23 -04:00
|
|
|
vTaskSuspendAll();
|
2022-02-25 08:50:58 -05:00
|
|
|
#endif // CONFIG_FREERTOS_SMP
|
2017-06-05 02:08:23 -04:00
|
|
|
esp_intr_noniram_disable();
|
|
|
|
|
2017-09-27 20:58:32 -04:00
|
|
|
unsigned no_sched_task = count_config.counter;
|
2017-06-05 02:08:23 -04:00
|
|
|
|
|
|
|
// scheduler off on this CPU...
|
2020-07-21 01:07:34 -04:00
|
|
|
esp_rom_delay_us(20 * 1000);
|
2017-06-05 02:08:23 -04:00
|
|
|
|
2017-09-27 20:58:32 -04:00
|
|
|
TEST_ASSERT_EQUAL(count_config.counter, no_sched_task);
|
2017-06-05 02:08:23 -04:00
|
|
|
|
|
|
|
// disable timer interrupts
|
2022-01-02 03:19:49 -05:00
|
|
|
esp_intr_disable(intr_handle);
|
2017-06-05 02:08:23 -04:00
|
|
|
|
|
|
|
// When we resume scheduler, we expect the counter task
|
|
|
|
// will preempt and count at least one more item
|
|
|
|
esp_intr_noniram_enable();
|
2022-01-02 03:19:49 -05:00
|
|
|
esp_intr_enable(intr_handle);
|
2022-02-25 08:50:58 -05:00
|
|
|
#ifdef CONFIG_FREERTOS_SMP
|
|
|
|
//Note: Scheduler suspension behavior changed in FreeRTOS SMP
|
|
|
|
vTaskPreemptionEnable(NULL);
|
|
|
|
#else
|
2017-06-05 02:08:23 -04:00
|
|
|
xTaskResumeAll();
|
2022-02-25 08:50:58 -05:00
|
|
|
#endif // CONFIG_FREERTOS_SMP
|
2017-06-05 02:08:23 -04:00
|
|
|
|
2017-09-27 20:58:32 -04:00
|
|
|
TEST_ASSERT_NOT_EQUAL(count_config.counter, no_sched_task);
|
2017-06-05 02:08:23 -04:00
|
|
|
}
|
|
|
|
|
2022-01-02 03:19:49 -05:00
|
|
|
TEST_ESP_OK(gptimer_stop(gptimer));
|
|
|
|
TEST_ESP_OK(gptimer_del_timer(gptimer));
|
2017-06-05 02:08:23 -04:00
|
|
|
vTaskDelete(counter_task);
|
|
|
|
vSemaphoreDelete(isr_semaphore);
|
|
|
|
}
|
2017-09-27 20:58:32 -04:00
|
|
|
|
|
|
|
/* Multiple tasks on different cores can be added to the pending ready list
|
|
|
|
while scheduler is suspended, and should be started once the scheduler
|
|
|
|
resumes.
|
|
|
|
*/
|
2017-09-27 22:08:34 -04:00
|
|
|
TEST_CASE("Scheduler disabled can wake multiple tasks on resume", "[freertos]")
|
2017-09-27 20:58:32 -04:00
|
|
|
{
|
2022-01-02 03:19:49 -05:00
|
|
|
#define TASKS_PER_PROC 4
|
2017-09-27 20:58:32 -04:00
|
|
|
TaskHandle_t tasks[portNUM_PROCESSORS][TASKS_PER_PROC] = { 0 };
|
|
|
|
counter_config_t counters[portNUM_PROCESSORS][TASKS_PER_PROC] = { 0 };
|
|
|
|
|
|
|
|
/* Start all the tasks, they will block on isr_semaphore */
|
|
|
|
for (int p = 0; p < portNUM_PROCESSORS; p++) {
|
|
|
|
for (int t = 0; t < TASKS_PER_PROC; t++) {
|
|
|
|
counters[p][t].trigger_sem = xSemaphoreCreateMutex();
|
|
|
|
TEST_ASSERT_NOT_NULL( counters[p][t].trigger_sem );
|
|
|
|
TEST_ASSERT( xSemaphoreTake(counters[p][t].trigger_sem, 0) );
|
|
|
|
xTaskCreatePinnedToCore(counter_task_fn, "counter", 2048,
|
|
|
|
&counters[p][t], UNITY_FREERTOS_PRIORITY + 1,
|
|
|
|
&tasks[p][t], p);
|
|
|
|
TEST_ASSERT_NOT_NULL( tasks[p][t] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* takes a while to initialize tasks on both cores, sometimes... */
|
|
|
|
vTaskDelay(TASKS_PER_PROC * portNUM_PROCESSORS * 3);
|
|
|
|
|
|
|
|
/* Check nothing is counting, each counter should be blocked on its trigger_sem */
|
|
|
|
for (int p = 0; p < portNUM_PROCESSORS; p++) {
|
|
|
|
for (int t = 0; t < TASKS_PER_PROC; t++) {
|
|
|
|
TEST_ASSERT_EQUAL(0, counters[p][t].counter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Suspend scheduler on this CPU */
|
2022-02-25 08:50:58 -05:00
|
|
|
#ifdef CONFIG_FREERTOS_SMP
|
|
|
|
//Note: Scheduler suspension behavior changed in FreeRTOS SMP
|
|
|
|
vTaskPreemptionDisable(NULL);
|
|
|
|
#else
|
2017-09-27 20:58:32 -04:00
|
|
|
vTaskSuspendAll();
|
2022-02-25 08:50:58 -05:00
|
|
|
#endif // CONFIG_FREERTOS_SMP
|
2017-09-27 20:58:32 -04:00
|
|
|
|
2017-09-27 21:46:53 -04:00
|
|
|
/* Give all the semaphores once. This will wake tasks immediately on the other
|
|
|
|
CPU, but they are deferred here until the scheduler resumes.
|
2017-09-27 20:58:32 -04:00
|
|
|
*/
|
|
|
|
for (int p = 0; p < portNUM_PROCESSORS; p++) {
|
|
|
|
for (int t = 0; t < TASKS_PER_PROC; t++) {
|
|
|
|
xSemaphoreGive(counters[p][t].trigger_sem);
|
|
|
|
}
|
2022-01-02 03:19:49 -05:00
|
|
|
}
|
2017-09-27 20:58:32 -04:00
|
|
|
|
2020-07-21 01:07:34 -04:00
|
|
|
esp_rom_delay_us(200); /* Let the other CPU do some things */
|
2017-09-27 20:58:32 -04:00
|
|
|
|
|
|
|
for (int p = 0; p < portNUM_PROCESSORS; p++) {
|
|
|
|
for (int t = 0; t < TASKS_PER_PROC; t++) {
|
2017-09-27 21:46:53 -04:00
|
|
|
int expected = (p == UNITY_FREERTOS_CPU) ? 0 : 1; // Has run if it was on the other CPU
|
2020-07-21 01:07:34 -04:00
|
|
|
esp_rom_printf("Checking CPU %d task %d (expected %d actual %d)\n", p, t, expected, counters[p][t].counter);
|
2017-09-27 21:46:53 -04:00
|
|
|
TEST_ASSERT_EQUAL(expected, counters[p][t].counter);
|
2017-09-27 20:58:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Resume scheduler */
|
2022-02-25 08:50:58 -05:00
|
|
|
#ifdef CONFIG_FREERTOS_SMP
|
|
|
|
//Note: Scheduler suspension behavior changed in FreeRTOS SMP
|
|
|
|
vTaskPreemptionEnable(NULL);
|
|
|
|
#else
|
2017-09-27 20:58:32 -04:00
|
|
|
xTaskResumeAll();
|
2022-02-25 08:50:58 -05:00
|
|
|
#endif // CONFIG_FREERTOS_SMP
|
2017-09-27 20:58:32 -04:00
|
|
|
|
|
|
|
/* Now the tasks on both CPUs should have been woken once and counted once. */
|
|
|
|
for (int p = 0; p < portNUM_PROCESSORS; p++) {
|
|
|
|
for (int t = 0; t < TASKS_PER_PROC; t++) {
|
2020-07-21 01:07:34 -04:00
|
|
|
esp_rom_printf("Checking CPU %d task %d (expected 1 actual %d)\n", p, t, counters[p][t].counter);
|
2017-09-27 20:58:32 -04:00
|
|
|
TEST_ASSERT_EQUAL(1, counters[p][t].counter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clean up */
|
|
|
|
for (int p = 0; p < portNUM_PROCESSORS; p++) {
|
|
|
|
for (int t = 0; t < TASKS_PER_PROC; t++) {
|
|
|
|
vTaskDelete(tasks[p][t]);
|
|
|
|
vSemaphoreDelete(counters[p][t].trigger_sem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-27 22:08:34 -04:00
|
|
|
|
2018-12-05 07:22:25 -05:00
|
|
|
#ifndef CONFIG_FREERTOS_UNICORE
|
2017-09-27 22:08:34 -04:00
|
|
|
static volatile bool sched_suspended;
|
|
|
|
static void suspend_scheduler_5ms_task_fn(void *ignore)
|
|
|
|
{
|
2022-02-25 08:50:58 -05:00
|
|
|
#ifdef CONFIG_FREERTOS_SMP
|
|
|
|
//Note: Scheduler suspension behavior changed in FreeRTOS SMP
|
|
|
|
vTaskPreemptionDisable(NULL);
|
|
|
|
#else
|
2017-09-27 22:08:34 -04:00
|
|
|
vTaskSuspendAll();
|
2022-02-25 08:50:58 -05:00
|
|
|
#endif // CONFIG_FREERTOS_SMP
|
2017-09-27 22:08:34 -04:00
|
|
|
sched_suspended = true;
|
2022-01-02 03:19:49 -05:00
|
|
|
for (int i = 0; i < 5; i++) {
|
2020-07-21 01:07:34 -04:00
|
|
|
esp_rom_delay_us(1000);
|
2017-09-27 22:08:34 -04:00
|
|
|
}
|
2022-02-25 08:50:58 -05:00
|
|
|
#ifdef CONFIG_FREERTOS_SMP
|
|
|
|
//Note: Scheduler suspension behavior changed in FreeRTOS SMP
|
|
|
|
vTaskPreemptionEnable(NULL);
|
|
|
|
#else
|
2017-09-27 22:08:34 -04:00
|
|
|
xTaskResumeAll();
|
2022-02-25 08:50:58 -05:00
|
|
|
#endif // CONFIG_FREERTOS_SMP
|
2017-09-27 22:08:34 -04:00
|
|
|
sched_suspended = false;
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the scheduler is disabled on one CPU (A) with a task blocked on something, and a task
|
|
|
|
on B (where scheduler is running) wakes it, then the task on A should be woken on resume.
|
|
|
|
*/
|
|
|
|
TEST_CASE("Scheduler disabled on CPU B, tasks on A can wake", "[freertos]")
|
|
|
|
{
|
|
|
|
TaskHandle_t counter_task;
|
|
|
|
SemaphoreHandle_t wake_sem = xSemaphoreCreateMutex();
|
|
|
|
xSemaphoreTake(wake_sem, 0);
|
|
|
|
counter_config_t count_config = {
|
|
|
|
.trigger_sem = wake_sem,
|
|
|
|
.counter = 0,
|
|
|
|
};
|
|
|
|
xTaskCreatePinnedToCore(counter_task_fn, "counter", 2048,
|
|
|
|
&count_config, UNITY_FREERTOS_PRIORITY + 1,
|
|
|
|
&counter_task, !UNITY_FREERTOS_CPU);
|
|
|
|
|
|
|
|
xTaskCreatePinnedToCore(suspend_scheduler_5ms_task_fn, "suspender", 2048,
|
|
|
|
NULL, UNITY_FREERTOS_PRIORITY - 1,
|
|
|
|
NULL, !UNITY_FREERTOS_CPU);
|
|
|
|
|
|
|
|
/* counter task is now blocked on other CPU, waiting for wake_sem, and we expect
|
|
|
|
that this CPU's scheduler will be suspended for 5ms shortly... */
|
2022-01-02 03:19:49 -05:00
|
|
|
while (!sched_suspended) { }
|
2017-09-27 22:08:34 -04:00
|
|
|
|
|
|
|
xSemaphoreGive(wake_sem);
|
2020-07-21 01:07:34 -04:00
|
|
|
esp_rom_delay_us(1000);
|
2017-09-27 22:08:34 -04:00
|
|
|
// Bit of a race here if the other CPU resumes its scheduler, but 5ms is a long time... */
|
|
|
|
TEST_ASSERT(sched_suspended);
|
|
|
|
TEST_ASSERT_EQUAL(0, count_config.counter); // the other task hasn't woken yet, because scheduler is off
|
|
|
|
TEST_ASSERT(sched_suspended);
|
|
|
|
|
|
|
|
/* wait for the rest of the 5ms... */
|
2022-01-02 03:19:49 -05:00
|
|
|
while (sched_suspended) { }
|
2017-09-27 22:08:34 -04:00
|
|
|
|
2020-07-21 01:07:34 -04:00
|
|
|
esp_rom_delay_us(100);
|
2017-09-27 22:08:34 -04:00
|
|
|
TEST_ASSERT_EQUAL(1, count_config.counter); // when scheduler resumes, counter task should immediately count
|
|
|
|
|
|
|
|
vTaskDelete(counter_task);
|
|
|
|
}
|
|
|
|
#endif
|