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.

136 lines
4.2KB

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