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.

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