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.

117 satır
3.7KB

  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2018 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. #include "debug/printf.h"
  32. static void pit_isr(void);
  33. #define NUM_CHANNELS 4
  34. static void (*funct_table[4])(void) __attribute((aligned(32))) = {nullptr, nullptr, nullptr, nullptr};
  35. uint8_t IntervalTimer::nvic_priorites[4] = {255, 255, 255, 255};
  36. bool IntervalTimer::beginCycles(void (*funct)(), uint32_t cycles)
  37. {
  38. printf("beginCycles %u\n", cycles);
  39. if (channel) {
  40. channel->TCTRL = 0;
  41. channel->TFLG = 1;
  42. } else {
  43. CCM_CCGR1 |= CCM_CCGR1_PIT(CCM_CCGR_ON);
  44. //__asm__ volatile("nop"); // solves timing problem on Teensy 3.5
  45. PIT_MCR = 1;
  46. channel = IMXRT_PIT_CHANNELS;
  47. while (1) {
  48. if (channel->TCTRL == 0) break;
  49. if (++channel >= IMXRT_PIT_CHANNELS + NUM_CHANNELS) {
  50. channel = NULL;
  51. return false;
  52. }
  53. }
  54. }
  55. int index = channel - IMXRT_PIT_CHANNELS;
  56. funct_table[index] = funct;
  57. channel->LDVAL = cycles;
  58. channel->TCTRL = 3;
  59. nvic_priorites[index] = nvic_priority;
  60. uint8_t top_priority = 255;
  61. for (int i=0; i < NUM_CHANNELS; i++) {
  62. if (top_priority > nvic_priorites[i]) top_priority = nvic_priorites[i];
  63. }
  64. attachInterruptVector(IRQ_PIT, &pit_isr);
  65. NVIC_SET_PRIORITY(IRQ_PIT, top_priority);
  66. NVIC_ENABLE_IRQ(IRQ_PIT);
  67. return true;
  68. }
  69. void IntervalTimer::end() {
  70. #if 1
  71. if (channel) {
  72. int index = channel - IMXRT_PIT_CHANNELS;
  73. // TODO: disable IRQ_PIT, but only if all instances ended
  74. funct_table[index] = nullptr;
  75. channel->TCTRL = 0;
  76. nvic_priorites[index] = 255;
  77. uint8_t top_priority = 255;
  78. for (int i=0; i < NUM_CHANNELS; i++) {
  79. if (top_priority > nvic_priorites[i]) top_priority = nvic_priorites[i];
  80. }
  81. NVIC_SET_PRIORITY(IRQ_PIT, top_priority);
  82. channel = 0;
  83. }
  84. #endif
  85. }
  86. //FASTRUN
  87. static void pit_isr()
  88. {
  89. #if 0
  90. for (int i=0; i < NUM_CHANNELS; i++) {
  91. IMXRT_PIT_CHANNEL_t *channel = IMXRT_PIT_CHANNELS + i;
  92. if (funct_table[0] && channel->TFLG) {
  93. channel->TFLG = 1;
  94. funct_table[i]();
  95. }
  96. }
  97. #else
  98. IMXRT_PIT_CHANNEL_t *channel= IMXRT_PIT_CHANNELS;
  99. if (funct_table[0] != nullptr && channel->TFLG) {channel->TFLG = 1;funct_table[0]();}
  100. channel++;
  101. if (funct_table[1] != nullptr && channel->TFLG) {channel->TFLG = 1;funct_table[1]();}
  102. channel++;
  103. if (funct_table[2] != nullptr && channel->TFLG) {channel->TFLG = 1;funct_table[2]();}
  104. channel++;
  105. if (funct_table[3] != nullptr && channel->TFLG) {channel->TFLG = 1;funct_table[3]();}
  106. #endif
  107. }