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.

563 lines
18KB

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