2022-04-13 01:12:30 -04:00
|
|
|
/*
|
2023-03-06 00:32:35 -05:00
|
|
|
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
|
2022-04-13 01:12:30 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2022-04-20 07:31:49 -04:00
|
|
|
|
2017-09-22 06:45:57 -04:00
|
|
|
/*
|
|
|
|
Test of FreeRTOS task notifications. This test creates a sender and receiver
|
|
|
|
task under different core permutations. For each permutation, the sender task
|
|
|
|
will test the xTaskNotify(), xTaskNotifyGive(), xTaskNotifyFromISR(), and
|
|
|
|
vTaskNotifyGiveFromISR(), whereas the receiver task will test
|
|
|
|
xTaskNotifyWait() and ulTaskNotifyTake().
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2022-01-02 03:19:49 -05:00
|
|
|
#include "freertos/semphr.h"
|
|
|
|
#include "driver/gptimer.h"
|
2017-09-22 06:45:57 -04:00
|
|
|
#include "unity.h"
|
2018-10-25 00:52:32 -04:00
|
|
|
#include "test_utils.h"
|
2017-09-22 06:45:57 -04:00
|
|
|
|
|
|
|
#define NO_OF_NOTIFS 4
|
|
|
|
#define NO_OF_TASKS 2 //Sender and receiver
|
2022-01-02 03:19:49 -05:00
|
|
|
#define MESSAGE 0xFF
|
2017-09-22 06:45:57 -04:00
|
|
|
|
|
|
|
static uint32_t send_core_message = 0;
|
2022-01-02 03:19:49 -05:00
|
|
|
static TaskHandle_t recv_task_handle;
|
2017-09-22 06:45:57 -04:00
|
|
|
static bool isr_give = false;
|
2022-01-02 03:19:49 -05:00
|
|
|
static bool test_start = false;
|
|
|
|
static gptimer_handle_t gptimers[portNUM_PROCESSORS];
|
2017-09-22 06:45:57 -04:00
|
|
|
static SemaphoreHandle_t trigger_send_semphr;
|
|
|
|
static SemaphoreHandle_t task_delete_semphr;
|
|
|
|
|
|
|
|
//Test tracking vars
|
|
|
|
static volatile uint32_t notifs_sent = 0;
|
|
|
|
static volatile uint32_t notifs_rec = 0;
|
|
|
|
static bool wrong_core = false;
|
|
|
|
|
2022-01-02 03:19:49 -05:00
|
|
|
static bool on_alarm_sender_cb(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_ctx)
|
|
|
|
{
|
|
|
|
gptimer_stop(timer);
|
|
|
|
if (!test_start) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int curcore = xPortGetCoreID();
|
|
|
|
if (isr_give) { //Test vTaskNotifyGiveFromISR() on same core
|
|
|
|
notifs_sent++;
|
|
|
|
vTaskNotifyGiveFromISR(recv_task_handle, NULL);
|
|
|
|
} else { //Test xTaskNotifyFromISR()
|
|
|
|
notifs_sent++;
|
|
|
|
xTaskNotifyFromISR(recv_task_handle, (MESSAGE << curcore), eSetValueWithOverwrite, NULL);
|
|
|
|
}
|
|
|
|
// always trigger a task switch when exit ISR context
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_gptimer_start(void *arg)
|
|
|
|
{
|
|
|
|
gptimer_handle_t gptimer = (gptimer_handle_t)arg;
|
|
|
|
gptimer_alarm_config_t alarm_config = {
|
|
|
|
.reload_count = 0,
|
|
|
|
.alarm_count = 1000,
|
|
|
|
};
|
|
|
|
TEST_ESP_OK(gptimer_set_raw_count(gptimer, 0));
|
|
|
|
TEST_ESP_OK(gptimer_set_alarm_action(gptimer, &alarm_config));
|
|
|
|
TEST_ESP_OK(gptimer_start(gptimer));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sender_task(void *arg)
|
|
|
|
{
|
|
|
|
gptimer_handle_t gptimer = (gptimer_handle_t)arg;
|
2017-09-22 06:45:57 -04:00
|
|
|
int curcore = xPortGetCoreID();
|
|
|
|
|
|
|
|
//Test xTaskNotify
|
|
|
|
xSemaphoreTake(trigger_send_semphr, portMAX_DELAY);
|
|
|
|
notifs_sent++;
|
2022-01-02 03:19:49 -05:00
|
|
|
xTaskNotify(recv_task_handle, (MESSAGE << curcore), eSetValueWithOverwrite);
|
2017-09-22 06:45:57 -04:00
|
|
|
|
|
|
|
//Test xTaskNotifyGive
|
|
|
|
xSemaphoreTake(trigger_send_semphr, portMAX_DELAY);
|
|
|
|
notifs_sent++;
|
2022-01-02 03:19:49 -05:00
|
|
|
xTaskNotifyGive(recv_task_handle);
|
2017-09-22 06:45:57 -04:00
|
|
|
|
|
|
|
//Test xTaskNotifyFromISR
|
|
|
|
xSemaphoreTake(trigger_send_semphr, portMAX_DELAY);
|
|
|
|
isr_give = false;
|
2022-01-02 03:19:49 -05:00
|
|
|
test_gptimer_start(gptimer);
|
2017-09-22 06:45:57 -04:00
|
|
|
|
|
|
|
//Test vTaskNotifyGiveFromISR
|
|
|
|
xSemaphoreTake(trigger_send_semphr, portMAX_DELAY);
|
|
|
|
isr_give = true;
|
2022-01-02 03:19:49 -05:00
|
|
|
test_gptimer_start(gptimer);
|
2017-09-22 06:45:57 -04:00
|
|
|
|
|
|
|
//Delete Task and Semaphores
|
|
|
|
xSemaphoreGive(task_delete_semphr);
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
2022-01-02 03:19:49 -05:00
|
|
|
static void receiver_task(void *arg)
|
|
|
|
{
|
2017-09-22 06:45:57 -04:00
|
|
|
uint32_t notify_value;
|
|
|
|
|
|
|
|
//Test xTaskNotifyWait from task
|
|
|
|
xTaskNotifyWait(0, 0xFFFFFFFF, ¬ify_value, portMAX_DELAY);
|
2022-01-02 03:19:49 -05:00
|
|
|
if (notify_value != send_core_message) {
|
2017-09-22 06:45:57 -04:00
|
|
|
wrong_core = true;
|
|
|
|
}
|
|
|
|
notifs_rec++;
|
|
|
|
|
|
|
|
//Test ulTaskNotifyTake from task
|
|
|
|
xSemaphoreGive(trigger_send_semphr);
|
|
|
|
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
|
|
|
notifs_rec++;
|
|
|
|
|
|
|
|
//Test xTaskNotifyWait from ISR
|
|
|
|
xSemaphoreGive(trigger_send_semphr);
|
|
|
|
xTaskNotifyWait(0, 0xFFFFFFFF, ¬ify_value, portMAX_DELAY);
|
2022-01-02 03:19:49 -05:00
|
|
|
if (notify_value != send_core_message) {
|
2017-09-22 06:45:57 -04:00
|
|
|
wrong_core = true;
|
|
|
|
}
|
|
|
|
notifs_rec++;
|
|
|
|
|
|
|
|
//Test ulTaskNotifyTake from ISR
|
|
|
|
xSemaphoreGive(trigger_send_semphr);
|
|
|
|
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
|
|
|
notifs_rec++;
|
|
|
|
|
|
|
|
//Test complete, stop timer and delete task
|
|
|
|
xSemaphoreGive(task_delete_semphr);
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
2022-01-02 03:19:49 -05:00
|
|
|
static void install_gptimer_on_core(void *arg)
|
2017-09-22 06:45:57 -04:00
|
|
|
{
|
2022-01-02 03:19:49 -05:00
|
|
|
int core_id = (int)arg;
|
|
|
|
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
|
2021-09-27 00:46:51 -04:00
|
|
|
};
|
2022-01-02 03:19:49 -05:00
|
|
|
TEST_ESP_OK(gptimer_new_timer(&timer_config, &gptimers[core_id]));
|
2022-04-23 06:59:38 -04:00
|
|
|
gptimer_event_callbacks_t cbs = {
|
|
|
|
.on_alarm = on_alarm_sender_cb,
|
|
|
|
};
|
|
|
|
TEST_ESP_OK(gptimer_register_event_callbacks(gptimers[core_id], &cbs, NULL));
|
|
|
|
TEST_ESP_OK(gptimer_enable(gptimers[core_id]));
|
2022-01-02 03:19:49 -05:00
|
|
|
test_gptimer_start(gptimers[core_id]);
|
|
|
|
xSemaphoreGive(task_delete_semphr);
|
|
|
|
vTaskDelete(NULL);
|
2017-09-22 06:45:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Test Task_Notify", "[freertos]")
|
|
|
|
{
|
2022-01-02 03:19:49 -05:00
|
|
|
test_start = false;
|
2017-09-22 06:45:57 -04:00
|
|
|
trigger_send_semphr = xSemaphoreCreateBinary();
|
2022-01-02 03:19:49 -05:00
|
|
|
task_delete_semphr = xQueueCreateCountingSemaphore(10, 0);
|
|
|
|
for (int i = 0; i < portNUM_PROCESSORS; i++) {
|
|
|
|
xTaskCreatePinnedToCore(install_gptimer_on_core, "install_gptimer", 4096, (void *const)i, UNITY_FREERTOS_PRIORITY + 1, NULL, i);
|
|
|
|
TEST_ASSERT(xSemaphoreTake(task_delete_semphr, pdMS_TO_TICKS(1000)));
|
|
|
|
}
|
|
|
|
// wait the gptimer installation done on specific core
|
|
|
|
vTaskDelay(10);
|
|
|
|
// test start
|
|
|
|
test_start = true;
|
|
|
|
for (int i = 0; i < portNUM_PROCESSORS; i++) { //Sending Core
|
|
|
|
for (int j = 0; j < portNUM_PROCESSORS; j++) { //Receiving Core
|
2017-09-22 06:45:57 -04:00
|
|
|
//Reset Values
|
|
|
|
notifs_sent = 0;
|
|
|
|
notifs_rec = 0;
|
|
|
|
wrong_core = false;
|
2022-01-02 03:19:49 -05:00
|
|
|
send_core_message = (0xFF << i); //0xFF if core 0, 0xFF0 if core 1
|
|
|
|
// receiver task has higher priority than sender task
|
|
|
|
xTaskCreatePinnedToCore(receiver_task, "recv task", 1000, NULL, UNITY_FREERTOS_PRIORITY + 2, &recv_task_handle, j);
|
|
|
|
xTaskCreatePinnedToCore(sender_task, "send task", 1000, gptimers[i], UNITY_FREERTOS_PRIORITY + 1, NULL, i);
|
2017-09-22 06:45:57 -04:00
|
|
|
vTaskDelay(5); //Wait for task creation to complete
|
|
|
|
|
|
|
|
xSemaphoreGive(trigger_send_semphr); //Trigger sender task
|
2022-01-02 03:19:49 -05:00
|
|
|
for (int k = 0; k < NO_OF_TASKS; k++) { //Wait for sender and receiver task deletion
|
|
|
|
TEST_ASSERT(xSemaphoreTake(task_delete_semphr, pdMS_TO_TICKS(2000)));
|
2017-09-22 06:45:57 -04:00
|
|
|
}
|
|
|
|
vTaskDelay(5); //Give time tasks to delete
|
|
|
|
|
2022-01-02 03:19:49 -05:00
|
|
|
TEST_ASSERT_EQUAL(NO_OF_NOTIFS, notifs_sent);
|
|
|
|
TEST_ASSERT_EQUAL(NO_OF_NOTIFS, notifs_rec);
|
|
|
|
TEST_ASSERT_EQUAL(false, wrong_core);
|
2017-09-22 06:45:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Delete Semaphroes and timer ISRs
|
|
|
|
vSemaphoreDelete(trigger_send_semphr);
|
|
|
|
vSemaphoreDelete(task_delete_semphr);
|
2022-01-02 03:19:49 -05:00
|
|
|
for (int i = 0; i < portNUM_PROCESSORS; i++) {
|
2022-04-23 06:59:38 -04:00
|
|
|
TEST_ESP_OK(gptimer_disable(gptimers[i]));
|
2022-01-02 03:19:49 -05:00
|
|
|
TEST_ESP_OK(gptimer_del_timer(gptimers[i]));
|
|
|
|
}
|
2017-09-22 06:45:57 -04:00
|
|
|
}
|