test_timers.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* FreeRTOS timer tests
  2. */
  3. #include <stdio.h>
  4. #include "unity.h"
  5. #include "freertos/FreeRTOS.h"
  6. #include "freertos/task.h"
  7. #include "freertos/timers.h"
  8. static void timer_callback(TimerHandle_t timer)
  9. {
  10. volatile int *count;
  11. count = (volatile int *)pvTimerGetTimerID( timer );
  12. (*count)++;
  13. printf("Callback timer %p count %p = %d\n", timer, count, *count);
  14. }
  15. TEST_CASE("Oneshot FreeRTOS timers", "[freertos]")
  16. {
  17. volatile int count = 0;
  18. TimerHandle_t oneshot = xTimerCreate("oneshot", 100 / portTICK_PERIOD_MS, pdFALSE,
  19. (void *)&count, timer_callback);
  20. TEST_ASSERT(oneshot);
  21. TEST_ASSERT_EQUAL(pdFALSE, xTimerIsTimerActive(oneshot));
  22. TEST_ASSERT_EQUAL(0, count);
  23. TEST_ASSERT( xTimerStart(oneshot, 1) );
  24. vTaskDelay(2); /* give the timer task a chance to process the message */
  25. TEST_ASSERT_EQUAL(pdTRUE, xTimerIsTimerActive(oneshot));
  26. TEST_ASSERT_EQUAL(0, count);
  27. vTaskDelay(250 / portTICK_PERIOD_MS); // 2.5 timer periods
  28. TEST_ASSERT_EQUAL(1, count);
  29. TEST_ASSERT_EQUAL(pdFALSE, xTimerIsTimerActive(oneshot));
  30. TEST_ASSERT( xTimerDelete(oneshot, 1) );
  31. }
  32. TEST_CASE("Recurring FreeRTOS timers", "[freertos]")
  33. {
  34. volatile int count = 0;
  35. TimerHandle_t recurring = xTimerCreate("oneshot", 100 / portTICK_PERIOD_MS, pdTRUE,
  36. (void *)&count, timer_callback);
  37. TEST_ASSERT(recurring);
  38. TEST_ASSERT_EQUAL(pdFALSE, xTimerIsTimerActive(recurring));
  39. TEST_ASSERT_EQUAL(0, count);
  40. TEST_ASSERT( xTimerStart(recurring, 1) );
  41. vTaskDelay(2); // let timer task process the queue
  42. TEST_ASSERT_EQUAL(pdTRUE, xTimerIsTimerActive(recurring));
  43. TEST_ASSERT_EQUAL(0, count);
  44. vTaskDelay(250 / portTICK_PERIOD_MS); // 2.5 timer periods
  45. TEST_ASSERT_EQUAL(2, count);
  46. TEST_ASSERT_EQUAL(pdTRUE, xTimerIsTimerActive(recurring));
  47. TEST_ASSERT( xTimerStop(recurring, 1) );
  48. TEST_ASSERT_EQUAL(2, count);
  49. vTaskDelay(100 / portTICK_PERIOD_MS); // One more timer period
  50. TEST_ASSERT_EQUAL(2, count); // hasn't gone up
  51. TEST_ASSERT_EQUAL(pdFALSE, xTimerIsTimerActive(recurring));
  52. TEST_ASSERT( xTimerDelete(recurring, 1) );
  53. }