timer.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. void timer_init(void)
  24. {
  25. #if TIMER_PRESCALER == 1
  26. uint8_t prescaler = 0x01;
  27. #elif TIMER_PRESCALER == 8
  28. uint8_t prescaler = 0x02;
  29. #elif TIMER_PRESCALER == 64
  30. uint8_t prescaler = 0x03;
  31. #elif TIMER_PRESCALER == 256
  32. uint8_t prescaler = 0x04;
  33. #elif TIMER_PRESCALER == 1024
  34. uint8_t prescaler = 0x05;
  35. #else
  36. # error "Timer prescaler value is NOT vaild."
  37. #endif
  38. #ifndef __AVR_ATmega32A__
  39. // Timer0 CTC mode
  40. TCCR0A = 0x02;
  41. TCCR0B = prescaler;
  42. OCR0A = TIMER_RAW_TOP;
  43. TIMSK0 = (1<<OCIE0A);
  44. #else
  45. // Timer0 CTC mode
  46. TCCR0 = (1 << WGM01) | prescaler;
  47. OCR0 = TIMER_RAW_TOP;
  48. TIMSK = (1 << OCIE0);
  49. #endif
  50. }
  51. inline
  52. void timer_clear(void)
  53. {
  54. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  55. timer_count = 0;
  56. }
  57. }
  58. inline
  59. uint16_t timer_read(void)
  60. {
  61. uint32_t t;
  62. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  63. t = timer_count;
  64. }
  65. return (t & 0xFFFF);
  66. }
  67. inline
  68. uint32_t timer_read32(void)
  69. {
  70. uint32_t t;
  71. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  72. t = timer_count;
  73. }
  74. return t;
  75. }
  76. inline
  77. uint16_t timer_elapsed(uint16_t last)
  78. {
  79. uint32_t t;
  80. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  81. t = timer_count;
  82. }
  83. return TIMER_DIFF_16((t & 0xFFFF), last);
  84. }
  85. inline
  86. uint32_t timer_elapsed32(uint32_t last)
  87. {
  88. uint32_t t;
  89. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  90. t = timer_count;
  91. }
  92. return TIMER_DIFF_32(t, last);
  93. }
  94. // excecuted once per 1ms.(excess for just timer count?)
  95. #ifndef __AVR_ATmega32A__
  96. #define TIMER_INTERRUPT_VECTOR TIMER0_COMPA_vect
  97. #else
  98. #define TIMER_INTERRUPT_VECTOR TIMER0_COMP_vect
  99. #endif
  100. ISR(TIMER_INTERRUPT_VECTOR, ISR_NOBLOCK)
  101. {
  102. timer_count++;
  103. }