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