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.

145 lines
4.5KB

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