You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

407 line
13KB

  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2014, 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_i2s2.h"
  29. #include "memcpy_audio.h"
  30. audio_block_t * AudioOutputI2S2::block_left_1st = NULL;
  31. audio_block_t * AudioOutputI2S2::block_right_1st = NULL;
  32. audio_block_t * AudioOutputI2S2::block_left_2nd = NULL;
  33. audio_block_t * AudioOutputI2S2::block_right_2nd = NULL;
  34. uint16_t AudioOutputI2S2::block_left_offset = 0;
  35. uint16_t AudioOutputI2S2::block_right_offset = 0;
  36. bool AudioOutputI2S2::update_responsibility = false;
  37. static uint32_t i2s_tx_buffer[AUDIO_BLOCK_SAMPLES];
  38. DMAChannel AudioOutputI2S2::dma(false);
  39. #include "utility/imxrt_hw.h"
  40. void AudioOutputI2S2::begin(void)
  41. {
  42. dma.begin(true); // Allocate the DMA channel first
  43. block_left_1st = NULL;
  44. block_right_1st = NULL;
  45. config_i2s();
  46. CORE_PIN2_CONFIG = 2; //2:TX_DATA0
  47. dma.TCD->SADDR = i2s_tx_buffer;
  48. dma.TCD->SOFF = 2;
  49. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  50. dma.TCD->NBYTES_MLNO = 2;
  51. dma.TCD->SLAST = -sizeof(i2s_tx_buffer);
  52. dma.TCD->DOFF = 0;
  53. dma.TCD->CITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  54. dma.TCD->DLASTSGA = 0;
  55. dma.TCD->BITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  56. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  57. dma.TCD->DADDR = (void *)((uint32_t)&I2S2_TDR0 + 2);
  58. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_SAI2_TX);
  59. // I2S2_RCSR |= I2S_RCSR_RE;
  60. I2S2_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE;
  61. update_responsibility = update_setup();
  62. dma.attachInterrupt(isr);
  63. dma.enable();
  64. }
  65. void AudioOutputI2S2::isr(void)
  66. {
  67. int16_t *dest;
  68. audio_block_t *blockL, *blockR;
  69. uint32_t saddr, offsetL, offsetR;
  70. saddr = (uint32_t)(dma.TCD->SADDR);
  71. dma.clearInterrupt();
  72. if (saddr < (uint32_t)i2s_tx_buffer + sizeof(i2s_tx_buffer) / 2) {
  73. // DMA is transmitting the first half of the buffer
  74. // so we must fill the second half
  75. dest = (int16_t *)&i2s_tx_buffer[AUDIO_BLOCK_SAMPLES/2];
  76. if (AudioOutputI2S2::update_responsibility) AudioStream::update_all();
  77. } else {
  78. // DMA is transmitting the second half of the buffer
  79. // so we must fill the first half
  80. dest = (int16_t *)i2s_tx_buffer;
  81. }
  82. blockL = AudioOutputI2S2::block_left_1st;
  83. blockR = AudioOutputI2S2::block_right_1st;
  84. offsetL = AudioOutputI2S2::block_left_offset;
  85. offsetR = AudioOutputI2S2::block_right_offset;
  86. if (blockL && blockR) {
  87. memcpy_tointerleaveLR(dest, blockL->data + offsetL, blockR->data + offsetR);
  88. offsetL += AUDIO_BLOCK_SAMPLES / 2;
  89. offsetR += AUDIO_BLOCK_SAMPLES / 2;
  90. } else if (blockL) {
  91. memcpy_tointerleaveL(dest, blockL->data + offsetL);
  92. offsetL += AUDIO_BLOCK_SAMPLES / 2;
  93. } else if (blockR) {
  94. memcpy_tointerleaveR(dest, blockR->data + offsetR);
  95. offsetR += AUDIO_BLOCK_SAMPLES / 2;
  96. } else {
  97. memset(dest,0,AUDIO_BLOCK_SAMPLES * 2);
  98. return;
  99. }
  100. if (offsetL < AUDIO_BLOCK_SAMPLES) {
  101. AudioOutputI2S2::block_left_offset = offsetL;
  102. } else {
  103. AudioOutputI2S2::block_left_offset = 0;
  104. AudioStream::release(blockL);
  105. AudioOutputI2S2::block_left_1st = AudioOutputI2S2::block_left_2nd;
  106. AudioOutputI2S2::block_left_2nd = NULL;
  107. }
  108. if (offsetR < AUDIO_BLOCK_SAMPLES) {
  109. AudioOutputI2S2::block_right_offset = offsetR;
  110. } else {
  111. AudioOutputI2S2::block_right_offset = 0;
  112. AudioStream::release(blockR);
  113. AudioOutputI2S2::block_right_1st = AudioOutputI2S2::block_right_2nd;
  114. AudioOutputI2S2::block_right_2nd = NULL;
  115. }
  116. }
  117. void AudioOutputI2S2::update(void)
  118. {
  119. // null audio device: discard all incoming data
  120. //if (!active) return;
  121. //audio_block_t *block = receiveReadOnly();
  122. //if (block) release(block);
  123. audio_block_t *block;
  124. block = receiveReadOnly(0); // input 0 = left channel
  125. if (block) {
  126. __disable_irq();
  127. if (block_left_1st == NULL) {
  128. block_left_1st = block;
  129. block_left_offset = 0;
  130. __enable_irq();
  131. } else if (block_left_2nd == NULL) {
  132. block_left_2nd = block;
  133. __enable_irq();
  134. } else {
  135. audio_block_t *tmp = block_left_1st;
  136. block_left_1st = block_left_2nd;
  137. block_left_2nd = block;
  138. block_left_offset = 0;
  139. __enable_irq();
  140. release(tmp);
  141. }
  142. }
  143. block = receiveReadOnly(1); // input 1 = right channel
  144. if (block) {
  145. __disable_irq();
  146. if (block_right_1st == NULL) {
  147. block_right_1st = block;
  148. block_right_offset = 0;
  149. __enable_irq();
  150. } else if (block_right_2nd == NULL) {
  151. block_right_2nd = block;
  152. __enable_irq();
  153. } else {
  154. audio_block_t *tmp = block_right_1st;
  155. block_right_1st = block_right_2nd;
  156. block_right_2nd = block;
  157. block_right_offset = 0;
  158. __enable_irq();
  159. release(tmp);
  160. }
  161. }
  162. }
  163. void AudioOutputI2S2::config_i2s(void)
  164. {
  165. CCM_CCGR5 |= CCM_CCGR5_SAI2(CCM_CCGR_ON);
  166. //PLL:
  167. int fs = AUDIO_SAMPLE_RATE_EXACT;
  168. // PLL between 27*24 = 648MHz und 54*24=1296MHz
  169. int n1 = 4; //SAI prescaler 4 => (n1*n2) = multiple of 4
  170. int n2 = 1 + (24000000 * 27) / (fs * 256 * n1);
  171. double C = ((double)fs * 256 * n1 * n2) / 24000000;
  172. int c0 = C;
  173. int c2 = 10000;
  174. int c1 = C * c2 - (c0 * c2);
  175. set_audioClock(c0, c1, c2);
  176. // clear SAI2_CLK register locations
  177. CCM_CSCMR1 = (CCM_CSCMR1 & ~(CCM_CSCMR1_SAI2_CLK_SEL_MASK))
  178. | CCM_CSCMR1_SAI2_CLK_SEL(2); // &0x03 // (0,1,2): PLL3PFD0, PLL5, PLL4,
  179. CCM_CS2CDR = (CCM_CS2CDR & ~(CCM_CS2CDR_SAI2_CLK_PRED_MASK | CCM_CS2CDR_SAI2_CLK_PODF_MASK))
  180. | CCM_CS2CDR_SAI2_CLK_PRED(n1-1) | CCM_CS2CDR_SAI2_CLK_PODF(n2-1);
  181. IOMUXC_GPR_GPR1 = (IOMUXC_GPR_GPR1 & ~(IOMUXC_GPR_GPR1_SAI2_MCLK3_SEL_MASK))
  182. | (IOMUXC_GPR_GPR1_SAI2_MCLK_DIR | IOMUXC_GPR_GPR1_SAI2_MCLK3_SEL(0)); //Select MCLK
  183. // if either transmitter or receiver is enabled, do nothing
  184. if (I2S2_TCSR & I2S_TCSR_TE) return;
  185. if (I2S2_RCSR & I2S_RCSR_RE) return;
  186. CORE_PIN5_CONFIG = 2; //2:MCLK
  187. CORE_PIN4_CONFIG = 2; //2:TX_BCLK
  188. CORE_PIN3_CONFIG = 2; //2:TX_SYNC
  189. // CORE_PIN2_CONFIG = 2; //2:TX_DATA0
  190. // CORE_PIN33_CONFIG = 2; //2:RX_DATA0
  191. int rsync = 1;
  192. int tsync = 0;
  193. I2S2_TMR = 0;
  194. //I2S2_TCSR = (1<<25); //Reset
  195. I2S2_TCR1 = I2S_TCR1_RFW(1);
  196. I2S2_TCR2 = I2S_TCR2_SYNC(tsync) | I2S_TCR2_BCP // sync=0; tx is async;
  197. | (I2S_TCR2_BCD | I2S_TCR2_DIV((1)) | I2S_TCR2_MSEL(1));
  198. I2S2_TCR3 = I2S_TCR3_TCE;
  199. I2S2_TCR4 = I2S_TCR4_FRSZ((2-1)) | I2S_TCR4_SYWD((32-1)) | I2S_TCR4_MF | I2S_TCR4_FSD | I2S_TCR4_FSE | I2S_TCR4_FSP;
  200. I2S2_TCR5 = I2S_TCR5_WNW((32-1)) | I2S_TCR5_W0W((32-1)) | I2S_TCR5_FBT((32-1));
  201. I2S2_RMR = 0;
  202. //I2S2_RCSR = (1<<25); //Reset
  203. I2S2_RCR1 = I2S_RCR1_RFW(1);
  204. I2S2_RCR2 = I2S_RCR2_SYNC(rsync) | I2S_RCR2_BCP // sync=0; rx is async;
  205. | (I2S_RCR2_BCD | I2S_RCR2_DIV((1)) | I2S_RCR2_MSEL(1));
  206. I2S2_RCR3 = I2S_RCR3_RCE;
  207. I2S2_RCR4 = I2S_RCR4_FRSZ((2-1)) | I2S_RCR4_SYWD((32-1)) | I2S_RCR4_MF | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
  208. I2S2_RCR5 = I2S_RCR5_WNW((32-1)) | I2S_RCR5_W0W((32-1)) | I2S_RCR5_FBT((32-1));
  209. }
  210. /******************************************************************/
  211. #if 0
  212. void AudioOutputI2Sslave::begin(void)
  213. {
  214. dma.begin(true); // Allocate the DMA channel first
  215. //pinMode(2, OUTPUT);
  216. block_left_1st = NULL;
  217. block_right_1st = NULL;
  218. AudioOutputI2Sslave::config_i2s();
  219. #if defined(KINETISK)
  220. CORE_PIN22_CONFIG = PORT_PCR_MUX(6); // pin 22, PTC1, I2S0_TXD0
  221. dma.TCD->SADDR = i2s_tx_buffer;
  222. dma.TCD->SOFF = 2;
  223. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  224. dma.TCD->NBYTES_MLNO = 2;
  225. dma.TCD->SLAST = -sizeof(i2s_tx_buffer);
  226. dma.TCD->DADDR = (void *)((uint32_t)&I2S0_TDR0 + 2);
  227. dma.TCD->DOFF = 0;
  228. dma.TCD->CITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  229. dma.TCD->DLASTSGA = 0;
  230. dma.TCD->BITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  231. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  232. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_TX);
  233. I2S0_TCSR = I2S_TCSR_SR;
  234. I2S0_TCSR = I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE;
  235. #elif 0 && ( defined(__IMXRT1052__) || defined(__IMXRT1062__) )
  236. #if defined(SAI1)
  237. CORE_PIN6_CONFIG = 3; //1:TX_DATA0
  238. //CORE_PIN7_CONFIG = 3; //1:RX_DATA0
  239. #elif defined(SAI2)
  240. CORE_PIN2_CONFIG = 2; //2:TX_DATA0
  241. //CORE_PIN33_CONFIG = 2; //2:RX_DATA0
  242. #endif
  243. dma.TCD->SADDR = i2s_tx_buffer;
  244. dma.TCD->SOFF = 2;
  245. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  246. dma.TCD->NBYTES_MLNO = 2;
  247. dma.TCD->SLAST = -sizeof(i2s_tx_buffer);
  248. dma.TCD->DADDR = (void *)&i2s->TX.DR16[1];
  249. dma.TCD->DOFF = 0;
  250. dma.TCD->CITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  251. dma.TCD->DLASTSGA = 0;
  252. dma.TCD->BITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  253. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_SAI2_TX);
  254. #endif
  255. update_responsibility = update_setup();
  256. dma.enable();
  257. dma.attachInterrupt(isr);
  258. }
  259. void AudioOutputI2Sslave::config_i2s(void)
  260. {
  261. #if defined(KINETISK)
  262. // if either transmitter or receiver is enabled, do nothing
  263. if (I2S0_TCSR & I2S_TCSR_TE) return;
  264. if (I2S0_RCSR & I2S_RCSR_RE) return;
  265. SIM_SCGC6 |= SIM_SCGC6_I2S;
  266. SIM_SCGC7 |= SIM_SCGC7_DMA;
  267. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  268. // configure pin mux for 3 clock signals
  269. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)
  270. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK
  271. CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK
  272. // Select input clock 0
  273. // Configure to input the bit-clock from pin, bypasses the MCLK divider
  274. I2S0_MCR = I2S_MCR_MICS(0);
  275. I2S0_MDR = 0;
  276. // configure transmitter
  277. I2S0_TMR = 0;
  278. I2S0_TCR1 = I2S_TCR1_TFW(1); // watermark at half fifo size
  279. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP;
  280. I2S0_TCR3 = I2S_TCR3_TCE;
  281. I2S0_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(31) | I2S_TCR4_MF
  282. | I2S_TCR4_FSE | I2S_TCR4_FSP;
  283. I2S0_TCR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31);
  284. // configure receiver (sync'd to transmitter clocks)
  285. I2S0_RMR = 0;
  286. I2S0_RCR1 = I2S_RCR1_RFW(1);
  287. I2S0_RCR2 = I2S_RCR2_SYNC(1) | I2S_TCR2_BCP;
  288. I2S0_RCR3 = I2S_RCR3_RCE;
  289. I2S0_RCR4 = I2S_RCR4_FRSZ(1) | I2S_RCR4_SYWD(31) | I2S_RCR4_MF
  290. | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
  291. I2S0_RCR5 = I2S_RCR5_WNW(31) | I2S_RCR5_W0W(31) | I2S_RCR5_FBT(31);
  292. #elif 0 && (defined(__IMXRT1052__) || defined(__IMXRT1062__) )
  293. #if defined(SAI1)
  294. i2s = ((I2S_STRUCT *)0x40384000);
  295. // if either transmitter or receiver is enabled, do nothing
  296. if (i2s->TX.CSR & I2S_TCSR_TE) return;
  297. if (i2s->RX.CSR & I2S_RCSR_RE) return;
  298. CCM_CCGR5 |= CCM_CCGR5_SAI1(CCM_CCGR_ON);
  299. /*
  300. CCM_CSCMR1 = (CCM_CSCMR1 & ~(CCM_CSCMR1_SAI1_CLK_SEL_MASK))
  301. | CCM_CSCMR1_SAI1_CLK_SEL(2); // &0x03 // (0,1,2): PLL3PFD0, PLL5, PLL4
  302. CCM_CS1CDR = (CCM_CS1CDR & ~(CCM_CS1CDR_SAI1_CLK_PRED_MASK | CCM_CS1CDR_SAI1_CLK_PODF_MASK))
  303. | CCM_CS1CDR_SAI1_CLK_PRED(n1-1) // &0x07
  304. | CCM_CS1CDR_SAI1_CLK_PODF(n2-1); // &0x3f
  305. */
  306. //TODO:
  307. IOMUXC_GPR_GPR1 = (IOMUXC_GPR_GPR1 & ~(IOMUXC_GPR_GPR1_SAI1_MCLK1_SEL_MASK | ((uint32_t)(1<<20)) ))
  308. | (IOMUXC_GPR_GPR1_SAI1_MCLK_DIR | IOMUXC_GPR_GPR1_SAI1_MCLK1_SEL(0)); //Select MCLK
  309. CORE_PIN23_CONFIG = 3; //1:MCLK
  310. CORE_PIN21_CONFIG = 3; //1:RX_BCLK
  311. CORE_PIN20_CONFIG = 3; //1:RX_SYNC
  312. int rsync = 0;
  313. int tsync = 1;
  314. #elif defined(SAI2)
  315. i2s = ((I2S_STRUCT *)0x40388000);
  316. if (i2s->TX.CSR & I2S_TCSR_TE) return;
  317. if (i2s->RX.CSR & I2S_RCSR_RE) return;
  318. CCM_CCGR5 |= CCM_CCGR5_SAI2(CCM_CCGR_ON);
  319. /*
  320. CCM_CSCMR1 = (CCM_CSCMR1 & ~(CCM_CSCMR1_SAI2_CLK_SEL_MASK))
  321. | CCM_CSCMR1_SAI2_CLK_SEL(2); // &0x03 // (0,1,2): PLL3PFD0, PLL5, PLL4,
  322. CCM_CS2CDR = (CCM_CS2CDR & ~(CCM_CS2CDR_SAI2_CLK_PRED_MASK | CCM_CS2CDR_SAI2_CLK_PODF_MASK))
  323. | CCM_CS2CDR_SAI2_CLK_PRED(n1-1) | CCM_CS2CDR_SAI2_CLK_PODF(n2-1);
  324. */
  325. //TODO:
  326. IOMUXC_GPR_GPR1 = (IOMUXC_GPR_GPR1 & ~(IOMUXC_GPR_GPR1_SAI2_MCLK3_SEL_MASK | ((uint32_t)(1<<19)) ))
  327. /*| (IOMUXC_GPR_GPR1_SAI2_MCLK_DIR*/ | IOMUXC_GPR_GPR1_SAI2_MCLK3_SEL(0); //Select MCLK
  328. CORE_PIN5_CONFIG = 2; //2:MCLK
  329. CORE_PIN4_CONFIG = 2; //2:TX_BCLK
  330. CORE_PIN3_CONFIG = 2; //2:TX_SYNC
  331. int rsync = 1;
  332. int tsync = 0;
  333. #endif
  334. // configure transmitter
  335. i2s->TX.MR = 0;
  336. i2s->TX.CR1 = I2S_TCR1_RFW(1); // watermark at half fifo size
  337. i2s->TX.CR2 = I2S_TCR2_SYNC(tsync) | I2S_TCR2_BCP;
  338. i2s->TX.CR3 = I2S_TCR3_TCE;
  339. i2s->TX.CR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(31) | I2S_TCR4_MF
  340. | I2S_TCR4_FSE | I2S_TCR4_FSP;
  341. i2s->TX.CR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31);
  342. // configure receiver
  343. i2s->RX.MR = 0;
  344. i2s->RX.CR1 = I2S_RCR1_RFW(1);
  345. i2s->RX.CR2 = I2S_RCR2_SYNC(rsync) | I2S_TCR2_BCP;
  346. i2s->RX.CR3 = I2S_RCR3_RCE;
  347. i2s->RX.CR4 = I2S_RCR4_FRSZ(1) | I2S_RCR4_SYWD(31) | I2S_RCR4_MF
  348. | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
  349. i2s->RX.CR5 = I2S_RCR5_WNW(31) | I2S_RCR5_W0W(31) | I2S_RCR5_FBT(31);
  350. #endif
  351. }
  352. #endif //if 0
  353. #endif //defined(__IMXRT1062__)