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_i2s.cpp 18KB

10 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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 12
  279. #define MCLK_DIV 17
  280. #define MCLK_SRC 1
  281. #elif F_CPU == 240000000
  282. #define MCLK_MULT 2
  283. #define MCLK_DIV 85
  284. #define MCLK_SRC 0
  285. #elif F_CPU == 256000000
  286. #define MCLK_MULT 12
  287. #define MCLK_DIV 17
  288. #define MCLK_SRC 1
  289. #elif F_CPU == 16000000
  290. #define MCLK_MULT 12
  291. #define MCLK_DIV 17
  292. #else
  293. #error "This CPU Clock Speed is not supported by the Audio library";
  294. #endif
  295. #ifndef MCLK_SRC
  296. #if F_CPU >= 20000000
  297. #define MCLK_SRC 3 // the PLL
  298. #else
  299. #define MCLK_SRC 0 // system clock
  300. #endif
  301. #endif
  302. #endif
  303. void AudioOutputI2S::config_i2s(void)
  304. {
  305. #if defined(KINETISK) || defined(KINETISL)
  306. SIM_SCGC6 |= SIM_SCGC6_I2S;
  307. SIM_SCGC7 |= SIM_SCGC7_DMA;
  308. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  309. // if either transmitter or receiver is enabled, do nothing
  310. if (I2S0_TCSR & I2S_TCSR_TE) return;
  311. if (I2S0_RCSR & I2S_RCSR_RE) return;
  312. // enable MCLK output
  313. I2S0_MCR = I2S_MCR_MICS(MCLK_SRC) | I2S_MCR_MOE;
  314. while (I2S0_MCR & I2S_MCR_DUF) ;
  315. I2S0_MDR = I2S_MDR_FRACT((MCLK_MULT-1)) | I2S_MDR_DIVIDE((MCLK_DIV-1));
  316. // configure transmitter
  317. I2S0_TMR = 0;
  318. I2S0_TCR1 = I2S_TCR1_TFW(1); // watermark at half fifo size
  319. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP | I2S_TCR2_MSEL(1)
  320. | I2S_TCR2_BCD | I2S_TCR2_DIV(1);
  321. I2S0_TCR3 = I2S_TCR3_TCE;
  322. I2S0_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(31) | I2S_TCR4_MF
  323. | I2S_TCR4_FSE | I2S_TCR4_FSP | I2S_TCR4_FSD;
  324. I2S0_TCR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31);
  325. // configure receiver (sync'd to transmitter clocks)
  326. I2S0_RMR = 0;
  327. I2S0_RCR1 = I2S_RCR1_RFW(1);
  328. I2S0_RCR2 = I2S_RCR2_SYNC(1) | I2S_TCR2_BCP | I2S_RCR2_MSEL(1)
  329. | I2S_RCR2_BCD | I2S_RCR2_DIV(1);
  330. I2S0_RCR3 = I2S_RCR3_RCE;
  331. I2S0_RCR4 = I2S_RCR4_FRSZ(1) | I2S_RCR4_SYWD(31) | I2S_RCR4_MF
  332. | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
  333. I2S0_RCR5 = I2S_RCR5_WNW(31) | I2S_RCR5_W0W(31) | I2S_RCR5_FBT(31);
  334. // configure pin mux for 3 clock signals
  335. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)
  336. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK
  337. CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK
  338. #elif defined(__IMXRT1062__)
  339. CCM_CCGR5 |= CCM_CCGR5_SAI1(CCM_CCGR_ON);
  340. // if either transmitter or receiver is enabled, do nothing
  341. if (I2S1_TCSR & I2S_TCSR_TE) return;
  342. if (I2S1_RCSR & I2S_RCSR_RE) return;
  343. //PLL:
  344. int fs = AUDIO_SAMPLE_RATE_EXACT;
  345. // PLL between 27*24 = 648MHz und 54*24=1296MHz
  346. int n1 = 4; //SAI prescaler 4 => (n1*n2) = multiple of 4
  347. int n2 = 1 + (24000000 * 27) / (fs * 256 * n1);
  348. double C = ((double)fs * 256 * n1 * n2) / 24000000;
  349. int c0 = C;
  350. int c2 = 10000;
  351. int c1 = C * c2 - (c0 * c2);
  352. set_audioClock(c0, c1, c2);
  353. // clear SAI1_CLK register locations
  354. CCM_CSCMR1 = (CCM_CSCMR1 & ~(CCM_CSCMR1_SAI1_CLK_SEL_MASK))
  355. | CCM_CSCMR1_SAI1_CLK_SEL(2); // &0x03 // (0,1,2): PLL3PFD0, PLL5, PLL4
  356. CCM_CS1CDR = (CCM_CS1CDR & ~(CCM_CS1CDR_SAI1_CLK_PRED_MASK | CCM_CS1CDR_SAI1_CLK_PODF_MASK))
  357. | CCM_CS1CDR_SAI1_CLK_PRED(n1-1) // &0x07
  358. | CCM_CS1CDR_SAI1_CLK_PODF(n2-1); // &0x3f
  359. // Select MCLK
  360. IOMUXC_GPR_GPR1 = (IOMUXC_GPR_GPR1
  361. & ~(IOMUXC_GPR_GPR1_SAI1_MCLK1_SEL_MASK))
  362. | (IOMUXC_GPR_GPR1_SAI1_MCLK_DIR | IOMUXC_GPR_GPR1_SAI1_MCLK1_SEL(0));
  363. CORE_PIN23_CONFIG = 3; //1:MCLK
  364. CORE_PIN21_CONFIG = 3; //1:RX_BCLK
  365. CORE_PIN20_CONFIG = 3; //1:RX_SYNC
  366. int rsync = 0;
  367. int tsync = 1;
  368. I2S1_TMR = 0;
  369. //I2S1_TCSR = (1<<25); //Reset
  370. I2S1_TCR1 = I2S_TCR1_RFW(1);
  371. I2S1_TCR2 = I2S_TCR2_SYNC(tsync) | I2S_TCR2_BCP // sync=0; tx is async;
  372. | (I2S_TCR2_BCD | I2S_TCR2_DIV((1)) | I2S_TCR2_MSEL(1));
  373. I2S1_TCR3 = I2S_TCR3_TCE;
  374. I2S1_TCR4 = I2S_TCR4_FRSZ((2-1)) | I2S_TCR4_SYWD((32-1)) | I2S_TCR4_MF
  375. | I2S_TCR4_FSD | I2S_TCR4_FSE | I2S_TCR4_FSP;
  376. I2S1_TCR5 = I2S_TCR5_WNW((32-1)) | I2S_TCR5_W0W((32-1)) | I2S_TCR5_FBT((32-1));
  377. I2S1_RMR = 0;
  378. //I2S1_RCSR = (1<<25); //Reset
  379. I2S1_RCR1 = I2S_RCR1_RFW(1);
  380. I2S1_RCR2 = I2S_RCR2_SYNC(rsync) | I2S_RCR2_BCP // sync=0; rx is async;
  381. | (I2S_RCR2_BCD | I2S_RCR2_DIV((1)) | I2S_RCR2_MSEL(1));
  382. I2S1_RCR3 = I2S_RCR3_RCE;
  383. I2S1_RCR4 = I2S_RCR4_FRSZ((2-1)) | I2S_RCR4_SYWD((32-1)) | I2S_RCR4_MF
  384. | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
  385. I2S1_RCR5 = I2S_RCR5_WNW((32-1)) | I2S_RCR5_W0W((32-1)) | I2S_RCR5_FBT((32-1));
  386. #endif
  387. }
  388. /******************************************************************/
  389. void AudioOutputI2Sslave::begin(void)
  390. {
  391. dma.begin(true); // Allocate the DMA channel first
  392. block_left_1st = NULL;
  393. block_right_1st = NULL;
  394. AudioOutputI2Sslave::config_i2s();
  395. #if defined(KINETISK)
  396. CORE_PIN22_CONFIG = PORT_PCR_MUX(6); // pin 22, PTC1, I2S0_TXD0
  397. dma.TCD->SADDR = i2s_tx_buffer;
  398. dma.TCD->SOFF = 2;
  399. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  400. dma.TCD->NBYTES_MLNO = 2;
  401. dma.TCD->SLAST = -sizeof(i2s_tx_buffer);
  402. dma.TCD->DADDR = (void *)((uint32_t)&I2S0_TDR0 + 2);
  403. dma.TCD->DOFF = 0;
  404. dma.TCD->CITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  405. dma.TCD->DLASTSGA = 0;
  406. dma.TCD->BITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  407. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  408. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_TX);
  409. dma.enable();
  410. I2S0_TCSR = I2S_TCSR_SR;
  411. I2S0_TCSR = I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE;
  412. #elif defined(__IMXRT1062__)
  413. CORE_PIN7_CONFIG = 3; //1:TX_DATA0
  414. dma.TCD->SADDR = i2s_tx_buffer;
  415. dma.TCD->SOFF = 2;
  416. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  417. dma.TCD->NBYTES_MLNO = 2;
  418. dma.TCD->SLAST = -sizeof(i2s_tx_buffer);
  419. dma.TCD->DOFF = 0;
  420. dma.TCD->CITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  421. dma.TCD->DLASTSGA = 0;
  422. dma.TCD->BITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  423. dma.TCD->DADDR = (void *)((uint32_t)&I2S1_TDR0 + 2);
  424. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  425. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_SAI1_TX);
  426. dma.enable();
  427. I2S1_RCSR |= I2S_RCSR_RE | I2S_RCSR_BCE;
  428. I2S1_TCSR = I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE;
  429. #endif
  430. update_responsibility = update_setup();
  431. dma.attachInterrupt(isr);
  432. }
  433. void AudioOutputI2Sslave::config_i2s(void)
  434. {
  435. #if defined(KINETISK)
  436. SIM_SCGC6 |= SIM_SCGC6_I2S;
  437. SIM_SCGC7 |= SIM_SCGC7_DMA;
  438. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  439. // if either transmitter or receiver is enabled, do nothing
  440. if (I2S0_TCSR & I2S_TCSR_TE) return;
  441. if (I2S0_RCSR & I2S_RCSR_RE) return;
  442. // Select input clock 0
  443. // Configure to input the bit-clock from pin, bypasses the MCLK divider
  444. I2S0_MCR = I2S_MCR_MICS(0);
  445. I2S0_MDR = 0;
  446. // configure transmitter
  447. I2S0_TMR = 0;
  448. I2S0_TCR1 = I2S_TCR1_TFW(1); // watermark at half fifo size
  449. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP;
  450. I2S0_TCR3 = I2S_TCR3_TCE;
  451. I2S0_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(31) | I2S_TCR4_MF
  452. | I2S_TCR4_FSE | I2S_TCR4_FSP;
  453. I2S0_TCR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31);
  454. // configure receiver (sync'd to transmitter clocks)
  455. I2S0_RMR = 0;
  456. I2S0_RCR1 = I2S_RCR1_RFW(1);
  457. I2S0_RCR2 = I2S_RCR2_SYNC(1) | I2S_TCR2_BCP;
  458. I2S0_RCR3 = I2S_RCR3_RCE;
  459. I2S0_RCR4 = I2S_RCR4_FRSZ(1) | I2S_RCR4_SYWD(31) | I2S_RCR4_MF
  460. | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
  461. I2S0_RCR5 = I2S_RCR5_WNW(31) | I2S_RCR5_W0W(31) | I2S_RCR5_FBT(31);
  462. // configure pin mux for 3 clock signals
  463. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)
  464. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK
  465. CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK
  466. #elif defined(__IMXRT1062__)
  467. CCM_CCGR5 |= CCM_CCGR5_SAI1(CCM_CCGR_ON);
  468. // if either transmitter or receiver is enabled, do nothing
  469. if (I2S1_TCSR & I2S_TCSR_TE) return;
  470. if (I2S1_RCSR & I2S_RCSR_RE) return;
  471. // not using MCLK in slave mode - hope that's ok?
  472. //CORE_PIN23_CONFIG = 3; // AD_B1_09 ALT3=SAI1_MCLK
  473. CORE_PIN21_CONFIG = 3; // AD_B1_11 ALT3=SAI1_RX_BCLK
  474. CORE_PIN20_CONFIG = 3; // AD_B1_10 ALT3=SAI1_RX_SYNC
  475. IOMUXC_SAI1_RX_BCLK_SELECT_INPUT = 1; // 1=GPIO_AD_B1_11_ALT3, page 868
  476. IOMUXC_SAI1_RX_SYNC_SELECT_INPUT = 1; // 1=GPIO_AD_B1_10_ALT3, page 872
  477. // configure transmitter
  478. I2S1_TMR = 0;
  479. I2S1_TCR1 = I2S_TCR1_RFW(1); // watermark at half fifo size
  480. I2S1_TCR2 = I2S_TCR2_SYNC(1) | I2S_TCR2_BCP;
  481. I2S1_TCR3 = I2S_TCR3_TCE;
  482. I2S1_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(31) | I2S_TCR4_MF
  483. | I2S_TCR4_FSE | I2S_TCR4_FSP | I2S_RCR4_FSD;
  484. I2S1_TCR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31);
  485. // configure receiver
  486. I2S1_RMR = 0;
  487. I2S1_RCR1 = I2S_RCR1_RFW(1);
  488. I2S1_RCR2 = I2S_RCR2_SYNC(0) | I2S_TCR2_BCP;
  489. I2S1_RCR3 = I2S_RCR3_RCE;
  490. I2S1_RCR4 = I2S_RCR4_FRSZ(1) | I2S_RCR4_SYWD(31) | I2S_RCR4_MF
  491. | I2S_RCR4_FSE | I2S_RCR4_FSP;
  492. I2S1_RCR5 = I2S_RCR5_WNW(31) | I2S_RCR5_W0W(31) | I2S_RCR5_FBT(31);
  493. #endif
  494. }