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.

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