Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

592 lines
18KB

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