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.

654 lines
21KB

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