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

206 lines
6.3KB

  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. #if defined(__IMXRT1052__) || defined(__IMXRT1062__)
  27. #include <Arduino.h>
  28. #include "output_tdm2.h"
  29. #include "memcpy_audio.h"
  30. #include "utility/imxrt_hw.h"
  31. audio_block_t * AudioOutputTDM2::block_input[16] = {
  32. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  33. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
  34. };
  35. bool AudioOutputTDM2::update_responsibility = false;
  36. static uint32_t zeros[AUDIO_BLOCK_SAMPLES/2];
  37. static uint32_t tdm_tx_buffer[AUDIO_BLOCK_SAMPLES*16];
  38. DMAChannel AudioOutputTDM2::dma(false);
  39. void AudioOutputTDM2::begin(void)
  40. {
  41. dma.begin(true); // Allocate the DMA channel first
  42. for (int i=0; i < 16; i++) {
  43. block_input[i] = NULL;
  44. }
  45. // TODO: should we set & clear the I2S_TCSR_SR bit here?
  46. config_tdm();
  47. CORE_PIN2_CONFIG = 2; //2:TX_DATA0
  48. dma.TCD->SADDR = tdm_tx_buffer;
  49. dma.TCD->SOFF = 4;
  50. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(2) | DMA_TCD_ATTR_DSIZE(2);
  51. dma.TCD->NBYTES_MLNO = 4;
  52. dma.TCD->SLAST = -sizeof(tdm_tx_buffer);
  53. dma.TCD->DADDR = &I2S2_TDR0;
  54. dma.TCD->DOFF = 0;
  55. dma.TCD->CITER_ELINKNO = sizeof(tdm_tx_buffer) / 4;
  56. dma.TCD->DLASTSGA = 0;
  57. dma.TCD->BITER_ELINKNO = sizeof(tdm_tx_buffer) / 4;
  58. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  59. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_SAI2_TX);
  60. update_responsibility = update_setup();
  61. dma.enable();
  62. //I2S2_RCSR |= I2S_RCSR_RE;
  63. I2S2_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE;
  64. dma.attachInterrupt(isr);
  65. }
  66. // TODO: needs optimization...
  67. static void memcpy_tdm_tx(uint32_t *dest, const uint32_t *src1, const uint32_t *src2)
  68. {
  69. uint32_t i, in1, in2, out1, out2;
  70. for (i=0; i < AUDIO_BLOCK_SAMPLES/2; i++) {
  71. in1 = *src1++;
  72. in2 = *src2++;
  73. out1 = (in1 << 16) | (in2 & 0xFFFF);
  74. out2 = (in1 & 0xFFFF0000) | (in2 >> 16);
  75. *dest = out1;
  76. *(dest + 8) = out2;
  77. dest += 16;
  78. }
  79. }
  80. void AudioOutputTDM2::isr(void)
  81. {
  82. uint32_t *dest;
  83. const uint32_t *src1, *src2;
  84. uint32_t i, saddr;
  85. saddr = (uint32_t)(dma.TCD->SADDR);
  86. dma.clearInterrupt();
  87. if (saddr < (uint32_t)tdm_tx_buffer + sizeof(tdm_tx_buffer) / 2) {
  88. // DMA is transmitting the first half of the buffer
  89. // so we must fill the second half
  90. dest = tdm_tx_buffer + AUDIO_BLOCK_SAMPLES*8;
  91. } else {
  92. // DMA is transmitting the second half of the buffer
  93. // so we must fill the first half
  94. dest = tdm_tx_buffer;
  95. }
  96. if (update_responsibility) AudioStream::update_all();
  97. for (i=0; i < 16; i += 2) {
  98. src1 = block_input[i] ? (uint32_t *)(block_input[i]->data) : zeros;
  99. src2 = block_input[i+1] ? (uint32_t *)(block_input[i+1]->data) : zeros;
  100. memcpy_tdm_tx(dest, src1, src2);
  101. dest++;
  102. }
  103. for (i=0; i < 16; i++) {
  104. if (block_input[i]) {
  105. release(block_input[i]);
  106. block_input[i] = NULL;
  107. }
  108. }
  109. }
  110. void AudioOutputTDM2::update(void)
  111. {
  112. audio_block_t *prev[16];
  113. unsigned int i;
  114. __disable_irq();
  115. for (i=0; i < 16; i++) {
  116. prev[i] = block_input[i];
  117. block_input[i] = receiveReadOnly(i);
  118. }
  119. __enable_irq();
  120. for (i=0; i < 16; i++) {
  121. if (prev[i]) release(prev[i]);
  122. }
  123. }
  124. void AudioOutputTDM2::config_tdm(void)
  125. {
  126. CCM_CCGR5 |= CCM_CCGR5_SAI2(CCM_CCGR_ON);
  127. //PLL:
  128. int fs = AUDIO_SAMPLE_RATE_EXACT*2; //176.4 khZ
  129. // PLL between 27*24 = 648MHz und 54*24=1296MHz
  130. int n1 = 4; //SAI prescaler 4 => (n1*n2) = multiple of 4
  131. int n2 = 1 + (24000000 * 27) / (fs * 256 * n1);
  132. double C = ((double)fs * 256 * n1 * n2) / 24000000;
  133. int c0 = C;
  134. int c2 = 10000;
  135. int c1 = C * c2 - (c0 * c2);
  136. set_audioClock(c0, c1, c2);
  137. // clear SAI1_CLK register locations
  138. CCM_CSCMR1 = (CCM_CSCMR1 & ~(CCM_CSCMR1_SAI2_CLK_SEL_MASK))
  139. | CCM_CSCMR1_SAI2_CLK_SEL(2); // &0x03 // (0,1,2): PLL3PFD0, PLL5, PLL4
  140. //n1 = n1 / 2; //Double Speed for TDM
  141. CCM_CS2CDR = (CCM_CS2CDR & ~(CCM_CS2CDR_SAI2_CLK_PRED_MASK | CCM_CS2CDR_SAI2_CLK_PODF_MASK))
  142. | CCM_CS2CDR_SAI2_CLK_PRED(n1-1) // &0x07
  143. | CCM_CS2CDR_SAI2_CLK_PODF(n2-1); // &0x3f
  144. IOMUXC_GPR_GPR1 = (IOMUXC_GPR_GPR1 & ~(IOMUXC_GPR_GPR1_SAI2_MCLK3_SEL_MASK))
  145. | (IOMUXC_GPR_GPR1_SAI2_MCLK_DIR | IOMUXC_GPR_GPR1_SAI2_MCLK3_SEL(0)); //Select MCLK
  146. // if either transmitter or receiver is enabled, do nothing
  147. if (I2S2_TCSR & I2S_TCSR_TE) return;
  148. if (I2S2_RCSR & I2S_RCSR_RE) return;
  149. // configure transmitter
  150. int rsync = 1;
  151. int tsync = 0;
  152. I2S2_TMR = 0;
  153. I2S2_TCR1 = I2S_TCR1_RFW(4);
  154. I2S2_TCR2 = I2S_TCR2_SYNC(tsync) | I2S_TCR2_BCP | I2S_TCR2_MSEL(1)
  155. | I2S_TCR2_BCD | I2S_TCR2_DIV(0);
  156. I2S2_TCR3 = I2S_TCR3_TCE;
  157. I2S2_TCR4 = I2S_TCR4_FRSZ(7) | I2S_TCR4_SYWD(0) | I2S_TCR4_MF
  158. | I2S_TCR4_FSE | I2S_TCR4_FSD;
  159. I2S2_TCR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31);
  160. // configure receiver (sync'd to transmitter clocks)
  161. I2S2_RMR = 0;
  162. I2S2_RCR1 = I2S_RCR1_RFW(4);
  163. I2S2_RCR2 = I2S_RCR2_SYNC(rsync) | I2S_TCR2_BCP | I2S_RCR2_MSEL(1)
  164. | I2S_RCR2_BCD | I2S_RCR2_DIV(0);
  165. I2S2_RCR3 = I2S_RCR3_RCE;
  166. I2S2_RCR4 = I2S_RCR4_FRSZ(7) | I2S_RCR4_SYWD(0) | I2S_RCR4_MF
  167. | I2S_RCR4_FSE | I2S_RCR4_FSD;
  168. I2S2_RCR5 = I2S_RCR5_WNW(31) | I2S_RCR5_W0W(31) | I2S_RCR5_FBT(31);
  169. CORE_PIN5_CONFIG = 2; //2:MCLK
  170. CORE_PIN4_CONFIG = 2; //2:TX_BCLK
  171. CORE_PIN3_CONFIG = 2; //2:TX_SYNC
  172. }
  173. #endif