選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Tone.cpp 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 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. #if 1
  35. // IntervalTimer based tone. This allows tone() to share the timers with other
  36. // libraries, rather than permanently hogging one PIT timer even for projects
  37. // which never use tone(). Someday this single-tone implementation might be
  38. // changed to allow multiple simultaneous tones.
  39. static uint32_t tone_toggle_count;
  40. static volatile uint8_t *tone_reg;
  41. static uint8_t tone_pin=255;
  42. static uint16_t tone_frequency=0;
  43. IntervalTimer tone_timer;
  44. void tone_interrupt(void);
  45. void tone(uint8_t pin, uint16_t frequency, uint32_t duration)
  46. {
  47. uint32_t count;
  48. volatile uint32_t *config;
  49. float usec;
  50. if (pin >= CORE_NUM_DIGITAL) return;
  51. if (duration) {
  52. count = (frequency * duration / 1000) * 2;
  53. } else {
  54. count = 0xFFFFFFFF;
  55. }
  56. usec = (float)500000.0 / (float)frequency;
  57. config = portConfigRegister(pin);
  58. // TODO: IntervalTimer really needs an API to disable and enable
  59. // the interrupt on a single timer.
  60. __disable_irq();
  61. if (pin == tone_pin) {
  62. if (frequency == tone_frequency) {
  63. // same pin, same frequency, so just update the
  64. // duration. Users will call repetitively call
  65. // tone() with the same setting, expecting a
  66. // continuous output with no glitches or phase
  67. // changes or jitter at each call.
  68. tone_toggle_count = count;
  69. } else {
  70. // same pin, but a new frequency.
  71. tone_reg[0] = 1; // clear pin
  72. tone_timer.begin(tone_interrupt, usec);
  73. }
  74. } else {
  75. if (tone_pin < CORE_NUM_DIGITAL) {
  76. tone_reg[0] = 1; // clear pin
  77. }
  78. tone_pin = pin;
  79. tone_reg = portClearRegister(pin);
  80. tone_reg[0] = 1; // clear pin
  81. tone_reg[384] = 1; // output mode;
  82. *config = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  83. tone_toggle_count = count;
  84. tone_timer.begin(tone_interrupt, usec);
  85. }
  86. __enable_irq();
  87. }
  88. void tone_interrupt(void)
  89. {
  90. if (tone_toggle_count) {
  91. tone_reg[128] = 1; // toggle
  92. if (tone_toggle_count < 0xFFFFFFFF) tone_toggle_count--;
  93. } else {
  94. tone_timer.end();
  95. tone_reg[0] = 0; // clear
  96. tone_pin = 255;
  97. tone_frequency = 0;
  98. }
  99. }
  100. void noTone(uint8_t pin)
  101. {
  102. if (pin >= CORE_NUM_DIGITAL) return;
  103. __disable_irq();
  104. if (pin == tone_pin) {
  105. tone_timer.end();
  106. tone_reg[0] = 0; // clear
  107. tone_pin = 255;
  108. tone_frequency = 0;
  109. }
  110. __enable_irq();
  111. }
  112. #endif
  113. #if 0
  114. // Old PIT timer based tone(). This implementation is slightly more efficient,
  115. // but it consumes one of the PIT timers, even for projects which never use tone().
  116. static uint32_t tone_toggle_count;
  117. static volatile uint8_t *tone_reg;
  118. static uint8_t tone_pin;
  119. void init_tone(void)
  120. {
  121. if (SIM_SCGC6 & SIM_SCGC6_PIT) return;
  122. SIM_SCGC6 |= SIM_SCGC6_PIT; // TODO: use bitband for atomic read-mod-write
  123. PIT_MCR = 0;
  124. PIT_TCTRL3 = 0; // disabled
  125. tone_pin = 255;
  126. NVIC_ENABLE_IRQ(IRQ_PIT_CH3);
  127. }
  128. void tone(uint8_t pin, uint16_t frequency, uint32_t duration)
  129. {
  130. uint32_t count, load;
  131. volatile uint32_t *config;
  132. init_tone();
  133. if (pin >= CORE_NUM_DIGITAL) return;
  134. if (duration) {
  135. count = (frequency * duration / 1000) * 2;
  136. } else {
  137. count = 0xFFFFFFFF;
  138. }
  139. load = (F_BUS / 2) / frequency;
  140. config = portConfigRegister(pin);
  141. __disable_irq();
  142. if (pin != tone_pin) {
  143. if (tone_pin < CORE_NUM_DIGITAL) {
  144. tone_reg[0] = 1; // clear pin
  145. }
  146. tone_pin = pin;
  147. tone_reg = portClearRegister(pin);
  148. tone_reg[0] = 1; // clear pin
  149. tone_reg[384] = 1; // output mode;
  150. *config = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  151. }
  152. tone_toggle_count = count;
  153. if (PIT_LDVAL3 != load) {
  154. PIT_TCTRL3 = 0;
  155. PIT_LDVAL3 = load;
  156. PIT_TCTRL3 = 3;
  157. }
  158. __enable_irq();
  159. }
  160. void pit3_isr(void)
  161. {
  162. PIT_TFLG3 = 1;
  163. if (tone_toggle_count) {
  164. tone_reg[128] = 1; // toggle
  165. if (tone_toggle_count < 0xFFFFFFFF) tone_toggle_count--;
  166. } else {
  167. PIT_TCTRL3 = 0;
  168. PIT_LDVAL3 = 0;
  169. tone_reg[0] = 0; // clear
  170. tone_pin = 255;
  171. }
  172. }
  173. void noTone(uint8_t pin)
  174. {
  175. if (pin >= CORE_NUM_DIGITAL) return;
  176. __disable_irq();
  177. if (pin == tone_pin) {
  178. PIT_TCTRL3 = 0;
  179. PIT_LDVAL3 = 0;
  180. tone_reg[0] = 0; // clear
  181. tone_pin = 255;
  182. }
  183. __enable_irq();
  184. }
  185. #endif