選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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