test_freertos_task_delay_until.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. Test for FreeRTOS vTaskDelayUntil() function by comparing the delay period of
  3. the function to comparing it to ref clock.
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #include "freertos/semphr.h"
  10. #include "freertos/FreeRTOSConfig.h"
  11. #include "unity.h"
  12. #include "test_utils.h"
  13. #define TSK_PRIORITY (UNITY_FREERTOS_PRIORITY + 1)
  14. #define NO_OF_CYCLES 5
  15. #define NO_OF_TASKS_PER_CORE 2
  16. #define TICKS_TO_DELAY 10
  17. #define TICK_RATE configTICK_RATE_HZ
  18. #define TICK_PERIOD_US (1000000/TICK_RATE)
  19. #define IDEAL_DELAY_PERIOD_MS ((1000*TICKS_TO_DELAY)/TICK_RATE)
  20. #define IDEAL_DELAY_PERIOD_US (IDEAL_DELAY_PERIOD_MS*1000)
  21. #define TICKS_TO_MS(x) (((x)*1000)/TICK_RATE)
  22. #define REF_TO_ROUND_MS(x) (((x)+500)/1000)
  23. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2BETA)
  24. static SemaphoreHandle_t task_delete_semphr;
  25. static void delaying_task(void* arg)
  26. {
  27. uint64_t ref_prev, ref_current;
  28. TickType_t last_wake_time;
  29. TickType_t ticks_before_delay;
  30. vTaskDelay(1); //Delay until next tick to synchronize operations to tick boundaries
  31. last_wake_time = xTaskGetTickCount();
  32. ticks_before_delay = last_wake_time;
  33. ref_prev = ref_clock_get();
  34. for(int i = 0; i < NO_OF_CYCLES; i++){
  35. //Delay of TICKS_TO_DELAY
  36. vTaskDelayUntil(&last_wake_time, TICKS_TO_DELAY);
  37. //Get current ref clock
  38. TEST_ASSERT_EQUAL(IDEAL_DELAY_PERIOD_MS, TICKS_TO_MS(xTaskGetTickCount() - ticks_before_delay));
  39. ref_current = ref_clock_get();
  40. TEST_ASSERT_UINT32_WITHIN(TICK_PERIOD_US, IDEAL_DELAY_PERIOD_US, (uint32_t)(ref_current - ref_prev));
  41. ref_prev = ref_current;
  42. ticks_before_delay = last_wake_time;
  43. }
  44. //Delete Task after prescribed number of cycles
  45. xSemaphoreGive(task_delete_semphr);
  46. vTaskDelete(NULL);
  47. }
  48. TEST_CASE("Test vTaskDelayUntil", "[freertos]")
  49. {
  50. task_delete_semphr = xQueueCreateCountingSemaphore(NO_OF_TASKS_PER_CORE*portNUM_PROCESSORS, 0);
  51. ref_clock_init();
  52. for(int i = 0; i < portNUM_PROCESSORS; i++){
  53. xTaskCreatePinnedToCore(delaying_task, "delay_pinned", 1024, NULL, TSK_PRIORITY, NULL, i);
  54. xTaskCreatePinnedToCore(delaying_task, "delay_no_aff", 1024, NULL, TSK_PRIORITY, NULL, tskNO_AFFINITY);
  55. }
  56. for(int i = 0; i < NO_OF_TASKS_PER_CORE*portNUM_PROCESSORS; i++){
  57. xSemaphoreTake(task_delete_semphr, portMAX_DELAY);
  58. }
  59. //Cleanup
  60. vSemaphoreDelete(task_delete_semphr);
  61. ref_clock_deinit();
  62. }
  63. #endif // CONFIG_IDF_TARGET_ESP32S2BETA