2022-04-13 01:12:30 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2022-04-20 07:31:49 -04:00
|
|
|
|
2016-11-17 03:36:10 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "freertos/semphr.h"
|
|
|
|
#include "freertos/queue.h"
|
|
|
|
#include "freertos/event_groups.h"
|
2022-01-02 03:19:49 -05:00
|
|
|
#include "driver/gptimer.h"
|
2016-11-17 03:36:10 -05:00
|
|
|
#include "unity.h"
|
2022-06-28 13:10:57 -04:00
|
|
|
#include "unity_test_utils.h"
|
2016-11-17 03:36:10 -05:00
|
|
|
|
2022-08-10 02:13:48 -04:00
|
|
|
#define NUM_TASKS 8
|
|
|
|
#define ALL_TASK_BITS ((1 << NUM_TASKS) - 1)
|
|
|
|
#define BIT_CALL(task) (1 << (task))
|
|
|
|
#define BIT_RESPONSE(task) (1 << ((task) + NUM_TASKS))
|
|
|
|
#define ALL_CALL_BITS (ALL_TASK_BITS)
|
|
|
|
#define ALL_RESPONSE_BITS (ALL_TASK_BITS << NUM_TASKS)
|
2016-11-17 03:36:10 -05:00
|
|
|
|
2017-02-27 20:06:36 -05:00
|
|
|
static const int COUNT = 1000;
|
2016-11-17 03:36:10 -05:00
|
|
|
static EventGroupHandle_t eg;
|
2017-02-27 20:06:36 -05:00
|
|
|
static SemaphoreHandle_t done_sem;
|
2016-11-17 03:36:10 -05:00
|
|
|
|
|
|
|
static void task_event_group_call_response(void *param)
|
|
|
|
{
|
|
|
|
int task_num = (int)param;
|
|
|
|
|
|
|
|
printf("Started %d\n", task_num);
|
|
|
|
|
|
|
|
for (int i = 0; i < COUNT; i++) {
|
|
|
|
/* Wait until the common "call" bit is set, starts off all tasks
|
|
|
|
(clear on return) */
|
2022-08-10 02:13:48 -04:00
|
|
|
TEST_ASSERT( xEventGroupWaitBits(eg, BIT_CALL(task_num), true, false, portMAX_DELAY) );
|
2016-11-17 03:36:10 -05:00
|
|
|
|
|
|
|
/* Set our individual "response" bit */
|
|
|
|
xEventGroupSetBits(eg, BIT_RESPONSE(task_num));
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Task %d done\n", task_num);
|
2017-02-27 20:06:36 -05:00
|
|
|
xSemaphoreGive(done_sem);
|
2022-06-28 13:10:57 -04:00
|
|
|
// Wait to be deleted
|
|
|
|
vTaskSuspend(NULL);
|
2016-11-17 03:36:10 -05:00
|
|
|
}
|
|
|
|
|
2017-02-27 20:06:36 -05:00
|
|
|
TEST_CASE("FreeRTOS Event Groups", "[freertos]")
|
2016-11-17 03:36:10 -05:00
|
|
|
{
|
|
|
|
eg = xEventGroupCreate();
|
2017-02-27 20:06:36 -05:00
|
|
|
done_sem = xSemaphoreCreateCounting(NUM_TASKS, 0);
|
2016-11-17 03:36:10 -05:00
|
|
|
|
2022-06-28 13:10:57 -04:00
|
|
|
TaskHandle_t task_handles[NUM_TASKS];
|
|
|
|
|
2017-02-27 23:13:30 -05:00
|
|
|
/* Note: task_event_group_call_response all have higher priority than this task, so on this core
|
|
|
|
they will always preempt this task.
|
2016-11-17 03:36:10 -05:00
|
|
|
|
2022-08-10 02:13:48 -04:00
|
|
|
This is important because we need to know all tasks have blocked on their own BIT_CALL(task_num) each time we
|
|
|
|
signal it, or they get out of sync.
|
2016-11-17 03:36:10 -05:00
|
|
|
*/
|
|
|
|
for (int c = 0; c < NUM_TASKS; c++) {
|
2022-06-28 13:10:57 -04:00
|
|
|
xTaskCreatePinnedToCore(task_event_group_call_response, "tsk_call_resp", 4096, (void *)c, configMAX_PRIORITIES - 1, &task_handles[c], c % portNUM_PROCESSORS);
|
2016-11-17 03:36:10 -05:00
|
|
|
}
|
2017-02-27 23:13:30 -05:00
|
|
|
|
|
|
|
/* Tasks all start instantly, but this task will resume running at the same time as the higher priority tasks on the
|
2022-08-10 02:13:48 -04:00
|
|
|
other processor may still be setting up, so allow time for them to also block on BIT_CALL()... */
|
2017-03-27 23:16:53 -04:00
|
|
|
vTaskDelay(10);
|
2016-11-17 03:36:10 -05:00
|
|
|
|
|
|
|
for (int i = 0; i < COUNT; i++) {
|
2022-08-10 02:13:48 -04:00
|
|
|
/* signal all the "CALL" bits of each task */
|
|
|
|
xEventGroupSetBits(eg, ALL_CALL_BITS);
|
2016-11-17 03:36:10 -05:00
|
|
|
|
2022-06-28 13:10:57 -04:00
|
|
|
/* Wait until all tasks have set their respective response bits */
|
|
|
|
TEST_ASSERT_EQUAL_HEX16(ALL_RESPONSE_BITS, xEventGroupWaitBits(eg, ALL_RESPONSE_BITS, true, true, portMAX_DELAY));
|
2016-11-17 03:36:10 -05:00
|
|
|
}
|
|
|
|
|
2022-06-28 13:10:57 -04:00
|
|
|
/* Ensure all tasks have suspend themselves */
|
2017-02-27 20:06:36 -05:00
|
|
|
for (int c = 0; c < NUM_TASKS; c++) {
|
2022-01-02 03:19:49 -05:00
|
|
|
TEST_ASSERT( xSemaphoreTake(done_sem, 100 / portTICK_PERIOD_MS) );
|
2017-02-27 20:06:36 -05:00
|
|
|
}
|
|
|
|
|
2022-06-28 13:10:57 -04:00
|
|
|
for (int c = 0; c < NUM_TASKS; c++) {
|
|
|
|
unity_utils_task_delete(task_handles[c]);
|
|
|
|
}
|
|
|
|
|
2017-02-27 20:06:36 -05:00
|
|
|
vSemaphoreDelete(done_sem);
|
|
|
|
vEventGroupDelete(eg);
|
|
|
|
}
|
2016-11-17 03:36:10 -05:00
|
|
|
|
|
|
|
static void task_test_sync(void *param)
|
|
|
|
{
|
|
|
|
int task_num = (int)param;
|
|
|
|
|
|
|
|
printf("Started %d\n", task_num);
|
|
|
|
|
|
|
|
for (int i = 0; i < COUNT; i++) {
|
|
|
|
/* set our bit, and wait on all tasks to set their bits */
|
2022-08-10 02:13:48 -04:00
|
|
|
xEventGroupSync(eg, BIT_CALL(task_num), ALL_CALL_BITS, portMAX_DELAY);
|
2016-11-17 03:36:10 -05:00
|
|
|
/* clear our bit */
|
2022-08-10 02:13:48 -04:00
|
|
|
xEventGroupClearBits(eg, BIT_CALL(task_num));
|
2016-11-17 03:36:10 -05:00
|
|
|
}
|
2022-08-10 02:13:48 -04:00
|
|
|
int after_done = xEventGroupSetBits(eg, BIT_RESPONSE(task_num));
|
2016-11-17 03:36:10 -05:00
|
|
|
|
2017-02-27 20:06:36 -05:00
|
|
|
printf("Done %d = 0x%08x\n", task_num, after_done);
|
2016-11-17 03:36:10 -05:00
|
|
|
|
2017-02-27 20:06:36 -05:00
|
|
|
xSemaphoreGive(done_sem);
|
2016-11-17 03:36:10 -05:00
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
2017-02-27 20:06:36 -05:00
|
|
|
TEST_CASE("FreeRTOS Event Group Sync", "[freertos]")
|
2016-11-17 03:36:10 -05:00
|
|
|
{
|
|
|
|
eg = xEventGroupCreate();
|
2017-02-27 20:06:36 -05:00
|
|
|
done_sem = xSemaphoreCreateCounting(NUM_TASKS, 0);
|
2016-11-17 03:36:10 -05:00
|
|
|
|
|
|
|
for (int c = 0; c < NUM_TASKS; c++) {
|
|
|
|
xTaskCreatePinnedToCore(task_test_sync, "task_test_sync", 4096, (void *)c, configMAX_PRIORITIES - 1, NULL, c % portNUM_PROCESSORS);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int c = 0; c < NUM_TASKS; c++) {
|
2022-08-10 02:13:48 -04:00
|
|
|
printf("Waiting on %d (0x%08x)\n", c, BIT_RESPONSE(c));
|
|
|
|
TEST_ASSERT( xEventGroupWaitBits(eg, BIT_RESPONSE(c), false, false, portMAX_DELAY) );
|
2016-11-17 03:36:10 -05:00
|
|
|
}
|
2017-02-27 20:06:36 -05:00
|
|
|
|
|
|
|
/* Ensure all tasks cleaned up correctly */
|
|
|
|
for (int c = 0; c < NUM_TASKS; c++) {
|
2022-01-02 03:19:49 -05:00
|
|
|
TEST_ASSERT( xSemaphoreTake(done_sem, 100 / portTICK_PERIOD_MS) );
|
2017-02-27 20:06:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
vSemaphoreDelete(done_sem);
|
|
|
|
vEventGroupDelete(eg);
|
2016-11-17 03:36:10 -05:00
|
|
|
}
|
|
|
|
|
2017-09-28 00:33:53 -04:00
|
|
|
/*-----------------Test case for event group trace facilities-----------------*/
|
2019-11-28 13:27:47 -05:00
|
|
|
#ifdef CONFIG_FREERTOS_USE_TRACE_FACILITY
|
2017-09-28 00:33:53 -04:00
|
|
|
/*
|
|
|
|
* Test event group Trace Facility functions such as
|
|
|
|
* xEventGroupClearBitsFromISR(), xEventGroupSetBitsFromISR()
|
|
|
|
*/
|
|
|
|
|
|
|
|
//Use a timer to trigger an ISr
|
2018-01-02 05:57:28 -05:00
|
|
|
#define BITS 0xAA
|
2022-01-02 03:19:49 -05:00
|
|
|
static gptimer_handle_t gptimer;
|
2018-01-02 05:57:28 -05:00
|
|
|
static bool test_set_bits;
|
|
|
|
static bool test_clear_bits;
|
2017-09-28 00:33:53 -04:00
|
|
|
|
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-09-28 00:33:53 -04:00
|
|
|
{
|
2018-01-02 05:57:28 -05:00
|
|
|
portBASE_TYPE task_woken = pdFALSE;
|
|
|
|
|
2022-01-02 03:19:49 -05:00
|
|
|
gptimer_stop(timer);
|
|
|
|
|
|
|
|
if (test_set_bits) {
|
2018-01-02 05:57:28 -05:00
|
|
|
xEventGroupSetBitsFromISR(eg, BITS, &task_woken);
|
|
|
|
test_set_bits = false;
|
2022-01-02 03:19:49 -05:00
|
|
|
} else if (test_clear_bits) {
|
2018-01-02 05:57:28 -05:00
|
|
|
xEventGroupClearBitsFromISR(eg, BITS);
|
|
|
|
xSemaphoreGiveFromISR(done_sem, &task_woken);
|
|
|
|
test_clear_bits = false;
|
|
|
|
}
|
|
|
|
//Switch context if necessary
|
2022-01-02 03:19:49 -05:00
|
|
|
return task_woken == pdTRUE;
|
2017-09-28 00:33:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("FreeRTOS Event Group ISR", "[freertos]")
|
|
|
|
{
|
|
|
|
done_sem = xSemaphoreCreateBinary();
|
|
|
|
eg = xEventGroupCreate();
|
2018-01-02 05:57:28 -05:00
|
|
|
test_set_bits = false;
|
|
|
|
test_clear_bits = false;
|
2022-01-02 03:19:49 -05:00
|
|
|
//Setup timer for ISR
|
|
|
|
gptimer_config_t 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,
|
|
|
|
};
|
|
|
|
TEST_ESP_OK(gptimer_new_timer(&config, &gptimer));
|
|
|
|
gptimer_alarm_config_t alarm_config = {
|
|
|
|
.reload_count = 0,
|
|
|
|
.alarm_count = 200000,
|
|
|
|
};
|
|
|
|
gptimer_event_callbacks_t cbs = {
|
|
|
|
.on_alarm = on_timer_alarm_cb,
|
|
|
|
};
|
|
|
|
TEST_ESP_OK(gptimer_register_event_callbacks(gptimer, &cbs, NULL));
|
2022-04-23 06:59:38 -04:00
|
|
|
TEST_ESP_OK(gptimer_enable(gptimer));
|
2022-01-02 03:19:49 -05:00
|
|
|
TEST_ESP_OK(gptimer_set_alarm_action(gptimer, &alarm_config));
|
2017-09-28 00:33:53 -04:00
|
|
|
|
2018-01-02 05:57:28 -05:00
|
|
|
//Test set bits
|
2022-01-02 03:19:49 -05:00
|
|
|
printf("test set bits\r\n");
|
2018-01-02 05:57:28 -05:00
|
|
|
test_set_bits = true;
|
2022-01-02 03:19:49 -05:00
|
|
|
TEST_ESP_OK(gptimer_start(gptimer));
|
2018-01-02 05:57:28 -05:00
|
|
|
TEST_ASSERT_EQUAL(BITS, xEventGroupWaitBits(eg, BITS, pdFALSE, pdTRUE, portMAX_DELAY)); //Let ISR set event group bits
|
|
|
|
|
|
|
|
//Test clear bits
|
2022-01-02 03:19:49 -05:00
|
|
|
printf("test clear bits\r\n");
|
2018-01-02 05:57:28 -05:00
|
|
|
xEventGroupSetBits(eg, BITS); //Set bits to be cleared
|
|
|
|
test_clear_bits = true;
|
2022-01-02 03:19:49 -05:00
|
|
|
TEST_ESP_OK(gptimer_set_raw_count(gptimer, 0));
|
|
|
|
TEST_ESP_OK(gptimer_start(gptimer));
|
2018-01-02 05:57:28 -05:00
|
|
|
xSemaphoreTake(done_sem, portMAX_DELAY); //Wait for ISR to clear bits
|
|
|
|
vTaskDelay(10); //Event group clear bits runs via daemon task, delay so daemon can run
|
|
|
|
TEST_ASSERT_EQUAL(0, xEventGroupGetBits(eg)); //Check bits are cleared
|
2017-09-28 00:33:53 -04:00
|
|
|
|
|
|
|
//Clean up
|
2022-04-23 06:59:38 -04:00
|
|
|
TEST_ESP_OK(gptimer_disable(gptimer));
|
2022-01-02 03:19:49 -05:00
|
|
|
TEST_ESP_OK(gptimer_del_timer(gptimer));
|
2017-09-28 00:33:53 -04:00
|
|
|
vEventGroupDelete(eg);
|
2018-01-02 05:57:28 -05:00
|
|
|
vSemaphoreDelete(done_sem);
|
|
|
|
vTaskDelay(10); //Give time for idle task to clear up deleted tasks
|
2017-09-28 00:33:53 -04:00
|
|
|
}
|
|
|
|
#endif //CONFIG_FREERTOS_USE_TRACE_FACILITY
|