You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
3.1 KiB

  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) {
  66. timer_count = 0;
  67. }
  68. }
  69. /** \brief timer read
  70. *
  71. * FIXME: needs doc
  72. */
  73. inline uint16_t timer_read(void) {
  74. uint32_t t;
  75. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  76. t = timer_count;
  77. }
  78. return (t & 0xFFFF);
  79. }
  80. /** \brief timer read32
  81. *
  82. * FIXME: needs doc
  83. */
  84. inline uint32_t timer_read32(void) {
  85. uint32_t t;
  86. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  87. t = timer_count;
  88. }
  89. return t;
  90. }
  91. /** \brief timer elapsed
  92. *
  93. * FIXME: needs doc
  94. */
  95. inline uint16_t timer_elapsed(uint16_t last) {
  96. uint32_t t;
  97. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  98. t = timer_count;
  99. }
  100. return TIMER_DIFF_16((t & 0xFFFF), last);
  101. }
  102. /** \brief timer elapsed32
  103. *
  104. * FIXME: needs doc
  105. */
  106. inline uint32_t timer_elapsed32(uint32_t last) {
  107. uint32_t t;
  108. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  109. t = timer_count;
  110. }
  111. return TIMER_DIFF_32(t, last);
  112. }
  113. // excecuted once per 1ms.(excess for just timer count?)
  114. #ifndef __AVR_ATmega32A__
  115. # define TIMER_INTERRUPT_VECTOR TIMER0_COMPA_vect
  116. #else
  117. # define TIMER_INTERRUPT_VECTOR TIMER0_COMP_vect
  118. #endif
  119. ISR(TIMER_INTERRUPT_VECTOR, ISR_NOBLOCK) {
  120. timer_count++;
  121. }