timer.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Timer routines
  3. *
  4. * Copyright (c) 2015-2016 Michael Buesch <m@bues.ch>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "timer.h"
  21. #include <avr/io.h>
  22. static _timcnt_t _timer_count_now;
  23. ISR(TIMER0_COMPA_vect)
  24. {
  25. _timer_count_now++;
  26. mb();
  27. }
  28. _timcnt_t timer_now(void)
  29. {
  30. _timcnt_t count;
  31. uint8_t sreg;
  32. sreg = irq_disable_save();
  33. count = _timer_count_now;
  34. irq_restore(sreg);
  35. return count;
  36. }
  37. static _signed_timcnt_t _timer_ms_to_count(int32_t millisec)
  38. {
  39. return (_signed_timcnt_t)sdiv_round_up((int32_t)TIMERCPS * millisec,
  40. (int32_t)1000);
  41. }
  42. static int32_t _timer_count_to_ms(_signed_timcnt_t count)
  43. {
  44. return sdiv_round((int32_t)count * (int32_t)1000,
  45. (int32_t)TIMERCPS);
  46. }
  47. static _signed_timcnt_t _timer_count_since(const struct timer *timer)
  48. {
  49. return (_signed_timcnt_t)(timer_now() - timer->count);
  50. }
  51. bool timer_expired(const struct timer *timer)
  52. {
  53. return _timer_count_since(timer) >= 0;
  54. }
  55. int32_t timer_ms_since(const struct timer *timer)
  56. {
  57. return _timer_count_to_ms(_timer_count_since(timer));
  58. }
  59. void timer_arm(struct timer *timer, int32_t millisec)
  60. {
  61. timer->count = (_timcnt_t)(timer_now() +
  62. (_timcnt_t)_timer_ms_to_count(millisec));
  63. }
  64. void timer_set_now(struct timer *timer)
  65. {
  66. timer->count = timer_now();
  67. }
  68. void timer_add(struct timer *timer, int32_t millisec)
  69. {
  70. timer->count = (_timcnt_t)(timer->count +
  71. (_timcnt_t)_timer_ms_to_count(millisec));
  72. }
  73. void timer_init(void)
  74. {
  75. build_assert(F_CPU == 8000000ul);
  76. /* Initialize timer-0 as system timer. */
  77. OCR0A = 125;
  78. TCNT0 = 0;
  79. /* CTC mode; PS 64 */
  80. TCCR0A = (1 << WGM01) | (0 << WGM00);
  81. TCCR0B = (0 << WGM02) | (0 << CS02) | (1 << CS01) | (1 << CS00);
  82. /* Enable OC interrupt. */
  83. TIFR0 = (1 << OCF0A);
  84. TIMSK0 |= (1 << OCIE0A);
  85. }