Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

output_i2s.cpp 19KB

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