Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. // 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 uint16_t tone_frequency=0;
  42. IntervalTimer tone_timer;
  43. void tone_interrupt(void);
  44. #if defined(KINETISK)
  45. #define TONE_CLEAR_PIN tone_reg[0] = 1
  46. #define TONE_TOGGLE_PIN tone_reg[128] = 1
  47. #define TONE_OUTPUT_PIN tone_reg[384] = 1
  48. #elif defined(KINETISL)
  49. static uint8_t tone_mask;
  50. #define TONE_CLEAR_PIN tone_reg[0] = tone_mask
  51. #define TONE_TOGGLE_PIN tone_reg[4] = tone_mask
  52. #define TONE_OUTPUT_PIN __disable_irq(); tone_reg[12] |= tone_mask; __enable_irq()
  53. #endif
  54. void tone(uint8_t pin, uint16_t frequency, uint32_t duration)
  55. {
  56. uint32_t count;
  57. volatile uint32_t *config;
  58. float usec;
  59. if (pin >= CORE_NUM_DIGITAL) return;
  60. if (duration) {
  61. count = (frequency * duration / 1000) * 2;
  62. } else {
  63. count = 0xFFFFFFFF;
  64. }
  65. usec = (float)500000.0 / (float)frequency;
  66. config = portConfigRegister(pin);
  67. // TODO: IntervalTimer really needs an API to disable and enable
  68. // the interrupt on a single timer.
  69. __disable_irq();
  70. if (pin == tone_pin) {
  71. if (frequency == tone_frequency) {
  72. // same pin, same frequency, so just update the
  73. // duration. Users will call repetitively call
  74. // tone() with the same setting, expecting a
  75. // continuous output with no glitches or phase
  76. // changes or jitter at each call.
  77. tone_toggle_count = count;
  78. } else {
  79. // same pin, but a new frequency.
  80. TONE_CLEAR_PIN; // clear pin
  81. tone_timer.begin(tone_interrupt, usec);
  82. }
  83. } else {
  84. if (tone_pin < CORE_NUM_DIGITAL) {
  85. TONE_CLEAR_PIN; // clear pin
  86. }
  87. tone_pin = pin;
  88. tone_reg = portClearRegister(pin);
  89. #if defined(KINETISL)
  90. tone_mask = digitalPinToBitMask(pin);
  91. #endif
  92. TONE_CLEAR_PIN; // clear pin
  93. TONE_OUTPUT_PIN; // output mode;
  94. *config = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  95. tone_toggle_count = count;
  96. tone_timer.begin(tone_interrupt, usec);
  97. }
  98. __enable_irq();
  99. }
  100. void tone_interrupt(void)
  101. {
  102. if (tone_toggle_count) {
  103. TONE_TOGGLE_PIN; // toggle
  104. if (tone_toggle_count < 0xFFFFFFFF) tone_toggle_count--;
  105. } else {
  106. tone_timer.end();
  107. TONE_CLEAR_PIN; // clear
  108. tone_pin = 255;
  109. tone_frequency = 0;
  110. }
  111. }
  112. void noTone(uint8_t pin)
  113. {
  114. if (pin >= CORE_NUM_DIGITAL) return;
  115. __disable_irq();
  116. if (pin == tone_pin) {
  117. tone_timer.end();
  118. TONE_CLEAR_PIN; // clear
  119. tone_pin = 255;
  120. tone_frequency = 0;
  121. }
  122. __enable_irq();
  123. }