PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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