Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

output_tdm.cpp 6.7KB

7 anos atrás
7 anos atrás
7 anos atrás
7 anos atrás
7 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2017, 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_tdm.h"
  27. #include "memcpy_audio.h"
  28. #if defined(KINETISK)
  29. audio_block_t * AudioOutputTDM::block_input[16] = {
  30. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  31. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
  32. };
  33. bool AudioOutputTDM::update_responsibility = false;
  34. static uint32_t zeros[AUDIO_BLOCK_SAMPLES/2];
  35. DMAMEM static uint32_t tdm_tx_buffer[AUDIO_BLOCK_SAMPLES*16];
  36. DMAChannel AudioOutputTDM::dma(false);
  37. void AudioOutputTDM::begin(void)
  38. {
  39. dma.begin(true); // Allocate the DMA channel first
  40. for (int i=0; i < 16; i++) {
  41. block_input[i] = NULL;
  42. }
  43. // TODO: should we set & clear the I2S_TCSR_SR bit here?
  44. config_tdm();
  45. CORE_PIN22_CONFIG = PORT_PCR_MUX(6); // pin 22, PTC1, I2S0_TXD0
  46. dma.TCD->SADDR = tdm_tx_buffer;
  47. dma.TCD->SOFF = 4;
  48. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(2) | DMA_TCD_ATTR_DSIZE(2);
  49. dma.TCD->NBYTES_MLNO = 4;
  50. dma.TCD->SLAST = -sizeof(tdm_tx_buffer);
  51. dma.TCD->DADDR = &I2S0_TDR0;
  52. dma.TCD->DOFF = 0;
  53. dma.TCD->CITER_ELINKNO = sizeof(tdm_tx_buffer) / 4;
  54. dma.TCD->DLASTSGA = 0;
  55. dma.TCD->BITER_ELINKNO = sizeof(tdm_tx_buffer) / 4;
  56. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  57. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_TX);
  58. update_responsibility = update_setup();
  59. dma.enable();
  60. I2S0_TCSR = I2S_TCSR_SR;
  61. I2S0_TCSR = I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE;
  62. dma.attachInterrupt(isr);
  63. }
  64. // TODO: needs optimization...
  65. static void memcpy_tdm_tx(uint32_t *dest, const uint32_t *src1, const uint32_t *src2)
  66. {
  67. uint32_t i, in1, in2, out1, out2;
  68. for (i=0; i < AUDIO_BLOCK_SAMPLES/2; i++) {
  69. in1 = *src1++;
  70. in2 = *src2++;
  71. out1 = (in1 << 16) | (in2 & 0xFFFF);
  72. out2 = (in1 & 0xFFFF0000) | (in2 >> 16);
  73. *dest = out1;
  74. *(dest + 8) = out2;
  75. dest += 16;
  76. }
  77. }
  78. void AudioOutputTDM::isr(void)
  79. {
  80. uint32_t *dest;
  81. const uint32_t *src1, *src2;
  82. uint32_t i, saddr;
  83. saddr = (uint32_t)(dma.TCD->SADDR);
  84. dma.clearInterrupt();
  85. if (saddr < (uint32_t)tdm_tx_buffer + sizeof(tdm_tx_buffer) / 2) {
  86. // DMA is transmitting the first half of the buffer
  87. // so we must fill the second half
  88. dest = tdm_tx_buffer + AUDIO_BLOCK_SAMPLES*8;
  89. } else {
  90. // DMA is transmitting the second half of the buffer
  91. // so we must fill the first half
  92. dest = tdm_tx_buffer;
  93. }
  94. if (update_responsibility) AudioStream::update_all();
  95. for (i=0; i < 16; i += 2) {
  96. src1 = block_input[i] ? (uint32_t *)(block_input[i]->data) : zeros;
  97. src2 = block_input[i+1] ? (uint32_t *)(block_input[i+1]->data) : zeros;
  98. memcpy_tdm_tx(dest, src1, src2);
  99. dest++;
  100. }
  101. for (i=0; i < 16; i++) {
  102. if (block_input[i]) {
  103. release(block_input[i]);
  104. block_input[i] = NULL;
  105. }
  106. }
  107. }
  108. void AudioOutputTDM::update(void)
  109. {
  110. audio_block_t *prev[16];
  111. unsigned int i;
  112. __disable_irq();
  113. for (i=0; i < 16; i++) {
  114. prev[i] = block_input[i];
  115. block_input[i] = receiveReadOnly(i);
  116. }
  117. __enable_irq();
  118. for (i=0; i < 16; i++) {
  119. if (prev[i]) release(prev[i]);
  120. }
  121. }
  122. // MCLK needs to be 48e6 / 1088 * 512 = 22.588235 MHz -> 44.117647 kHz sample rate
  123. //
  124. #if F_CPU == 96000000 || F_CPU == 48000000 || F_CPU == 24000000
  125. // PLL is at 96 MHz in these modes
  126. #define MCLK_MULT 4
  127. #define MCLK_DIV 17
  128. #elif F_CPU == 72000000
  129. #define MCLK_MULT 16
  130. #define MCLK_DIV 51
  131. #elif F_CPU == 120000000
  132. #define MCLK_MULT 16
  133. #define MCLK_DIV 85
  134. #elif F_CPU == 144000000
  135. #define MCLK_MULT 8
  136. #define MCLK_DIV 51
  137. #elif F_CPU == 168000000
  138. #define MCLK_MULT 16
  139. #define MCLK_DIV 119
  140. #elif F_CPU == 180000000
  141. #define MCLK_MULT 32
  142. #define MCLK_DIV 255
  143. #define MCLK_SRC 0
  144. #elif F_CPU == 192000000
  145. #define MCLK_MULT 2
  146. #define MCLK_DIV 17
  147. #elif F_CPU == 216000000
  148. #define MCLK_MULT 16
  149. #define MCLK_DIV 153
  150. #define MCLK_SRC 0
  151. #elif F_CPU == 240000000
  152. #define MCLK_MULT 8
  153. #define MCLK_DIV 85
  154. #else
  155. #error "This CPU Clock Speed is not supported by the Audio library";
  156. #endif
  157. #ifndef MCLK_SRC
  158. #if F_CPU >= 20000000
  159. #define MCLK_SRC 3 // the PLL
  160. #else
  161. #define MCLK_SRC 0 // system clock
  162. #endif
  163. #endif
  164. void AudioOutputTDM::config_tdm(void)
  165. {
  166. SIM_SCGC6 |= SIM_SCGC6_I2S;
  167. SIM_SCGC7 |= SIM_SCGC7_DMA;
  168. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  169. // if either transmitter or receiver is enabled, do nothing
  170. if (I2S0_TCSR & I2S_TCSR_TE) return;
  171. if (I2S0_RCSR & I2S_RCSR_RE) return;
  172. // enable MCLK output
  173. I2S0_MCR = I2S_MCR_MICS(MCLK_SRC) | I2S_MCR_MOE;
  174. while (I2S0_MCR & I2S_MCR_DUF) ;
  175. I2S0_MDR = I2S_MDR_FRACT((MCLK_MULT-1)) | I2S_MDR_DIVIDE((MCLK_DIV-1));
  176. // configure transmitter
  177. I2S0_TMR = 0;
  178. I2S0_TCR1 = I2S_TCR1_TFW(4);
  179. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP | I2S_TCR2_MSEL(1)
  180. | I2S_TCR2_BCD | I2S_TCR2_DIV(0);
  181. I2S0_TCR3 = I2S_TCR3_TCE;
  182. I2S0_TCR4 = I2S_TCR4_FRSZ(7) | I2S_TCR4_SYWD(0) | I2S_TCR4_MF
  183. | I2S_TCR4_FSE | I2S_TCR4_FSD;
  184. I2S0_TCR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31);
  185. // configure receiver (sync'd to transmitter clocks)
  186. I2S0_RMR = 0;
  187. I2S0_RCR1 = I2S_RCR1_RFW(4);
  188. I2S0_RCR2 = I2S_RCR2_SYNC(1) | I2S_TCR2_BCP | I2S_RCR2_MSEL(1)
  189. | I2S_RCR2_BCD | I2S_RCR2_DIV(0);
  190. I2S0_RCR3 = I2S_RCR3_RCE;
  191. I2S0_RCR4 = I2S_RCR4_FRSZ(7) | I2S_RCR4_SYWD(0) | I2S_RCR4_MF
  192. | I2S_RCR4_FSE | I2S_RCR4_FSD;
  193. I2S0_RCR5 = I2S_RCR5_WNW(31) | I2S_RCR5_W0W(31) | I2S_RCR5_FBT(31);
  194. // configure pin mux for 3 clock signals
  195. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)
  196. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK
  197. CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK
  198. }
  199. #endif // KINETISK