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.

152 lines
2.8 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. {
  29. #if TIMER_PRESCALER == 1
  30. uint8_t prescaler = 0x01;
  31. #elif TIMER_PRESCALER == 8
  32. uint8_t prescaler = 0x02;
  33. #elif TIMER_PRESCALER == 64
  34. uint8_t prescaler = 0x03;
  35. #elif TIMER_PRESCALER == 256
  36. uint8_t prescaler = 0x04;
  37. #elif TIMER_PRESCALER == 1024
  38. uint8_t prescaler = 0x05;
  39. #else
  40. # error "Timer prescaler value is NOT vaild."
  41. #endif
  42. #ifndef __AVR_ATmega32A__
  43. // Timer0 CTC mode
  44. TCCR0A = 0x02;
  45. TCCR0B = prescaler;
  46. OCR0A = TIMER_RAW_TOP;
  47. TIMSK0 = (1<<OCIE0A);
  48. #else
  49. // Timer0 CTC mode
  50. TCCR0 = (1 << WGM01) | prescaler;
  51. OCR0 = TIMER_RAW_TOP;
  52. TIMSK = (1 << OCIE0);
  53. #endif
  54. }
  55. /** \brief timer clear
  56. *
  57. * FIXME: needs doc
  58. */
  59. inline
  60. void timer_clear(void)
  61. {
  62. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  63. timer_count = 0;
  64. }
  65. }
  66. /** \brief timer read
  67. *
  68. * FIXME: needs doc
  69. */
  70. inline
  71. uint16_t timer_read(void)
  72. {
  73. uint32_t t;
  74. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  75. t = timer_count;
  76. }
  77. return (t & 0xFFFF);
  78. }
  79. /** \brief timer read32
  80. *
  81. * FIXME: needs doc
  82. */
  83. inline
  84. uint32_t timer_read32(void)
  85. {
  86. uint32_t t;
  87. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  88. t = timer_count;
  89. }
  90. return t;
  91. }
  92. /** \brief timer elapsed
  93. *
  94. * FIXME: needs doc
  95. */
  96. inline
  97. uint16_t timer_elapsed(uint16_t last)
  98. {
  99. uint32_t t;
  100. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  101. t = timer_count;
  102. }
  103. return TIMER_DIFF_16((t & 0xFFFF), last);
  104. }
  105. /** \brief timer elapsed32
  106. *
  107. * FIXME: needs doc
  108. */
  109. inline
  110. uint32_t timer_elapsed32(uint32_t last)
  111. {
  112. uint32_t t;
  113. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  114. t = timer_count;
  115. }
  116. return TIMER_DIFF_32(t, last);
  117. }
  118. // excecuted once per 1ms.(excess for just timer count?)
  119. #ifndef __AVR_ATmega32A__
  120. #define TIMER_INTERRUPT_VECTOR TIMER0_COMPA_vect
  121. #else
  122. #define TIMER_INTERRUPT_VECTOR TIMER0_COMP_vect
  123. #endif
  124. ISR(TIMER_INTERRUPT_VECTOR, ISR_NOBLOCK)
  125. {
  126. timer_count++;
  127. }