No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

471 líneas
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.
  27. #include "output_pt8211.h"
  28. #include "memcpy_audio.h"
  29. //uncomment to enable oversampling:
  30. #define OVERSAMPLING
  31. //uncomment ONE of these to define interpolation type for oversampling:
  32. // #define INTERPOLATION_LINEAR
  33. #define INTERPOLATION_CIC
  34. audio_block_t * AudioOutputPT8211::block_left_1st = NULL;
  35. audio_block_t * AudioOutputPT8211::block_right_1st = NULL;
  36. audio_block_t * AudioOutputPT8211::block_left_2nd = NULL;
  37. audio_block_t * AudioOutputPT8211::block_right_2nd = NULL;
  38. uint16_t AudioOutputPT8211::block_left_offset = 0;
  39. uint16_t AudioOutputPT8211::block_right_offset = 0;
  40. bool AudioOutputPT8211::update_responsibility = false;
  41. #if defined(OVERSAMPLING)
  42. DMAMEM static uint32_t i2s_tx_buffer[AUDIO_BLOCK_SAMPLES*4];
  43. #else
  44. DMAMEM static uint32_t i2s_tx_buffer[AUDIO_BLOCK_SAMPLES];
  45. #endif
  46. DMAChannel AudioOutputPT8211::dma(false);
  47. void AudioOutputPT8211::begin(void)
  48. {
  49. dma.begin(true); // Allocate the DMA channel first
  50. block_left_1st = NULL;
  51. block_right_1st = NULL;
  52. // TODO: should we set & clear the I2S_TCSR_SR bit here?
  53. config_i2s();
  54. CORE_PIN22_CONFIG = PORT_PCR_MUX(6); // pin 22, PTC1, I2S0_TXD0
  55. #if defined(KINETISK)
  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->DADDR = &I2S0_TDR0;
  62. dma.TCD->DOFF = 0;
  63. dma.TCD->CITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  64. dma.TCD->DLASTSGA = 0;
  65. dma.TCD->BITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  66. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  67. #endif
  68. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_TX);
  69. update_responsibility = update_setup();
  70. dma.enable();
  71. I2S0_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE | I2S_TCSR_FR;
  72. dma.attachInterrupt(isr);
  73. }
  74. void AudioOutputPT8211::isr(void)
  75. {
  76. digitalWriteFast(LED_BUILTIN, HIGH);
  77. int16_t *dest;
  78. audio_block_t *blockL, *blockR;
  79. uint32_t saddr, offsetL, offsetR;
  80. saddr = (uint32_t)(dma.TCD->SADDR);
  81. dma.clearInterrupt();
  82. if (saddr < (uint32_t)i2s_tx_buffer + sizeof(i2s_tx_buffer) / 2) {
  83. // DMA is transmitting the first half of the buffer
  84. // so we must fill the second half
  85. #if defined(OVERSAMPLING)
  86. dest = (int16_t *)&i2s_tx_buffer[(AUDIO_BLOCK_SAMPLES/2)*4];
  87. #else
  88. dest = (int16_t *)&i2s_tx_buffer[AUDIO_BLOCK_SAMPLES/2];
  89. #endif
  90. if (AudioOutputPT8211::update_responsibility) AudioStream::update_all();
  91. } else {
  92. // DMA is transmitting the second half of the buffer
  93. // so we must fill the first half
  94. dest = (int16_t *)i2s_tx_buffer;
  95. }
  96. blockL = AudioOutputPT8211::block_left_1st;
  97. blockR = AudioOutputPT8211::block_right_1st;
  98. offsetL = AudioOutputPT8211::block_left_offset;
  99. offsetR = AudioOutputPT8211::block_right_offset;
  100. #if defined(OVERSAMPLING)
  101. static int32_t oldL = 0;
  102. static int32_t oldR = 0;
  103. #endif
  104. if (blockL && blockR) {
  105. #if defined(OVERSAMPLING)
  106. #if defined(INTERPOLATION_LINEAR)
  107. for (int i=0; i< AUDIO_BLOCK_SAMPLES / 2; i++, offsetL++, offsetR++) {
  108. int32_t valL = blockL->data[offsetL];
  109. int32_t valR = blockR->data[offsetR];
  110. int32_t nL = (oldL+valL) >> 1;
  111. int32_t nR = (oldR+valR) >> 1;
  112. *(dest+0) = (oldL+nL) >> 1;
  113. *(dest+1) = (oldR+nR) >> 1;
  114. *(dest+2) = nL;
  115. *(dest+3) = nR;
  116. *(dest+4) = (nL+valL) >> 1;
  117. *(dest+5) = (nR+valR) >> 1;
  118. *(dest+6) = valL;
  119. *(dest+7) = valR;
  120. dest+=8;
  121. oldL = valL;
  122. oldR = valR;
  123. }
  124. #elif defined(INTERPOLATION_CIC)
  125. for (int i=0; i< AUDIO_BLOCK_SAMPLES / 2; i++, offsetL++, offsetR++) {
  126. int32_t valL = blockL->data[offsetL];
  127. int32_t valR = blockR->data[offsetR];
  128. int32_t combL[3] = {0};
  129. static int32_t combLOld[2] = {0};
  130. int32_t combR[3] = {0};
  131. static int32_t combROld[2] = {0};
  132. combL[0] = valL - oldL;
  133. combL[1] = combL[0] - combLOld[0];
  134. combL[2] = combL[1] - combLOld[1];
  135. // combL[2] now holds input val
  136. combLOld[0] = combL[0];
  137. combLOld[1] = combL[1];
  138. for (int j = 0; j < 4; j++) {
  139. int32_t integrateL[3];
  140. static int32_t integrateLOld[3] = {0};
  141. integrateL[0] = ( (j==0) ? (combL[2]) : (0) ) + integrateLOld[0];
  142. integrateL[1] = integrateL[0] + integrateLOld[1];
  143. integrateL[2] = integrateL[1] + integrateLOld[2];
  144. // integrateL[2] now holds j'th upsampled value
  145. *(dest+j*2) = integrateL[2] >> 4;
  146. integrateLOld[0] = integrateL[0];
  147. integrateLOld[1] = integrateL[1];
  148. integrateLOld[2] = integrateL[2];
  149. }
  150. combR[0] = valR - oldR;
  151. combR[1] = combR[0] - combROld[0];
  152. combR[2] = combR[1] - combROld[1];
  153. // combR[2] now holds input val
  154. combROld[0] = combR[0];
  155. combROld[1] = combR[1];
  156. for (int j = 0; j < 4; j++) {
  157. int32_t integrateR[3];
  158. static int32_t integrateROld[3] = {0};
  159. integrateR[0] = ( (j==0) ? (combR[2]) : (0) ) + integrateROld[0];
  160. integrateR[1] = integrateR[0] + integrateROld[1];
  161. integrateR[2] = integrateR[1] + integrateROld[2];
  162. // integrateR[2] now holds j'th upsampled value
  163. *(dest+j*2+1) = integrateR[2] >> 4;
  164. integrateROld[0] = integrateR[0];
  165. integrateROld[1] = integrateR[1];
  166. integrateROld[2] = integrateR[2];
  167. }
  168. dest+=8;
  169. oldL = valL;
  170. oldR = valR;
  171. }
  172. #else
  173. #error no interpolation method defined for oversampling.
  174. #endif //defined(INTERPOLATION_LINEAR)
  175. #else
  176. memcpy_tointerleaveLR(dest, blockL->data + offsetL, blockR->data + offsetR);
  177. offsetL += AUDIO_BLOCK_SAMPLES / 2;
  178. offsetR += AUDIO_BLOCK_SAMPLES / 2;
  179. #endif //defined(OVERSAMPLING)
  180. } else if (blockL) {
  181. #if defined(OVERSAMPLING)
  182. #if defined(INTERPOLATION_LINEAR)
  183. for (int i=0; i< AUDIO_BLOCK_SAMPLES / 2; i++, offsetL++) {
  184. int32_t val = blockL->data[offsetL];
  185. int32_t n = (oldL+val) >> 1;
  186. *(dest+0) = (oldL+n) >> 1;
  187. *(dest+1) = 0;
  188. *(dest+2) = n;
  189. *(dest+3) = 0;
  190. *(dest+4) = (n+val) >> 1;
  191. *(dest+5) = 0;
  192. *(dest+6) = val;
  193. *(dest+7) = 0;
  194. dest+=8;
  195. oldL = val;
  196. }
  197. #elif defined(INTERPOLATION_CIC)
  198. for (int i=0; i< AUDIO_BLOCK_SAMPLES / 2; i++, offsetL++, offsetR++) {
  199. int32_t valL = blockL->data[offsetL];
  200. int32_t combL[3] = {0};
  201. static int32_t combLOld[2] = {0};
  202. combL[0] = valL - oldL;
  203. combL[1] = combL[0] - combLOld[0];
  204. combL[2] = combL[1] - combLOld[1];
  205. // combL[2] now holds input val
  206. combLOld[0] = combL[0];
  207. combLOld[1] = combL[1];
  208. for (int j = 0; j < 4; j++) {
  209. int32_t integrateL[3];
  210. static int32_t integrateLOld[3] = {0};
  211. integrateL[0] = ( (j==0) ? (combL[2]) : (0) ) + integrateLOld[0];
  212. integrateL[1] = integrateL[0] + integrateLOld[1];
  213. integrateL[2] = integrateL[1] + integrateLOld[2];
  214. // integrateL[2] now holds j'th upsampled value
  215. *(dest+j*2) = integrateL[2] >> 4;
  216. integrateLOld[0] = integrateL[0];
  217. integrateLOld[1] = integrateL[1];
  218. integrateLOld[2] = integrateL[2];
  219. }
  220. // fill right channel with zeros:
  221. *(dest+1) = 0;
  222. *(dest+3) = 0;
  223. *(dest+5) = 0;
  224. *(dest+7) = 0;
  225. dest+=8;
  226. oldL = valL;
  227. }
  228. #else
  229. #error no interpolation method defined for oversampling.
  230. #endif //defined(INTERPOLATION_LINEAR)
  231. #else
  232. memcpy_tointerleaveL(dest, blockL->data + offsetL);
  233. offsetL += (AUDIO_BLOCK_SAMPLES / 2);
  234. #endif //defined(OVERSAMPLING)
  235. } else if (blockR) {
  236. #if defined(OVERSAMPLING)
  237. #if defined(INTERPOLATION_LINEAR)
  238. for (int i=0; i< AUDIO_BLOCK_SAMPLES / 2; i++, offsetR++) {
  239. int32_t val = blockR->data[offsetR];
  240. int32_t n = (oldR+val) >> 1;
  241. *(dest+0) = 0;
  242. *(dest+1) = ((oldR+n) >> 1);
  243. *(dest+2) = 0;
  244. *(dest+3) = n;
  245. *(dest+4) = 0;
  246. *(dest+5) = ((n+val) >> 1);
  247. *(dest+6) = 0;
  248. *(dest+7) = val;
  249. dest+=8;
  250. oldR = val;
  251. }
  252. #elif defined(INTERPOLATION_CIC)
  253. for (int i=0; i< AUDIO_BLOCK_SAMPLES / 2; i++, offsetL++, offsetR++) {
  254. int32_t valR = blockR->data[offsetR];
  255. int32_t combR[3] = {0};
  256. static int32_t combROld[2] = {0};
  257. combR[0] = valR - oldR;
  258. combR[1] = combR[0] - combROld[0];
  259. combR[2] = combR[1] - combROld[1];
  260. // combR[2] now holds input val
  261. combROld[0] = combR[0];
  262. combROld[1] = combR[1];
  263. for (int j = 0; j < 4; j++) {
  264. int32_t integrateR[3];
  265. static int32_t integrateROld[3] = {0};
  266. integrateR[0] = ( (j==0) ? (combR[2]) : (0) ) + integrateROld[0];
  267. integrateR[1] = integrateR[0] + integrateROld[1];
  268. integrateR[2] = integrateR[1] + integrateROld[2];
  269. // integrateR[2] now holds j'th upsampled value
  270. *(dest+j*2+1) = integrateR[2] >> 4;
  271. integrateROld[0] = integrateR[0];
  272. integrateROld[1] = integrateR[1];
  273. integrateROld[2] = integrateR[2];
  274. }
  275. // fill left channel with zeros:
  276. *(dest+0) = 0;
  277. *(dest+2) = 0;
  278. *(dest+4) = 0;
  279. *(dest+6) = 0;
  280. dest+=8;
  281. oldR = valR;
  282. }
  283. #else
  284. #error no interpolation method defined for oversampling.
  285. #endif //defined(INTERPOLATION_LINEAR)
  286. #else
  287. memcpy_tointerleaveR(dest, blockR->data + offsetR);
  288. offsetR += AUDIO_BLOCK_SAMPLES / 2;
  289. #endif //defined(OVERSAMPLING)
  290. } else {
  291. memset(dest,0,AUDIO_BLOCK_SAMPLES * 2);
  292. return;
  293. }
  294. if (offsetL < AUDIO_BLOCK_SAMPLES) {
  295. AudioOutputPT8211::block_left_offset = offsetL;
  296. } else {
  297. AudioOutputPT8211::block_left_offset = 0;
  298. AudioStream::release(blockL);
  299. AudioOutputPT8211::block_left_1st = AudioOutputPT8211::block_left_2nd;
  300. AudioOutputPT8211::block_left_2nd = NULL;
  301. }
  302. if (offsetR < AUDIO_BLOCK_SAMPLES) {
  303. AudioOutputPT8211::block_right_offset = offsetR;
  304. } else {
  305. AudioOutputPT8211::block_right_offset = 0;
  306. AudioStream::release(blockR);
  307. AudioOutputPT8211::block_right_1st = AudioOutputPT8211::block_right_2nd;
  308. AudioOutputPT8211::block_right_2nd = NULL;
  309. }
  310. digitalWriteFast(LED_BUILTIN, LOW);
  311. }
  312. void AudioOutputPT8211::update(void)
  313. {
  314. audio_block_t *block;
  315. block = receiveReadOnly(0); // input 0 = left channel
  316. if (block) {
  317. __disable_irq();
  318. if (block_left_1st == NULL) {
  319. block_left_1st = block;
  320. block_left_offset = 0;
  321. __enable_irq();
  322. } else if (block_left_2nd == NULL) {
  323. block_left_2nd = block;
  324. __enable_irq();
  325. } else {
  326. audio_block_t *tmp = block_left_1st;
  327. block_left_1st = block_left_2nd;
  328. block_left_2nd = block;
  329. block_left_offset = 0;
  330. __enable_irq();
  331. release(tmp);
  332. }
  333. }
  334. block = receiveReadOnly(1); // input 1 = right channel
  335. if (block) {
  336. __disable_irq();
  337. if (block_right_1st == NULL) {
  338. block_right_1st = block;
  339. block_right_offset = 0;
  340. __enable_irq();
  341. } else if (block_right_2nd == NULL) {
  342. block_right_2nd = block;
  343. __enable_irq();
  344. } else {
  345. audio_block_t *tmp = block_right_1st;
  346. block_right_1st = block_right_2nd;
  347. block_right_2nd = block;
  348. block_right_offset = 0;
  349. __enable_irq();
  350. release(tmp);
  351. }
  352. }
  353. }
  354. // MCLK needs to be 48e6 / 1088 * 256 = 11.29411765 MHz -> 44.117647 kHz sample rate
  355. //
  356. #if F_CPU == 96000000 || F_CPU == 48000000 || F_CPU == 24000000
  357. // PLL is at 96 MHz in these modes
  358. #define MCLK_MULT 2
  359. #define MCLK_DIV 17
  360. #elif F_CPU == 72000000
  361. #define MCLK_MULT 8
  362. #define MCLK_DIV 51
  363. #elif F_CPU == 120000000
  364. #define MCLK_MULT 8
  365. #define MCLK_DIV 85
  366. #elif F_CPU == 144000000
  367. #define MCLK_MULT 4
  368. #define MCLK_DIV 51
  369. #elif F_CPU == 168000000
  370. #define MCLK_MULT 8
  371. #define MCLK_DIV 119
  372. #elif F_CPU == 180000000
  373. #define MCLK_MULT 16
  374. #define MCLK_DIV 255
  375. #define MCLK_SRC 0
  376. #elif F_CPU == 192000000
  377. #define MCLK_MULT 1
  378. #define MCLK_DIV 17
  379. #elif F_CPU == 216000000
  380. #define MCLK_MULT 8
  381. #define MCLK_DIV 153
  382. #define MCLK_SRC 0
  383. #elif F_CPU == 240000000
  384. #define MCLK_MULT 4
  385. #define MCLK_DIV 85
  386. #elif F_CPU == 16000000
  387. #define MCLK_MULT 12
  388. #define MCLK_DIV 17
  389. #else
  390. #error "This CPU Clock Speed is not supported by the Audio library";
  391. #endif
  392. #ifndef MCLK_SRC
  393. #if F_CPU >= 20000000
  394. #define MCLK_SRC 3 // the PLL
  395. #else
  396. #define MCLK_SRC 0 // system clock
  397. #endif
  398. #endif
  399. void AudioOutputPT8211::config_i2s(void)
  400. {
  401. SIM_SCGC6 |= SIM_SCGC6_I2S;
  402. SIM_SCGC7 |= SIM_SCGC7_DMA;
  403. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  404. // if transmitter is enabled, do nothing
  405. if (I2S0_TCSR & I2S_TCSR_TE) return;
  406. // enable MCLK output
  407. I2S0_MCR = I2S_MCR_MICS(MCLK_SRC) | I2S_MCR_MOE;
  408. while (I2S0_MCR & I2S_MCR_DUF) ;
  409. I2S0_MDR = I2S_MDR_FRACT((MCLK_MULT-1)) | I2S_MDR_DIVIDE((MCLK_DIV-1));
  410. // configure transmitter
  411. I2S0_TMR = 0;
  412. I2S0_TCR1 = I2S_TCR1_TFW(1); // watermark at half fifo size
  413. #if defined(OVERSAMPLING)
  414. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP | I2S_TCR2_MSEL(1) | I2S_TCR2_BCD | I2S_TCR2_DIV(0);
  415. #else
  416. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP | I2S_TCR2_MSEL(1) | I2S_TCR2_BCD | I2S_TCR2_DIV(3);
  417. #endif
  418. I2S0_TCR3 = I2S_TCR3_TCE;
  419. // I2S0_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(15) | I2S_TCR4_MF | I2S_TCR4_FSE | I2S_TCR4_FSP | I2S_TCR4_FSD;
  420. I2S0_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(15) | I2S_TCR4_MF /*| I2S_TCR4_FSE*/ | I2S_TCR4_FSP | I2S_TCR4_FSD; //PT8211
  421. I2S0_TCR5 = I2S_TCR5_WNW(15) | I2S_TCR5_W0W(15) | I2S_TCR5_FBT(15);
  422. // configure pin mux for 3 clock signals
  423. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)
  424. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK
  425. #if 0
  426. CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK
  427. #endif
  428. }