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

326 lines
9.9KB

  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. #include "utility/imxrt_hw.h"
  30. audio_block_t * AudioOutputTDM::block_input[16] = {
  31. nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
  32. nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr
  33. };
  34. bool AudioOutputTDM::update_responsibility = false;
  35. DMAChannel AudioOutputTDM::dma(false);
  36. DMAMEM __attribute__((aligned(32)))
  37. static uint32_t zeros[AUDIO_BLOCK_SAMPLES/2];
  38. DMAMEM __attribute__((aligned(32)))
  39. static uint32_t tdm_tx_buffer[AUDIO_BLOCK_SAMPLES*16];
  40. void AudioOutputTDM::begin(void)
  41. {
  42. dma.begin(true); // Allocate the DMA channel first
  43. for (int i=0; i < 16; i++) {
  44. block_input[i] = nullptr;
  45. }
  46. // TODO: should we set & clear the I2S_TCSR_SR bit here?
  47. config_tdm();
  48. #if defined(KINETISK)
  49. CORE_PIN22_CONFIG = PORT_PCR_MUX(6); // pin 22, PTC1, I2S0_TXD0
  50. dma.TCD->SADDR = tdm_tx_buffer;
  51. dma.TCD->SOFF = 4;
  52. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(2) | DMA_TCD_ATTR_DSIZE(2);
  53. dma.TCD->NBYTES_MLNO = 4;
  54. dma.TCD->SLAST = -sizeof(tdm_tx_buffer);
  55. dma.TCD->DADDR = &I2S0_TDR0;
  56. dma.TCD->DOFF = 0;
  57. dma.TCD->CITER_ELINKNO = sizeof(tdm_tx_buffer) / 4;
  58. dma.TCD->DLASTSGA = 0;
  59. dma.TCD->BITER_ELINKNO = sizeof(tdm_tx_buffer) / 4;
  60. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  61. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_TX);
  62. update_responsibility = update_setup();
  63. dma.enable();
  64. I2S0_TCSR = I2S_TCSR_SR;
  65. I2S0_TCSR = I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE;
  66. #elif defined(__IMXRT1052__) || defined(__IMXRT1062__)
  67. CORE_PIN6_CONFIG = 3; //1:TX_DATA0
  68. dma.TCD->SADDR = tdm_tx_buffer;
  69. dma.TCD->SOFF = 4;
  70. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(2) | DMA_TCD_ATTR_DSIZE(2);
  71. dma.TCD->NBYTES_MLNO = 4;
  72. dma.TCD->SLAST = -sizeof(tdm_tx_buffer);
  73. dma.TCD->DADDR = &I2S1_TDR0;
  74. dma.TCD->DOFF = 0;
  75. dma.TCD->CITER_ELINKNO = sizeof(tdm_tx_buffer) / 4;
  76. dma.TCD->DLASTSGA = 0;
  77. dma.TCD->BITER_ELINKNO = sizeof(tdm_tx_buffer) / 4;
  78. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  79. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_SAI1_TX);
  80. update_responsibility = update_setup();
  81. dma.enable();
  82. I2S1_RCSR |= I2S_RCSR_RE;
  83. I2S1_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE;
  84. #endif
  85. dma.attachInterrupt(isr);
  86. }
  87. // TODO: needs optimization...
  88. static void memcpy_tdm_tx(uint32_t *dest, const uint32_t *src1, const uint32_t *src2)
  89. {
  90. uint32_t i, in1, in2, out1, out2;
  91. for (i=0; i < AUDIO_BLOCK_SAMPLES/4; i++) {
  92. in1 = *src1++;
  93. in2 = *src2++;
  94. out1 = (in1 << 16) | (in2 & 0xFFFF);
  95. out2 = (in1 & 0xFFFF0000) | (in2 >> 16);
  96. *dest = out1;
  97. *(dest + 8) = out2;
  98. in1 = *src1++;
  99. in2 = *src2++;
  100. out1 = (in1 << 16) | (in2 & 0xFFFF);
  101. out2 = (in1 & 0xFFFF0000) | (in2 >> 16);
  102. *(dest + 16)= out1;
  103. *(dest + 24) = out2;
  104. dest += 32;
  105. }
  106. }
  107. void AudioOutputTDM::isr(void)
  108. {
  109. uint32_t *dest, *dc;
  110. const uint32_t *src1, *src2;
  111. uint32_t i, saddr;
  112. saddr = (uint32_t)(dma.TCD->SADDR);
  113. dma.clearInterrupt();
  114. if (saddr < (uint32_t)tdm_tx_buffer + sizeof(tdm_tx_buffer) / 2) {
  115. // DMA is transmitting the first half of the buffer
  116. // so we must fill the second half
  117. dest = tdm_tx_buffer + AUDIO_BLOCK_SAMPLES*8;
  118. } else {
  119. // DMA is transmitting the second half of the buffer
  120. // so we must fill the first half
  121. dest = tdm_tx_buffer;
  122. }
  123. if (update_responsibility) AudioStream::update_all();
  124. dc = dest;
  125. for (i=0; i < 16; i += 2) {
  126. src1 = block_input[i] ? (uint32_t *)(block_input[i]->data) : zeros;
  127. src2 = block_input[i+1] ? (uint32_t *)(block_input[i+1]->data) : zeros;
  128. memcpy_tdm_tx(dest, src1, src2);
  129. dest++;
  130. }
  131. #if IMXRT_CACHE_ENABLED >= 2
  132. arm_dcache_flush_delete(dc, sizeof(tdm_tx_buffer) / 2 );
  133. #endif
  134. for (i=0; i < 16; i++) {
  135. if (block_input[i]) {
  136. release(block_input[i]);
  137. block_input[i] = nullptr;
  138. }
  139. }
  140. }
  141. void AudioOutputTDM::update(void)
  142. {
  143. audio_block_t *prev[16];
  144. unsigned int i;
  145. __disable_irq();
  146. for (i=0; i < 16; i++) {
  147. prev[i] = block_input[i];
  148. block_input[i] = receiveReadOnly(i);
  149. }
  150. __enable_irq();
  151. for (i=0; i < 16; i++) {
  152. if (prev[i]) release(prev[i]);
  153. }
  154. }
  155. #if defined(KINETISK)
  156. // MCLK needs to be 48e6 / 1088 * 512 = 22.588235 MHz -> 44.117647 kHz sample rate
  157. //
  158. #if F_CPU == 96000000 || F_CPU == 48000000 || F_CPU == 24000000
  159. // PLL is at 96 MHz in these modes
  160. #define MCLK_MULT 4
  161. #define MCLK_DIV 17
  162. #elif F_CPU == 72000000
  163. #define MCLK_MULT 16
  164. #define MCLK_DIV 51
  165. #elif F_CPU == 120000000
  166. #define MCLK_MULT 16
  167. #define MCLK_DIV 85
  168. #elif F_CPU == 144000000
  169. #define MCLK_MULT 8
  170. #define MCLK_DIV 51
  171. #elif F_CPU == 168000000
  172. #define MCLK_MULT 16
  173. #define MCLK_DIV 119
  174. #elif F_CPU == 180000000
  175. #define MCLK_MULT 32
  176. #define MCLK_DIV 255
  177. #define MCLK_SRC 0
  178. #elif F_CPU == 192000000
  179. #define MCLK_MULT 2
  180. #define MCLK_DIV 17
  181. #elif F_CPU == 216000000
  182. #define MCLK_MULT 16
  183. #define MCLK_DIV 153
  184. #define MCLK_SRC 0
  185. #elif F_CPU == 240000000
  186. #define MCLK_MULT 8
  187. #define MCLK_DIV 85
  188. #else
  189. #error "This CPU Clock Speed is not supported by the Audio library";
  190. #endif
  191. #ifndef MCLK_SRC
  192. #if F_CPU >= 20000000
  193. #define MCLK_SRC 3 // the PLL
  194. #else
  195. #define MCLK_SRC 0 // system clock
  196. #endif
  197. #endif
  198. #endif
  199. void AudioOutputTDM::config_tdm(void)
  200. {
  201. #if defined(KINETISK)
  202. SIM_SCGC6 |= SIM_SCGC6_I2S;
  203. SIM_SCGC7 |= SIM_SCGC7_DMA;
  204. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  205. // if either transmitter or receiver is enabled, do nothing
  206. if (I2S0_TCSR & I2S_TCSR_TE) return;
  207. if (I2S0_RCSR & I2S_RCSR_RE) return;
  208. // enable MCLK output
  209. I2S0_MCR = I2S_MCR_MICS(MCLK_SRC) | I2S_MCR_MOE;
  210. while (I2S0_MCR & I2S_MCR_DUF) ;
  211. I2S0_MDR = I2S_MDR_FRACT((MCLK_MULT-1)) | I2S_MDR_DIVIDE((MCLK_DIV-1));
  212. // configure transmitter
  213. I2S0_TMR = 0;
  214. I2S0_TCR1 = I2S_TCR1_TFW(4);
  215. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP | I2S_TCR2_MSEL(1)
  216. | I2S_TCR2_BCD | I2S_TCR2_DIV(0);
  217. I2S0_TCR3 = I2S_TCR3_TCE;
  218. I2S0_TCR4 = I2S_TCR4_FRSZ(7) | I2S_TCR4_SYWD(0) | I2S_TCR4_MF
  219. | I2S_TCR4_FSE | I2S_TCR4_FSD;
  220. I2S0_TCR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31);
  221. // configure receiver (sync'd to transmitter clocks)
  222. I2S0_RMR = 0;
  223. I2S0_RCR1 = I2S_RCR1_RFW(4);
  224. I2S0_RCR2 = I2S_RCR2_SYNC(1) | I2S_TCR2_BCP | I2S_RCR2_MSEL(1)
  225. | I2S_RCR2_BCD | I2S_RCR2_DIV(0);
  226. I2S0_RCR3 = I2S_RCR3_RCE;
  227. I2S0_RCR4 = I2S_RCR4_FRSZ(7) | I2S_RCR4_SYWD(0) | I2S_RCR4_MF
  228. | I2S_RCR4_FSE | I2S_RCR4_FSD;
  229. I2S0_RCR5 = I2S_RCR5_WNW(31) | I2S_RCR5_W0W(31) | I2S_RCR5_FBT(31);
  230. // configure pin mux for 3 clock signals
  231. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK) - 44.1kHz
  232. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK - 11.2 MHz
  233. CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK - 22.5 MHz
  234. #elif defined(__IMXRT1052__) || defined(__IMXRT1062__)
  235. CCM_CCGR5 |= CCM_CCGR5_SAI1(CCM_CCGR_ON);
  236. //PLL:
  237. int fs = AUDIO_SAMPLE_RATE_EXACT;
  238. // PLL between 27*24 = 648MHz und 54*24=1296MHz
  239. int n1 = 4; //SAI prescaler 4 => (n1*n2) = multiple of 4
  240. int n2 = 1 + (24000000 * 27) / (fs * 256 * n1);
  241. double C = ((double)fs * 256 * n1 * n2) / 24000000;
  242. int c0 = C;
  243. int c2 = 10000;
  244. int c1 = C * c2 - (c0 * c2);
  245. set_audioClock(c0, c1, c2);
  246. // clear SAI1_CLK register locations
  247. CCM_CSCMR1 = (CCM_CSCMR1 & ~(CCM_CSCMR1_SAI1_CLK_SEL_MASK))
  248. | CCM_CSCMR1_SAI1_CLK_SEL(2); // &0x03 // (0,1,2): PLL3PFD0, PLL5, PLL4
  249. n1 = n1 / 2; //Double Speed for TDM
  250. CCM_CS1CDR = (CCM_CS1CDR & ~(CCM_CS1CDR_SAI1_CLK_PRED_MASK | CCM_CS1CDR_SAI1_CLK_PODF_MASK))
  251. | CCM_CS1CDR_SAI1_CLK_PRED(n1-1) // &0x07
  252. | CCM_CS1CDR_SAI1_CLK_PODF(n2-1); // &0x3f
  253. IOMUXC_GPR_GPR1 = (IOMUXC_GPR_GPR1 & ~(IOMUXC_GPR_GPR1_SAI1_MCLK1_SEL_MASK))
  254. | (IOMUXC_GPR_GPR1_SAI1_MCLK_DIR | IOMUXC_GPR_GPR1_SAI1_MCLK1_SEL(0)); //Select MCLK
  255. // if either transmitter or receiver is enabled, do nothing
  256. if (I2S1_TCSR & I2S_TCSR_TE) return;
  257. if (I2S1_RCSR & I2S_RCSR_RE) return;
  258. // configure transmitter
  259. int rsync = 0;
  260. int tsync = 1;
  261. I2S1_TMR = 0;
  262. I2S1_TCR1 = I2S_TCR1_RFW(4);
  263. I2S1_TCR2 = I2S_TCR2_SYNC(tsync) | I2S_TCR2_BCP | I2S_TCR2_MSEL(1)
  264. | I2S_TCR2_BCD | I2S_TCR2_DIV(0);
  265. I2S1_TCR3 = I2S_TCR3_TCE;
  266. I2S1_TCR4 = I2S_TCR4_FRSZ(7) | I2S_TCR4_SYWD(0) | I2S_TCR4_MF
  267. | I2S_TCR4_FSE | I2S_TCR4_FSD;
  268. I2S1_TCR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31);
  269. I2S1_RMR = 0;
  270. I2S1_RCR1 = I2S_RCR1_RFW(4);
  271. I2S1_RCR2 = I2S_RCR2_SYNC(rsync) | I2S_TCR2_BCP | I2S_RCR2_MSEL(1)
  272. | I2S_RCR2_BCD | I2S_RCR2_DIV(0);
  273. I2S1_RCR3 = I2S_RCR3_RCE;
  274. I2S1_RCR4 = I2S_RCR4_FRSZ(7) | I2S_RCR4_SYWD(0) | I2S_RCR4_MF
  275. | I2S_RCR4_FSE | I2S_RCR4_FSD;
  276. I2S1_RCR5 = I2S_RCR5_WNW(31) | I2S_RCR5_W0W(31) | I2S_RCR5_FBT(31);
  277. CORE_PIN23_CONFIG = 3; //1:MCLK
  278. CORE_PIN21_CONFIG = 3; //1:RX_BCLK
  279. CORE_PIN20_CONFIG = 3; //1:RX_SYNC
  280. #endif
  281. }