test_freertos_task_delay_until.c 2.4 KB

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