選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

output_pt8211.cpp 14KB

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