Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

227 lines
6.7KB

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