Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

output_pt8211.cpp 14KB

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