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

111 lines
3.3KB

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