PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

output_i2s2.cpp 11KB

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