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.

428 satır
14KB

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