timer.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Copyright 2011 Jun Wako <wakojun@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <avr/io.h>
  15. #include <avr/interrupt.h>
  16. #include <util/atomic.h>
  17. #include <stdint.h>
  18. #include "timer_avr.h"
  19. #include "timer.h"
  20. // counter resolution 1ms
  21. // NOTE: union { uint32_t timer32; struct { uint16_t dummy; uint16_t timer16; }}
  22. volatile uint32_t timer_count;
  23. /** \brief timer initialization
  24. *
  25. * FIXME: needs doc
  26. */
  27. void timer_init(void) {
  28. #if TIMER_PRESCALER == 1
  29. uint8_t prescaler = _BV(CS00);
  30. #elif TIMER_PRESCALER == 8
  31. uint8_t prescaler = _BV(CS01);
  32. #elif TIMER_PRESCALER == 64
  33. uint8_t prescaler = _BV(CS00) | _BV(CS01);
  34. #elif TIMER_PRESCALER == 256
  35. uint8_t prescaler = _BV(CS02);
  36. #elif TIMER_PRESCALER == 1024
  37. uint8_t prescaler = _BV(CS00) | _BV(CS02);
  38. #else
  39. # error "Timer prescaler value is not valid"
  40. #endif
  41. #ifndef __AVR_ATmega32A__
  42. // Timer0 CTC mode
  43. TCCR0A = _BV(WGM01);
  44. TCCR0B = prescaler;
  45. OCR0A = TIMER_RAW_TOP;
  46. TIMSK0 = _BV(OCIE0A);
  47. #else
  48. // Timer0 CTC mode
  49. TCCR0 = _BV(WGM01) | prescaler;
  50. OCR0 = TIMER_RAW_TOP;
  51. TIMSK = _BV(OCIE0);
  52. #endif
  53. }
  54. /** \brief timer clear
  55. *
  56. * FIXME: needs doc
  57. */
  58. inline void timer_clear(void) {
  59. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { timer_count = 0; }
  60. }
  61. /** \brief timer read
  62. *
  63. * FIXME: needs doc
  64. */
  65. inline uint16_t timer_read(void) {
  66. uint32_t t;
  67. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; }
  68. return (t & 0xFFFF);
  69. }
  70. /** \brief timer read32
  71. *
  72. * FIXME: needs doc
  73. */
  74. inline uint32_t timer_read32(void) {
  75. uint32_t t;
  76. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; }
  77. return t;
  78. }
  79. /** \brief timer elapsed
  80. *
  81. * FIXME: needs doc
  82. */
  83. inline uint16_t timer_elapsed(uint16_t last) {
  84. uint32_t t;
  85. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; }
  86. return TIMER_DIFF_16((t & 0xFFFF), last);
  87. }
  88. /** \brief timer elapsed32
  89. *
  90. * FIXME: needs doc
  91. */
  92. inline uint32_t timer_elapsed32(uint32_t last) {
  93. uint32_t t;
  94. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; }
  95. return TIMER_DIFF_32(t, last);
  96. }
  97. // excecuted once per 1ms.(excess for just timer count?)
  98. #ifndef __AVR_ATmega32A__
  99. # define TIMER_INTERRUPT_VECTOR TIMER0_COMPA_vect
  100. #else
  101. # define TIMER_INTERRUPT_VECTOR TIMER0_COMP_vect
  102. #endif
  103. ISR(TIMER_INTERRUPT_VECTOR, ISR_NOBLOCK) { timer_count++; }