選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

821 行
25KB

  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)
  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)
  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. config_i2s();
  518. CORE_PIN22_CONFIG = PORT_PCR_MUX(6); // pin 22, PTC1, I2S0_TXD0
  519. //configure both DMA channels
  520. dma1.sourceBuffer(i2s_tx_buffer1, sizeof(i2s_tx_buffer1));
  521. dma1.CFG->DAR = (void *)((uint32_t)&I2S0_TDR0);
  522. dma1.CFG->DCR = (dma1.CFG->DCR & 0xF0F0F0FF) | DMA_DCR_DSIZE(2);
  523. dma1.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_TX);
  524. dma1.interruptAtCompletion();
  525. dma1.disableOnCompletion();
  526. dma1.attachInterrupt(isr1);
  527. dma2.sourceBuffer(i2s_tx_buffer2, sizeof(i2s_tx_buffer2));
  528. dma2.CFG->DAR = dma1.CFG->DAR;
  529. dma2.CFG->DCR = dma1.CFG->DCR;
  530. dma2.interruptAtCompletion();
  531. dma2.disableOnCompletion();
  532. dma2.attachInterrupt(isr2);
  533. update_responsibility = update_setup();
  534. dma1.enable();
  535. I2S0_TCSR = I2S_TCSR_SR;
  536. I2S0_TCSR = I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FWDE;
  537. }
  538. void AudioOutputI2S::config_i2s(void)
  539. {
  540. SIM_SCGC6 |= SIM_SCGC6_I2S;//Enable I2S periphal
  541. // enable MCLK, 16MHZ
  542. I2S0_MCR = I2S_MCR_MICS(0) | I2S_MCR_MOE;
  543. //MDR is not available on Teensy LC
  544. // configure transmitter
  545. I2S0_TMR = 0;
  546. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP | I2S_TCR2_MSEL(1) | I2S_TCR2_BCD | I2S_TCR2_DIV(16);
  547. I2S0_TCR3 = I2S_TCR3_TCE;
  548. I2S0_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(15) | I2S_TCR4_MF | I2S_TCR4_FSE | I2S_TCR4_FSP | I2S_TCR4_FSD;
  549. I2S0_TCR5 = I2S_TCR5_WNW(15) | I2S_TCR5_W0W(15) | I2S_TCR5_FBT(15);
  550. // configure receiver (sync'd to transmitter clocks)
  551. I2S0_RMR = 0;
  552. I2S0_RCR2 = I2S_RCR2_SYNC(1) | I2S_TCR2_BCP;
  553. I2S0_RCR3 = I2S_RCR3_RCE;
  554. I2S0_RCR4 = I2S_RCR4_FRSZ(1) | I2S_RCR4_SYWD(15) | I2S_RCR4_MF | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
  555. I2S0_RCR5 = I2S_RCR5_WNW(15) | I2S_RCR5_W0W(15) | I2S_RCR5_FBT(15);
  556. // configure pin mux
  557. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)
  558. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK
  559. //No Mclk here - it would be 16MHz
  560. //CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK
  561. }
  562. void AudioOutputI2S::update(void)
  563. {
  564. if (!block_left) block_left = receiveReadOnly(0);// input 0 = left channel
  565. if (!block_right) block_right = receiveReadOnly(1);// input 1 = right channel
  566. }
  567. inline __attribute__((always_inline, hot))
  568. static void interleave(const int16_t *dest,const audio_block_t *block_left, const audio_block_t *block_right, const size_t offset)
  569. {
  570. //return;
  571. uint32_t *p = (uint32_t*)dest;
  572. uint32_t *end = p + NUM_SAMPLES;
  573. if (block_left != nullptr && block_right != nullptr) {
  574. uint16_t *l = (uint16_t*)&block_left->data[offset];
  575. uint16_t *r = (uint16_t*)&block_right->data[offset];
  576. do {
  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. *p++ = (((uint32_t)(*l++)) << 16) | (uint32_t)(*r++);
  581. } while (p < end);
  582. return;
  583. }
  584. if (block_left != nullptr) {
  585. uint16_t *l = (uint16_t*)&block_left->data[offset];
  586. do {
  587. *p++ = (uint32_t)(*l++) << 16;
  588. *p++ = (uint32_t)(*l++) << 16;
  589. *p++ = (uint32_t)(*l++) << 16;
  590. *p++ = (uint32_t)(*l++) << 16;
  591. } while (p < end);
  592. return;
  593. }
  594. if (block_right != nullptr) {
  595. uint16_t *r = (uint16_t*)&block_right->data[offset];
  596. do {
  597. *p++ =(uint32_t)(*r++);
  598. *p++ =(uint32_t)(*r++);
  599. *p++ =(uint32_t)(*r++);
  600. *p++ =(uint32_t)(*r++);
  601. } while (p < end);
  602. return;
  603. }
  604. do {
  605. *p++ = 0;
  606. *p++ = 0;
  607. } while (p < end);
  608. }
  609. void AudioOutputI2S::isr1(void)
  610. { //DMA Channel 1 Interrupt
  611. //Start Channel 2:
  612. dma2.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_TX);
  613. dma2.enable();
  614. //Reset & Copy Data Channel 1
  615. dma1.clearInterrupt();
  616. dma1.sourceBuffer(i2s_tx_buffer1, sizeof(i2s_tx_buffer1));
  617. interleave(&i2s_tx_buffer1[0], AudioOutputI2S::block_left, AudioOutputI2S::block_right, 0);
  618. }
  619. void __attribute__((interrupt("IRQ"))) AudioOutputI2S::isr2(void)
  620. { //DMA Channel 2 Interrupt
  621. //Start Channel 1:
  622. dma1.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_TX);
  623. dma1.enable();
  624. //Reset & Copy Data Channel 2
  625. dma2.clearInterrupt();
  626. dma2.sourceBuffer(i2s_tx_buffer2, sizeof(i2s_tx_buffer2));
  627. audio_block_t *block_left = AudioOutputI2S::block_left;
  628. audio_block_t *block_right = AudioOutputI2S::block_right;
  629. interleave(&i2s_tx_buffer2[0], block_left, block_right, NUM_SAMPLES);
  630. if (block_left) AudioStream::release(block_left);
  631. if (block_right) AudioStream::release(block_right);
  632. AudioOutputI2S::block_left = nullptr;
  633. AudioOutputI2S::block_right = nullptr;
  634. if (AudioOutputI2S::update_responsibility) AudioStream::update_all();
  635. }
  636. void AudioOutputI2Sslave::begin(void)
  637. {
  638. memset(i2s_tx_buffer1, 0, sizeof( i2s_tx_buffer1 ) );
  639. memset(i2s_tx_buffer2, 0, sizeof( i2s_tx_buffer2 ) );
  640. dma1.begin(true); // Allocate the DMA channels first
  641. dma2.begin(true);
  642. config_i2s();
  643. CORE_PIN22_CONFIG = PORT_PCR_MUX(6); // pin 22, PTC1, I2S0_TXD0
  644. //configure both DMA channels
  645. dma1.sourceBuffer(i2s_tx_buffer1, sizeof(i2s_tx_buffer1));
  646. dma1.CFG->DAR = (void *)((uint32_t)&I2S0_TDR0 + 2);
  647. dma1.CFG->DCR = (dma1.CFG->DCR & 0xF0F0F0FF) | DMA_DCR_DSIZE(2);
  648. dma1.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_TX);
  649. dma1.interruptAtCompletion();
  650. dma1.disableOnCompletion();
  651. dma1.attachInterrupt(isr1);
  652. dma2.sourceBuffer(i2s_tx_buffer2, sizeof(i2s_tx_buffer2));
  653. dma2.CFG->DAR = dma1.CFG->DAR;
  654. dma2.CFG->DCR = dma1.CFG->DCR;
  655. dma2.interruptAtCompletion();
  656. dma2.disableOnCompletion();
  657. dma2.attachInterrupt(isr2);
  658. update_responsibility = update_setup();
  659. dma1.enable();
  660. I2S0_TCSR = I2S_TCSR_SR;
  661. I2S0_TCSR = I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FWDE;
  662. }
  663. void AudioOutputI2Sslave::config_i2s(void)
  664. {
  665. SIM_SCGC6 |= SIM_SCGC6_I2S;//Enable I2S periphal
  666. // enable MCLK, 16MHZ
  667. I2S0_MCR = I2S_MCR_MICS(1) | I2S_MCR_MOE;
  668. //MDR is not available on Teensy LC
  669. // configure transmitter
  670. I2S0_TMR = 0;
  671. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP;
  672. I2S0_TCR3 = I2S_TCR3_TCE;
  673. I2S0_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(31) | I2S_TCR4_MF | I2S_TCR4_FSE | I2S_TCR4_FSP;
  674. I2S0_TCR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31);
  675. // configure receiver (sync'd to transmitter clocks)
  676. I2S0_RMR = 0;
  677. I2S0_RCR2 = I2S_RCR2_SYNC(1) | I2S_TCR2_BCP;
  678. I2S0_RCR3 = I2S_RCR3_RCE;
  679. I2S0_RCR4 = I2S_RCR4_FRSZ(1) | I2S_RCR4_SYWD(31) | I2S_RCR4_MF | I2S_RCR4_FSE | I2S_RCR4_FSP;
  680. I2S0_RCR5 = I2S_RCR5_WNW(31) | I2S_RCR5_W0W(31) | I2S_RCR5_FBT(31);
  681. // configure pin mux
  682. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)
  683. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK
  684. CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK !!16MHz!!
  685. }
  686. #endif