Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

141 lines
3.5KB

  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2016 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. funct_table[index] = dummy_funct;
  80. channel->TCTRL = 0;
  81. #if defined(KINETISL)
  82. nvic_priorites[index] = 255;
  83. if (nvic_priorites[0] <= nvic_priorites[1]) {
  84. NVIC_SET_PRIORITY(IRQ_PIT, nvic_priorites[0]);
  85. } else {
  86. NVIC_SET_PRIORITY(IRQ_PIT, nvic_priorites[1]);
  87. }
  88. #endif
  89. channel = 0;
  90. }
  91. }
  92. #if defined(KINETISK)
  93. void pit0_isr()
  94. {
  95. PIT_TFLG0 = 1;
  96. funct_table[0]();
  97. }
  98. void pit1_isr() {
  99. PIT_TFLG1 = 1;
  100. funct_table[1]();
  101. }
  102. void pit2_isr() {
  103. PIT_TFLG2 = 1;
  104. funct_table[2]();
  105. }
  106. void pit3_isr() {
  107. PIT_TFLG3 = 1;
  108. funct_table[3]();
  109. }
  110. #elif defined(KINETISL)
  111. void pit_isr() {
  112. if (PIT_TFLG0) {
  113. PIT_TFLG0 = 1;
  114. funct_table[0]();
  115. }
  116. if (PIT_TFLG1) {
  117. PIT_TFLG1 = 1;
  118. funct_table[1]();
  119. }
  120. }
  121. #endif
  122. static void dummy_funct(void)
  123. {
  124. }