test_timer.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "test.h"
  2. #include "timer.c"
  3. static void check_timer(void)
  4. {
  5. struct timer t;
  6. CHECK(TIMER_CPS == 1000);
  7. /* Check: init */
  8. timer_init();
  9. CHECK(TIMSK0 & (1 << OCIE0A));
  10. CHECK(TIFR0 == (1 << OCF0A));
  11. CHECK(TCCR0A & (1 << WGM01));
  12. CHECK((TCCR0B & ((0 << CS02) | (1 << CS01) | (1 << CS00))) ==
  13. ((0 << CS02) | (1 << CS01) | (1 << CS00)));
  14. CHECK(abs((long)F_CPU - (OCR0A * 64 * TIMER_CPS)) < ((long)F_CPU / 100));
  15. /* Check: ISR */
  16. _timer_count_now = 42;
  17. TIMER0_COMPA_vect();
  18. CHECK_EQ_UU(_timer_count_now, 43);
  19. /* Check: get */
  20. _timer_count_now = 42;
  21. CHECK_EQ_UU(timer_now(), 42);
  22. /* Check: ms to count */
  23. CHECK_EQ_II(_timer_ms_to_count(50),
  24. TIMER_CPS * 50 / 1000);
  25. CHECK_EQ_II(_timer_ms_to_count(100),
  26. TIMER_CPS * 100 / 1000);
  27. CHECK_EQ_II(_timer_ms_to_count(1000),
  28. TIMER_CPS * 1000 / 1000);
  29. CHECK_EQ_II(_timer_ms_to_count(2000),
  30. TIMER_CPS * 2000 / 1000);
  31. /* Rounded (round up) */
  32. // CHECK_EQ_II(_timer_ms_to_count(1), 1);
  33. // CHECK_EQ_II(_timer_ms_to_count(2), 1);
  34. /* Negative */
  35. CHECK_EQ_II(_timer_ms_to_count(-50),
  36. (_signed_timcnt_t)(TIMER_CPS * -50 / 1000));
  37. CHECK_EQ_II(_timer_ms_to_count(-100),
  38. (_signed_timcnt_t)(TIMER_CPS * -100 / 1000));
  39. CHECK_EQ_II(_timer_ms_to_count(-1000),
  40. (_signed_timcnt_t)(TIMER_CPS * -1000 / 1000));
  41. CHECK_EQ_II(_timer_ms_to_count(-2000),
  42. (_signed_timcnt_t)(TIMER_CPS * -2000 / 1000));
  43. /* Rounded (round up) */
  44. // CHECK_EQ_II(_timer_ms_to_count(-1), 0);
  45. // CHECK_EQ_II(_timer_ms_to_count(-2), 0);
  46. // CHECK_EQ_II(_timer_ms_to_count(-9), 0);
  47. // CHECK_EQ_II(_timer_ms_to_count(-10), -1);
  48. /* Check: count to ms */
  49. CHECK_EQ_II(_timer_count_to_ms((_signed_timcnt_t)0),
  50. (int32_t)(1000 * 0 / TIMER_CPS));
  51. CHECK_EQ_II(_timer_count_to_ms((_signed_timcnt_t)1),
  52. (int32_t)(1000 * 1 / TIMER_CPS));
  53. CHECK_EQ_II(_timer_count_to_ms((_signed_timcnt_t)50),
  54. (int32_t)(1000 * 50 / TIMER_CPS));
  55. CHECK_EQ_II(_timer_count_to_ms((_signed_timcnt_t)100),
  56. (int32_t)(1000 * 100 / TIMER_CPS));
  57. CHECK_EQ_II(_timer_count_to_ms((_signed_timcnt_t)10000),
  58. (int32_t)(1000 * 10000 / TIMER_CPS));
  59. CHECK_EQ_II(_timer_count_to_ms((_signed_timcnt_t)32767),
  60. (int32_t)(1000 * 32767 / TIMER_CPS));
  61. /* negative */
  62. CHECK_EQ_II(_timer_count_to_ms((_signed_timcnt_t)-1),
  63. (int32_t)(1000 * -1 / TIMER_CPS));
  64. CHECK_EQ_II(_timer_count_to_ms((_signed_timcnt_t)-50),
  65. (int32_t)(1000 * -50 / TIMER_CPS));
  66. CHECK_EQ_II(_timer_count_to_ms((_signed_timcnt_t)-100),
  67. (int32_t)(1000 * -100 / TIMER_CPS));
  68. CHECK_EQ_II(_timer_count_to_ms((_signed_timcnt_t)-10000),
  69. (int32_t)(1000 * -10000 / TIMER_CPS));
  70. CHECK_EQ_II(_timer_count_to_ms((_signed_timcnt_t)-32768),
  71. (int32_t)(1000 * -32768 / TIMER_CPS));
  72. /* Check: arm */
  73. _timer_count_now = 42;
  74. timer_arm(&t, 100);
  75. CHECK_EQ_UU(t.count,
  76. 42u + (uint32_t)_timer_ms_to_count(100));
  77. /* Check: add */
  78. _timer_count_now = 42;
  79. timer_arm(&t, 100);
  80. timer_add(&t, 50);
  81. CHECK_EQ_UU(t.count,
  82. 42u + (uint32_t)_timer_ms_to_count(100) +
  83. (uint32_t)_timer_ms_to_count(50));
  84. /* Check: count since */
  85. _timer_count_now = 42;
  86. timer_arm(&t, 0);
  87. t.count = 42;
  88. CHECK_EQ_II(_timer_count_since(&t),
  89. (_signed_timcnt_t)0);
  90. t.count = 43;
  91. CHECK_EQ_II(_timer_count_since(&t),
  92. (_signed_timcnt_t)-1);
  93. t.count = 41;
  94. CHECK_EQ_II(_timer_count_since(&t),
  95. (_signed_timcnt_t)1);
  96. t.count = (_timcnt_t)-42;
  97. CHECK_EQ_II(_timer_count_since(&t),
  98. (_signed_timcnt_t)84);
  99. /* Check: ms since */
  100. _timer_count_now = (_timcnt_t)_timer_ms_to_count(500);
  101. timer_arm(&t, 0);
  102. t.count = (_timcnt_t)_timer_ms_to_count(500);
  103. CHECK_EQ_II(timer_ms_since(&t), 0);
  104. t.count = (_timcnt_t)_timer_ms_to_count(600);
  105. CHECK_EQ_II(timer_ms_since(&t), -100);
  106. t.count = (_timcnt_t)_timer_ms_to_count(400);
  107. CHECK_EQ_II(timer_ms_since(&t), 100);
  108. t.count = (_timcnt_t)_timer_ms_to_count(-500);
  109. CHECK_EQ_II(timer_ms_since(&t), 1000);
  110. /* Check: expired */
  111. _timer_count_now = (_timcnt_t)_timer_ms_to_count(500);
  112. timer_arm(&t, 0);
  113. t.count = (_timcnt_t)_timer_ms_to_count(500);
  114. CHECK(timer_expired(&t));
  115. t.count = (_timcnt_t)_timer_ms_to_count(600);
  116. CHECK(!timer_expired(&t));
  117. t.count = (_timcnt_t)_timer_ms_to_count(400);
  118. CHECK(timer_expired(&t));
  119. t.count = (_timcnt_t)_timer_ms_to_count(-500);
  120. CHECK(timer_expired(&t));
  121. }
  122. int main(void)
  123. {
  124. check_timer();
  125. return 0;
  126. }