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.

464 satır
15KB

  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.h"
  30. #include "memcpy_audio.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 static uint32_t i2s_tx_buffer[AUDIO_BLOCK_SAMPLES*4];
  40. #else
  41. DMAMEM 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. CORE_PIN22_CONFIG = PORT_PCR_MUX(6); // pin 22, PTC1, I2S0_TXD0
  52. #if defined(KINETISK)
  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. #endif
  65. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_TX);
  66. update_responsibility = update_setup();
  67. dma.enable();
  68. I2S0_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE | I2S_TCSR_FR;
  69. dma.attachInterrupt(isr);
  70. }
  71. void AudioOutputPT8211::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::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::block_left_1st;
  93. blockR = AudioOutputPT8211::block_right_1st;
  94. offsetL = AudioOutputPT8211::block_left_offset;
  95. offsetR = AudioOutputPT8211::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::block_left_offset = offsetL;
  294. } else {
  295. AudioOutputPT8211::block_left_offset = 0;
  296. AudioStream::release(blockL);
  297. AudioOutputPT8211::block_left_1st = AudioOutputPT8211::block_left_2nd;
  298. AudioOutputPT8211::block_left_2nd = NULL;
  299. }
  300. if (offsetR < AUDIO_BLOCK_SAMPLES) {
  301. AudioOutputPT8211::block_right_offset = offsetR;
  302. } else {
  303. AudioOutputPT8211::block_right_offset = 0;
  304. AudioStream::release(blockR);
  305. AudioOutputPT8211::block_right_1st = AudioOutputPT8211::block_right_2nd;
  306. AudioOutputPT8211::block_right_2nd = NULL;
  307. }
  308. }
  309. void AudioOutputPT8211::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. // MCLK needs to be 48e6 / 1088 * 256 = 11.29411765 MHz -> 44.117647 kHz sample rate
  352. //
  353. #if F_CPU == 96000000 || F_CPU == 48000000 || F_CPU == 24000000
  354. // PLL is at 96 MHz in these modes
  355. #define MCLK_MULT 2
  356. #define MCLK_DIV 17
  357. #elif F_CPU == 72000000
  358. #define MCLK_MULT 8
  359. #define MCLK_DIV 51
  360. #elif F_CPU == 120000000
  361. #define MCLK_MULT 8
  362. #define MCLK_DIV 85
  363. #elif F_CPU == 144000000
  364. #define MCLK_MULT 4
  365. #define MCLK_DIV 51
  366. #elif F_CPU == 168000000
  367. #define MCLK_MULT 8
  368. #define MCLK_DIV 119
  369. #elif F_CPU == 180000000
  370. #define MCLK_MULT 16
  371. #define MCLK_DIV 255
  372. #define MCLK_SRC 0
  373. #elif F_CPU == 192000000
  374. #define MCLK_MULT 1
  375. #define MCLK_DIV 17
  376. #elif F_CPU == 216000000
  377. #define MCLK_MULT 8
  378. #define MCLK_DIV 153
  379. #define MCLK_SRC 0
  380. #elif F_CPU == 240000000
  381. #define MCLK_MULT 4
  382. #define MCLK_DIV 85
  383. #elif F_CPU == 16000000
  384. #define MCLK_MULT 12
  385. #define MCLK_DIV 17
  386. #else
  387. #error "This CPU Clock Speed is not supported by the Audio library";
  388. #endif
  389. #ifndef MCLK_SRC
  390. #if F_CPU >= 20000000
  391. #define MCLK_SRC 3 // the PLL
  392. #else
  393. #define MCLK_SRC 0 // system clock
  394. #endif
  395. #endif
  396. void AudioOutputPT8211::config_i2s(void)
  397. {
  398. SIM_SCGC6 |= SIM_SCGC6_I2S;
  399. SIM_SCGC7 |= SIM_SCGC7_DMA;
  400. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  401. // if transmitter is enabled, do nothing
  402. if (I2S0_TCSR & I2S_TCSR_TE) return;
  403. // enable MCLK output
  404. I2S0_MCR = I2S_MCR_MICS(MCLK_SRC) | I2S_MCR_MOE;
  405. while (I2S0_MCR & I2S_MCR_DUF) ;
  406. I2S0_MDR = I2S_MDR_FRACT((MCLK_MULT-1)) | I2S_MDR_DIVIDE((MCLK_DIV-1));
  407. // configure transmitter
  408. I2S0_TMR = 0;
  409. I2S0_TCR1 = I2S_TCR1_TFW(1); // watermark at half fifo size
  410. #if defined(AUDIO_PT8211_OVERSAMPLING)
  411. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP | I2S_TCR2_MSEL(1) | I2S_TCR2_BCD | I2S_TCR2_DIV(0);
  412. #else
  413. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP | I2S_TCR2_MSEL(1) | I2S_TCR2_BCD | I2S_TCR2_DIV(3);
  414. #endif
  415. I2S0_TCR3 = I2S_TCR3_TCE;
  416. // I2S0_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(15) | I2S_TCR4_MF | I2S_TCR4_FSE | I2S_TCR4_FSP | I2S_TCR4_FSD; //TDA1543
  417. I2S0_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(15) | I2S_TCR4_MF /*| I2S_TCR4_FSE*/ | I2S_TCR4_FSP | I2S_TCR4_FSD; //PT8211
  418. I2S0_TCR5 = I2S_TCR5_WNW(15) | I2S_TCR5_W0W(15) | I2S_TCR5_FBT(15);
  419. // configure pin mux for 3 clock signals
  420. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)
  421. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK
  422. #if 0
  423. CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK
  424. #endif
  425. }
  426. #endif