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.

560 satır
17KB

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