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ů.

203 lines
6.0KB

  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 "output_pwm.h"
  27. audio_block_t * AudioOutputPWM::block_1st = NULL;
  28. audio_block_t * AudioOutputPWM::block_2nd = NULL;
  29. uint32_t AudioOutputPWM::block_offset = 0;
  30. bool AudioOutputPWM::update_responsibility = false;
  31. uint8_t AudioOutputPWM::interrupt_count = 0;
  32. DMAMEM uint32_t pwm_dma_buffer[AUDIO_BLOCK_SAMPLES*2];
  33. DMAChannel AudioOutputPWM::dma(false);
  34. // TODO: this code assumes F_BUS is 48 MHz.
  35. // supporting other speeds is not easy, but should be done someday
  36. #if defined(KINETISK)
  37. void AudioOutputPWM::begin(void)
  38. {
  39. dma.begin(true); // Allocate the DMA channel first
  40. //Serial.println("AudioPwmOutput constructor");
  41. block_1st = NULL;
  42. FTM1_SC = 0;
  43. FTM1_CNT = 0;
  44. FTM1_MOD = 543;
  45. FTM1_C0SC = 0x69; // send DMA request on match
  46. FTM1_C1SC = 0x28;
  47. FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0);
  48. CORE_PIN3_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  49. CORE_PIN4_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  50. FTM1_C0V = 120; // range 120 to 375
  51. FTM1_C1V = 0; // range 0 to 255
  52. for (int i=0; i<(AUDIO_BLOCK_SAMPLES*2); i+=2) {
  53. pwm_dma_buffer[i] = 120; // zero must not be used
  54. pwm_dma_buffer[i+1] = 0;
  55. }
  56. dma.TCD->SADDR = pwm_dma_buffer;
  57. dma.TCD->SOFF = 4;
  58. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(2)
  59. | DMA_TCD_ATTR_DSIZE(2) | DMA_TCD_ATTR_DMOD(4);
  60. dma.TCD->NBYTES_MLNO = 8;
  61. dma.TCD->SLAST = -sizeof(pwm_dma_buffer);
  62. dma.TCD->DADDR = &FTM1_C0V;
  63. dma.TCD->DOFF = 8;
  64. dma.TCD->CITER_ELINKNO = sizeof(pwm_dma_buffer) / 8;
  65. dma.TCD->DLASTSGA = 0;
  66. dma.TCD->BITER_ELINKNO = sizeof(pwm_dma_buffer) / 8;
  67. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  68. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_FTM1_CH0);
  69. dma.enable();
  70. update_responsibility = update_setup();
  71. dma.attachInterrupt(isr);
  72. }
  73. void AudioOutputPWM::update(void)
  74. {
  75. audio_block_t *block;
  76. block = receiveReadOnly();
  77. if (!block) return;
  78. __disable_irq();
  79. if (block_1st == NULL) {
  80. block_1st = block;
  81. block_offset = 0;
  82. __enable_irq();
  83. } else if (block_2nd == NULL) {
  84. block_2nd = block;
  85. __enable_irq();
  86. } else {
  87. audio_block_t *tmp = block_1st;
  88. block_1st = block_2nd;
  89. block_2nd = block;
  90. block_offset = 0;
  91. __enable_irq();
  92. release(tmp);
  93. }
  94. }
  95. void AudioOutputPWM::isr(void)
  96. {
  97. int16_t *src;
  98. uint32_t *dest;
  99. audio_block_t *block;
  100. uint32_t saddr, offset;
  101. saddr = (uint32_t)(dma.TCD->SADDR);
  102. dma.clearInterrupt();
  103. if (saddr < (uint32_t)pwm_dma_buffer + sizeof(pwm_dma_buffer) / 2) {
  104. // DMA is transmitting the first half of the buffer
  105. // so we must fill the second half
  106. dest = &pwm_dma_buffer[AUDIO_BLOCK_SAMPLES];
  107. } else {
  108. // DMA is transmitting the second half of the buffer
  109. // so we must fill the first half
  110. dest = pwm_dma_buffer;
  111. }
  112. block = AudioOutputPWM::block_1st;
  113. offset = AudioOutputPWM::block_offset;
  114. if (block) {
  115. src = &block->data[offset];
  116. for (int i=0; i < AUDIO_BLOCK_SAMPLES/4; i++) {
  117. uint16_t sample = *src++ + 0x8000;
  118. uint32_t msb = ((sample >> 8) & 255) + 120;
  119. uint32_t lsb = sample & 255;
  120. *dest++ = msb;
  121. *dest++ = lsb;
  122. *dest++ = msb;
  123. *dest++ = lsb;
  124. }
  125. offset += AUDIO_BLOCK_SAMPLES/4;
  126. if (offset < AUDIO_BLOCK_SAMPLES) {
  127. AudioOutputPWM::block_offset = offset;
  128. } else {
  129. AudioOutputPWM::block_offset = 0;
  130. AudioStream::release(block);
  131. AudioOutputPWM::block_1st = AudioOutputPWM::block_2nd;
  132. AudioOutputPWM::block_2nd = NULL;
  133. }
  134. } else {
  135. // fill with silence when no data available
  136. for (int i=0; i < AUDIO_BLOCK_SAMPLES/4; i++) {
  137. *dest++ = 248;
  138. *dest++ = 0;
  139. *dest++ = 248;
  140. *dest++ = 0;
  141. }
  142. }
  143. if (AudioOutputPWM::update_responsibility) {
  144. if (++AudioOutputPWM::interrupt_count >= 4) {
  145. AudioOutputPWM::interrupt_count = 0;
  146. AudioStream::update_all();
  147. }
  148. }
  149. }
  150. // DMA target is: (registers require 32 bit writes)
  151. // 40039010 Channel 0 Value (FTM1_C0V)
  152. // 40039018 Channel 1 Value (FTM1_C1V)
  153. // TCD:
  154. // source address = buffer address
  155. // source offset = 4 bytes
  156. // attr = no src mod, ssize = 32 bit, dest mod = 16 bytes (4), dsize = 32 bit
  157. // minor loop byte count = 8
  158. // source last adjust = -sizeof(buffer)
  159. // dest address = FTM1_C0V
  160. // dest address offset = 8
  161. // citer = sizeof(buffer) / 8 (no minor loop linking)
  162. // dest last adjust = 0 (dest modulo keeps it ready for more)
  163. // control:
  164. // throttling = 0
  165. // major link to same channel
  166. // done = 0
  167. // active = 0
  168. // majorlink = 1
  169. // scatter/gather = 0
  170. // disable request = 0
  171. // inthalf = 1
  172. // intmajor = 1
  173. // start = 0
  174. // biter = sizeof(buffer) / 8 (no minor loop linking)
  175. #elif defined(KINETISL)
  176. void AudioOutputPWM::update(void)
  177. {
  178. audio_block_t *block;
  179. block = receiveReadOnly();
  180. if (block) release(block);
  181. }
  182. #endif