Keyboard firmwares for Atmel AVR and Cortex-M
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.

timer.c 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. Copyright 2011 Jun Wako <[email protected]>
  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 <stdint.h>
  17. #include "timer_avr.h"
  18. #include "timer.h"
  19. // counter resolution 1ms
  20. // NOTE: union { uint32_t timer32; struct { uint16_t dummy; uint16_t timer16; }}
  21. volatile uint32_t timer_count = 0;
  22. void timer_init(void)
  23. {
  24. // Timer0 CTC mode
  25. TCCR0A = 0x02;
  26. #if TIMER_PRESCALER == 1
  27. TCCR0B = 0x01;
  28. #elif TIMER_PRESCALER == 8
  29. TCCR0B = 0x02;
  30. #elif TIMER_PRESCALER == 64
  31. TCCR0B = 0x03;
  32. #elif TIMER_PRESCALER == 256
  33. TCCR0B = 0x04;
  34. #elif TIMER_PRESCALER == 1024
  35. TCCR0B = 0x05;
  36. #else
  37. # error "Timer prescaler value is NOT vaild."
  38. #endif
  39. OCR0A = TIMER_RAW_TOP;
  40. TIMSK0 = (1<<OCIE0A);
  41. }
  42. inline
  43. void timer_clear(void)
  44. {
  45. uint8_t sreg = SREG;
  46. cli();
  47. timer_count = 0;
  48. SREG = sreg;
  49. }
  50. inline
  51. uint16_t timer_read(void)
  52. {
  53. uint32_t t;
  54. uint8_t sreg = SREG;
  55. cli();
  56. t = timer_count;
  57. SREG = sreg;
  58. return (t & 0xFFFF);
  59. }
  60. inline
  61. uint32_t timer_read32(void)
  62. {
  63. uint32_t t;
  64. uint8_t sreg = SREG;
  65. cli();
  66. t = timer_count;
  67. SREG = sreg;
  68. return t;
  69. }
  70. inline
  71. uint16_t timer_elapsed(uint16_t last)
  72. {
  73. uint32_t t;
  74. uint8_t sreg = SREG;
  75. cli();
  76. t = timer_count;
  77. SREG = sreg;
  78. return TIMER_DIFF_16((t & 0xFFFF), last);
  79. }
  80. inline
  81. uint32_t timer_elapsed32(uint32_t last)
  82. {
  83. uint32_t t;
  84. uint8_t sreg = SREG;
  85. cli();
  86. t = timer_count;
  87. SREG = sreg;
  88. return TIMER_DIFF_32(t, last);
  89. }
  90. // excecuted once per 1ms.(excess for just timer count?)
  91. ISR(TIMER0_COMPA_vect)
  92. {
  93. timer_count++;
  94. }