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.

617 line
19KB

  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. #include <Arduino.h>
  27. #include "output_i2s.h"
  28. #include "memcpy_audio.h"
  29. audio_block_t * AudioOutputI2S::block_left_1st = NULL;
  30. audio_block_t * AudioOutputI2S::block_right_1st = NULL;
  31. audio_block_t * AudioOutputI2S::block_left_2nd = NULL;
  32. audio_block_t * AudioOutputI2S::block_right_2nd = NULL;
  33. uint16_t AudioOutputI2S::block_left_offset = 0;
  34. uint16_t AudioOutputI2S::block_right_offset = 0;
  35. bool AudioOutputI2S::update_responsibility = false;
  36. DMAChannel AudioOutputI2S::dma(false);
  37. DMAMEM __attribute__((aligned(32))) static uint32_t i2s_tx_buffer[AUDIO_BLOCK_SAMPLES];
  38. #if defined(__IMXRT1052__) || defined(__IMXRT1062__)
  39. #include "utility/imxrt_hw.h"
  40. void AudioOutputI2S::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. #if defined(__IMXRT1052__)
  47. CORE_PIN6_CONFIG = 3; //1:TX_DATA0
  48. #elif defined(__IMXRT1062__)
  49. CORE_PIN7_CONFIG = 3; //1:TX_DATA0
  50. #endif
  51. dma.TCD->SADDR = i2s_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(i2s_tx_buffer);
  56. dma.TCD->DOFF = 0;
  57. dma.TCD->CITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  58. dma.TCD->DLASTSGA = 0;
  59. dma.TCD->BITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  60. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  61. dma.TCD->DADDR = (void *)((uint32_t)&I2S1_TDR0 + 2);
  62. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_SAI1_TX);
  63. I2S1_RCSR |= I2S_RCSR_RE;
  64. I2S1_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE;
  65. update_responsibility = update_setup();
  66. dma.attachInterrupt(isr);
  67. dma.enable();
  68. }
  69. #endif
  70. #if defined(KINETISK)
  71. void AudioOutputI2S::begin(void)
  72. {
  73. dma.begin(true); // Allocate the DMA channel first
  74. block_left_1st = NULL;
  75. block_right_1st = NULL;
  76. // TODO: should we set & clear the I2S_TCSR_SR bit here?
  77. config_i2s();
  78. CORE_PIN22_CONFIG = PORT_PCR_MUX(6); // pin 22, PTC1, I2S0_TXD0
  79. dma.TCD->SADDR = i2s_tx_buffer;
  80. dma.TCD->SOFF = 2;
  81. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  82. dma.TCD->NBYTES_MLNO = 2;
  83. dma.TCD->SLAST = -sizeof(i2s_tx_buffer);
  84. dma.TCD->DADDR = (void *)((uint32_t)&I2S0_TDR0 + 2);
  85. dma.TCD->DOFF = 0;
  86. dma.TCD->CITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  87. dma.TCD->DLASTSGA = 0;
  88. dma.TCD->BITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  89. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  90. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_TX);
  91. update_responsibility = update_setup();
  92. dma.enable();
  93. I2S0_TCSR = I2S_TCSR_SR;
  94. I2S0_TCSR = I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE;
  95. dma.attachInterrupt(isr);
  96. }
  97. #endif
  98. void AudioOutputI2S::isr(void)
  99. {
  100. #if defined(KINETISK) || defined(__IMXRT1052__) || defined(__IMXRT1062__)
  101. int16_t *dest;
  102. audio_block_t *blockL, *blockR;
  103. uint32_t saddr, offsetL, offsetR;
  104. saddr = (uint32_t)(dma.TCD->SADDR);
  105. dma.clearInterrupt();
  106. if (saddr < (uint32_t)i2s_tx_buffer + sizeof(i2s_tx_buffer) / 2) {
  107. // DMA is transmitting the first half of the buffer
  108. // so we must fill the second half
  109. dest = (int16_t *)&i2s_tx_buffer[AUDIO_BLOCK_SAMPLES/2];
  110. if (AudioOutputI2S::update_responsibility) AudioStream::update_all();
  111. } else {
  112. // DMA is transmitting the second half of the buffer
  113. // so we must fill the first half
  114. dest = (int16_t *)i2s_tx_buffer;
  115. }
  116. blockL = AudioOutputI2S::block_left_1st;
  117. blockR = AudioOutputI2S::block_right_1st;
  118. offsetL = AudioOutputI2S::block_left_offset;
  119. offsetR = AudioOutputI2S::block_right_offset;
  120. if (blockL && blockR) {
  121. memcpy_tointerleaveLR(dest, blockL->data + offsetL, blockR->data + offsetR);
  122. offsetL += AUDIO_BLOCK_SAMPLES / 2;
  123. offsetR += AUDIO_BLOCK_SAMPLES / 2;
  124. } else if (blockL) {
  125. memcpy_tointerleaveL(dest, blockL->data + offsetL);
  126. offsetL += AUDIO_BLOCK_SAMPLES / 2;
  127. } else if (blockR) {
  128. memcpy_tointerleaveR(dest, blockR->data + offsetR);
  129. offsetR += AUDIO_BLOCK_SAMPLES / 2;
  130. } else {
  131. memset(dest,0,AUDIO_BLOCK_SAMPLES * 2);
  132. }
  133. #if IMXRT_CACHE_ENABLED >= 2
  134. arm_dcache_flush_delete(dest, sizeof(i2s_tx_buffer) / 2 );
  135. #endif
  136. if (offsetL < AUDIO_BLOCK_SAMPLES) {
  137. AudioOutputI2S::block_left_offset = offsetL;
  138. } else {
  139. AudioOutputI2S::block_left_offset = 0;
  140. AudioStream::release(blockL);
  141. AudioOutputI2S::block_left_1st = AudioOutputI2S::block_left_2nd;
  142. AudioOutputI2S::block_left_2nd = NULL;
  143. }
  144. if (offsetR < AUDIO_BLOCK_SAMPLES) {
  145. AudioOutputI2S::block_right_offset = offsetR;
  146. } else {
  147. AudioOutputI2S::block_right_offset = 0;
  148. AudioStream::release(blockR);
  149. AudioOutputI2S::block_right_1st = AudioOutputI2S::block_right_2nd;
  150. AudioOutputI2S::block_right_2nd = NULL;
  151. }
  152. #else
  153. const int16_t *src, *end;
  154. int16_t *dest;
  155. audio_block_t *block;
  156. uint32_t saddr, offset;
  157. saddr = (uint32_t)(dma.CFG->SAR);
  158. dma.clearInterrupt();
  159. if (saddr < (uint32_t)i2s_tx_buffer + sizeof(i2s_tx_buffer) / 2) {
  160. // DMA is transmitting the first half of the buffer
  161. // so we must fill the second half
  162. dest = (int16_t *)&i2s_tx_buffer[AUDIO_BLOCK_SAMPLES/2];
  163. end = (int16_t *)&i2s_tx_buffer[AUDIO_BLOCK_SAMPLES];
  164. if (AudioOutputI2S::update_responsibility) AudioStream::update_all();
  165. } else {
  166. // DMA is transmitting the second half of the buffer
  167. // so we must fill the first half
  168. dest = (int16_t *)i2s_tx_buffer;
  169. end = (int16_t *)&i2s_tx_buffer[AUDIO_BLOCK_SAMPLES/2];
  170. }
  171. block = AudioOutputI2S::block_left_1st;
  172. if (block) {
  173. offset = AudioOutputI2S::block_left_offset;
  174. src = &block->data[offset];
  175. do {
  176. *dest = *src++;
  177. dest += 2;
  178. } while (dest < end);
  179. offset += AUDIO_BLOCK_SAMPLES/2;
  180. if (offset < AUDIO_BLOCK_SAMPLES) {
  181. AudioOutputI2S::block_left_offset = offset;
  182. } else {
  183. AudioOutputI2S::block_left_offset = 0;
  184. AudioStream::release(block);
  185. AudioOutputI2S::block_left_1st = AudioOutputI2S::block_left_2nd;
  186. AudioOutputI2S::block_left_2nd = NULL;
  187. }
  188. } else {
  189. do {
  190. *dest = 0;
  191. dest += 2;
  192. } while (dest < end);
  193. }
  194. dest -= AUDIO_BLOCK_SAMPLES - 1;
  195. block = AudioOutputI2S::block_right_1st;
  196. if (block) {
  197. offset = AudioOutputI2S::block_right_offset;
  198. src = &block->data[offset];
  199. do {
  200. *dest = *src++;
  201. dest += 2;
  202. } while (dest < end);
  203. offset += AUDIO_BLOCK_SAMPLES/2;
  204. if (offset < AUDIO_BLOCK_SAMPLES) {
  205. AudioOutputI2S::block_right_offset = offset;
  206. } else {
  207. AudioOutputI2S::block_right_offset = 0;
  208. AudioStream::release(block);
  209. AudioOutputI2S::block_right_1st = AudioOutputI2S::block_right_2nd;
  210. AudioOutputI2S::block_right_2nd = NULL;
  211. }
  212. } else {
  213. do {
  214. *dest = 0;
  215. dest += 2;
  216. } while (dest < end);
  217. }
  218. #endif
  219. }
  220. void AudioOutputI2S::update(void)
  221. {
  222. // null audio device: discard all incoming data
  223. //if (!active) return;
  224. //audio_block_t *block = receiveReadOnly();
  225. //if (block) release(block);
  226. audio_block_t *block;
  227. block = receiveReadOnly(0); // input 0 = left channel
  228. if (block) {
  229. __disable_irq();
  230. if (block_left_1st == NULL) {
  231. block_left_1st = block;
  232. block_left_offset = 0;
  233. __enable_irq();
  234. } else if (block_left_2nd == NULL) {
  235. block_left_2nd = block;
  236. __enable_irq();
  237. } else {
  238. audio_block_t *tmp = block_left_1st;
  239. block_left_1st = block_left_2nd;
  240. block_left_2nd = block;
  241. block_left_offset = 0;
  242. __enable_irq();
  243. release(tmp);
  244. }
  245. }
  246. block = receiveReadOnly(1); // input 1 = right channel
  247. if (block) {
  248. __disable_irq();
  249. if (block_right_1st == NULL) {
  250. block_right_1st = block;
  251. block_right_offset = 0;
  252. __enable_irq();
  253. } else if (block_right_2nd == NULL) {
  254. block_right_2nd = block;
  255. __enable_irq();
  256. } else {
  257. audio_block_t *tmp = block_right_1st;
  258. block_right_1st = block_right_2nd;
  259. block_right_2nd = block;
  260. block_right_offset = 0;
  261. __enable_irq();
  262. release(tmp);
  263. }
  264. }
  265. }
  266. #if defined(KINETISK) || defined(KINETISL)
  267. // MCLK needs to be 48e6 / 1088 * 256 = 11.29411765 MHz -> 44.117647 kHz sample rate
  268. //
  269. #if F_CPU == 96000000 || F_CPU == 48000000 || F_CPU == 24000000
  270. // PLL is at 96 MHz in these modes
  271. #define MCLK_MULT 2
  272. #define MCLK_DIV 17
  273. #elif F_CPU == 72000000
  274. #define MCLK_MULT 8
  275. #define MCLK_DIV 51
  276. #elif F_CPU == 120000000
  277. #define MCLK_MULT 8
  278. #define MCLK_DIV 85
  279. #elif F_CPU == 144000000
  280. #define MCLK_MULT 4
  281. #define MCLK_DIV 51
  282. #elif F_CPU == 168000000
  283. #define MCLK_MULT 8
  284. #define MCLK_DIV 119
  285. #elif F_CPU == 180000000
  286. #define MCLK_MULT 16
  287. #define MCLK_DIV 255
  288. #define MCLK_SRC 0
  289. #elif F_CPU == 192000000
  290. #define MCLK_MULT 1
  291. #define MCLK_DIV 17
  292. #elif F_CPU == 216000000
  293. #define MCLK_MULT 8
  294. #define MCLK_DIV 153
  295. #define MCLK_SRC 0
  296. #elif F_CPU == 240000000
  297. #define MCLK_MULT 4
  298. #define MCLK_DIV 85
  299. #elif F_CPU == 16000000
  300. #define MCLK_MULT 12
  301. #define MCLK_DIV 17
  302. #else
  303. #error "This CPU Clock Speed is not supported by the Audio library";
  304. #endif
  305. #ifndef MCLK_SRC
  306. #if F_CPU >= 20000000
  307. #define MCLK_SRC 3 // the PLL
  308. #else
  309. #define MCLK_SRC 0 // system clock
  310. #endif
  311. #endif
  312. #endif
  313. void AudioOutputI2S::config_i2s(void)
  314. {
  315. #if defined(KINETISK) || defined(KINETISL)
  316. SIM_SCGC6 |= SIM_SCGC6_I2S;
  317. SIM_SCGC7 |= SIM_SCGC7_DMA;
  318. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  319. // if either transmitter or receiver is enabled, do nothing
  320. if (I2S0_TCSR & I2S_TCSR_TE) return;
  321. if (I2S0_RCSR & I2S_RCSR_RE) return;
  322. // enable MCLK output
  323. I2S0_MCR = I2S_MCR_MICS(MCLK_SRC) | I2S_MCR_MOE;
  324. while (I2S0_MCR & I2S_MCR_DUF) ;
  325. I2S0_MDR = I2S_MDR_FRACT((MCLK_MULT-1)) | I2S_MDR_DIVIDE((MCLK_DIV-1));
  326. // configure transmitter
  327. I2S0_TMR = 0;
  328. I2S0_TCR1 = I2S_TCR1_TFW(1); // watermark at half fifo size
  329. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP | I2S_TCR2_MSEL(1)
  330. | I2S_TCR2_BCD | I2S_TCR2_DIV(1);
  331. I2S0_TCR3 = I2S_TCR3_TCE;
  332. I2S0_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(31) | I2S_TCR4_MF
  333. | I2S_TCR4_FSE | I2S_TCR4_FSP | I2S_TCR4_FSD;
  334. I2S0_TCR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31);
  335. // configure receiver (sync'd to transmitter clocks)
  336. I2S0_RMR = 0;
  337. I2S0_RCR1 = I2S_RCR1_RFW(1);
  338. I2S0_RCR2 = I2S_RCR2_SYNC(1) | I2S_TCR2_BCP | I2S_RCR2_MSEL(1)
  339. | I2S_RCR2_BCD | I2S_RCR2_DIV(1);
  340. I2S0_RCR3 = I2S_RCR3_RCE;
  341. I2S0_RCR4 = I2S_RCR4_FRSZ(1) | I2S_RCR4_SYWD(31) | I2S_RCR4_MF
  342. | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
  343. I2S0_RCR5 = I2S_RCR5_WNW(31) | I2S_RCR5_W0W(31) | I2S_RCR5_FBT(31);
  344. // configure pin mux for 3 clock signals
  345. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)
  346. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK
  347. CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK
  348. #elif ( defined(__IMXRT1052__) || defined(__IMXRT1062__) )
  349. CCM_CCGR5 |= CCM_CCGR5_SAI1(CCM_CCGR_ON);
  350. //PLL:
  351. int fs = AUDIO_SAMPLE_RATE_EXACT;
  352. // PLL between 27*24 = 648MHz und 54*24=1296MHz
  353. int n1 = 4; //SAI prescaler 4 => (n1*n2) = multiple of 4
  354. int n2 = 1 + (24000000 * 27) / (fs * 256 * n1);
  355. double C = ((double)fs * 256 * n1 * n2) / 24000000;
  356. int c0 = C;
  357. int c2 = 10000;
  358. int c1 = C * c2 - (c0 * c2);
  359. set_audioClock(c0, c1, c2);
  360. // clear SAI1_CLK register locations
  361. CCM_CSCMR1 = (CCM_CSCMR1 & ~(CCM_CSCMR1_SAI1_CLK_SEL_MASK))
  362. | CCM_CSCMR1_SAI1_CLK_SEL(2); // &0x03 // (0,1,2): PLL3PFD0, PLL5, PLL4
  363. CCM_CS1CDR = (CCM_CS1CDR & ~(CCM_CS1CDR_SAI1_CLK_PRED_MASK | CCM_CS1CDR_SAI1_CLK_PODF_MASK))
  364. | CCM_CS1CDR_SAI1_CLK_PRED(n1-1) // &0x07
  365. | CCM_CS1CDR_SAI1_CLK_PODF(n2-1); // &0x3f
  366. IOMUXC_GPR_GPR1 = (IOMUXC_GPR_GPR1 & ~(IOMUXC_GPR_GPR1_SAI1_MCLK1_SEL_MASK))
  367. | (IOMUXC_GPR_GPR1_SAI1_MCLK_DIR | IOMUXC_GPR_GPR1_SAI1_MCLK1_SEL(0)); //Select MCLK
  368. // if either transmitter or receiver is enabled, do nothing
  369. if (I2S1_TCSR & I2S_TCSR_TE) return;
  370. if (I2S1_RCSR & I2S_RCSR_RE) return;
  371. CORE_PIN23_CONFIG = 3; //1:MCLK
  372. CORE_PIN21_CONFIG = 3; //1:RX_BCLK
  373. CORE_PIN20_CONFIG = 3; //1:RX_SYNC
  374. // CORE_PIN6_CONFIG = 3; //1:TX_DATA0
  375. // CORE_PIN7_CONFIG = 3; //1:RX_DATA0
  376. int rsync = 0;
  377. int tsync = 1;
  378. I2S1_TMR = 0;
  379. //I2S1_TCSR = (1<<25); //Reset
  380. I2S1_TCR1 = I2S_TCR1_RFW(1);
  381. I2S1_TCR2 = I2S_TCR2_SYNC(tsync) | I2S_TCR2_BCP // sync=0; tx is async;
  382. | (I2S_TCR2_BCD | I2S_TCR2_DIV((1)) | I2S_TCR2_MSEL(1));
  383. I2S1_TCR3 = I2S_TCR3_TCE;
  384. I2S1_TCR4 = I2S_TCR4_FRSZ((2-1)) | I2S_TCR4_SYWD((32-1)) | I2S_TCR4_MF | I2S_TCR4_FSD | I2S_TCR4_FSE | I2S_TCR4_FSP;
  385. I2S1_TCR5 = I2S_TCR5_WNW((32-1)) | I2S_TCR5_W0W((32-1)) | I2S_TCR5_FBT((32-1));
  386. I2S1_RMR = 0;
  387. //I2S1_RCSR = (1<<25); //Reset
  388. I2S1_RCR1 = I2S_RCR1_RFW(1);
  389. I2S1_RCR2 = I2S_RCR2_SYNC(rsync) | I2S_RCR2_BCP // sync=0; rx is async;
  390. | (I2S_RCR2_BCD | I2S_RCR2_DIV((1)) | I2S_RCR2_MSEL(1));
  391. I2S1_RCR3 = I2S_RCR3_RCE;
  392. I2S1_RCR4 = I2S_RCR4_FRSZ((2-1)) | I2S_RCR4_SYWD((32-1)) | I2S_RCR4_MF | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
  393. I2S1_RCR5 = I2S_RCR5_WNW((32-1)) | I2S_RCR5_W0W((32-1)) | I2S_RCR5_FBT((32-1));
  394. #endif
  395. }
  396. /******************************************************************/
  397. void AudioOutputI2Sslave::begin(void)
  398. {
  399. dma.begin(true); // Allocate the DMA channel first
  400. //pinMode(2, OUTPUT);
  401. block_left_1st = NULL;
  402. block_right_1st = NULL;
  403. AudioOutputI2Sslave::config_i2s();
  404. #if defined(KINETISK)
  405. CORE_PIN22_CONFIG = PORT_PCR_MUX(6); // pin 22, PTC1, I2S0_TXD0
  406. dma.TCD->SADDR = i2s_tx_buffer;
  407. dma.TCD->SOFF = 2;
  408. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  409. dma.TCD->NBYTES_MLNO = 2;
  410. dma.TCD->SLAST = -sizeof(i2s_tx_buffer);
  411. dma.TCD->DADDR = (void *)((uint32_t)&I2S0_TDR0 + 2);
  412. dma.TCD->DOFF = 0;
  413. dma.TCD->CITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  414. dma.TCD->DLASTSGA = 0;
  415. dma.TCD->BITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  416. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  417. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_TX);
  418. I2S0_TCSR = I2S_TCSR_SR;
  419. I2S0_TCSR = I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE;
  420. #elif 0 && ( defined(__IMXRT1052__) || defined(__IMXRT1062__) )
  421. #if defined(SAI1)
  422. CORE_PIN6_CONFIG = 3; //1:TX_DATA0
  423. //CORE_PIN7_CONFIG = 3; //1:RX_DATA0
  424. #elif defined(SAI2)
  425. CORE_PIN2_CONFIG = 2; //2:TX_DATA0
  426. //CORE_PIN33_CONFIG = 2; //2:RX_DATA0
  427. #endif
  428. dma.TCD->SADDR = i2s_tx_buffer;
  429. dma.TCD->SOFF = 2;
  430. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  431. dma.TCD->NBYTES_MLNO = 2;
  432. dma.TCD->SLAST = -sizeof(i2s_tx_buffer);
  433. dma.TCD->DADDR = (void *)&i2s->TX.DR16[1];
  434. dma.TCD->DOFF = 0;
  435. dma.TCD->CITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  436. dma.TCD->DLASTSGA = 0;
  437. dma.TCD->BITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  438. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_SAI2_TX);
  439. #endif
  440. update_responsibility = update_setup();
  441. dma.enable();
  442. dma.attachInterrupt(isr);
  443. }
  444. void AudioOutputI2Sslave::config_i2s(void)
  445. {
  446. SIM_SCGC6 |= SIM_SCGC6_I2S;
  447. SIM_SCGC7 |= SIM_SCGC7_DMA;
  448. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  449. // if either transmitter or receiver is enabled, do nothing
  450. if (I2S0_TCSR & I2S_TCSR_TE) return;
  451. if (I2S0_RCSR & I2S_RCSR_RE) return;
  452. // Select input clock 0
  453. // Configure to input the bit-clock from pin, bypasses the MCLK divider
  454. I2S0_MCR = I2S_MCR_MICS(0);
  455. I2S0_MDR = 0;
  456. // configure transmitter
  457. I2S0_TMR = 0;
  458. I2S0_TCR1 = I2S_TCR1_TFW(1); // watermark at half fifo size
  459. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP;
  460. I2S0_TCR3 = I2S_TCR3_TCE;
  461. I2S0_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(31) | I2S_TCR4_MF
  462. | I2S_TCR4_FSE | I2S_TCR4_FSP;
  463. I2S0_TCR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31);
  464. // configure receiver (sync'd to transmitter clocks)
  465. I2S0_RMR = 0;
  466. I2S0_RCR1 = I2S_RCR1_RFW(1);
  467. I2S0_RCR2 = I2S_RCR2_SYNC(1) | I2S_TCR2_BCP;
  468. I2S0_RCR3 = I2S_RCR3_RCE;
  469. I2S0_RCR4 = I2S_RCR4_FRSZ(1) | I2S_RCR4_SYWD(31) | I2S_RCR4_MF
  470. | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
  471. I2S0_RCR5 = I2S_RCR5_WNW(31) | I2S_RCR5_W0W(31) | I2S_RCR5_FBT(31);
  472. // configure pin mux for 3 clock signals
  473. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)
  474. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK
  475. CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK
  476. #elif 0 && (defined(__IMXRT1052__) || defined(__IMXRT1062__) )
  477. #if defined(SAI1)
  478. i2s = ((I2S_STRUCT *)0x40384000);
  479. // if either transmitter or receiver is enabled, do nothing
  480. if (i2s->TX.CSR & I2S_TCSR_TE) return;
  481. if (i2s->RX.CSR & I2S_RCSR_RE) return;
  482. CCM_CCGR5 |= CCM_CCGR5_SAI1(CCM_CCGR_ON);
  483. /*
  484. CCM_CSCMR1 = (CCM_CSCMR1 & ~(CCM_CSCMR1_SAI1_CLK_SEL_MASK))
  485. | CCM_CSCMR1_SAI1_CLK_SEL(2); // &0x03 // (0,1,2): PLL3PFD0, PLL5, PLL4
  486. CCM_CS1CDR = (CCM_CS1CDR & ~(CCM_CS1CDR_SAI1_CLK_PRED_MASK | CCM_CS1CDR_SAI1_CLK_PODF_MASK))
  487. | CCM_CS1CDR_SAI1_CLK_PRED(n1-1) // &0x07
  488. | CCM_CS1CDR_SAI1_CLK_PODF(n2-1); // &0x3f
  489. */
  490. //TODO:
  491. IOMUXC_GPR_GPR1 = (IOMUXC_GPR_GPR1 & ~(IOMUXC_GPR_GPR1_SAI1_MCLK1_SEL_MASK | ((uint32_t)(1<<20)) ))
  492. | (IOMUXC_GPR_GPR1_SAI1_MCLK_DIR | IOMUXC_GPR_GPR1_SAI1_MCLK1_SEL(0)); //Select MCLK
  493. CORE_PIN23_CONFIG = 3; //1:MCLK
  494. CORE_PIN21_CONFIG = 3; //1:RX_BCLK
  495. CORE_PIN20_CONFIG = 3; //1:RX_SYNC
  496. int rsync = 0;
  497. int tsync = 1;
  498. #elif defined(SAI2)
  499. i2s = ((I2S_STRUCT *)0x40388000);
  500. if (i2s->TX.CSR & I2S_TCSR_TE) return;
  501. if (i2s->RX.CSR & I2S_RCSR_RE) return;
  502. CCM_CCGR5 |= CCM_CCGR5_SAI2(CCM_CCGR_ON);
  503. /*
  504. CCM_CSCMR1 = (CCM_CSCMR1 & ~(CCM_CSCMR1_SAI2_CLK_SEL_MASK))
  505. | CCM_CSCMR1_SAI2_CLK_SEL(2); // &0x03 // (0,1,2): PLL3PFD0, PLL5, PLL4,
  506. CCM_CS2CDR = (CCM_CS2CDR & ~(CCM_CS2CDR_SAI2_CLK_PRED_MASK | CCM_CS2CDR_SAI2_CLK_PODF_MASK))
  507. | CCM_CS2CDR_SAI2_CLK_PRED(n1-1) | CCM_CS2CDR_SAI2_CLK_PODF(n2-1);
  508. */
  509. //TODO:
  510. IOMUXC_GPR_GPR1 = (IOMUXC_GPR_GPR1 & ~(IOMUXC_GPR_GPR1_SAI2_MCLK3_SEL_MASK | ((uint32_t)(1<<19)) ))
  511. /*| (IOMUXC_GPR_GPR1_SAI2_MCLK_DIR*/ | IOMUXC_GPR_GPR1_SAI2_MCLK3_SEL(0); //Select MCLK
  512. CORE_PIN5_CONFIG = 2; //2:MCLK
  513. CORE_PIN4_CONFIG = 2; //2:TX_BCLK
  514. CORE_PIN3_CONFIG = 2; //2:TX_SYNC
  515. int rsync = 1;
  516. int tsync = 0;
  517. #endif
  518. // configure transmitter
  519. i2s->TX.MR = 0;
  520. i2s->TX.CR1 = I2S_TCR1_RFW(1); // watermark at half fifo size
  521. i2s->TX.CR2 = I2S_TCR2_SYNC(tsync) | I2S_TCR2_BCP;
  522. i2s->TX.CR3 = I2S_TCR3_TCE;
  523. i2s->TX.CR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(31) | I2S_TCR4_MF
  524. | I2S_TCR4_FSE | I2S_TCR4_FSP;
  525. i2s->TX.CR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31);
  526. // configure receiver
  527. i2s->RX.MR = 0;
  528. i2s->RX.CR1 = I2S_RCR1_RFW(1);
  529. i2s->RX.CR2 = I2S_RCR2_SYNC(rsync) | I2S_TCR2_BCP;
  530. i2s->RX.CR3 = I2S_RCR3_RCE;
  531. i2s->RX.CR4 = I2S_RCR4_FRSZ(1) | I2S_RCR4_SYWD(31) | I2S_RCR4_MF
  532. | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
  533. i2s->RX.CR5 = I2S_RCR5_WNW(31) | I2S_RCR5_W0W(31) | I2S_RCR5_FBT(31);
  534. #endif
  535. }