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

output_pt8211.cpp 18KB

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