Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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