Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

140 linhas
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. PIT_MCR = 1;
  48. channel = KINETISK_PIT_CHANNELS;
  49. while (1) {
  50. if (channel->TCTRL == 0) break;
  51. if (++channel >= KINETISK_PIT_CHANNELS + NUM_CHANNELS) {
  52. channel = NULL;
  53. return false;
  54. }
  55. }
  56. }
  57. int index = channel - KINETISK_PIT_CHANNELS;
  58. funct_table[index] = funct;
  59. channel->LDVAL = cycles;
  60. channel->TCTRL = 3;
  61. #if defined(KINETISK)
  62. NVIC_SET_PRIORITY(IRQ_PIT_CH0 + index, nvic_priority);
  63. NVIC_ENABLE_IRQ(IRQ_PIT_CH0 + index);
  64. #elif defined(KINETISL)
  65. nvic_priorites[index] = nvic_priority;
  66. if (nvic_priorites[0] <= nvic_priorites[1]) {
  67. NVIC_SET_PRIORITY(IRQ_PIT, nvic_priorites[0]);
  68. } else {
  69. NVIC_SET_PRIORITY(IRQ_PIT, nvic_priorites[1]);
  70. }
  71. NVIC_ENABLE_IRQ(IRQ_PIT);
  72. #endif
  73. return true;
  74. }
  75. void IntervalTimer::end() {
  76. if (channel) {
  77. int index = channel - KINETISK_PIT_CHANNELS;
  78. funct_table[index] = dummy_funct;
  79. channel->TCTRL = 0;
  80. #if defined(KINETISL)
  81. nvic_priorites[index] = 255;
  82. if (nvic_priorites[0] <= nvic_priorites[1]) {
  83. NVIC_SET_PRIORITY(IRQ_PIT, nvic_priorites[0]);
  84. } else {
  85. NVIC_SET_PRIORITY(IRQ_PIT, nvic_priorites[1]);
  86. }
  87. #endif
  88. channel = 0;
  89. }
  90. }
  91. #if defined(KINETISK)
  92. void pit0_isr()
  93. {
  94. PIT_TFLG0 = 1;
  95. funct_table[0]();
  96. }
  97. void pit1_isr() {
  98. PIT_TFLG1 = 1;
  99. funct_table[1]();
  100. }
  101. void pit2_isr() {
  102. PIT_TFLG2 = 1;
  103. funct_table[2]();
  104. }
  105. void pit3_isr() {
  106. PIT_TFLG3 = 1;
  107. funct_table[3]();
  108. }
  109. #elif defined(KINETISL)
  110. void pit_isr() {
  111. if (PIT_TFLG0) {
  112. PIT_TFLG0 = 1;
  113. funct_table[0]();
  114. }
  115. if (PIT_TFLG1) {
  116. PIT_TFLG1 = 1;
  117. funct_table[1]();
  118. }
  119. }
  120. #endif
  121. static void dummy_funct(void)
  122. {
  123. }