Teensy 4.1 core updated for C++20
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

IntervalTimer.cpp 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2017 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #include "IntervalTimer.h"
  31. static void dummy_funct(void);
  32. #if defined(KINETISK)
  33. #define NUM_CHANNELS 4
  34. static void (*funct_table[4])(void) = {dummy_funct, dummy_funct, dummy_funct, dummy_funct};
  35. #elif defined(KINETISL)
  36. #define NUM_CHANNELS 2
  37. static void (*funct_table[2])(void) = {dummy_funct, dummy_funct};
  38. uint8_t IntervalTimer::nvic_priorites[2] = {255, 255};
  39. #endif
  40. bool IntervalTimer::beginCycles(void (*funct)(), uint32_t cycles)
  41. {
  42. if (channel) {
  43. channel->TCTRL = 0;
  44. channel->TFLG = 1;
  45. } else {
  46. SIM_SCGC6 |= SIM_SCGC6_PIT;
  47. __asm__ volatile("nop"); // solves timing problem on Teensy 3.5
  48. PIT_MCR = 1;
  49. channel = KINETISK_PIT_CHANNELS;
  50. while (1) {
  51. if (channel->TCTRL == 0) break;
  52. if (++channel >= KINETISK_PIT_CHANNELS + NUM_CHANNELS) {
  53. channel = NULL;
  54. return false;
  55. }
  56. }
  57. }
  58. int index = channel - KINETISK_PIT_CHANNELS;
  59. funct_table[index] = funct;
  60. channel->LDVAL = cycles;
  61. channel->TCTRL = 3;
  62. #if defined(KINETISK)
  63. NVIC_SET_PRIORITY(IRQ_PIT_CH0 + index, nvic_priority);
  64. NVIC_ENABLE_IRQ(IRQ_PIT_CH0 + index);
  65. #elif defined(KINETISL)
  66. nvic_priorites[index] = nvic_priority;
  67. if (nvic_priorites[0] <= nvic_priorites[1]) {
  68. NVIC_SET_PRIORITY(IRQ_PIT, nvic_priorites[0]);
  69. } else {
  70. NVIC_SET_PRIORITY(IRQ_PIT, nvic_priorites[1]);
  71. }
  72. NVIC_ENABLE_IRQ(IRQ_PIT);
  73. #endif
  74. return true;
  75. }
  76. void IntervalTimer::end() {
  77. if (channel) {
  78. int index = channel - KINETISK_PIT_CHANNELS;
  79. NVIC_DISABLE_IRQ(IRQ_PIT_CH0 + index);
  80. funct_table[index] = dummy_funct;
  81. channel->TCTRL = 0;
  82. #if defined(KINETISL)
  83. nvic_priorites[index] = 255;
  84. if (nvic_priorites[0] <= nvic_priorites[1]) {
  85. NVIC_SET_PRIORITY(IRQ_PIT, nvic_priorites[0]);
  86. } else {
  87. NVIC_SET_PRIORITY(IRQ_PIT, nvic_priorites[1]);
  88. }
  89. #endif
  90. channel = 0;
  91. }
  92. }
  93. #if defined(KINETISK)
  94. void pit0_isr()
  95. {
  96. PIT_TFLG0 = 1;
  97. funct_table[0]();
  98. }
  99. void pit1_isr() {
  100. PIT_TFLG1 = 1;
  101. funct_table[1]();
  102. }
  103. void pit2_isr() {
  104. PIT_TFLG2 = 1;
  105. funct_table[2]();
  106. }
  107. void pit3_isr() {
  108. PIT_TFLG3 = 1;
  109. funct_table[3]();
  110. }
  111. #elif defined(KINETISL)
  112. void pit_isr() {
  113. if (PIT_TFLG0) {
  114. PIT_TFLG0 = 1;
  115. funct_table[0]();
  116. }
  117. if (PIT_TFLG1) {
  118. PIT_TFLG1 = 1;
  119. funct_table[1]();
  120. }
  121. }
  122. #endif
  123. static void dummy_funct(void)
  124. {
  125. }