Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

output_pt8211_2.cpp 14KB

4 anos atrás
4 anos atrás
4 anos atrás
3 anos atrás
4 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. #if defined(__IMXRT1052__) || defined(__IMXRT1062__)
  28. #include <Arduino.h>
  29. #include "output_pt8211_2.h"
  30. #include "memcpy_audio.h"
  31. #include "utility/imxrt_hw.h"
  32. audio_block_t * AudioOutputPT8211_2::block_left_1st = NULL;
  33. audio_block_t * AudioOutputPT8211_2::block_right_1st = NULL;
  34. audio_block_t * AudioOutputPT8211_2::block_left_2nd = NULL;
  35. audio_block_t * AudioOutputPT8211_2::block_right_2nd = NULL;
  36. uint16_t AudioOutputPT8211_2::block_left_offset = 0;
  37. uint16_t AudioOutputPT8211_2::block_right_offset = 0;
  38. bool AudioOutputPT8211_2::update_responsibility = false;
  39. #if defined(AUDIO_PT8211_OVERSAMPLING)
  40. DMAMEM __attribute__((aligned(32))) static uint32_t i2s_tx_buffer[AUDIO_BLOCK_SAMPLES*4];
  41. #else
  42. DMAMEM __attribute__((aligned(32))) static uint32_t i2s_tx_buffer[AUDIO_BLOCK_SAMPLES];
  43. #endif
  44. DMAChannel AudioOutputPT8211_2::dma(false);
  45. FLASHMEM
  46. void AudioOutputPT8211_2::begin(void)
  47. {
  48. memset(i2s_tx_buffer, 0, sizeof(i2s_tx_buffer));
  49. arm_dcache_flush_delete(i2s_tx_buffer, sizeof(i2s_tx_buffer));
  50. dma.begin(true); // Allocate the DMA channel first
  51. block_left_1st = NULL;
  52. block_right_1st = NULL;
  53. // TODO: should we set & clear the I2S_TCSR_SR bit here?
  54. config_i2s();
  55. CORE_PIN2_CONFIG = 2; //2:TX_DATA0
  56. dma.TCD->SADDR = i2s_tx_buffer;
  57. dma.TCD->SOFF = 2;
  58. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  59. dma.TCD->NBYTES_MLNO = 2;
  60. dma.TCD->SLAST = -sizeof(i2s_tx_buffer);
  61. dma.TCD->DOFF = 0;
  62. dma.TCD->CITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  63. dma.TCD->DLASTSGA = 0;
  64. dma.TCD->BITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  65. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  66. dma.TCD->DADDR = (void *)((uint32_t)&I2S2_TDR0);
  67. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_SAI2_TX);
  68. I2S2_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE;
  69. update_responsibility = update_setup();
  70. dma.attachInterrupt(isr);
  71. dma.enable();
  72. }
  73. void AudioOutputPT8211_2::isr(void)
  74. {
  75. int16_t *dest, *dest_copy;
  76. audio_block_t *blockL, *blockR;
  77. uint32_t saddr, offsetL, offsetR;
  78. saddr = (uint32_t)(dma.TCD->SADDR);
  79. dma.clearInterrupt();
  80. if (saddr < (uint32_t)i2s_tx_buffer + sizeof(i2s_tx_buffer) / 2) {
  81. // DMA is transmitting the first half of the buffer
  82. // so we must fill the second half
  83. #if defined(AUDIO_PT8211_OVERSAMPLING)
  84. dest = (int16_t *)&i2s_tx_buffer[(AUDIO_BLOCK_SAMPLES/2)*4];
  85. #else
  86. dest = (int16_t *)&i2s_tx_buffer[AUDIO_BLOCK_SAMPLES/2];
  87. #endif
  88. if (AudioOutputPT8211_2::update_responsibility) AudioStream::update_all();
  89. } else {
  90. // DMA is transmitting the second half of the buffer
  91. // so we must fill the first half
  92. dest = (int16_t *)i2s_tx_buffer;
  93. }
  94. dest_copy = dest;
  95. blockL = AudioOutputPT8211_2::block_left_1st;
  96. blockR = AudioOutputPT8211_2::block_right_1st;
  97. offsetL = AudioOutputPT8211_2::block_left_offset;
  98. offsetR = AudioOutputPT8211_2::block_right_offset;
  99. #if defined(AUDIO_PT8211_OVERSAMPLING)
  100. static int32_t oldL = 0;
  101. static int32_t oldR = 0;
  102. #endif
  103. if (blockL && blockR) {
  104. #if defined(AUDIO_PT8211_OVERSAMPLING)
  105. #if defined(AUDIO_PT8211_INTERPOLATION_LINEAR)
  106. for (int i=0; i< AUDIO_BLOCK_SAMPLES / 2; i++, offsetL++, offsetR++) {
  107. int32_t valL = blockL->data[offsetL];
  108. int32_t valR = blockR->data[offsetR];
  109. int32_t nL = (oldL+valL) >> 1;
  110. int32_t nR = (oldR+valR) >> 1;
  111. *(dest+0) = (oldL+nL) >> 1;
  112. *(dest+1) = (oldR+nR) >> 1;
  113. *(dest+2) = nL;
  114. *(dest+3) = nR;
  115. *(dest+4) = (nL+valL) >> 1;
  116. *(dest+5) = (nR+valR) >> 1;
  117. *(dest+6) = valL;
  118. *(dest+7) = valR;
  119. dest+=8;
  120. oldL = valL;
  121. oldR = valR;
  122. }
  123. #elif defined(AUDIO_PT8211_INTERPOLATION_CIC)
  124. for (int i=0; i< AUDIO_BLOCK_SAMPLES / 2; i++, offsetL++, offsetR++) {
  125. int32_t valL = blockL->data[offsetL];
  126. int32_t valR = blockR->data[offsetR];
  127. int32_t combL[3] = {0};
  128. static int32_t combLOld[2] = {0};
  129. int32_t combR[3] = {0};
  130. static int32_t combROld[2] = {0};
  131. combL[0] = valL - oldL;
  132. combR[0] = valR - oldR;
  133. combL[1] = combL[0] - combLOld[0];
  134. combR[1] = combR[0] - combROld[0];
  135. combL[2] = combL[1] - combLOld[1];
  136. combR[2] = combR[1] - combROld[1];
  137. // combL[2] now holds input val
  138. // combR[2] now holds input val
  139. oldL = valL;
  140. oldR = valR;
  141. combLOld[0] = combL[0];
  142. combROld[0] = combR[0];
  143. combLOld[1] = combL[1];
  144. combROld[1] = combR[1];
  145. for (int j = 0; j < 4; j++) {
  146. int32_t integrateL[3];
  147. int32_t integrateR[3];
  148. static int32_t integrateLOld[3] = {0};
  149. static int32_t integrateROld[3] = {0};
  150. integrateL[0] = ( (j==0) ? (combL[2]) : (0) ) + integrateLOld[0];
  151. integrateR[0] = ( (j==0) ? (combR[2]) : (0) ) + integrateROld[0];
  152. integrateL[1] = integrateL[0] + integrateLOld[1];
  153. integrateR[1] = integrateR[0] + integrateROld[1];
  154. integrateL[2] = integrateL[1] + integrateLOld[2];
  155. integrateR[2] = integrateR[1] + integrateROld[2];
  156. // integrateL[2] now holds j'th upsampled value
  157. // integrateR[2] now holds j'th upsampled value
  158. *(dest+j*2) = integrateL[2] >> 4;
  159. *(dest+j*2+1) = integrateR[2] >> 4;
  160. integrateLOld[0] = integrateL[0];
  161. integrateROld[0] = integrateR[0];
  162. integrateLOld[1] = integrateL[1];
  163. integrateROld[1] = integrateR[1];
  164. integrateLOld[2] = integrateL[2];
  165. integrateROld[2] = integrateR[2];
  166. }
  167. dest+=8;
  168. }
  169. #else
  170. #error no interpolation method defined for oversampling.
  171. #endif //defined(AUDIO_PT8211_INTERPOLATION_LINEAR)
  172. #else
  173. memcpy_tointerleaveLR(dest, blockL->data + offsetL, blockR->data + offsetR);
  174. offsetL += AUDIO_BLOCK_SAMPLES / 2;
  175. offsetR += AUDIO_BLOCK_SAMPLES / 2;
  176. #endif //defined(AUDIO_PT8211_OVERSAMPLING)
  177. } else if (blockL) {
  178. #if defined(AUDIO_PT8211_OVERSAMPLING)
  179. #if defined(AUDIO_PT8211_INTERPOLATION_LINEAR)
  180. for (int i=0; i< AUDIO_BLOCK_SAMPLES / 2; i++, offsetL++) {
  181. int32_t val = blockL->data[offsetL];
  182. int32_t n = (oldL+val) >> 1;
  183. *(dest+0) = (oldL+n) >> 1;
  184. *(dest+1) = 0;
  185. *(dest+2) = n;
  186. *(dest+3) = 0;
  187. *(dest+4) = (n+val) >> 1;
  188. *(dest+5) = 0;
  189. *(dest+6) = val;
  190. *(dest+7) = 0;
  191. dest+=8;
  192. oldL = val;
  193. }
  194. #elif defined(AUDIO_PT8211_INTERPOLATION_CIC)
  195. for (int i=0; i< AUDIO_BLOCK_SAMPLES / 2; i++, offsetL++) {
  196. int32_t valL = blockL->data[offsetL];
  197. int32_t combL[3] = {0};
  198. static int32_t combLOld[2] = {0};
  199. combL[0] = valL - oldL;
  200. combL[1] = combL[0] - combLOld[0];
  201. combL[2] = combL[1] - combLOld[1];
  202. // combL[2] now holds input val
  203. combLOld[0] = combL[0];
  204. combLOld[1] = combL[1];
  205. for (int j = 0; j < 4; j++) {
  206. int32_t integrateL[3];
  207. static int32_t integrateLOld[3] = {0};
  208. integrateL[0] = ( (j==0) ? (combL[2]) : (0) ) + integrateLOld[0];
  209. integrateL[1] = integrateL[0] + integrateLOld[1];
  210. integrateL[2] = integrateL[1] + integrateLOld[2];
  211. // integrateL[2] now holds j'th upsampled value
  212. *(dest+j*2) = integrateL[2] >> 4;
  213. integrateLOld[0] = integrateL[0];
  214. integrateLOld[1] = integrateL[1];
  215. integrateLOld[2] = integrateL[2];
  216. }
  217. // fill right channel with zeros:
  218. *(dest+1) = 0;
  219. *(dest+3) = 0;
  220. *(dest+5) = 0;
  221. *(dest+7) = 0;
  222. dest+=8;
  223. oldL = valL;
  224. }
  225. #else
  226. #error no interpolation method defined for oversampling.
  227. #endif //defined(AUDIO_PT8211_INTERPOLATION_LINEAR)
  228. #else
  229. memcpy_tointerleaveL(dest, blockL->data + offsetL);
  230. offsetL += (AUDIO_BLOCK_SAMPLES / 2);
  231. #endif //defined(AUDIO_PT8211_OVERSAMPLING)
  232. } else if (blockR) {
  233. #if defined(AUDIO_PT8211_OVERSAMPLING)
  234. #if defined(AUDIO_PT8211_INTERPOLATION_LINEAR)
  235. for (int i=0; i< AUDIO_BLOCK_SAMPLES / 2; i++, offsetR++) {
  236. int32_t val = blockR->data[offsetR];
  237. int32_t n = (oldR+val) >> 1;
  238. *(dest+0) = 0;
  239. *(dest+1) = ((oldR+n) >> 1);
  240. *(dest+2) = 0;
  241. *(dest+3) = n;
  242. *(dest+4) = 0;
  243. *(dest+5) = ((n+val) >> 1);
  244. *(dest+6) = 0;
  245. *(dest+7) = val;
  246. dest+=8;
  247. oldR = val;
  248. }
  249. #elif defined(AUDIO_PT8211_INTERPOLATION_CIC)
  250. for (int i=0; i< AUDIO_BLOCK_SAMPLES / 2; i++, offsetR++) {
  251. int32_t valR = blockR->data[offsetR];
  252. int32_t combR[3] = {0};
  253. static int32_t combROld[2] = {0};
  254. combR[0] = valR - oldR;
  255. combR[1] = combR[0] - combROld[0];
  256. combR[2] = combR[1] - combROld[1];
  257. // combR[2] now holds input val
  258. combROld[0] = combR[0];
  259. combROld[1] = combR[1];
  260. for (int j = 0; j < 4; j++) {
  261. int32_t integrateR[3];
  262. static int32_t integrateROld[3] = {0};
  263. integrateR[0] = ( (j==0) ? (combR[2]) : (0) ) + integrateROld[0];
  264. integrateR[1] = integrateR[0] + integrateROld[1];
  265. integrateR[2] = integrateR[1] + integrateROld[2];
  266. // integrateR[2] now holds j'th upsampled value
  267. *(dest+j*2+1) = integrateR[2] >> 4;
  268. integrateROld[0] = integrateR[0];
  269. integrateROld[1] = integrateR[1];
  270. integrateROld[2] = integrateR[2];
  271. }
  272. // fill left channel with zeros:
  273. *(dest+0) = 0;
  274. *(dest+2) = 0;
  275. *(dest+4) = 0;
  276. *(dest+6) = 0;
  277. dest+=8;
  278. oldR = valR;
  279. }
  280. #else
  281. #error no interpolation method defined for oversampling.
  282. #endif //defined(AUDIO_PT8211_INTERPOLATION_LINEAR)
  283. #else
  284. memcpy_tointerleaveR(dest, blockR->data + offsetR);
  285. offsetR += AUDIO_BLOCK_SAMPLES / 2;
  286. #endif //defined(AUDIO_PT8211_OVERSAMPLING)
  287. } else {
  288. #if defined(AUDIO_PT8211_OVERSAMPLING)
  289. memset(dest,0,AUDIO_BLOCK_SAMPLES*8);
  290. #else
  291. memset(dest,0,AUDIO_BLOCK_SAMPLES*2);
  292. #endif
  293. return;
  294. }
  295. arm_dcache_flush_delete(dest_copy, sizeof(i2s_tx_buffer) / 2);
  296. if (offsetL < AUDIO_BLOCK_SAMPLES) {
  297. AudioOutputPT8211_2::block_left_offset = offsetL;
  298. } else {
  299. AudioOutputPT8211_2::block_left_offset = 0;
  300. AudioStream::release(blockL);
  301. AudioOutputPT8211_2::block_left_1st = AudioOutputPT8211_2::block_left_2nd;
  302. AudioOutputPT8211_2::block_left_2nd = NULL;
  303. }
  304. if (offsetR < AUDIO_BLOCK_SAMPLES) {
  305. AudioOutputPT8211_2::block_right_offset = offsetR;
  306. } else {
  307. AudioOutputPT8211_2::block_right_offset = 0;
  308. AudioStream::release(blockR);
  309. AudioOutputPT8211_2::block_right_1st = AudioOutputPT8211_2::block_right_2nd;
  310. AudioOutputPT8211_2::block_right_2nd = NULL;
  311. }
  312. }
  313. void AudioOutputPT8211_2::update(void)
  314. {
  315. audio_block_t *block;
  316. block = receiveReadOnly(0); // input 0 = left channel
  317. if (block) {
  318. __disable_irq();
  319. if (block_left_1st == NULL) {
  320. block_left_1st = block;
  321. block_left_offset = 0;
  322. __enable_irq();
  323. } else if (block_left_2nd == NULL) {
  324. block_left_2nd = block;
  325. __enable_irq();
  326. } else {
  327. audio_block_t *tmp = block_left_1st;
  328. block_left_1st = block_left_2nd;
  329. block_left_2nd = block;
  330. block_left_offset = 0;
  331. __enable_irq();
  332. release(tmp);
  333. }
  334. }
  335. block = receiveReadOnly(1); // input 1 = right channel
  336. if (block) {
  337. __disable_irq();
  338. if (block_right_1st == NULL) {
  339. block_right_1st = block;
  340. block_right_offset = 0;
  341. __enable_irq();
  342. } else if (block_right_2nd == NULL) {
  343. block_right_2nd = block;
  344. __enable_irq();
  345. } else {
  346. audio_block_t *tmp = block_right_1st;
  347. block_right_1st = block_right_2nd;
  348. block_right_2nd = block;
  349. block_right_offset = 0;
  350. __enable_irq();
  351. release(tmp);
  352. }
  353. }
  354. }
  355. FLASHMEM
  356. void AudioOutputPT8211_2::config_i2s(void)
  357. {
  358. CCM_CCGR5 |= CCM_CCGR5_SAI2(CCM_CCGR_ON);
  359. //PLL:
  360. int fs = AUDIO_SAMPLE_RATE_EXACT;
  361. // PLL between 27*24 = 648MHz und 54*24=1296MHz
  362. int n1 = 4; //SAI prescaler 4 => (n1*n2) = multiple of 4
  363. int n2 = 1 + (24000000 * 27) / (fs * 256 * n1);
  364. double C = ((double)fs * 256 * n1 * n2) / 24000000;
  365. int c0 = C;
  366. int c2 = 10000;
  367. int c1 = C * c2 - (c0 * c2);
  368. set_audioClock(c0, c1, c2);
  369. CCM_CSCMR1 = (CCM_CSCMR1 & ~(CCM_CSCMR1_SAI2_CLK_SEL_MASK))
  370. | CCM_CSCMR1_SAI2_CLK_SEL(2); // &0x03 // (0,1,2): PLL3PFD0, PLL5, PLL4,
  371. CCM_CS2CDR = (CCM_CS2CDR & ~(CCM_CS2CDR_SAI2_CLK_PRED_MASK | CCM_CS2CDR_SAI2_CLK_PODF_MASK))
  372. | CCM_CS2CDR_SAI2_CLK_PRED(n1-1)
  373. | CCM_CS2CDR_SAI2_CLK_PODF(n2-1);
  374. IOMUXC_GPR_GPR1 = (IOMUXC_GPR_GPR1 & ~(IOMUXC_GPR_GPR1_SAI2_MCLK3_SEL_MASK))
  375. | (IOMUXC_GPR_GPR1_SAI2_MCLK_DIR | IOMUXC_GPR_GPR1_SAI2_MCLK3_SEL(0)); //Select MCLK
  376. if (I2S2_TCSR & I2S_TCSR_TE) return;
  377. //CORE_PIN5_CONFIG = 2; //2:MCLK
  378. CORE_PIN4_CONFIG = 2; //2:TX_BCLK
  379. CORE_PIN3_CONFIG = 2; //2:TX_SYNC
  380. #if defined(AUDIO_PT8211_OVERSAMPLING)
  381. int div = 0;
  382. #else
  383. int div = 3;
  384. #endif
  385. // configure transmitter
  386. I2S2_TMR = 0;
  387. I2S2_TCR1 = I2S_TCR1_RFW(0);
  388. I2S2_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP | I2S_TCR2_MSEL(1) | I2S_TCR2_BCD | I2S_TCR2_DIV(div);
  389. I2S2_TCR3 = I2S_TCR3_TCE;
  390. // I2S2_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(15) | I2S_TCR4_MF | I2S_TCR4_FSE | I2S_TCR4_FSP | I2S_TCR4_FSD; //TDA1543
  391. I2S2_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(15) | I2S_TCR4_MF /*| I2S_TCR4_FSE*/ | I2S_TCR4_FSP | I2S_TCR4_FSD; //PT8211
  392. I2S2_TCR5 = I2S_TCR5_WNW(15) | I2S_TCR5_W0W(15) | I2S_TCR5_FBT(15);
  393. }
  394. #endif