Teensy 4.1 core updated for C++20
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

153 lines
4.7KB

  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 <Arduino.h>
  31. // IntervalTimer based tone. This allows tone() to share the timers with other
  32. // libraries, rather than permanently hogging one PIT timer even for projects
  33. // which never use tone(). Someday this single-tone implementation might be
  34. // changed to allow multiple simultaneous tones.
  35. static uint32_t tone_toggle_count;
  36. static volatile uint8_t *tone_reg;
  37. static uint8_t tone_pin=255;
  38. static float tone_usec=0.0;
  39. static uint32_t tone_new_count=0;
  40. IntervalTimer tone_timer;
  41. void tone_interrupt(void);
  42. #if defined(KINETISK)
  43. #define TONE_CLEAR_PIN tone_reg[0] = 1
  44. #define TONE_TOGGLE_PIN tone_reg[128] = 1
  45. #define TONE_OUTPUT_PIN tone_reg[384] = 1
  46. #elif defined(KINETISL)
  47. static uint8_t tone_mask;
  48. #define TONE_CLEAR_PIN tone_reg[0] = tone_mask
  49. #define TONE_TOGGLE_PIN tone_reg[4] = tone_mask
  50. #define TONE_OUTPUT_PIN __disable_irq(); tone_reg[12] |= tone_mask; __enable_irq()
  51. #endif
  52. void tone(uint8_t pin, uint16_t frequency, uint32_t duration)
  53. {
  54. uint32_t count;
  55. volatile uint32_t *config;
  56. float usec;
  57. if (pin >= CORE_NUM_DIGITAL) return;
  58. if (duration) {
  59. count = (frequency * duration / 1000) * 2;
  60. if (!(count & 1)) count++; // always full waveform cycles
  61. } else {
  62. count = 0xFFFFFFFD;
  63. }
  64. if (frequency < 1) frequency = 1; // minimum is 1 Hz
  65. usec = (float)500000.0 / (float)frequency;
  66. config = portConfigRegister(pin);
  67. // TODO: IntervalTimer really needs an API to disable and enable
  68. // the interrupt on a single timer.
  69. __disable_irq();
  70. if (pin == tone_pin) {
  71. // changing a pin which is already playing a tone
  72. if (usec == tone_usec) {
  73. // same frequency, so just change the duration
  74. tone_toggle_count = (tone_toggle_count & 1) + count - 1;
  75. } else {
  76. // different frequency, reduce duration to only the
  77. // remainder of its current cycle, and configure for
  78. // the transition to the new frequency when the
  79. // current cycle completes
  80. tone_usec = usec;
  81. tone_new_count = count;
  82. tone_toggle_count = (tone_toggle_count & 1);
  83. }
  84. } else {
  85. // if playing on a different pin, immediately stop, even mid-cycle :-(
  86. if (tone_pin < CORE_NUM_DIGITAL) {
  87. TONE_CLEAR_PIN; // clear pin
  88. }
  89. // configure the new tone to play
  90. tone_pin = pin;
  91. tone_reg = portClearRegister(pin);
  92. #if defined(KINETISL)
  93. tone_mask = digitalPinToBitMask(pin);
  94. #endif
  95. TONE_CLEAR_PIN; // clear pin
  96. TONE_OUTPUT_PIN; // output mode;
  97. *config = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  98. tone_toggle_count = count;
  99. tone_usec = usec;
  100. tone_timer.begin(tone_interrupt, usec);
  101. }
  102. __enable_irq();
  103. }
  104. void tone_interrupt(void)
  105. {
  106. if (tone_toggle_count) { // odd = rising edge, even = falling edge
  107. // not the end
  108. TONE_TOGGLE_PIN; // toggle
  109. tone_toggle_count--;
  110. if (tone_toggle_count == 0xFFFFFFFB) tone_toggle_count = 0xFFFFFFFD;
  111. } else {
  112. // this transition completes the tone
  113. TONE_CLEAR_PIN; // clear
  114. if (tone_new_count > 0) {
  115. // begin playing a new tone
  116. tone_timer.begin(tone_interrupt, tone_usec);
  117. tone_toggle_count = tone_new_count;
  118. tone_new_count = 0;
  119. } else {
  120. // finished playing
  121. tone_timer.end();
  122. tone_pin = 255;
  123. }
  124. }
  125. }
  126. void noTone(uint8_t pin)
  127. {
  128. if (pin >= CORE_NUM_DIGITAL) return;
  129. __disable_irq();
  130. if (pin == tone_pin) {
  131. tone_timer.end();
  132. TONE_CLEAR_PIN; // clear
  133. tone_pin = 255;
  134. }
  135. __enable_irq();
  136. }