Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

output_pt8211.cpp 18KB

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