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.

317 lines
10KB

  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. #include "utility/imxrt_hw.h"
  31. audio_block_t * AudioOutputI2S2::block_left_1st = NULL;
  32. audio_block_t * AudioOutputI2S2::block_right_1st = NULL;
  33. audio_block_t * AudioOutputI2S2::block_left_2nd = NULL;
  34. audio_block_t * AudioOutputI2S2::block_right_2nd = NULL;
  35. uint16_t AudioOutputI2S2::block_left_offset = 0;
  36. uint16_t AudioOutputI2S2::block_right_offset = 0;
  37. bool AudioOutputI2S2::update_responsibility = false;
  38. DMAChannel AudioOutputI2S2::dma(false);
  39. DMAMEM __attribute__((aligned(32))) static uint32_t i2s2_tx_buffer[AUDIO_BLOCK_SAMPLES];
  40. #include "utility/imxrt_hw.h"
  41. void AudioOutputI2S2::begin(void)
  42. {
  43. dma.begin(true); // Allocate the DMA channel first
  44. block_left_1st = NULL;
  45. block_right_1st = NULL;
  46. config_i2s();
  47. CORE_PIN2_CONFIG = 2; //2:TX_DATA0
  48. dma.TCD->SADDR = i2s2_tx_buffer;
  49. dma.TCD->SOFF = 2;
  50. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  51. dma.TCD->NBYTES_MLNO = 2;
  52. dma.TCD->SLAST = -sizeof(i2s2_tx_buffer);
  53. dma.TCD->DOFF = 0;
  54. dma.TCD->CITER_ELINKNO = sizeof(i2s2_tx_buffer) / 2;
  55. dma.TCD->DLASTSGA = 0;
  56. dma.TCD->BITER_ELINKNO = sizeof(i2s2_tx_buffer) / 2;
  57. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  58. dma.TCD->DADDR = (void *)((uint32_t)&I2S2_TDR0 + 2);
  59. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_SAI2_TX);
  60. // I2S2_RCSR |= I2S_RCSR_RE;
  61. I2S2_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE;
  62. update_responsibility = update_setup();
  63. dma.attachInterrupt(isr);
  64. dma.enable();
  65. }
  66. void AudioOutputI2S2::isr(void)
  67. {
  68. int16_t *dest;
  69. audio_block_t *blockL, *blockR;
  70. uint32_t saddr, offsetL, offsetR;
  71. saddr = (uint32_t)(dma.TCD->SADDR);
  72. dma.clearInterrupt();
  73. if (saddr < (uint32_t)i2s2_tx_buffer + sizeof(i2s2_tx_buffer) / 2) {
  74. // DMA is transmitting the first half of the buffer
  75. // so we must fill the second half
  76. dest = (int16_t *)&i2s2_tx_buffer[AUDIO_BLOCK_SAMPLES/2];
  77. if (AudioOutputI2S2::update_responsibility) AudioStream::update_all();
  78. } else {
  79. // DMA is transmitting the second half of the buffer
  80. // so we must fill the first half
  81. dest = (int16_t *)i2s2_tx_buffer;
  82. }
  83. blockL = AudioOutputI2S2::block_left_1st;
  84. blockR = AudioOutputI2S2::block_right_1st;
  85. offsetL = AudioOutputI2S2::block_left_offset;
  86. offsetR = AudioOutputI2S2::block_right_offset;
  87. if (blockL && blockR) {
  88. memcpy_tointerleaveLR(dest, blockL->data + offsetL, blockR->data + offsetR);
  89. offsetL += AUDIO_BLOCK_SAMPLES / 2;
  90. offsetR += AUDIO_BLOCK_SAMPLES / 2;
  91. } else if (blockL) {
  92. memcpy_tointerleaveL(dest, blockL->data + offsetL);
  93. offsetL += AUDIO_BLOCK_SAMPLES / 2;
  94. } else if (blockR) {
  95. memcpy_tointerleaveR(dest, blockR->data + offsetR);
  96. offsetR += AUDIO_BLOCK_SAMPLES / 2;
  97. } else {
  98. memset(dest,0,AUDIO_BLOCK_SAMPLES * 2);
  99. }
  100. #if IMXRT_CACHE_ENABLED >= 2
  101. arm_dcache_flush_delete(dest, sizeof(i2s2_tx_buffer) / 2 );
  102. #endif
  103. if (offsetL < AUDIO_BLOCK_SAMPLES) {
  104. AudioOutputI2S2::block_left_offset = offsetL;
  105. } else {
  106. AudioOutputI2S2::block_left_offset = 0;
  107. AudioStream::release(blockL);
  108. AudioOutputI2S2::block_left_1st = AudioOutputI2S2::block_left_2nd;
  109. AudioOutputI2S2::block_left_2nd = NULL;
  110. }
  111. if (offsetR < AUDIO_BLOCK_SAMPLES) {
  112. AudioOutputI2S2::block_right_offset = offsetR;
  113. } else {
  114. AudioOutputI2S2::block_right_offset = 0;
  115. AudioStream::release(blockR);
  116. AudioOutputI2S2::block_right_1st = AudioOutputI2S2::block_right_2nd;
  117. AudioOutputI2S2::block_right_2nd = NULL;
  118. }
  119. }
  120. void AudioOutputI2S2::update(void)
  121. {
  122. // null audio device: discard all incoming data
  123. //if (!active) return;
  124. //audio_block_t *block = receiveReadOnly();
  125. //if (block) release(block);
  126. audio_block_t *block;
  127. block = receiveReadOnly(0); // input 0 = left channel
  128. if (block) {
  129. __disable_irq();
  130. if (block_left_1st == NULL) {
  131. block_left_1st = block;
  132. block_left_offset = 0;
  133. __enable_irq();
  134. } else if (block_left_2nd == NULL) {
  135. block_left_2nd = block;
  136. __enable_irq();
  137. } else {
  138. audio_block_t *tmp = block_left_1st;
  139. block_left_1st = block_left_2nd;
  140. block_left_2nd = block;
  141. block_left_offset = 0;
  142. __enable_irq();
  143. release(tmp);
  144. }
  145. }
  146. block = receiveReadOnly(1); // input 1 = right channel
  147. if (block) {
  148. __disable_irq();
  149. if (block_right_1st == NULL) {
  150. block_right_1st = block;
  151. block_right_offset = 0;
  152. __enable_irq();
  153. } else if (block_right_2nd == NULL) {
  154. block_right_2nd = block;
  155. __enable_irq();
  156. } else {
  157. audio_block_t *tmp = block_right_1st;
  158. block_right_1st = block_right_2nd;
  159. block_right_2nd = block;
  160. block_right_offset = 0;
  161. __enable_irq();
  162. release(tmp);
  163. }
  164. }
  165. }
  166. void AudioOutputI2S2::config_i2s(void)
  167. {
  168. CCM_CCGR5 |= CCM_CCGR5_SAI2(CCM_CCGR_ON);
  169. //PLL:
  170. int fs = AUDIO_SAMPLE_RATE_EXACT;
  171. // PLL between 27*24 = 648MHz und 54*24=1296MHz
  172. int n1 = 4; //SAI prescaler 4 => (n1*n2) = multiple of 4
  173. int n2 = 1 + (24000000 * 27) / (fs * 256 * n1);
  174. double C = ((double)fs * 256 * n1 * n2) / 24000000;
  175. int c0 = C;
  176. int c2 = 10000;
  177. int c1 = C * c2 - (c0 * c2);
  178. set_audioClock(c0, c1, c2);
  179. // clear SAI2_CLK register locations
  180. CCM_CSCMR1 = (CCM_CSCMR1 & ~(CCM_CSCMR1_SAI2_CLK_SEL_MASK))
  181. | CCM_CSCMR1_SAI2_CLK_SEL(2); // &0x03 // (0,1,2): PLL3PFD0, PLL5, PLL4,
  182. CCM_CS2CDR = (CCM_CS2CDR & ~(CCM_CS2CDR_SAI2_CLK_PRED_MASK | CCM_CS2CDR_SAI2_CLK_PODF_MASK))
  183. | CCM_CS2CDR_SAI2_CLK_PRED(n1-1)
  184. | CCM_CS2CDR_SAI2_CLK_PODF(n2-1);
  185. IOMUXC_GPR_GPR1 = (IOMUXC_GPR_GPR1 & ~(IOMUXC_GPR_GPR1_SAI2_MCLK3_SEL_MASK))
  186. | (IOMUXC_GPR_GPR1_SAI2_MCLK_DIR | IOMUXC_GPR_GPR1_SAI2_MCLK3_SEL(0)); //Select MCLK
  187. // if either transmitter or receiver is enabled, do nothing
  188. if (I2S2_TCSR & I2S_TCSR_TE) return;
  189. if (I2S2_RCSR & I2S_RCSR_RE) return;
  190. CORE_PIN5_CONFIG = 2; //2:MCLK
  191. CORE_PIN4_CONFIG = 2; //2:TX_BCLK
  192. CORE_PIN3_CONFIG = 2; //2:TX_SYNC
  193. // CORE_PIN2_CONFIG = 2; //2:TX_DATA0
  194. // CORE_PIN33_CONFIG = 2; //2:RX_DATA0
  195. int rsync = 1;
  196. int tsync = 0;
  197. I2S2_TMR = 0;
  198. //I2S2_TCSR = (1<<25); //Reset
  199. I2S2_TCR1 = I2S_TCR1_RFW(1);
  200. I2S2_TCR2 = I2S_TCR2_SYNC(tsync) | I2S_TCR2_BCP // sync=0; tx is async;
  201. | (I2S_TCR2_BCD | I2S_TCR2_DIV((1)) | I2S_TCR2_MSEL(1));
  202. I2S2_TCR3 = I2S_TCR3_TCE;
  203. I2S2_TCR4 = I2S_TCR4_FRSZ((2-1)) | I2S_TCR4_SYWD((32-1)) | I2S_TCR4_MF | I2S_TCR4_FSD | I2S_TCR4_FSE | I2S_TCR4_FSP;
  204. I2S2_TCR5 = I2S_TCR5_WNW((32-1)) | I2S_TCR5_W0W((32-1)) | I2S_TCR5_FBT((32-1));
  205. I2S2_RMR = 0;
  206. //I2S2_RCSR = (1<<25); //Reset
  207. I2S2_RCR1 = I2S_RCR1_RFW(1);
  208. I2S2_RCR2 = I2S_RCR2_SYNC(rsync) | I2S_RCR2_BCP // sync=0; rx is async;
  209. | (I2S_RCR2_BCD | I2S_RCR2_DIV((1)) | I2S_RCR2_MSEL(1));
  210. I2S2_RCR3 = I2S_RCR3_RCE;
  211. I2S2_RCR4 = I2S_RCR4_FRSZ((2-1)) | I2S_RCR4_SYWD((32-1)) | I2S_RCR4_MF | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
  212. I2S2_RCR5 = I2S_RCR5_WNW((32-1)) | I2S_RCR5_W0W((32-1)) | I2S_RCR5_FBT((32-1));
  213. }
  214. /******************************************************************/
  215. #if 0
  216. void AudioOutputI2S2slave::begin(void)
  217. {
  218. dma.begin(true); // Allocate the DMA channel first
  219. //pinMode(2, OUTPUT);
  220. block_left_1st = NULL;
  221. block_right_1st = NULL;
  222. AudioOutputI2S2slave::config_i2s();
  223. CORE_PIN2_CONFIG = 2; //2:TX_DATA0
  224. //CORE_PIN33_CONFIG = 2; //2:RX_DATA0
  225. dma.TCD->SADDR = i2s2_tx_buffer;
  226. dma.TCD->SOFF = 2;
  227. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  228. dma.TCD->NBYTES_MLNO = 2;
  229. dma.TCD->SLAST = -sizeof(i2s2_tx_buffer);
  230. dma.TCD->DADDR = (void *)((uint32_t)&I2S2_TDR0 + 2);
  231. dma.TCD->DOFF = 0;
  232. dma.TCD->CITER_ELINKNO = sizeof(i2s2_tx_buffer) / 2;
  233. dma.TCD->DLASTSGA = 0;
  234. dma.TCD->BITER_ELINKNO = sizeof(i2s2_tx_buffer) / 2;
  235. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_SAI2_TX);
  236. update_responsibility = update_setup();
  237. dma.enable();
  238. dma.attachInterrupt(isr);
  239. }
  240. void AudioOutputI2S2slave::config_i2s(void)
  241. {
  242. if (I2S2_TCSR & I2S_TCSR_TE) return;
  243. if (I2S2_TCSR & I2S_RCSR_RE) return;
  244. CCM_CCGR5 |= CCM_CCGR5_SAI2(CCM_CCGR_ON);
  245. /*
  246. CCM_CSCMR1 = (CCM_CSCMR1 & ~(CCM_CSCMR1_SAI2_CLK_SEL_MASK))
  247. | CCM_CSCMR1_SAI2_CLK_SEL(2); // &0x03 // (0,1,2): PLL3PFD0, PLL5, PLL4,
  248. CCM_CS2CDR = (CCM_CS2CDR & ~(CCM_CS2CDR_SAI2_CLK_PRED_MASK | CCM_CS2CDR_SAI2_CLK_PODF_MASK))
  249. | CCM_CS2CDR_SAI2_CLK_PRED(n1-1) | CCM_CS2CDR_SAI2_CLK_PODF(n2-1);
  250. */
  251. // IOMUXC_GPR_GPR1 = (IOMUXC_GPR_GPR1 & ~(IOMUXC_GPR_GPR1_SAI2_MCLK3_SEL_MASK | ((uint32_t)(1<<19)) ))
  252. // /*| (IOMUXC_GPR_GPR1_SAI2_MCLK_DIR*/ | IOMUXC_GPR_GPR1_SAI2_MCLK3_SEL(0); //Select MCLK
  253. CORE_PIN5_CONFIG = 2; //2:MCLK
  254. CORE_PIN4_CONFIG = 2; //2:TX_BCLK
  255. CORE_PIN3_CONFIG = 2; //2:TX_SYNC
  256. int rsync = 1;
  257. int tsync = 0;
  258. // configure transmitter
  259. I2S2_TMR = 0;
  260. I2S2_TCR1 = I2S_TCR1_RFW(1); // watermark at half fifo size
  261. I2S2_TCR2 = I2S_TCR2_SYNC(tsync) | I2S_TCR2_BCP;
  262. I2S2_TCR3 = I2S_TCR3_TCE;
  263. I2S2_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(31) | I2S_TCR4_MF
  264. | I2S_TCR4_FSE | I2S_TCR4_FSP;
  265. I2S2_TCR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31);
  266. // configure receiver
  267. I2S2_TMR = 0;
  268. I2S2_TCR1 = I2S_RCR1_RFW(1);
  269. I2S2_TCR2 = I2S_RCR2_SYNC(rsync) | I2S_TCR2_BCP;
  270. I2S2_TCR3 = I2S_RCR3_RCE;
  271. I2S2_TCR4 = I2S_RCR4_FRSZ(1) | I2S_RCR4_SYWD(31) | I2S_RCR4_MF
  272. | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
  273. I2S2_TCR5 = I2S_RCR5_WNW(31) | I2S_RCR5_W0W(31) | I2S_RCR5_FBT(31);
  274. }
  275. #endif
  276. #endif //defined(__IMXRT1062__)