Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

output_pt8211.cpp 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. #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. #if defined(AUDIO_PT8211_OVERSAMPLING)
  284. memset(dest,0,AUDIO_BLOCK_SAMPLES*8);
  285. #else
  286. memset(dest,0,AUDIO_BLOCK_SAMPLES*2);
  287. #endif
  288. return;
  289. }
  290. if (offsetL < AUDIO_BLOCK_SAMPLES) {
  291. AudioOutputPT8211::block_left_offset = offsetL;
  292. } else {
  293. AudioOutputPT8211::block_left_offset = 0;
  294. AudioStream::release(blockL);
  295. AudioOutputPT8211::block_left_1st = AudioOutputPT8211::block_left_2nd;
  296. AudioOutputPT8211::block_left_2nd = NULL;
  297. }
  298. if (offsetR < AUDIO_BLOCK_SAMPLES) {
  299. AudioOutputPT8211::block_right_offset = offsetR;
  300. } else {
  301. AudioOutputPT8211::block_right_offset = 0;
  302. AudioStream::release(blockR);
  303. AudioOutputPT8211::block_right_1st = AudioOutputPT8211::block_right_2nd;
  304. AudioOutputPT8211::block_right_2nd = NULL;
  305. }
  306. }
  307. void AudioOutputPT8211::update(void)
  308. {
  309. audio_block_t *block;
  310. block = receiveReadOnly(0); // input 0 = left channel
  311. if (block) {
  312. __disable_irq();
  313. if (block_left_1st == NULL) {
  314. block_left_1st = block;
  315. block_left_offset = 0;
  316. __enable_irq();
  317. } else if (block_left_2nd == NULL) {
  318. block_left_2nd = block;
  319. __enable_irq();
  320. } else {
  321. audio_block_t *tmp = block_left_1st;
  322. block_left_1st = block_left_2nd;
  323. block_left_2nd = block;
  324. block_left_offset = 0;
  325. __enable_irq();
  326. release(tmp);
  327. }
  328. }
  329. block = receiveReadOnly(1); // input 1 = right channel
  330. if (block) {
  331. __disable_irq();
  332. if (block_right_1st == NULL) {
  333. block_right_1st = block;
  334. block_right_offset = 0;
  335. __enable_irq();
  336. } else if (block_right_2nd == NULL) {
  337. block_right_2nd = block;
  338. __enable_irq();
  339. } else {
  340. audio_block_t *tmp = block_right_1st;
  341. block_right_1st = block_right_2nd;
  342. block_right_2nd = block;
  343. block_right_offset = 0;
  344. __enable_irq();
  345. release(tmp);
  346. }
  347. }
  348. }
  349. // MCLK needs to be 48e6 / 1088 * 256 = 11.29411765 MHz -> 44.117647 kHz sample rate
  350. //
  351. #if F_CPU == 96000000 || F_CPU == 48000000 || F_CPU == 24000000
  352. // PLL is at 96 MHz in these modes
  353. #define MCLK_MULT 2
  354. #define MCLK_DIV 17
  355. #elif F_CPU == 72000000
  356. #define MCLK_MULT 8
  357. #define MCLK_DIV 51
  358. #elif F_CPU == 120000000
  359. #define MCLK_MULT 8
  360. #define MCLK_DIV 85
  361. #elif F_CPU == 144000000
  362. #define MCLK_MULT 4
  363. #define MCLK_DIV 51
  364. #elif F_CPU == 168000000
  365. #define MCLK_MULT 8
  366. #define MCLK_DIV 119
  367. #elif F_CPU == 180000000
  368. #define MCLK_MULT 16
  369. #define MCLK_DIV 255
  370. #define MCLK_SRC 0
  371. #elif F_CPU == 192000000
  372. #define MCLK_MULT 1
  373. #define MCLK_DIV 17
  374. #elif F_CPU == 216000000
  375. #define MCLK_MULT 8
  376. #define MCLK_DIV 153
  377. #define MCLK_SRC 0
  378. #elif F_CPU == 240000000
  379. #define MCLK_MULT 4
  380. #define MCLK_DIV 85
  381. #elif F_CPU == 16000000
  382. #define MCLK_MULT 12
  383. #define MCLK_DIV 17
  384. #else
  385. #error "This CPU Clock Speed is not supported by the Audio library";
  386. #endif
  387. #ifndef MCLK_SRC
  388. #if F_CPU >= 20000000
  389. #define MCLK_SRC 3 // the PLL
  390. #else
  391. #define MCLK_SRC 0 // system clock
  392. #endif
  393. #endif
  394. void AudioOutputPT8211::config_i2s(void)
  395. {
  396. SIM_SCGC6 |= SIM_SCGC6_I2S;
  397. SIM_SCGC7 |= SIM_SCGC7_DMA;
  398. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  399. // if transmitter is enabled, do nothing
  400. if (I2S0_TCSR & I2S_TCSR_TE) return;
  401. // enable MCLK output
  402. I2S0_MCR = I2S_MCR_MICS(MCLK_SRC) | I2S_MCR_MOE;
  403. while (I2S0_MCR & I2S_MCR_DUF) ;
  404. I2S0_MDR = I2S_MDR_FRACT((MCLK_MULT-1)) | I2S_MDR_DIVIDE((MCLK_DIV-1));
  405. // configure transmitter
  406. I2S0_TMR = 0;
  407. I2S0_TCR1 = I2S_TCR1_TFW(1); // watermark at half fifo size
  408. #if defined(AUDIO_PT8211_OVERSAMPLING)
  409. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP | I2S_TCR2_MSEL(1) | I2S_TCR2_BCD | I2S_TCR2_DIV(0);
  410. #else
  411. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP | I2S_TCR2_MSEL(1) | I2S_TCR2_BCD | I2S_TCR2_DIV(3);
  412. #endif
  413. I2S0_TCR3 = I2S_TCR3_TCE;
  414. // I2S0_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(15) | I2S_TCR4_MF | I2S_TCR4_FSE | I2S_TCR4_FSP | I2S_TCR4_FSD; //TDA1543
  415. I2S0_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(15) | I2S_TCR4_MF /*| I2S_TCR4_FSE*/ | I2S_TCR4_FSP | I2S_TCR4_FSD; //PT8211
  416. I2S0_TCR5 = I2S_TCR5_WNW(15) | I2S_TCR5_W0W(15) | I2S_TCR5_FBT(15);
  417. // configure pin mux for 3 clock signals
  418. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)
  419. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK
  420. #if 0
  421. CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK
  422. #endif
  423. }