unity_utils_freertos.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include <stdbool.h>
  8. #include "unity.h"
  9. #include "unity_test_utils.h"
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/semphr.h"
  12. #include "freertos/task.h"
  13. #include "sdkconfig.h"
  14. #if !CONFIG_FREERTOS_UNICORE
  15. #include "esp_ipc.h"
  16. #include "esp_freertos_hooks.h"
  17. #endif
  18. #if !CONFIG_FREERTOS_UNICORE
  19. static StaticSemaphore_t s_test_sem_buffer;
  20. static SemaphoreHandle_t s_test_sem = NULL;
  21. static bool idle_hook_func(void)
  22. {
  23. if (s_test_sem) {
  24. xSemaphoreGive(s_test_sem);
  25. }
  26. return true;
  27. }
  28. static void task_delete_func(void *arg)
  29. {
  30. vTaskDelete(arg);
  31. }
  32. #define SEMAPHORE_COUNT_MAX 2
  33. #define SEMAPHORE_COUNT_INITIAL 0
  34. #endif // !CONFIG_FREERTOS_UNICORE
  35. void unity_utils_task_delete(TaskHandle_t thandle)
  36. {
  37. /* Self deletion can not free up associated task dynamic memory immediately,
  38. * hence not recommended for test scenarios */
  39. TEST_ASSERT_NOT_NULL_MESSAGE(thandle, "unity_utils_task_delete: handle is NULL");
  40. TEST_ASSERT_NOT_EQUAL_MESSAGE(thandle, xTaskGetCurrentTaskHandle(), "unity_utils_task_delete: handle is of currently executing task");
  41. #if CONFIG_FREERTOS_UNICORE
  42. vTaskDelete(thandle);
  43. #else // CONFIG_FREERTOS_UNICORE
  44. const BaseType_t tsk_affinity = xTaskGetCoreID(thandle);
  45. const BaseType_t core_id = xPortGetCoreID();
  46. printf("Task_affinity: 0x%x, current_core: %d\n", tsk_affinity, core_id);
  47. if (tsk_affinity == tskNO_AFFINITY) {
  48. // create the semaphore if not done yet, or else, make sure its count is 0
  49. if (s_test_sem == NULL) {
  50. s_test_sem = xSemaphoreCreateCountingStatic(SEMAPHORE_COUNT_MAX, SEMAPHORE_COUNT_INITIAL, &s_test_sem_buffer);
  51. } else {
  52. while (xSemaphoreTake(s_test_sem, 0) == pdTRUE) { }
  53. }
  54. /* For no affinity case, we wait for idle hook to trigger on different core */
  55. esp_err_t ret = esp_register_freertos_idle_hook_for_cpu(idle_hook_func, !core_id);
  56. TEST_ASSERT_EQUAL_MESSAGE(ret, ESP_OK, "unity_utils_task_delete: failed to register idle hook");
  57. vTaskDelete(thandle);
  58. // We want to make sure that the entire idle task has run (including the part that removes remaining
  59. // memory of deleted tasks). Since we don't know where the idle task has been stopped, we need to
  60. // wait until the idle hook has run twice. This makes sure that the entire idle task has run at least once.
  61. xSemaphoreTake(s_test_sem, portMAX_DELAY);
  62. xSemaphoreTake(s_test_sem, portMAX_DELAY);
  63. esp_deregister_freertos_idle_hook_for_cpu(idle_hook_func, !core_id);
  64. } else if (tsk_affinity != core_id) {
  65. /* Task affinity and current core are differnt, schedule IPC call (to delete task)
  66. * on core where task is pinned to */
  67. esp_ipc_call_blocking(tsk_affinity, task_delete_func, thandle);
  68. } else {
  69. /* Task affinity and current core are same, so we can safely proceed for deletion */
  70. vTaskDelete(thandle);
  71. }
  72. #endif // !CONFIG_FREERTOS_UNICORE
  73. }