Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

output_pt8211.cpp 15KB

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