Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

output_pwm.cpp 6.0KB

10 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
  3. *
  4. * Development of this audio library was funded by PJRC.COM, LLC by sales of
  5. * Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
  6. * open source software by purchasing Teensy or other PJRC products.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice, development funding notice, and this permission
  16. * notice shall be included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <Arduino.h>
  27. #include "output_pwm.h"
  28. audio_block_t * AudioOutputPWM::block_1st = NULL;
  29. audio_block_t * AudioOutputPWM::block_2nd = NULL;
  30. uint32_t AudioOutputPWM::block_offset = 0;
  31. bool AudioOutputPWM::update_responsibility = false;
  32. uint8_t AudioOutputPWM::interrupt_count = 0;
  33. DMAMEM uint32_t pwm_dma_buffer[AUDIO_BLOCK_SAMPLES*2];
  34. DMAChannel AudioOutputPWM::dma(false);
  35. // TODO: this code assumes F_BUS is 48 MHz.
  36. // supporting other speeds is not easy, but should be done someday
  37. #if defined(KINETISK)
  38. void AudioOutputPWM::begin(void)
  39. {
  40. dma.begin(true); // Allocate the DMA channel first
  41. //Serial.println("AudioPwmOutput constructor");
  42. block_1st = NULL;
  43. FTM1_SC = 0;
  44. FTM1_CNT = 0;
  45. FTM1_MOD = 543;
  46. FTM1_C0SC = 0x69; // send DMA request on match
  47. FTM1_C1SC = 0x28;
  48. FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0);
  49. CORE_PIN3_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  50. CORE_PIN4_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  51. FTM1_C0V = 120; // range 120 to 375
  52. FTM1_C1V = 0; // range 0 to 255
  53. for (int i=0; i<(AUDIO_BLOCK_SAMPLES*2); i+=2) {
  54. pwm_dma_buffer[i] = 120; // zero must not be used
  55. pwm_dma_buffer[i+1] = 0;
  56. }
  57. dma.TCD->SADDR = pwm_dma_buffer;
  58. dma.TCD->SOFF = 4;
  59. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(2)
  60. | DMA_TCD_ATTR_DSIZE(2) | DMA_TCD_ATTR_DMOD(4);
  61. dma.TCD->NBYTES_MLNO = 8;
  62. dma.TCD->SLAST = -sizeof(pwm_dma_buffer);
  63. dma.TCD->DADDR = &FTM1_C0V;
  64. dma.TCD->DOFF = 8;
  65. dma.TCD->CITER_ELINKNO = sizeof(pwm_dma_buffer) / 8;
  66. dma.TCD->DLASTSGA = 0;
  67. dma.TCD->BITER_ELINKNO = sizeof(pwm_dma_buffer) / 8;
  68. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  69. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_FTM1_CH0);
  70. dma.enable();
  71. update_responsibility = update_setup();
  72. dma.attachInterrupt(isr);
  73. }
  74. void AudioOutputPWM::update(void)
  75. {
  76. audio_block_t *block;
  77. block = receiveReadOnly();
  78. if (!block) return;
  79. __disable_irq();
  80. if (block_1st == NULL) {
  81. block_1st = block;
  82. block_offset = 0;
  83. __enable_irq();
  84. } else if (block_2nd == NULL) {
  85. block_2nd = block;
  86. __enable_irq();
  87. } else {
  88. audio_block_t *tmp = block_1st;
  89. block_1st = block_2nd;
  90. block_2nd = block;
  91. block_offset = 0;
  92. __enable_irq();
  93. release(tmp);
  94. }
  95. }
  96. void AudioOutputPWM::isr(void)
  97. {
  98. int16_t *src;
  99. uint32_t *dest;
  100. audio_block_t *block;
  101. uint32_t saddr, offset;
  102. saddr = (uint32_t)(dma.TCD->SADDR);
  103. dma.clearInterrupt();
  104. if (saddr < (uint32_t)pwm_dma_buffer + sizeof(pwm_dma_buffer) / 2) {
  105. // DMA is transmitting the first half of the buffer
  106. // so we must fill the second half
  107. dest = &pwm_dma_buffer[AUDIO_BLOCK_SAMPLES];
  108. } else {
  109. // DMA is transmitting the second half of the buffer
  110. // so we must fill the first half
  111. dest = pwm_dma_buffer;
  112. }
  113. block = AudioOutputPWM::block_1st;
  114. offset = AudioOutputPWM::block_offset;
  115. if (block) {
  116. src = &block->data[offset];
  117. for (int i=0; i < AUDIO_BLOCK_SAMPLES/4; i++) {
  118. uint16_t sample = *src++ + 0x8000;
  119. uint32_t msb = ((sample >> 8) & 255) + 120;
  120. uint32_t lsb = sample & 255;
  121. *dest++ = msb;
  122. *dest++ = lsb;
  123. *dest++ = msb;
  124. *dest++ = lsb;
  125. }
  126. offset += AUDIO_BLOCK_SAMPLES/4;
  127. if (offset < AUDIO_BLOCK_SAMPLES) {
  128. AudioOutputPWM::block_offset = offset;
  129. } else {
  130. AudioOutputPWM::block_offset = 0;
  131. AudioStream::release(block);
  132. AudioOutputPWM::block_1st = AudioOutputPWM::block_2nd;
  133. AudioOutputPWM::block_2nd = NULL;
  134. }
  135. } else {
  136. // fill with silence when no data available
  137. for (int i=0; i < AUDIO_BLOCK_SAMPLES/4; i++) {
  138. *dest++ = 248;
  139. *dest++ = 0;
  140. *dest++ = 248;
  141. *dest++ = 0;
  142. }
  143. }
  144. if (AudioOutputPWM::update_responsibility) {
  145. if (++AudioOutputPWM::interrupt_count >= 4) {
  146. AudioOutputPWM::interrupt_count = 0;
  147. AudioStream::update_all();
  148. }
  149. }
  150. }
  151. // DMA target is: (registers require 32 bit writes)
  152. // 40039010 Channel 0 Value (FTM1_C0V)
  153. // 40039018 Channel 1 Value (FTM1_C1V)
  154. // TCD:
  155. // source address = buffer address
  156. // source offset = 4 bytes
  157. // attr = no src mod, ssize = 32 bit, dest mod = 16 bytes (4), dsize = 32 bit
  158. // minor loop byte count = 8
  159. // source last adjust = -sizeof(buffer)
  160. // dest address = FTM1_C0V
  161. // dest address offset = 8
  162. // citer = sizeof(buffer) / 8 (no minor loop linking)
  163. // dest last adjust = 0 (dest modulo keeps it ready for more)
  164. // control:
  165. // throttling = 0
  166. // major link to same channel
  167. // done = 0
  168. // active = 0
  169. // majorlink = 1
  170. // scatter/gather = 0
  171. // disable request = 0
  172. // inthalf = 1
  173. // intmajor = 1
  174. // start = 0
  175. // biter = sizeof(buffer) / 8 (no minor loop linking)
  176. #elif defined(KINETISL)
  177. void AudioOutputPWM::update(void)
  178. {
  179. audio_block_t *block;
  180. block = receiveReadOnly();
  181. if (block) release(block);
  182. }
  183. #endif