logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

timer.c (2998B)


  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. static uint32_t saved_ms;
  24. /** \brief timer initialization
  25. *
  26. * FIXME: needs doc
  27. */
  28. void timer_init(void) {
  29. #if TIMER_PRESCALER == 1
  30. uint8_t prescaler = _BV(CS00);
  31. #elif TIMER_PRESCALER == 8
  32. uint8_t prescaler = _BV(CS01);
  33. #elif TIMER_PRESCALER == 64
  34. uint8_t prescaler = _BV(CS00) | _BV(CS01);
  35. #elif TIMER_PRESCALER == 256
  36. uint8_t prescaler = _BV(CS02);
  37. #elif TIMER_PRESCALER == 1024
  38. uint8_t prescaler = _BV(CS00) | _BV(CS02);
  39. #else
  40. # error "Timer prescaler value is not valid"
  41. #endif
  42. #if defined(__AVR_ATmega32A__)
  43. // Timer0 CTC mode
  44. TCCR0 = _BV(WGM01) | prescaler;
  45. OCR0 = TIMER_RAW_TOP;
  46. TIMSK = _BV(OCIE0);
  47. #elif defined(__AVR_ATtiny85__)
  48. // Timer0 CTC mode
  49. TCCR0A = _BV(WGM01);
  50. TCCR0B = prescaler;
  51. OCR0A = TIMER_RAW_TOP;
  52. TIMSK = _BV(OCIE0A);
  53. #else
  54. // Timer0 CTC mode
  55. TCCR0A = _BV(WGM01);
  56. TCCR0B = prescaler;
  57. OCR0A = TIMER_RAW_TOP;
  58. TIMSK0 = _BV(OCIE0A);
  59. #endif
  60. }
  61. /** \brief timer clear
  62. *
  63. * FIXME: needs doc
  64. */
  65. inline void timer_clear(void) {
  66. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  67. timer_count = 0;
  68. }
  69. }
  70. /** \brief timer save
  71. *
  72. * Set saved_ms to current time.
  73. */
  74. void timer_save(void) {
  75. saved_ms = timer_read32();
  76. }
  77. /** \brief timer restore
  78. *
  79. * Set timer_count to saved_ms
  80. */
  81. void timer_restore(void) {
  82. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  83. timer_count = saved_ms;
  84. }
  85. }
  86. /** \brief timer read
  87. *
  88. * FIXME: needs doc
  89. */
  90. inline uint16_t timer_read(void) {
  91. uint32_t t;
  92. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  93. t = timer_count;
  94. }
  95. return (t & 0xFFFF);
  96. }
  97. /** \brief timer read32
  98. *
  99. * FIXME: needs doc
  100. */
  101. inline uint32_t timer_read32(void) {
  102. uint32_t t;
  103. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  104. t = timer_count;
  105. }
  106. return t;
  107. }
  108. // excecuted once per 1ms.(excess for just timer count?)
  109. #ifndef __AVR_ATmega32A__
  110. # define TIMER_INTERRUPT_VECTOR TIMER0_COMPA_vect
  111. #else
  112. # define TIMER_INTERRUPT_VECTOR TIMER0_COMP_vect
  113. #endif
  114. ISR(TIMER_INTERRUPT_VECTOR, ISR_NOBLOCK) {
  115. timer_count++;
  116. }