test_legacy_hooks.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. Test of FreeRTOS "legacy" hook functions, and delete hook
  3. Only compiled in if the relevant options are enabled.
  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 "driver/timer.h"
  11. #include "unity.h"
  12. #include "test_utils.h"
  13. #if CONFIG_FREERTOS_LEGACY_HOOKS
  14. static volatile unsigned idle_count;
  15. void vApplicationIdleHook(void)
  16. {
  17. idle_count++;
  18. }
  19. TEST_CASE("legacy idle hook is correctly called based on config", "[freertos]")
  20. {
  21. idle_count = 0;
  22. vTaskDelay(10);
  23. TEST_ASSERT_NOT_EQUAL(0, idle_count); // The legacy idle hook should be called at least once
  24. }
  25. static volatile unsigned tick_count;
  26. void IRAM_ATTR vApplicationTickHook(void)
  27. {
  28. tick_count++;
  29. }
  30. TEST_CASE("legacy tick hook is called based on config", "[freertos]")
  31. {
  32. unsigned before = xTaskGetTickCount();
  33. const unsigned SLEEP_FOR = 20;
  34. tick_count = before;
  35. vTaskDelay(SLEEP_FOR);
  36. TEST_ASSERT_UINT32_WITHIN_MESSAGE(3 * portNUM_PROCESSORS, before + SLEEP_FOR * portNUM_PROCESSORS, tick_count,
  37. "The legacy tick hook should have been called approx 1 time per tick per CPU");
  38. }
  39. #endif // CONFIG_FREERTOS_LEGACY_HOOKS
  40. #if CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP
  41. static volatile void *deleted_tcb;
  42. static void taskDeletesItself(void *ignored)
  43. {
  44. vTaskDelete(NULL);
  45. }
  46. void vPortCleanUpTCB(void *pxTCB)
  47. {
  48. deleted_tcb = pxTCB;
  49. }
  50. TEST_CASE("static task cleanup hook is called based on config", "[freertos]")
  51. {
  52. for(int i = 0; i < portNUM_PROCESSORS; i++) {
  53. printf("Creating task CPU %d\n", i);
  54. TaskHandle_t new_task = NULL;
  55. deleted_tcb = NULL;
  56. xTaskCreatePinnedToCore(taskDeletesItself, "delete", 2048, NULL, UNITY_FREERTOS_PRIORITY, &new_task, i);
  57. vTaskDelay(5);
  58. TEST_ASSERT_EQUAL_PTR(deleted_tcb, new_task); // TCB & TaskHandle are the same in FreeRTOS
  59. }
  60. }
  61. #endif // CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP