PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

225 line
6.9KB

  1. /*
  2. FlexiTimer2.h - Using timer2 with a configurable resolution
  3. Wim Leers <work@wimleers.com>
  4. Based on MsTimer2
  5. Javier Valencia <javiervalencia80@gmail.com>
  6. History:
  7. 6/Jun/2014 - Added Teensy 3.0 & 3.1 support
  8. 16/Dec/2011 - Added Teensy/Teensy++ support (bperrybap)
  9. note: teensy uses timer4 instead of timer2
  10. 25/April/10 - Based on MsTimer2 V0.5 (from 29/May/09)
  11. This library is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU Lesser General Public
  13. License as published by the Free Software Foundation; either
  14. version 2.1 of the License, or (at your option) any later version.
  15. This library is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. Lesser General Public License for more details.
  19. You should have received a copy of the GNU Lesser General Public
  20. License along with this library; if not, write to the Free Software
  21. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include <FlexiTimer2.h>
  24. unsigned long FlexiTimer2::time_units;
  25. void (*FlexiTimer2::func)();
  26. volatile unsigned long FlexiTimer2::count;
  27. volatile char FlexiTimer2::overflowing;
  28. volatile unsigned int FlexiTimer2::tcnt2;
  29. #if defined(__arm__) && defined(TEENSYDUINO)
  30. static IntervalTimer itimer;
  31. #endif
  32. void FlexiTimer2::set(unsigned long ms, void (*f)()) {
  33. FlexiTimer2::set(ms, 0.001, f);
  34. }
  35. /**
  36. * @param resolution
  37. * 0.001 implies a 1 ms (1/1000s = 0.001s = 1ms) resolution. Therefore,
  38. * 0.0005 implies a 0.5 ms (1/2000s) resolution. And so on.
  39. */
  40. void FlexiTimer2::set(unsigned long units, double resolution, void (*f)()) {
  41. float prescaler = 0.0;
  42. if (units == 0)
  43. time_units = 1;
  44. else
  45. time_units = units;
  46. func = f;
  47. #if defined (__AVR_ATmega168__) || defined (__AVR_ATmega48__) || defined (__AVR_ATmega88__) || defined (__AVR_ATmega328P__) || defined (__AVR_ATmega1280__) || defined (__AVR_ATmega2560__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
  48. TIMSK2 &= ~(1<<TOIE2);
  49. TCCR2A &= ~((1<<WGM21) | (1<<WGM20));
  50. TCCR2B &= ~(1<<WGM22);
  51. ASSR &= ~(1<<AS2);
  52. TIMSK2 &= ~(1<<OCIE2A);
  53. if ((F_CPU >= 1000000UL) && (F_CPU <= 16000000UL)) { // prescaler set to 64
  54. TCCR2B |= (1<<CS22);
  55. TCCR2B &= ~((1<<CS21) | (1<<CS20));
  56. prescaler = 64.0;
  57. } else if (F_CPU < 1000000UL) { // prescaler set to 8
  58. TCCR2B |= (1<<CS21);
  59. TCCR2B &= ~((1<<CS22) | (1<<CS20));
  60. prescaler = 8.0;
  61. } else { // F_CPU > 16Mhz, prescaler set to 128
  62. TCCR2B |= ((1<<CS22) | (1<<CS20));
  63. TCCR2B &= ~(1<<CS21);
  64. prescaler = 128.0;
  65. }
  66. #elif defined (__AVR_ATmega8__)
  67. TIMSK &= ~(1<<TOIE2);
  68. TCCR2 &= ~((1<<WGM21) | (1<<WGM20));
  69. TIMSK &= ~(1<<OCIE2);
  70. ASSR &= ~(1<<AS2);
  71. if ((F_CPU >= 1000000UL) && (F_CPU <= 16000000UL)) { // prescaler set to 64
  72. TCCR2 |= (1<<CS22);
  73. TCCR2 &= ~((1<<CS21) | (1<<CS20));
  74. prescaler = 64.0;
  75. } else if (F_CPU < 1000000UL) { // prescaler set to 8
  76. TCCR2 |= (1<<CS21);
  77. TCCR2 &= ~((1<<CS22) | (1<<CS20));
  78. prescaler = 8.0;
  79. } else { // F_CPU > 16Mhz, prescaler set to 128
  80. TCCR2 |= ((1<<CS22) && (1<<CS20));
  81. TCCR2 &= ~(1<<CS21);
  82. prescaler = 128.0;
  83. }
  84. #elif defined (__AVR_ATmega128__)
  85. TIMSK &= ~(1<<TOIE2);
  86. TCCR2 &= ~((1<<WGM21) | (1<<WGM20));
  87. TIMSK &= ~(1<<OCIE2);
  88. if ((F_CPU >= 1000000UL) && (F_CPU <= 16000000UL)) { // prescaler set to 64
  89. TCCR2 |= ((1<<CS21) | (1<<CS20));
  90. TCCR2 &= ~(1<<CS22);
  91. prescaler = 64.0;
  92. } else if (F_CPU < 1000000UL) { // prescaler set to 8
  93. TCCR2 |= (1<<CS21);
  94. TCCR2 &= ~((1<<CS22) | (1<<CS20));
  95. prescaler = 8.0;
  96. } else { // F_CPU > 16Mhz, prescaler set to 256
  97. TCCR2 |= (1<<CS22);
  98. TCCR2 &= ~((1<<CS21) | (1<<CS20));
  99. prescaler = 256.0;
  100. }
  101. #elif defined (__AVR_ATmega32U4__)
  102. TCCR4B = 0;
  103. TCCR4A = 0;
  104. TCCR4C = 0;
  105. TCCR4D = 0;
  106. TCCR4E = 0;
  107. if (F_CPU >= 16000000L) {
  108. TCCR4B = (1<<CS43) | (1<<PSR4);
  109. prescaler = 128.0;
  110. } else if (F_CPU >= 8000000L) {
  111. TCCR4B = (1<<CS42) | (1<<CS41) | (1<<CS40) | (1<<PSR4);
  112. prescaler = 64.0;
  113. } else if (F_CPU >= 4000000L) {
  114. TCCR4B = (1<<CS42) | (1<<CS41) | (1<<PSR4);
  115. prescaler = 32.0;
  116. } else if (F_CPU >= 2000000L) {
  117. TCCR4B = (1<<CS42) | (1<<CS40) | (1<<PSR4);
  118. prescaler = 16.0;
  119. } else if (F_CPU >= 1000000L) {
  120. TCCR4B = (1<<CS42) | (1<<PSR4);
  121. prescaler = 8.0;
  122. } else if (F_CPU >= 500000L) {
  123. TCCR4B = (1<<CS41) | (1<<CS40) | (1<<PSR4);
  124. prescaler = 4.0;
  125. } else {
  126. TCCR4B = (1<<CS41) | (1<<PSR4);
  127. prescaler = 2.0;
  128. }
  129. tcnt2 = (int)((float)F_CPU * resolution / prescaler) - 1;
  130. OCR4C = tcnt2;
  131. return;
  132. #elif defined(__arm__) && defined(TEENSYDUINO)
  133. // TODO: should this emulate the limitations and numerical
  134. // range bugs from the versions above?
  135. tcnt2 = resolution * 1000000.0;
  136. return;
  137. #else
  138. #error Unsupported CPU type
  139. #endif
  140. tcnt2 = 256 - (int)((float)F_CPU * resolution / prescaler);
  141. }
  142. void FlexiTimer2::start() {
  143. count = 0;
  144. overflowing = 0;
  145. #if defined (__AVR_ATmega168__) || defined (__AVR_ATmega48__) || defined (__AVR_ATmega88__) || defined (__AVR_ATmega328P__) || defined (__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
  146. TCNT2 = tcnt2;
  147. TIMSK2 |= (1<<TOIE2);
  148. #elif defined (__AVR_ATmega128__)
  149. TCNT2 = tcnt2;
  150. TIMSK |= (1<<TOIE2);
  151. #elif defined (__AVR_ATmega8__)
  152. TCNT2 = tcnt2;
  153. TIMSK |= (1<<TOIE2);
  154. #elif defined (__AVR_ATmega32U4__)
  155. TIFR4 = (1<<TOV4);
  156. TCNT4 = 0;
  157. TIMSK4 = (1<<TOIE4);
  158. #elif defined(__arm__) && defined(TEENSYDUINO)
  159. itimer.begin(FlexiTimer2::_overflow, tcnt2);
  160. #endif
  161. }
  162. void FlexiTimer2::stop() {
  163. #if defined (__AVR_ATmega168__) || defined (__AVR_ATmega48__) || defined (__AVR_ATmega88__) || defined (__AVR_ATmega328P__) || defined (__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
  164. TIMSK2 &= ~(1<<TOIE2);
  165. #elif defined (__AVR_ATmega128__)
  166. TIMSK &= ~(1<<TOIE2);
  167. #elif defined (__AVR_ATmega8__)
  168. TIMSK &= ~(1<<TOIE2);
  169. #elif defined (__AVR_ATmega32U4__)
  170. TIMSK4 = 0;
  171. #elif defined(__arm__) && defined(TEENSYDUINO)
  172. itimer.end();
  173. #endif
  174. }
  175. void FlexiTimer2::_overflow() {
  176. count += 1;
  177. if (count >= time_units && !overflowing) {
  178. overflowing = 1;
  179. count = count - time_units; // subtract time_uints to catch missed overflows
  180. // set to 0 if you don't want this.
  181. (*func)();
  182. overflowing = 0;
  183. }
  184. }
  185. #if defined (__AVR__)
  186. #if defined (__AVR_ATmega32U4__)
  187. ISR(TIMER4_OVF_vect) {
  188. #else
  189. ISR(TIMER2_OVF_vect) {
  190. #endif
  191. #if defined (__AVR_ATmega168__) || defined (__AVR_ATmega48__) || defined (__AVR_ATmega88__) || defined (__AVR_ATmega328P__) || defined (__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
  192. TCNT2 = FlexiTimer2::tcnt2;
  193. #elif defined (__AVR_ATmega128__)
  194. TCNT2 = FlexiTimer2::tcnt2;
  195. #elif defined (__AVR_ATmega8__)
  196. TCNT2 = FlexiTimer2::tcnt2;
  197. #elif defined (__AVR_ATmega32U4__)
  198. // not necessary on 32u4's high speed timer4
  199. #endif
  200. FlexiTimer2::_overflow();
  201. }
  202. #endif // AVR