Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

146 Zeilen
3.7KB

  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. #if defined(KINETISK)
  80. NVIC_DISABLE_IRQ(IRQ_PIT_CH0 + index);
  81. #elif defined(KINETISL)
  82. // TODO: disable IRQ_PIT, but only if both instances ended
  83. #endif
  84. funct_table[index] = dummy_funct;
  85. channel->TCTRL = 0;
  86. #if defined(KINETISL)
  87. nvic_priorites[index] = 255;
  88. if (nvic_priorites[0] <= nvic_priorites[1]) {
  89. NVIC_SET_PRIORITY(IRQ_PIT, nvic_priorites[0]);
  90. } else {
  91. NVIC_SET_PRIORITY(IRQ_PIT, nvic_priorites[1]);
  92. }
  93. #endif
  94. channel = 0;
  95. }
  96. }
  97. #if defined(KINETISK)
  98. void pit0_isr()
  99. {
  100. PIT_TFLG0 = 1;
  101. funct_table[0]();
  102. }
  103. void pit1_isr() {
  104. PIT_TFLG1 = 1;
  105. funct_table[1]();
  106. }
  107. void pit2_isr() {
  108. PIT_TFLG2 = 1;
  109. funct_table[2]();
  110. }
  111. void pit3_isr() {
  112. PIT_TFLG3 = 1;
  113. funct_table[3]();
  114. }
  115. #elif defined(KINETISL)
  116. void pit_isr() {
  117. if (PIT_TFLG0) {
  118. PIT_TFLG0 = 1;
  119. funct_table[0]();
  120. }
  121. if (PIT_TFLG1) {
  122. PIT_TFLG1 = 1;
  123. funct_table[1]();
  124. }
  125. }
  126. #endif
  127. static void dummy_funct(void)
  128. {
  129. }