Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

output_pt8211.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. }