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.

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