timer.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #if defined(__AVR_ATmega32A__)
  42. // Timer0 CTC mode
  43. TCCR0 = _BV(WGM01) | prescaler;
  44. OCR0 = TIMER_RAW_TOP;
  45. TIMSK = _BV(OCIE0);
  46. #elif defined(__AVR_ATtiny85__)
  47. // Timer0 CTC mode
  48. TCCR0A = _BV(WGM01);
  49. TCCR0B = prescaler;
  50. OCR0A = TIMER_RAW_TOP;
  51. TIMSK = _BV(OCIE0A);
  52. #else
  53. // Timer0 CTC mode
  54. TCCR0A = _BV(WGM01);
  55. TCCR0B = prescaler;
  56. OCR0A = TIMER_RAW_TOP;
  57. TIMSK0 = _BV(OCIE0A);
  58. #endif
  59. }
  60. /** \brief timer clear
  61. *
  62. * FIXME: needs doc
  63. */
  64. inline void timer_clear(void) {
  65. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { timer_count = 0; }
  66. }
  67. /** \brief timer read
  68. *
  69. * FIXME: needs doc
  70. */
  71. inline uint16_t timer_read(void) {
  72. uint32_t t;
  73. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; }
  74. return (t & 0xFFFF);
  75. }
  76. /** \brief timer read32
  77. *
  78. * FIXME: needs doc
  79. */
  80. inline uint32_t timer_read32(void) {
  81. uint32_t t;
  82. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; }
  83. return t;
  84. }
  85. /** \brief timer elapsed
  86. *
  87. * FIXME: needs doc
  88. */
  89. inline uint16_t timer_elapsed(uint16_t last) {
  90. uint32_t t;
  91. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; }
  92. return TIMER_DIFF_16((t & 0xFFFF), last);
  93. }
  94. /** \brief timer elapsed32
  95. *
  96. * FIXME: needs doc
  97. */
  98. inline uint32_t timer_elapsed32(uint32_t last) {
  99. uint32_t t;
  100. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; }
  101. return TIMER_DIFF_32(t, last);
  102. }
  103. // excecuted once per 1ms.(excess for just timer count?)
  104. #ifndef __AVR_ATmega32A__
  105. # define TIMER_INTERRUPT_VECTOR TIMER0_COMPA_vect
  106. #else
  107. # define TIMER_INTERRUPT_VECTOR TIMER0_COMP_vect
  108. #endif
  109. ISR(TIMER_INTERRUPT_VECTOR, ISR_NOBLOCK) { timer_count++; }