Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

10 роки тому
5 роки тому
5 роки тому
10 роки тому
10 роки тому
5 роки тому
5 роки тому
5 роки тому
10 роки тому
9 роки тому
8 роки тому
9 роки тому
8 роки тому
8 роки тому
8 роки тому
8 роки тому
8 роки тому
5 роки тому
5 роки тому
10 роки тому
10 роки тому
10 роки тому
5 роки тому
10 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. #if !defined(KINETISL)
  29. #include "memcpy_audio.h"
  30. // high-level explanation of how this I2S & DMA code works:
  31. // https://forum.pjrc.com/threads/65229?p=263104&viewfull=1#post263104
  32. audio_block_t * AudioOutputI2S::block_left_1st = NULL;
  33. audio_block_t * AudioOutputI2S::block_right_1st = NULL;
  34. audio_block_t * AudioOutputI2S::block_left_2nd = NULL;
  35. audio_block_t * AudioOutputI2S::block_right_2nd = NULL;
  36. uint16_t AudioOutputI2S::block_left_offset = 0;
  37. uint16_t AudioOutputI2S::block_right_offset = 0;
  38. bool AudioOutputI2S::update_responsibility = false;
  39. DMAChannel AudioOutputI2S::dma(false);
  40. DMAMEM __attribute__((aligned(32))) static uint32_t i2s_tx_buffer[AUDIO_BLOCK_SAMPLES];
  41. #if defined(__IMXRT1062__)
  42. #include "utility/imxrt_hw.h"
  43. #endif
  44. void AudioOutputI2S::begin(void)
  45. {
  46. dma.begin(true); // Allocate the DMA channel first
  47. block_left_1st = NULL;
  48. block_right_1st = NULL;
  49. config_i2s();
  50. #if defined(KINETISK)
  51. CORE_PIN22_CONFIG = PORT_PCR_MUX(6); // pin 22, PTC1, I2S0_TXD0
  52. dma.TCD->SADDR = i2s_tx_buffer;
  53. dma.TCD->SOFF = 2;
  54. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  55. dma.TCD->NBYTES_MLNO = 2;
  56. dma.TCD->SLAST = -sizeof(i2s_tx_buffer);
  57. dma.TCD->DADDR = (void *)((uint32_t)&I2S0_TDR0 + 2);
  58. dma.TCD->DOFF = 0;
  59. dma.TCD->CITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  60. dma.TCD->DLASTSGA = 0;
  61. dma.TCD->BITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  62. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  63. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_TX);
  64. dma.enable();
  65. I2S0_TCSR = I2S_TCSR_SR;
  66. I2S0_TCSR = I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE;
  67. #elif defined(__IMXRT1062__)
  68. CORE_PIN7_CONFIG = 3; //1:TX_DATA0
  69. dma.TCD->SADDR = i2s_tx_buffer;
  70. dma.TCD->SOFF = 2;
  71. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  72. dma.TCD->NBYTES_MLNO = 2;
  73. dma.TCD->SLAST = -sizeof(i2s_tx_buffer);
  74. dma.TCD->DOFF = 0;
  75. dma.TCD->CITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  76. dma.TCD->DLASTSGA = 0;
  77. dma.TCD->BITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  78. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  79. dma.TCD->DADDR = (void *)((uint32_t)&I2S1_TDR0 + 2);
  80. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_SAI1_TX);
  81. dma.enable();
  82. I2S1_RCSR |= I2S_RCSR_RE | I2S_RCSR_BCE;
  83. I2S1_TCSR = I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE;
  84. #endif
  85. update_responsibility = update_setup();
  86. dma.attachInterrupt(isr);
  87. }
  88. void AudioOutputI2S::isr(void)
  89. {
  90. #if defined(KINETISK) || defined(__IMXRT1062__)
  91. int16_t *dest;
  92. audio_block_t *blockL, *blockR;
  93. uint32_t saddr, offsetL, offsetR;
  94. saddr = (uint32_t)(dma.TCD->SADDR);
  95. dma.clearInterrupt();
  96. if (saddr < (uint32_t)i2s_tx_buffer + sizeof(i2s_tx_buffer) / 2) {
  97. // DMA is transmitting the first half of the buffer
  98. // so we must fill the second half
  99. dest = (int16_t *)&i2s_tx_buffer[AUDIO_BLOCK_SAMPLES/2];
  100. if (AudioOutputI2S::update_responsibility) AudioStream::update_all();
  101. } else {
  102. // DMA is transmitting the second half of the buffer
  103. // so we must fill the first half
  104. dest = (int16_t *)i2s_tx_buffer;
  105. }
  106. blockL = AudioOutputI2S::block_left_1st;
  107. blockR = AudioOutputI2S::block_right_1st;
  108. offsetL = AudioOutputI2S::block_left_offset;
  109. offsetR = AudioOutputI2S::block_right_offset;
  110. if (blockL && blockR) {
  111. memcpy_tointerleaveLR(dest, blockL->data + offsetL, blockR->data + offsetR);
  112. offsetL += AUDIO_BLOCK_SAMPLES / 2;
  113. offsetR += AUDIO_BLOCK_SAMPLES / 2;
  114. } else if (blockL) {
  115. memcpy_tointerleaveL(dest, blockL->data + offsetL);
  116. offsetL += AUDIO_BLOCK_SAMPLES / 2;
  117. } else if (blockR) {
  118. memcpy_tointerleaveR(dest, blockR->data + offsetR);
  119. offsetR += AUDIO_BLOCK_SAMPLES / 2;
  120. } else {
  121. memset(dest,0,AUDIO_BLOCK_SAMPLES * 2);
  122. }
  123. arm_dcache_flush_delete(dest, sizeof(i2s_tx_buffer) / 2 );
  124. if (offsetL < AUDIO_BLOCK_SAMPLES) {
  125. AudioOutputI2S::block_left_offset = offsetL;
  126. } else {
  127. AudioOutputI2S::block_left_offset = 0;
  128. AudioStream::release(blockL);
  129. AudioOutputI2S::block_left_1st = AudioOutputI2S::block_left_2nd;
  130. AudioOutputI2S::block_left_2nd = NULL;
  131. }
  132. if (offsetR < AUDIO_BLOCK_SAMPLES) {
  133. AudioOutputI2S::block_right_offset = offsetR;
  134. } else {
  135. AudioOutputI2S::block_right_offset = 0;
  136. AudioStream::release(blockR);
  137. AudioOutputI2S::block_right_1st = AudioOutputI2S::block_right_2nd;
  138. AudioOutputI2S::block_right_2nd = NULL;
  139. }
  140. #else
  141. const int16_t *src, *end;
  142. int16_t *dest;
  143. audio_block_t *block;
  144. uint32_t saddr, offset;
  145. saddr = (uint32_t)(dma.CFG->SAR);
  146. dma.clearInterrupt();
  147. if (saddr < (uint32_t)i2s_tx_buffer + sizeof(i2s_tx_buffer) / 2) {
  148. // DMA is transmitting the first half of the buffer
  149. // so we must fill the second half
  150. dest = (int16_t *)&i2s_tx_buffer[AUDIO_BLOCK_SAMPLES/2];
  151. end = (int16_t *)&i2s_tx_buffer[AUDIO_BLOCK_SAMPLES];
  152. if (AudioOutputI2S::update_responsibility) AudioStream::update_all();
  153. } else {
  154. // DMA is transmitting the second half of the buffer
  155. // so we must fill the first half
  156. dest = (int16_t *)i2s_tx_buffer;
  157. end = (int16_t *)&i2s_tx_buffer[AUDIO_BLOCK_SAMPLES/2];
  158. }
  159. block = AudioOutputI2S::block_left_1st;
  160. if (block) {
  161. offset = AudioOutputI2S::block_left_offset;
  162. src = &block->data[offset];
  163. do {
  164. *dest = *src++;
  165. dest += 2;
  166. } while (dest < end);
  167. offset += AUDIO_BLOCK_SAMPLES/2;
  168. if (offset < AUDIO_BLOCK_SAMPLES) {
  169. AudioOutputI2S::block_left_offset = offset;
  170. } else {
  171. AudioOutputI2S::block_left_offset = 0;
  172. AudioStream::release(block);
  173. AudioOutputI2S::block_left_1st = AudioOutputI2S::block_left_2nd;
  174. AudioOutputI2S::block_left_2nd = NULL;
  175. }
  176. } else {
  177. do {
  178. *dest = 0;
  179. dest += 2;
  180. } while (dest < end);
  181. }
  182. dest -= AUDIO_BLOCK_SAMPLES - 1;
  183. block = AudioOutputI2S::block_right_1st;
  184. if (block) {
  185. offset = AudioOutputI2S::block_right_offset;
  186. src = &block->data[offset];
  187. do {
  188. *dest = *src++;
  189. dest += 2;
  190. } while (dest < end);
  191. offset += AUDIO_BLOCK_SAMPLES/2;
  192. if (offset < AUDIO_BLOCK_SAMPLES) {
  193. AudioOutputI2S::block_right_offset = offset;
  194. } else {
  195. AudioOutputI2S::block_right_offset = 0;
  196. AudioStream::release(block);
  197. AudioOutputI2S::block_right_1st = AudioOutputI2S::block_right_2nd;
  198. AudioOutputI2S::block_right_2nd = NULL;
  199. }
  200. } else {
  201. do {
  202. *dest = 0;
  203. dest += 2;
  204. } while (dest < end);
  205. }
  206. #endif
  207. }
  208. void AudioOutputI2S::update(void)
  209. {
  210. // null audio device: discard all incoming data
  211. //if (!active) return;
  212. //audio_block_t *block = receiveReadOnly();
  213. //if (block) release(block);
  214. audio_block_t *block;
  215. block = receiveReadOnly(0); // input 0 = left channel
  216. if (block) {
  217. __disable_irq();
  218. if (block_left_1st == NULL) {
  219. block_left_1st = block;
  220. block_left_offset = 0;
  221. __enable_irq();
  222. } else if (block_left_2nd == NULL) {
  223. block_left_2nd = block;
  224. __enable_irq();
  225. } else {
  226. audio_block_t *tmp = block_left_1st;
  227. block_left_1st = block_left_2nd;
  228. block_left_2nd = block;
  229. block_left_offset = 0;
  230. __enable_irq();
  231. release(tmp);
  232. }
  233. }
  234. block = receiveReadOnly(1); // input 1 = right channel
  235. if (block) {
  236. __disable_irq();
  237. if (block_right_1st == NULL) {
  238. block_right_1st = block;
  239. block_right_offset = 0;
  240. __enable_irq();
  241. } else if (block_right_2nd == NULL) {
  242. block_right_2nd = block;
  243. __enable_irq();
  244. } else {
  245. audio_block_t *tmp = block_right_1st;
  246. block_right_1st = block_right_2nd;
  247. block_right_2nd = block;
  248. block_right_offset = 0;
  249. __enable_irq();
  250. release(tmp);
  251. }
  252. }
  253. }
  254. #if defined(KINETISK) || defined(KINETISL)
  255. // MCLK needs to be 48e6 / 1088 * 256 = 11.29411765 MHz -> 44.117647 kHz sample rate
  256. //
  257. #if F_CPU == 96000000 || F_CPU == 48000000 || F_CPU == 24000000
  258. // PLL is at 96 MHz in these modes
  259. #define MCLK_MULT 2
  260. #define MCLK_DIV 17
  261. #elif F_CPU == 72000000
  262. #define MCLK_MULT 8
  263. #define MCLK_DIV 51
  264. #elif F_CPU == 120000000
  265. #define MCLK_MULT 8
  266. #define MCLK_DIV 85
  267. #elif F_CPU == 144000000
  268. #define MCLK_MULT 4
  269. #define MCLK_DIV 51
  270. #elif F_CPU == 168000000
  271. #define MCLK_MULT 8
  272. #define MCLK_DIV 119
  273. #elif F_CPU == 180000000
  274. #define MCLK_MULT 16
  275. #define MCLK_DIV 255
  276. #define MCLK_SRC 0
  277. #elif F_CPU == 192000000
  278. #define MCLK_MULT 1
  279. #define MCLK_DIV 17
  280. #elif F_CPU == 216000000
  281. #define MCLK_MULT 12
  282. #define MCLK_DIV 17
  283. #define MCLK_SRC 1
  284. #elif F_CPU == 240000000
  285. #define MCLK_MULT 2
  286. #define MCLK_DIV 85
  287. #define MCLK_SRC 0
  288. #elif F_CPU == 256000000
  289. #define MCLK_MULT 12
  290. #define MCLK_DIV 17
  291. #define MCLK_SRC 1
  292. #elif F_CPU == 16000000
  293. #define MCLK_MULT 12
  294. #define MCLK_DIV 17
  295. #else
  296. #error "This CPU Clock Speed is not supported by the Audio library";
  297. #endif
  298. #ifndef MCLK_SRC
  299. #if F_CPU >= 20000000
  300. #define MCLK_SRC 3 // the PLL
  301. #else
  302. #define MCLK_SRC 0 // system clock
  303. #endif
  304. #endif
  305. #endif
  306. void AudioOutputI2S::config_i2s(void)
  307. {
  308. #if defined(KINETISK) || defined(KINETISL)
  309. SIM_SCGC6 |= SIM_SCGC6_I2S;
  310. SIM_SCGC7 |= SIM_SCGC7_DMA;
  311. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  312. // if either transmitter or receiver is enabled, do nothing
  313. if (I2S0_TCSR & I2S_TCSR_TE) return;
  314. if (I2S0_RCSR & I2S_RCSR_RE) return;
  315. // enable MCLK output
  316. I2S0_MCR = I2S_MCR_MICS(MCLK_SRC) | I2S_MCR_MOE;
  317. while (I2S0_MCR & I2S_MCR_DUF) ;
  318. I2S0_MDR = I2S_MDR_FRACT((MCLK_MULT-1)) | I2S_MDR_DIVIDE((MCLK_DIV-1));
  319. // configure transmitter
  320. I2S0_TMR = 0;
  321. I2S0_TCR1 = I2S_TCR1_TFW(1); // watermark at half fifo size
  322. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP | I2S_TCR2_MSEL(1)
  323. | I2S_TCR2_BCD | I2S_TCR2_DIV(1);
  324. I2S0_TCR3 = I2S_TCR3_TCE;
  325. I2S0_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(31) | I2S_TCR4_MF
  326. | I2S_TCR4_FSE | I2S_TCR4_FSP | I2S_TCR4_FSD;
  327. I2S0_TCR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31);
  328. // configure receiver (sync'd to transmitter clocks)
  329. I2S0_RMR = 0;
  330. I2S0_RCR1 = I2S_RCR1_RFW(1);
  331. I2S0_RCR2 = I2S_RCR2_SYNC(1) | I2S_TCR2_BCP | I2S_RCR2_MSEL(1)
  332. | I2S_RCR2_BCD | I2S_RCR2_DIV(1);
  333. I2S0_RCR3 = I2S_RCR3_RCE;
  334. I2S0_RCR4 = I2S_RCR4_FRSZ(1) | I2S_RCR4_SYWD(31) | I2S_RCR4_MF
  335. | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
  336. I2S0_RCR5 = I2S_RCR5_WNW(31) | I2S_RCR5_W0W(31) | I2S_RCR5_FBT(31);
  337. // configure pin mux for 3 clock signals
  338. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)
  339. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK
  340. CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK
  341. #elif defined(__IMXRT1062__)
  342. CCM_CCGR5 |= CCM_CCGR5_SAI1(CCM_CCGR_ON);
  343. // if either transmitter or receiver is enabled, do nothing
  344. if (I2S1_TCSR & I2S_TCSR_TE) return;
  345. if (I2S1_RCSR & I2S_RCSR_RE) return;
  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. // Select MCLK
  363. IOMUXC_GPR_GPR1 = (IOMUXC_GPR_GPR1
  364. & ~(IOMUXC_GPR_GPR1_SAI1_MCLK1_SEL_MASK))
  365. | (IOMUXC_GPR_GPR1_SAI1_MCLK_DIR | IOMUXC_GPR_GPR1_SAI1_MCLK1_SEL(0));
  366. CORE_PIN23_CONFIG = 3; //1:MCLK
  367. CORE_PIN21_CONFIG = 3; //1:RX_BCLK
  368. CORE_PIN20_CONFIG = 3; //1:RX_SYNC
  369. int rsync = 0;
  370. int tsync = 1;
  371. I2S1_TMR = 0;
  372. //I2S1_TCSR = (1<<25); //Reset
  373. I2S1_TCR1 = I2S_TCR1_RFW(1);
  374. I2S1_TCR2 = I2S_TCR2_SYNC(tsync) | I2S_TCR2_BCP // sync=0; tx is async;
  375. | (I2S_TCR2_BCD | I2S_TCR2_DIV((1)) | I2S_TCR2_MSEL(1));
  376. I2S1_TCR3 = I2S_TCR3_TCE;
  377. I2S1_TCR4 = I2S_TCR4_FRSZ((2-1)) | I2S_TCR4_SYWD((32-1)) | I2S_TCR4_MF
  378. | I2S_TCR4_FSD | I2S_TCR4_FSE | I2S_TCR4_FSP;
  379. I2S1_TCR5 = I2S_TCR5_WNW((32-1)) | I2S_TCR5_W0W((32-1)) | I2S_TCR5_FBT((32-1));
  380. I2S1_RMR = 0;
  381. //I2S1_RCSR = (1<<25); //Reset
  382. I2S1_RCR1 = I2S_RCR1_RFW(1);
  383. I2S1_RCR2 = I2S_RCR2_SYNC(rsync) | I2S_RCR2_BCP // sync=0; rx is async;
  384. | (I2S_RCR2_BCD | I2S_RCR2_DIV((1)) | I2S_RCR2_MSEL(1));
  385. I2S1_RCR3 = I2S_RCR3_RCE;
  386. I2S1_RCR4 = I2S_RCR4_FRSZ((2-1)) | I2S_RCR4_SYWD((32-1)) | I2S_RCR4_MF
  387. | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
  388. I2S1_RCR5 = I2S_RCR5_WNW((32-1)) | I2S_RCR5_W0W((32-1)) | I2S_RCR5_FBT((32-1));
  389. #endif
  390. }
  391. /******************************************************************/
  392. void AudioOutputI2Sslave::begin(void)
  393. {
  394. dma.begin(true); // Allocate the DMA channel first
  395. block_left_1st = NULL;
  396. block_right_1st = NULL;
  397. AudioOutputI2Sslave::config_i2s();
  398. #if defined(KINETISK)
  399. CORE_PIN22_CONFIG = PORT_PCR_MUX(6); // pin 22, PTC1, I2S0_TXD0
  400. dma.TCD->SADDR = i2s_tx_buffer;
  401. dma.TCD->SOFF = 2;
  402. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  403. dma.TCD->NBYTES_MLNO = 2;
  404. dma.TCD->SLAST = -sizeof(i2s_tx_buffer);
  405. dma.TCD->DADDR = (void *)((uint32_t)&I2S0_TDR0 + 2);
  406. dma.TCD->DOFF = 0;
  407. dma.TCD->CITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  408. dma.TCD->DLASTSGA = 0;
  409. dma.TCD->BITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  410. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  411. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_TX);
  412. dma.enable();
  413. I2S0_TCSR = I2S_TCSR_SR;
  414. I2S0_TCSR = I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE;
  415. #elif defined(__IMXRT1062__)
  416. CORE_PIN7_CONFIG = 3; //1:TX_DATA0
  417. dma.TCD->SADDR = i2s_tx_buffer;
  418. dma.TCD->SOFF = 2;
  419. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  420. dma.TCD->NBYTES_MLNO = 2;
  421. dma.TCD->SLAST = -sizeof(i2s_tx_buffer);
  422. dma.TCD->DOFF = 0;
  423. dma.TCD->CITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  424. dma.TCD->DLASTSGA = 0;
  425. dma.TCD->BITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  426. dma.TCD->DADDR = (void *)((uint32_t)&I2S1_TDR0 + 2);
  427. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  428. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_SAI1_TX);
  429. dma.enable();
  430. I2S1_RCSR |= I2S_RCSR_RE | I2S_RCSR_BCE;
  431. I2S1_TCSR = I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE;
  432. #endif
  433. update_responsibility = update_setup();
  434. dma.attachInterrupt(isr);
  435. }
  436. void AudioOutputI2Sslave::config_i2s(void)
  437. {
  438. #if defined(KINETISK)
  439. SIM_SCGC6 |= SIM_SCGC6_I2S;
  440. SIM_SCGC7 |= SIM_SCGC7_DMA;
  441. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  442. // if either transmitter or receiver is enabled, do nothing
  443. if (I2S0_TCSR & I2S_TCSR_TE) return;
  444. if (I2S0_RCSR & I2S_RCSR_RE) return;
  445. // Select input clock 0
  446. // Configure to input the bit-clock from pin, bypasses the MCLK divider
  447. I2S0_MCR = I2S_MCR_MICS(0);
  448. I2S0_MDR = 0;
  449. // configure transmitter
  450. I2S0_TMR = 0;
  451. I2S0_TCR1 = I2S_TCR1_TFW(1); // watermark at half fifo size
  452. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP;
  453. I2S0_TCR3 = I2S_TCR3_TCE;
  454. I2S0_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(31) | I2S_TCR4_MF
  455. | I2S_TCR4_FSE | I2S_TCR4_FSP;
  456. I2S0_TCR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31);
  457. // configure receiver (sync'd to transmitter clocks)
  458. I2S0_RMR = 0;
  459. I2S0_RCR1 = I2S_RCR1_RFW(1);
  460. I2S0_RCR2 = I2S_RCR2_SYNC(1) | I2S_TCR2_BCP;
  461. I2S0_RCR3 = I2S_RCR3_RCE;
  462. I2S0_RCR4 = I2S_RCR4_FRSZ(1) | I2S_RCR4_SYWD(31) | I2S_RCR4_MF
  463. | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
  464. I2S0_RCR5 = I2S_RCR5_WNW(31) | I2S_RCR5_W0W(31) | I2S_RCR5_FBT(31);
  465. // configure pin mux for 3 clock signals
  466. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)
  467. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK
  468. CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK
  469. #elif defined(__IMXRT1062__)
  470. CCM_CCGR5 |= CCM_CCGR5_SAI1(CCM_CCGR_ON);
  471. // if either transmitter or receiver is enabled, do nothing
  472. if (I2S1_TCSR & I2S_TCSR_TE) return;
  473. if (I2S1_RCSR & I2S_RCSR_RE) return;
  474. // not using MCLK in slave mode - hope that's ok?
  475. //CORE_PIN23_CONFIG = 3; // AD_B1_09 ALT3=SAI1_MCLK
  476. CORE_PIN21_CONFIG = 3; // AD_B1_11 ALT3=SAI1_RX_BCLK
  477. CORE_PIN20_CONFIG = 3; // AD_B1_10 ALT3=SAI1_RX_SYNC
  478. IOMUXC_SAI1_RX_BCLK_SELECT_INPUT = 1; // 1=GPIO_AD_B1_11_ALT3, page 868
  479. IOMUXC_SAI1_RX_SYNC_SELECT_INPUT = 1; // 1=GPIO_AD_B1_10_ALT3, page 872
  480. // configure transmitter
  481. I2S1_TMR = 0;
  482. I2S1_TCR1 = I2S_TCR1_RFW(1); // watermark at half fifo size
  483. I2S1_TCR2 = I2S_TCR2_SYNC(1) | I2S_TCR2_BCP;
  484. I2S1_TCR3 = I2S_TCR3_TCE;
  485. I2S1_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(31) | I2S_TCR4_MF
  486. | I2S_TCR4_FSE | I2S_TCR4_FSP | I2S_RCR4_FSD;
  487. I2S1_TCR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31);
  488. // configure receiver
  489. I2S1_RMR = 0;
  490. I2S1_RCR1 = I2S_RCR1_RFW(1);
  491. I2S1_RCR2 = I2S_RCR2_SYNC(0) | I2S_TCR2_BCP;
  492. I2S1_RCR3 = I2S_RCR3_RCE;
  493. I2S1_RCR4 = I2S_RCR4_FRSZ(1) | I2S_RCR4_SYWD(31) | I2S_RCR4_MF
  494. | I2S_RCR4_FSE | I2S_RCR4_FSP;
  495. I2S1_RCR5 = I2S_RCR5_WNW(31) | I2S_RCR5_W0W(31) | I2S_RCR5_FBT(31);
  496. #endif
  497. }
  498. #elif defined(KINETISL)
  499. /**************************************************************************************
  500. * Teensy LC
  501. ***************************************************************************************/
  502. // added jan 2021, Frank Bösing
  503. audio_block_t * AudioOutputI2S::block_left = NULL;
  504. audio_block_t * AudioOutputI2S::block_right = NULL;
  505. bool AudioOutputI2S::update_responsibility = false;
  506. #define NUM_SAMPLES (AUDIO_BLOCK_SAMPLES / 2)
  507. DMAMEM static int16_t i2s_tx_buffer1[NUM_SAMPLES * 2];
  508. DMAMEM static int16_t i2s_tx_buffer2[NUM_SAMPLES * 2];
  509. DMAChannel AudioOutputI2S::dma1(false);
  510. DMAChannel AudioOutputI2S::dma2(false);
  511. void AudioOutputI2S::begin(void)
  512. {
  513. memset(i2s_tx_buffer1, 0, sizeof( i2s_tx_buffer1 ) );
  514. memset(i2s_tx_buffer2, 0, sizeof( i2s_tx_buffer2 ) );
  515. dma1.begin(true); // Allocate the DMA channel first
  516. dma2.begin(true);
  517. SIM_SCGC6 |= SIM_SCGC6_I2S;//Enable I2S periphal
  518. // enable MCLK, 16MHZ
  519. I2S0_MCR = I2S_MCR_MICS(1) | I2S_MCR_MOE;
  520. //MDR is not available on Teensy LC
  521. // configure transmitter
  522. I2S0_TMR = 0;
  523. I2S0_TCR1 = I2S_TCR1_TFW(0);
  524. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP | I2S_TCR2_MSEL(1) | I2S_TCR2_BCD | I2S_TCR2_DIV(16);
  525. I2S0_TCR3 = I2S_TCR3_TCE;
  526. I2S0_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(31) | I2S_TCR4_MF | I2S_TCR4_FSE | I2S_TCR4_FSP | I2S_TCR4_FSD;
  527. I2S0_TCR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31);
  528. #if 0 //TODO
  529. // configure receiver (sync'd to transmitter clocks)
  530. I2S0_RMR = 0;
  531. I2S0_RCR1 = I2S_RCR1_RFW(1);
  532. I2S0_RCR2 = I2S_RCR2_SYNC(1) | I2S_TCR2_BCP;
  533. I2S0_RCR3 = I2S_RCR3_RCE;
  534. I2S0_RCR4 = I2S_RCR4_FRSZ(1) | I2S_RCR4_SYWD(31) | I2S_RCR4_MF | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
  535. I2S0_RCR5 = I2S_RCR5_WNW(31) | I2S_RCR5_W0W(31) | I2S_RCR5_FBT(31);
  536. #endif
  537. // configure pin mux
  538. CORE_PIN22_CONFIG = PORT_PCR_MUX(6); // pin 22, PTC1, I2S0_TXD0
  539. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)
  540. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK
  541. CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK 16MHz
  542. //configure both DMA channels
  543. dma1.sourceBuffer(i2s_tx_buffer1, sizeof(i2s_tx_buffer1));
  544. dma1.CFG->DAR = (void *)((uint32_t)&I2S0_TDR0 + 2);
  545. dma1.CFG->DCR = (dma1.CFG->DCR & 0xF0F0F0FF) | DMA_DCR_DSIZE(2);
  546. dma1.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_TX);
  547. dma1.interruptAtCompletion();
  548. dma1.disableOnCompletion();
  549. dma1.attachInterrupt(isr1);
  550. dma2.sourceBuffer(i2s_tx_buffer2, sizeof(i2s_tx_buffer2));
  551. dma2.CFG->DAR = dma1.CFG->DAR;
  552. dma2.CFG->DCR = dma1.CFG->DCR;
  553. dma2.interruptAtCompletion();
  554. dma2.disableOnCompletion();
  555. dma2.attachInterrupt(isr2);
  556. update_responsibility = update_setup();
  557. dma1.enable();
  558. I2S0_TCSR = I2S_TCSR_SR;
  559. I2S0_TCSR = I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FWDE;
  560. }
  561. void AudioOutputI2S::update(void)
  562. {
  563. if (!block_left) block_left = receiveReadOnly(0);// input 0 = left channel
  564. if (!block_right) block_right = receiveReadOnly(1);// input 1 = right channel
  565. }
  566. inline __attribute__((always_inline, hot))
  567. static void interleave(const int16_t *dest,const audio_block_t *block_left, const audio_block_t *block_right, const size_t offset)
  568. {
  569. //return;
  570. uint32_t *p = (uint32_t*)dest;
  571. uint32_t *end = p + NUM_SAMPLES;
  572. if (block_left != nullptr && block_right != nullptr) {
  573. uint16_t *l = (uint16_t*)&block_left->data[offset];
  574. uint16_t *r = (uint16_t*)&block_right->data[offset];
  575. do {
  576. *p++ = (((uint32_t)(*l++)) << 16) | (uint32_t)(*r++);
  577. *p++ = (((uint32_t)(*l++)) << 16) | (uint32_t)(*r++);
  578. *p++ = (((uint32_t)(*l++)) << 16) | (uint32_t)(*r++);
  579. *p++ = (((uint32_t)(*l++)) << 16) | (uint32_t)(*r++);
  580. } while (p < end);
  581. return;
  582. }
  583. if (block_left != nullptr) {
  584. uint16_t *l = (uint16_t*)&block_left->data[offset];
  585. do {
  586. *p++ = (uint32_t)(*l++) << 16;
  587. *p++ = (uint32_t)(*l++) << 16;
  588. *p++ = (uint32_t)(*l++) << 16;
  589. *p++ = (uint32_t)(*l++) << 16;
  590. } while (p < end);
  591. return;
  592. }
  593. if (block_right != nullptr) {
  594. uint16_t *r = (uint16_t*)&block_right->data[offset];
  595. do {
  596. *p++ =(uint32_t)(*r++);
  597. *p++ =(uint32_t)(*r++);
  598. *p++ =(uint32_t)(*r++);
  599. *p++ =(uint32_t)(*r++);
  600. } while (p < end);
  601. return;
  602. }
  603. do {
  604. *p++ = 0;
  605. *p++ = 0;
  606. } while (p < end);
  607. }
  608. void AudioOutputI2S::isr1(void)
  609. { //DMA Channel 1 Interrupt
  610. //Start Channel 2:
  611. dma2.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_TX);
  612. dma2.enable();
  613. //Reset & Copy Data Channel 1
  614. dma1.clearInterrupt();
  615. dma1.sourceBuffer(i2s_tx_buffer1, sizeof(i2s_tx_buffer1));
  616. interleave(&i2s_tx_buffer1[0], AudioOutputI2S::block_left, AudioOutputI2S::block_right, 0);
  617. }
  618. void AudioOutputI2S::isr2(void)
  619. { //DMA Channel 2 Interrupt
  620. //Start Channel 1:
  621. dma1.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_TX);
  622. dma1.enable();
  623. //Reset & Copy Data Channel 2
  624. dma2.clearInterrupt();
  625. dma2.sourceBuffer(i2s_tx_buffer2, sizeof(i2s_tx_buffer2));
  626. audio_block_t *block_left = AudioOutputI2S::block_left;
  627. audio_block_t *block_right = AudioOutputI2S::block_right;
  628. interleave(&i2s_tx_buffer2[0], block_left, block_right, NUM_SAMPLES);
  629. if (block_left) AudioStream::release(block_left);
  630. if (block_right) AudioStream::release(block_right);
  631. AudioOutputI2S::block_left = nullptr;
  632. AudioOutputI2S::block_right = nullptr;
  633. if (AudioOutputI2S::update_responsibility) AudioStream::update_all();
  634. }
  635. void AudioOutputI2Sslave::begin(void)
  636. {
  637. memset(i2s_tx_buffer1, 0, sizeof( i2s_tx_buffer1 ) );
  638. memset(i2s_tx_buffer2, 0, sizeof( i2s_tx_buffer2 ) );
  639. dma1.begin(true); // Allocate the DMA channels first
  640. dma2.begin(true);
  641. SIM_SCGC6 |= SIM_SCGC6_I2S;//Enable I2S periphal
  642. // enable MCLK, 16MHZ
  643. I2S0_MCR = I2S_MCR_MICS(1) | I2S_MCR_MOE;
  644. //MDR is not available on Teensy LC
  645. // configure transmitter
  646. I2S0_TMR = 0;
  647. I2S0_TCR1 = I2S_TCR1_TFW(0);
  648. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP;
  649. I2S0_TCR3 = I2S_TCR3_TCE;
  650. I2S0_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(31) | I2S_TCR4_MF | I2S_TCR4_FSE | I2S_TCR4_FSP;
  651. I2S0_TCR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31);
  652. // configure pin mux
  653. CORE_PIN22_CONFIG = PORT_PCR_MUX(6); // pin 22, PTC1, I2S0_TXD0
  654. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)
  655. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK
  656. CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK 16MHz
  657. //configure both DMA channels
  658. dma1.sourceBuffer(i2s_tx_buffer1, sizeof(i2s_tx_buffer1));
  659. dma1.CFG->DAR = (void *)((uint32_t)&I2S0_TDR0 + 2);
  660. dma1.CFG->DCR = (dma1.CFG->DCR & 0xF0F0F0FF) | DMA_DCR_DSIZE(2);
  661. dma1.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_TX);
  662. dma1.interruptAtCompletion();
  663. dma1.disableOnCompletion();
  664. dma1.attachInterrupt(isr1);
  665. dma2.sourceBuffer(i2s_tx_buffer2, sizeof(i2s_tx_buffer2));
  666. dma2.CFG->DAR = dma1.CFG->DAR;
  667. dma2.CFG->DCR = dma1.CFG->DCR;
  668. dma2.interruptAtCompletion();
  669. dma2.disableOnCompletion();
  670. dma2.attachInterrupt(isr2);
  671. update_responsibility = update_setup();
  672. dma1.enable();
  673. I2S0_TCSR = I2S_TCSR_SR;
  674. I2S0_TCSR = I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FWDE;
  675. }
  676. #endif