您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

460 行
13KB

  1. /* SPDIF for Teensy 3.X
  2. * Copyright (c) 2015, Frank Bösing, f.boesing@gmx.de,
  3. * Thanks to KPC & Paul Stoffregen!
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice, development funding notice, and this permission
  13. * notice shall be included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. // 2015/08/23: (FB) added mute_PCM() - sets or unsets VALID in VUCP (and adjusts PARITY)
  24. #include <Arduino.h>
  25. #include "output_spdif.h"
  26. #include "utility/imxrt_hw.h"
  27. audio_block_t * AudioOutputSPDIF::block_left_1st = NULL;
  28. audio_block_t * AudioOutputSPDIF::block_right_1st = NULL;
  29. audio_block_t * AudioOutputSPDIF::block_left_2nd = NULL;
  30. audio_block_t * AudioOutputSPDIF::block_right_2nd = NULL;
  31. uint16_t AudioOutputSPDIF::block_left_offset = 0;
  32. uint16_t AudioOutputSPDIF::block_right_offset = 0;
  33. bool AudioOutputSPDIF::update_responsibility = false;
  34. DMAChannel AudioOutputSPDIF::dma(false);
  35. extern uint16_t spdif_bmclookup[256];
  36. DMAMEM __attribute__((aligned(32)))
  37. static uint32_t SPDIF_tx_buffer[AUDIO_BLOCK_SAMPLES * 4]; //2 KB
  38. #if defined(KINETISK) || defined(__IMXRT1052__) || defined(__IMXRT1062__)
  39. #define PREAMBLE_B (0xE8) //11101000
  40. #define PREAMBLE_M (0xE2) //11100010
  41. #define PREAMBLE_W (0xE4) //11100100
  42. #define VUCP_VALID ((0xCC) << 24)
  43. #define VUCP_INVALID ((0xD4) << 24)// To mute PCM, set VUCP = invalid.
  44. uint32_t AudioOutputSPDIF::vucp = VUCP_VALID;
  45. #endif
  46. FLASHMEM
  47. void AudioOutputSPDIF::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_SPDIF();
  54. #if defined(KINETISK)
  55. CORE_PIN22_CONFIG = PORT_PCR_MUX(6); // pin 22, PTC1, I2S0_TXD0
  56. const int nbytes_mlno = 2 * 4; // 8 Bytes per minor loop
  57. dma.TCD->SADDR = SPDIF_tx_buffer;
  58. dma.TCD->SOFF = 4;
  59. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(2) | DMA_TCD_ATTR_DSIZE(2);
  60. dma.TCD->NBYTES_MLNO = nbytes_mlno;
  61. dma.TCD->SLAST = -sizeof(SPDIF_tx_buffer);
  62. dma.TCD->DADDR = &I2S0_TDR0;
  63. dma.TCD->DOFF = 0;
  64. dma.TCD->CITER_ELINKNO = sizeof(SPDIF_tx_buffer) / nbytes_mlno;
  65. dma.TCD->DLASTSGA = 0;
  66. dma.TCD->BITER_ELINKNO = sizeof(SPDIF_tx_buffer) / nbytes_mlno;
  67. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  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. #elif defined(__IMXRT1052__) || defined(__IMXRT1062__)
  74. #if defined(__IMXRT1052__)
  75. CORE_PIN6_CONFIG = 3; //1:TX_DATA0
  76. #elif defined(__IMXRT1062__)
  77. CORE_PIN7_CONFIG = 3; //1:TX_DATA0
  78. #endif
  79. const int nbytes_mlno = 2 * 4; // 8 Bytes per minor loop
  80. dma.TCD->SADDR = SPDIF_tx_buffer;
  81. dma.TCD->SOFF = 4;
  82. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(2) | DMA_TCD_ATTR_DSIZE(2);
  83. dma.TCD->NBYTES_MLNO = nbytes_mlno;
  84. dma.TCD->SLAST = -sizeof(SPDIF_tx_buffer);
  85. dma.TCD->DADDR = &I2S1_TDR0;
  86. dma.TCD->DOFF = 0;
  87. dma.TCD->CITER_ELINKNO = sizeof(SPDIF_tx_buffer) / nbytes_mlno;
  88. dma.TCD->DLASTSGA = 0;
  89. dma.TCD->BITER_ELINKNO = sizeof(SPDIF_tx_buffer) / nbytes_mlno;
  90. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  91. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_SAI1_TX);
  92. update_responsibility = update_setup();
  93. dma.enable();
  94. I2S1_RCSR |= I2S_RCSR_RE;
  95. I2S1_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE | I2S_TCSR_FR;
  96. dma.attachInterrupt(isr);
  97. #endif
  98. }
  99. /*
  100. http://www.hardwarebook.info/S/PDIF
  101. 1. To make it easier and a bit faster, the parity-bit is always the same.
  102. - With a alternating parity we had to adjust the next subframe. Instead, use a bit from the aux-info as parity.
  103. 2. The buffer is filled with an offset of 1 byte, so the last parity (which is always 0 now (see 1.) ) is written as first byte.
  104. -> A bit easier and faster to construct both subframes.
  105. */
  106. void AudioOutputSPDIF::isr(void)
  107. {
  108. static uint16_t frame = 0;
  109. const int16_t *src;
  110. int32_t *end, *dest;
  111. audio_block_t *block;
  112. uint32_t saddr, offset;
  113. uint16_t sample, lo, hi, aux;
  114. saddr = (uint32_t)(dma.TCD->SADDR);
  115. dma.clearInterrupt();
  116. if (saddr < (uint32_t)SPDIF_tx_buffer + sizeof(SPDIF_tx_buffer) / 2) {
  117. // DMA is transmitting the first half of the buffer
  118. // so we must fill the second half
  119. dest = (int32_t *)&SPDIF_tx_buffer[AUDIO_BLOCK_SAMPLES * 4/2];
  120. end = (int32_t *)&SPDIF_tx_buffer[AUDIO_BLOCK_SAMPLES * 4];
  121. if (AudioOutputSPDIF::update_responsibility) AudioStream::update_all();
  122. } else {
  123. // DMA is transmitting the second half of the buffer
  124. // so we must fill the first half
  125. dest = (int32_t *)SPDIF_tx_buffer;
  126. end = (int32_t *)&SPDIF_tx_buffer[AUDIO_BLOCK_SAMPLES * 4/2];
  127. }
  128. block = AudioOutputSPDIF::block_left_1st;
  129. if (block) {
  130. offset = AudioOutputSPDIF::block_left_offset;
  131. src = &block->data[offset];
  132. do {
  133. sample = *src++;
  134. //Subframe Channel 1
  135. hi = spdif_bmclookup[(uint8_t)(sample >> 8)];
  136. lo = spdif_bmclookup[(uint8_t) sample];
  137. lo ^= (~((int16_t)hi) >> 16);
  138. // 16 Bit sample:
  139. *(dest+1) = ((uint32_t)lo << 16) | hi;
  140. // 4 Bit Auxillary-audio-databits, the first used as parity
  141. aux = (0xB333 ^ (((uint32_t)((int16_t)lo)) >> 17));
  142. if (++frame > 191) {
  143. // VUCP-Bits ("Valid, Subcode, Channelstatus, Parity) = 0 (0xcc) | Preamble (depends on Framno.) | Auxillary
  144. *(dest+0) = vucp | (PREAMBLE_B << 16 ) | aux; //special preamble for one of 192 frames
  145. frame = 0;
  146. } else {
  147. *(dest+0) = vucp | (PREAMBLE_M << 16 ) | aux;
  148. }
  149. dest += 4;
  150. } while (dest < end);
  151. offset += AUDIO_BLOCK_SAMPLES/2;
  152. if (offset < AUDIO_BLOCK_SAMPLES) {
  153. AudioOutputSPDIF::block_left_offset = offset;
  154. } else {
  155. AudioOutputSPDIF::block_left_offset = 0;
  156. AudioStream::release(block);
  157. AudioOutputSPDIF::block_left_1st = AudioOutputSPDIF::block_left_2nd;
  158. AudioOutputSPDIF::block_left_2nd = NULL;
  159. }
  160. } else {
  161. do {
  162. if ( ++frame > 191 ) {
  163. *(dest+0) = vucp | 0x00e8cccc;
  164. frame = 0;
  165. } else {
  166. *(dest+0) = vucp | 0x00e2cccc;
  167. }
  168. *(dest+1) = 0xccccccccUL;
  169. dest +=4;
  170. } while (dest < end);
  171. }
  172. dest -= AUDIO_BLOCK_SAMPLES * 4/2 - 4/2;
  173. block = AudioOutputSPDIF::block_right_1st;
  174. if (block) {
  175. offset = AudioOutputSPDIF::block_right_offset;
  176. src = &block->data[offset];
  177. do {
  178. sample = *src++;
  179. //Subframe Channel 2
  180. hi = spdif_bmclookup[(uint8_t)(sample >> 8)];
  181. lo = spdif_bmclookup[(uint8_t)sample];
  182. lo ^= (~((int16_t)hi) >> 16);
  183. *(dest+1) = ( ((uint32_t)lo << 16) | hi );
  184. aux = (0xB333 ^ (((uint32_t)((int16_t)lo)) >> 17));
  185. *(dest+0) = vucp | (PREAMBLE_W << 16 ) | aux;
  186. dest += 4;
  187. } while (dest < end);
  188. offset += AUDIO_BLOCK_SAMPLES/2;
  189. if (offset < AUDIO_BLOCK_SAMPLES) {
  190. AudioOutputSPDIF::block_right_offset = offset;
  191. } else {
  192. AudioOutputSPDIF::block_right_offset = 0;
  193. AudioStream::release(block);
  194. AudioOutputSPDIF::block_right_1st = AudioOutputSPDIF::block_right_2nd;
  195. AudioOutputSPDIF::block_right_2nd = NULL;
  196. }
  197. } else {
  198. do {
  199. *dest = vucp | 0x00e4ccccUL;
  200. *(dest+1) = 0xccccccccUL;
  201. dest += 4 ;
  202. } while (dest < end);
  203. }
  204. #if IMXRT_CACHE_ENABLED >= 2
  205. dest -= AUDIO_BLOCK_SAMPLES * 4/2 + 4/2;
  206. arm_dcache_flush_delete(dest, sizeof(SPDIF_tx_buffer) / 2 );
  207. #endif
  208. }
  209. void AudioOutputSPDIF::mute_PCM(const bool mute)
  210. {
  211. vucp = mute?VUCP_INVALID:VUCP_VALID;
  212. }
  213. void AudioOutputSPDIF::update(void)
  214. {
  215. audio_block_t *block;
  216. block = receiveReadOnly(0); // input 0 = left channel
  217. if (block) {
  218. __disable_irq();
  219. if (block_left_1st == NULL) {
  220. block_left_1st = block;
  221. block_left_offset = 0;
  222. __enable_irq();
  223. } else if (block_left_2nd == NULL) {
  224. block_left_2nd = block;
  225. __enable_irq();
  226. } else {
  227. audio_block_t *tmp = block_left_1st;
  228. block_left_1st = block_left_2nd;
  229. block_left_2nd = block;
  230. block_left_offset = 0;
  231. __enable_irq();
  232. release(tmp);
  233. }
  234. }
  235. block = receiveReadOnly(1); // input 1 = right channel
  236. if (block) {
  237. __disable_irq();
  238. if (block_right_1st == NULL) {
  239. block_right_1st = block;
  240. block_right_offset = 0;
  241. __enable_irq();
  242. } else if (block_right_2nd == NULL) {
  243. block_right_2nd = block;
  244. __enable_irq();
  245. } else {
  246. audio_block_t *tmp = block_right_1st;
  247. block_right_1st = block_right_2nd;
  248. block_right_2nd = block;
  249. block_right_offset = 0;
  250. __enable_irq();
  251. release(tmp);
  252. }
  253. }
  254. }
  255. #if defined(KINETISK)
  256. #if F_CPU == 96000000 || F_CPU == 48000000 || F_CPU == 24000000
  257. // PLL is at 96 MHz in these modes
  258. #define MCLK_MULT 2
  259. #define MCLK_DIV 17
  260. #elif F_CPU == 72000000
  261. #define MCLK_MULT 8
  262. #define MCLK_DIV 51
  263. #elif F_CPU == 120000000
  264. #define MCLK_MULT 8
  265. #define MCLK_DIV 85
  266. #elif F_CPU == 144000000
  267. #define MCLK_MULT 4
  268. #define MCLK_DIV 51
  269. #elif F_CPU == 168000000
  270. #define MCLK_MULT 8
  271. #define MCLK_DIV 119
  272. #elif F_CPU == 180000000
  273. #define MCLK_MULT 16
  274. #define MCLK_DIV 255
  275. #define MCLK_SRC 0
  276. #elif F_CPU == 192000000
  277. #define MCLK_MULT 1
  278. #define MCLK_DIV 17
  279. #elif F_CPU == 216000000
  280. #define MCLK_MULT 12
  281. #define MCLK_DIV 17
  282. #define MCLK_SRC 1
  283. #elif F_CPU == 240000000
  284. #define MCLK_MULT 2
  285. #define MCLK_DIV 85
  286. #define MCLK_SRC 0
  287. #elif F_CPU == 256000000
  288. #define MCLK_MULT 12
  289. #define MCLK_DIV 17
  290. #define MCLK_SRC 1
  291. #elif F_CPU == 16000000
  292. #define MCLK_MULT 12
  293. #define MCLK_DIV 17
  294. #else
  295. #error "This CPU Clock Speed is not supported by the Audio library";
  296. #endif
  297. #ifndef MCLK_SRC
  298. #if F_CPU >= 20000000
  299. #define MCLK_SRC 3 // the PLL
  300. #else
  301. #define MCLK_SRC 0 // system clock
  302. #endif
  303. #endif
  304. #endif
  305. FLASHMEM
  306. void AudioOutputSPDIF::config_SPDIF(void)
  307. {
  308. #if defined(KINETISK)
  309. SIM_SCGC6 |= SIM_SCGC6_I2S;
  310. SIM_SCGC7 |= SIM_SCGC7_DMA;
  311. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  312. // enable MCLK output
  313. I2S0_MCR = I2S_MCR_MICS(MCLK_SRC) | I2S_MCR_MOE;
  314. while (I2S0_MCR & I2S_MCR_DUF) ;
  315. I2S0_MDR = I2S_MDR_FRACT((MCLK_MULT-1)) | I2S_MDR_DIVIDE((MCLK_DIV-1));
  316. // configure transmitter
  317. I2S0_TMR = 0;
  318. I2S0_TCR1 = I2S_TCR1_TFW(1); // watermark
  319. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_MSEL(1) | I2S_TCR2_BCD | I2S_TCR2_DIV(0);
  320. I2S0_TCR3 = I2S_TCR3_TCE;
  321. //4 Words per Frame 32 Bit Word-Length -> 128 Bit Frame-Length, MSB First:
  322. I2S0_TCR4 = I2S_TCR4_FRSZ(3) | I2S_TCR4_SYWD(0) | I2S_TCR4_MF | I2S_TCR4_FSP | I2S_TCR4_FSD;
  323. I2S0_TCR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31);
  324. I2S0_RCSR = 0;
  325. #if 0
  326. // configure pin mux for 3 clock signals (debug only)
  327. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK) 44.1kHz
  328. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK 5.6 MHz
  329. CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK 11.43MHz
  330. #endif
  331. #elif defined(__IMXRT1052__) || defined(__IMXRT1062__)
  332. CCM_CCGR5 |= CCM_CCGR5_SAI1(CCM_CCGR_ON);
  333. //PLL:
  334. int fs = AUDIO_SAMPLE_RATE_EXACT;
  335. // PLL between 27*24 = 648MHz und 54*24=1296MHz
  336. int n1 = 4; //SAI prescaler 4 => (n1*n2) = multiple of 4
  337. int n2 = 1 + (24000000 * 27) / (fs * 256 * n1);
  338. double C = ((double)fs * 256 * n1 * n2) / 24000000;
  339. int c0 = C;
  340. int c2 = 10000;
  341. int c1 = C * c2 - (c0 * c2);
  342. set_audioClock(c0, c1, c2);
  343. CCM_CSCMR1 = (CCM_CSCMR1 & ~(CCM_CSCMR1_SAI1_CLK_SEL_MASK))
  344. | CCM_CSCMR1_SAI1_CLK_SEL(2); // &0x03 // (0,1,2): PLL3PFD0, PLL5, PLL4
  345. CCM_CS1CDR = (CCM_CS1CDR & ~(CCM_CS1CDR_SAI1_CLK_PRED_MASK | CCM_CS1CDR_SAI1_CLK_PODF_MASK))
  346. | CCM_CS1CDR_SAI1_CLK_PRED(n1-1) // &0x07
  347. | CCM_CS1CDR_SAI1_CLK_PODF(n2-1); // &0x3f
  348. IOMUXC_GPR_GPR1 = (IOMUXC_GPR_GPR1 & ~(IOMUXC_GPR_GPR1_SAI1_MCLK1_SEL_MASK))
  349. | (IOMUXC_GPR_GPR1_SAI1_MCLK_DIR | IOMUXC_GPR_GPR1_SAI1_MCLK1_SEL(0)); //Select MCLK
  350. int rsync = 0;
  351. int tsync = 1;
  352. // configure transmitter
  353. I2S1_TMR = 0;
  354. I2S1_TCR1 = I2S_TCR1_RFW(0); // watermark
  355. I2S1_TCR2 = I2S_TCR2_SYNC(tsync) | I2S_TCR2_MSEL(1) | I2S_TCR2_BCD | I2S_TCR2_DIV(0);
  356. I2S1_TCR3 = I2S_TCR3_TCE;
  357. //4 Words per Frame 32 Bit Word-Length -> 128 Bit Frame-Length, MSB First:
  358. I2S1_TCR4 = I2S_TCR4_FRSZ(3) | I2S_TCR4_SYWD(0) | I2S_TCR4_MF | I2S_TCR4_FSP | I2S_TCR4_FSD;
  359. I2S1_TCR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31);
  360. //I2S1_RCSR = 0;
  361. I2S1_RMR = 0;
  362. //I2S1_RCSR = (1<<25); //Reset
  363. I2S1_RCR1 = I2S_RCR1_RFW(0);
  364. I2S1_RCR2 = I2S_RCR2_SYNC(rsync) | I2S_TCR2_MSEL(1) | I2S_TCR2_BCD | I2S_TCR2_DIV(0);
  365. I2S1_RCR3 = I2S_RCR3_RCE;
  366. I2S1_RCR4 = I2S_TCR4_FRSZ(3) | I2S_TCR4_SYWD(0) | I2S_TCR4_MF | I2S_TCR4_FSP | I2S_TCR4_FSD;
  367. I2S1_RCR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31);
  368. #if 0
  369. //debug only:
  370. CORE_PIN23_CONFIG = 3; //1:MCLK 11.43MHz
  371. CORE_PIN21_CONFIG = 3; //1:RX_BCLK 5.6 MHz
  372. CORE_PIN20_CONFIG = 3; //1:RX_SYNC 44.1 KHz
  373. // CORE_PIN6_CONFIG = 3; //1:TX_DATA0
  374. // CORE_PIN7_CONFIG = 3; //1:RX_DATA0
  375. #endif
  376. #endif
  377. }
  378. #if defined(KINETISL)
  379. void AudioOutputSPDIF::update(void)
  380. {
  381. audio_block_t *block;
  382. block = receiveReadOnly(0); // input 0 = left channel
  383. if (block) release(block);
  384. block = receiveReadOnly(1); // input 1 = right channel
  385. if (block) release(block);
  386. }
  387. #endif