Teensy 4.1 core updated for C++20
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.

114 lines
3.2KB

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