Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

134 lines
4.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. #ifndef __INTERVALTIMER_H__
  31. #define __INTERVALTIMER_H__
  32. #include <stddef.h>
  33. #include "imxrt.h"
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. class IntervalTimer {
  38. private:
  39. static const uint32_t MAX_PERIOD = UINT32_MAX / (24000000 / 1000000);
  40. public:
  41. constexpr IntervalTimer() {
  42. }
  43. ~IntervalTimer() {
  44. end();
  45. }
  46. bool begin(void (*funct)(), unsigned int microseconds) {
  47. if (microseconds == 0 || microseconds > MAX_PERIOD) return false;
  48. uint32_t cycles = (24000000 / 1000000) * microseconds - 1;
  49. if (cycles < 17) return false;
  50. return beginCycles(funct, cycles);
  51. }
  52. bool begin(void (*funct)(), int microseconds) {
  53. if (microseconds < 0) return false;
  54. return begin(funct, (unsigned int)microseconds);
  55. }
  56. bool begin(void (*funct)(), unsigned long microseconds) {
  57. return begin(funct, (unsigned int)microseconds);
  58. }
  59. bool begin(void (*funct)(), long microseconds) {
  60. return begin(funct, (int)microseconds);
  61. }
  62. bool begin(void (*funct)(), float microseconds) {
  63. if (microseconds <= 0 || microseconds > MAX_PERIOD) return false;
  64. uint32_t cycles = (float)(24000000 / 1000000) * microseconds - 0.5;
  65. if (cycles < 17) return false;
  66. return beginCycles(funct, cycles);
  67. }
  68. bool begin(void (*funct)(), double microseconds) {
  69. return begin(funct, (float)microseconds);
  70. }
  71. void update(unsigned int microseconds) {
  72. if (microseconds == 0 || microseconds > MAX_PERIOD) return;
  73. uint32_t cycles = (24000000 / 1000000) * microseconds - 1;
  74. if (cycles < 17) return;
  75. if (channel) channel->LDVAL = cycles;
  76. }
  77. void update(int microseconds) {
  78. if (microseconds < 0) return;
  79. return update((unsigned int)microseconds);
  80. }
  81. void update(unsigned long microseconds) {
  82. return update((unsigned int)microseconds);
  83. }
  84. void update(long microseconds) {
  85. return update((int)microseconds);
  86. }
  87. void update(float microseconds) {
  88. if (microseconds <= 0 || microseconds > MAX_PERIOD) return;
  89. uint32_t cycles = (float)(24000000 / 1000000) * microseconds - 0.5;
  90. if (cycles < 17) return;
  91. if (channel) channel->LDVAL = cycles;
  92. }
  93. void update(double microseconds) {
  94. return update((float)microseconds);
  95. }
  96. void end();
  97. void priority(uint8_t n) {
  98. nvic_priority = n;
  99. if (channel) {
  100. int index = channel - IMXRT_PIT_CHANNELS;
  101. nvic_priorites[index] = nvic_priority;
  102. uint8_t top_priority = nvic_priorites[0];
  103. for (uint8_t i=1; i < (sizeof(nvic_priorites)/sizeof(nvic_priorites[0])); i++) {
  104. if (top_priority > nvic_priorites[i]) top_priority = nvic_priorites[i];
  105. }
  106. NVIC_SET_PRIORITY(IRQ_PIT, top_priority);
  107. }
  108. }
  109. operator IRQ_NUMBER_t() {
  110. if (channel) {
  111. return IRQ_PIT;
  112. }
  113. return (IRQ_NUMBER_t)NVIC_NUM_INTERRUPTS;
  114. }
  115. private:
  116. //#define IMXRT_PIT_CHANNELS ((IMXRT_PIT_CHANNEL_t *)(&(IMXRT_PIT.offset100)))
  117. IMXRT_PIT_CHANNEL_t *channel = nullptr;
  118. uint8_t nvic_priority = 128;
  119. static uint8_t nvic_priorites[4];
  120. bool beginCycles(void (*funct)(), uint32_t cycles);
  121. };
  122. #ifdef __cplusplus
  123. }
  124. #endif
  125. #endif