You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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