Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

155 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. usec = (float)500000.0 / (float)frequency;
  68. config = portConfigRegister(pin);
  69. // TODO: IntervalTimer really needs an API to disable and enable
  70. // the interrupt on a single timer.
  71. __disable_irq();
  72. if (pin == tone_pin) {
  73. // changing a pin which is already playing a tone
  74. if (usec == tone_usec) {
  75. // same frequency, so just change the duration
  76. tone_toggle_count = (tone_toggle_count & 1) + count - 1;
  77. } else {
  78. // different frequency, reduce duration to only the
  79. // remainder of its current cycle, and configure for
  80. // the transition to the new frequency when the
  81. // current cycle completes
  82. tone_usec = usec;
  83. tone_new_count = count;
  84. tone_toggle_count = (tone_toggle_count & 1);
  85. }
  86. } else {
  87. // if playing on a different pin, immediately stop, even mid-cycle :-(
  88. if (tone_pin < CORE_NUM_DIGITAL) {
  89. TONE_CLEAR_PIN; // clear pin
  90. }
  91. // configure the new tone to play
  92. tone_pin = pin;
  93. tone_reg = portClearRegister(pin);
  94. #if defined(KINETISL)
  95. tone_mask = digitalPinToBitMask(pin);
  96. #endif
  97. TONE_CLEAR_PIN; // clear pin
  98. TONE_OUTPUT_PIN; // output mode;
  99. *config = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  100. tone_toggle_count = count;
  101. tone_usec = usec;
  102. tone_timer.begin(tone_interrupt, usec);
  103. }
  104. __enable_irq();
  105. }
  106. void tone_interrupt(void)
  107. {
  108. if (tone_toggle_count) { // odd = rising edge, even = falling edge
  109. // not the end
  110. TONE_TOGGLE_PIN; // toggle
  111. tone_toggle_count--;
  112. if (tone_toggle_count == 0xFFFFFFFB) tone_toggle_count = 0xFFFFFFFD;
  113. } else {
  114. // this transition completes the tone
  115. TONE_CLEAR_PIN; // clear
  116. if (tone_new_count > 0) {
  117. // begin playing a new tone
  118. tone_timer.begin(tone_interrupt, tone_usec);
  119. tone_toggle_count = tone_new_count;
  120. tone_new_count = 0;
  121. } else {
  122. // finished playing
  123. tone_timer.end();
  124. tone_pin = 255;
  125. }
  126. }
  127. }
  128. void noTone(uint8_t pin)
  129. {
  130. if (pin >= CORE_NUM_DIGITAL) return;
  131. __disable_irq();
  132. if (pin == tone_pin) {
  133. tone_timer.end();
  134. TONE_CLEAR_PIN; // clear
  135. tone_pin = 255;
  136. }
  137. __enable_irq();
  138. }