Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

output_tdm.cpp 10KB

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