test_freertos_task_delay_until.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. static SemaphoreHandle_t task_delete_semphr;
  23. static void delaying_task(void* arg)
  24. {
  25. uint64_t ref_prev, ref_current;
  26. TickType_t last_wake_time;
  27. TickType_t ticks_before_delay;
  28. vTaskDelay(1); //Delay until next tick to synchronize operations to tick boundaries
  29. last_wake_time = xTaskGetTickCount();
  30. ticks_before_delay = last_wake_time;
  31. ref_prev = ref_clock_get();
  32. for(int i = 0; i < NO_OF_CYCLES; i++){
  33. //Delay of TICKS_TO_DELAY
  34. vTaskDelayUntil(&last_wake_time, TICKS_TO_DELAY);
  35. //Get current ref clock
  36. TEST_ASSERT_EQUAL(IDEAL_DELAY_PERIOD_MS, TICKS_TO_MS(xTaskGetTickCount() - ticks_before_delay));
  37. ref_current = ref_clock_get();
  38. TEST_ASSERT_UINT32_WITHIN(TICK_PERIOD_US, IDEAL_DELAY_PERIOD_US, (uint32_t)(ref_current - ref_prev));
  39. ref_prev = ref_current;
  40. ticks_before_delay = last_wake_time;
  41. }
  42. //Delete Task after prescribed number of cycles
  43. xSemaphoreGive(task_delete_semphr);
  44. vTaskDelete(NULL);
  45. }
  46. TEST_CASE("Test vTaskDelayUntil", "[freertos]")
  47. {
  48. task_delete_semphr = xQueueCreateCountingSemaphore(NO_OF_TASKS_PER_CORE*portNUM_PROCESSORS, 0);
  49. ref_clock_init();
  50. for(int i = 0; i < portNUM_PROCESSORS; i++){
  51. xTaskCreatePinnedToCore(delaying_task, "delay_pinned", 1024, NULL, TSK_PRIORITY, NULL, i);
  52. xTaskCreatePinnedToCore(delaying_task, "delay_no_aff", 1024, NULL, TSK_PRIORITY, NULL, tskNO_AFFINITY);
  53. }
  54. for(int i = 0; i < NO_OF_TASKS_PER_CORE*portNUM_PROCESSORS; i++){
  55. xSemaphoreTake(task_delete_semphr, portMAX_DELAY);
  56. }
  57. //Cleanup
  58. vSemaphoreDelete(task_delete_semphr);
  59. ref_clock_deinit();
  60. }