You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

449 satır
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. PROGMEM
  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. CORE_PIN6_CONFIG = 3; //1:TX_DATA0
  75. const int nbytes_mlno = 2 * 4; // 8 Bytes per minor loop
  76. dma.TCD->SADDR = SPDIF_tx_buffer;
  77. dma.TCD->SOFF = 4;
  78. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(2) | DMA_TCD_ATTR_DSIZE(2);
  79. dma.TCD->NBYTES_MLNO = nbytes_mlno;
  80. dma.TCD->SLAST = -sizeof(SPDIF_tx_buffer);
  81. dma.TCD->DADDR = &I2S1_TDR0;
  82. dma.TCD->DOFF = 0;
  83. dma.TCD->CITER_ELINKNO = sizeof(SPDIF_tx_buffer) / nbytes_mlno;
  84. dma.TCD->DLASTSGA = 0;
  85. dma.TCD->BITER_ELINKNO = sizeof(SPDIF_tx_buffer) / nbytes_mlno;
  86. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  87. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_SAI1_TX);
  88. update_responsibility = update_setup();
  89. dma.enable();
  90. I2S1_RCSR |= I2S_RCSR_RE;
  91. I2S1_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE | I2S_TCSR_FR;
  92. dma.attachInterrupt(isr);
  93. #endif
  94. }
  95. /*
  96. http://www.hardwarebook.info/S/PDIF
  97. 1. To make it easier and a bit faster, the parity-bit is always the same.
  98. - With a alternating parity we had to adjust the next subframe. Instead, use a bit from the aux-info as parity.
  99. 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.
  100. -> A bit easier and faster to construct both subframes.
  101. */
  102. void AudioOutputSPDIF::isr(void)
  103. {
  104. static uint16_t frame = 0;
  105. const int16_t *src;
  106. int32_t *end, *dest;
  107. audio_block_t *block;
  108. uint32_t saddr, offset;
  109. uint16_t sample, lo, hi, aux;
  110. saddr = (uint32_t)(dma.TCD->SADDR);
  111. dma.clearInterrupt();
  112. if (saddr < (uint32_t)SPDIF_tx_buffer + sizeof(SPDIF_tx_buffer) / 2) {
  113. // DMA is transmitting the first half of the buffer
  114. // so we must fill the second half
  115. dest = (int32_t *)&SPDIF_tx_buffer[AUDIO_BLOCK_SAMPLES * 4/2];
  116. end = (int32_t *)&SPDIF_tx_buffer[AUDIO_BLOCK_SAMPLES * 4];
  117. if (AudioOutputSPDIF::update_responsibility) AudioStream::update_all();
  118. } else {
  119. // DMA is transmitting the second half of the buffer
  120. // so we must fill the first half
  121. dest = (int32_t *)SPDIF_tx_buffer;
  122. end = (int32_t *)&SPDIF_tx_buffer[AUDIO_BLOCK_SAMPLES * 4/2];
  123. }
  124. block = AudioOutputSPDIF::block_left_1st;
  125. if (block) {
  126. offset = AudioOutputSPDIF::block_left_offset;
  127. src = &block->data[offset];
  128. do {
  129. sample = *src++;
  130. //Subframe Channel 1
  131. hi = spdif_bmclookup[(uint8_t)(sample >> 8)];
  132. lo = spdif_bmclookup[(uint8_t) sample];
  133. lo ^= (~((int16_t)hi) >> 16);
  134. // 16 Bit sample:
  135. *(dest+1) = ((uint32_t)lo << 16) | hi;
  136. // 4 Bit Auxillary-audio-databits, the first used as parity
  137. aux = (0xB333 ^ (((uint32_t)((int16_t)lo)) >> 17));
  138. if (++frame > 191) {
  139. // VUCP-Bits ("Valid, Subcode, Channelstatus, Parity) = 0 (0xcc) | Preamble (depends on Framno.) | Auxillary
  140. *(dest+0) = vucp | (PREAMBLE_B << 16 ) | aux; //special preamble for one of 192 frames
  141. frame = 0;
  142. } else {
  143. *(dest+0) = vucp | (PREAMBLE_M << 16 ) | aux;
  144. }
  145. dest += 4;
  146. } while (dest < end);
  147. offset += AUDIO_BLOCK_SAMPLES/2;
  148. if (offset < AUDIO_BLOCK_SAMPLES) {
  149. AudioOutputSPDIF::block_left_offset = offset;
  150. } else {
  151. AudioOutputSPDIF::block_left_offset = 0;
  152. AudioStream::release(block);
  153. AudioOutputSPDIF::block_left_1st = AudioOutputSPDIF::block_left_2nd;
  154. AudioOutputSPDIF::block_left_2nd = NULL;
  155. }
  156. } else {
  157. do {
  158. if ( ++frame > 191 ) {
  159. *(dest+0) = vucp | 0x00e8cccc;
  160. frame = 0;
  161. } else {
  162. *(dest+0) = vucp | 0x00e2cccc;
  163. }
  164. *(dest+1) = 0xccccccccUL;
  165. dest +=4;
  166. } while (dest < end);
  167. }
  168. dest -= AUDIO_BLOCK_SAMPLES * 4/2 - 4/2;
  169. block = AudioOutputSPDIF::block_right_1st;
  170. if (block) {
  171. offset = AudioOutputSPDIF::block_right_offset;
  172. src = &block->data[offset];
  173. do {
  174. sample = *src++;
  175. //Subframe Channel 2
  176. hi = spdif_bmclookup[(uint8_t)(sample >> 8)];
  177. lo = spdif_bmclookup[(uint8_t)sample];
  178. lo ^= (~((int16_t)hi) >> 16);
  179. *(dest+1) = ( ((uint32_t)lo << 16) | hi );
  180. aux = (0xB333 ^ (((uint32_t)((int16_t)lo)) >> 17));
  181. *(dest+0) = vucp | (PREAMBLE_W << 16 ) | aux;
  182. dest += 4;
  183. } while (dest < end);
  184. offset += AUDIO_BLOCK_SAMPLES/2;
  185. if (offset < AUDIO_BLOCK_SAMPLES) {
  186. AudioOutputSPDIF::block_right_offset = offset;
  187. } else {
  188. AudioOutputSPDIF::block_right_offset = 0;
  189. AudioStream::release(block);
  190. AudioOutputSPDIF::block_right_1st = AudioOutputSPDIF::block_right_2nd;
  191. AudioOutputSPDIF::block_right_2nd = NULL;
  192. }
  193. } else {
  194. do {
  195. *dest = vucp | 0x00e4ccccUL;
  196. *(dest+1) = 0xccccccccUL;
  197. dest += 4 ;
  198. } while (dest < end);
  199. }
  200. #if IMXRT_CACHE_ENABLED >= 2
  201. dest -= AUDIO_BLOCK_SAMPLES * 4/2 + 4/2;
  202. arm_dcache_flush_delete(dest, sizeof(SPDIF_tx_buffer) / 2 );
  203. #endif
  204. }
  205. void AudioOutputSPDIF::mute_PCM(const bool mute)
  206. {
  207. vucp = mute?VUCP_INVALID:VUCP_VALID;
  208. }
  209. void AudioOutputSPDIF::update(void)
  210. {
  211. audio_block_t *block;
  212. block = receiveReadOnly(0); // input 0 = left channel
  213. if (block) {
  214. __disable_irq();
  215. if (block_left_1st == NULL) {
  216. block_left_1st = block;
  217. block_left_offset = 0;
  218. __enable_irq();
  219. } else if (block_left_2nd == NULL) {
  220. block_left_2nd = block;
  221. __enable_irq();
  222. } else {
  223. audio_block_t *tmp = block_left_1st;
  224. block_left_1st = block_left_2nd;
  225. block_left_2nd = block;
  226. block_left_offset = 0;
  227. __enable_irq();
  228. release(tmp);
  229. }
  230. }
  231. block = receiveReadOnly(1); // input 1 = right channel
  232. if (block) {
  233. __disable_irq();
  234. if (block_right_1st == NULL) {
  235. block_right_1st = block;
  236. block_right_offset = 0;
  237. __enable_irq();
  238. } else if (block_right_2nd == NULL) {
  239. block_right_2nd = block;
  240. __enable_irq();
  241. } else {
  242. audio_block_t *tmp = block_right_1st;
  243. block_right_1st = block_right_2nd;
  244. block_right_2nd = block;
  245. block_right_offset = 0;
  246. __enable_irq();
  247. release(tmp);
  248. }
  249. }
  250. }
  251. #if defined(KINETISK)
  252. #if F_CPU == 96000000 || F_CPU == 48000000 || F_CPU == 24000000
  253. // PLL is at 96 MHz in these modes
  254. #define MCLK_MULT 2
  255. #define MCLK_DIV 17
  256. #elif F_CPU == 72000000
  257. #define MCLK_MULT 8
  258. #define MCLK_DIV 51
  259. #elif F_CPU == 120000000
  260. #define MCLK_MULT 8
  261. #define MCLK_DIV 85
  262. #elif F_CPU == 144000000
  263. #define MCLK_MULT 4
  264. #define MCLK_DIV 51
  265. #elif F_CPU == 168000000
  266. #define MCLK_MULT 8
  267. #define MCLK_DIV 119
  268. #elif F_CPU == 180000000
  269. #define MCLK_MULT 16
  270. #define MCLK_DIV 255
  271. #define MCLK_SRC 0
  272. #elif F_CPU == 192000000
  273. #define MCLK_MULT 1
  274. #define MCLK_DIV 17
  275. #elif F_CPU == 216000000
  276. #define MCLK_MULT 8
  277. #define MCLK_DIV 153
  278. #define MCLK_SRC 0
  279. #elif F_CPU == 240000000
  280. #define MCLK_MULT 4
  281. #define MCLK_DIV 85
  282. #elif F_CPU == 16000000
  283. #define MCLK_MULT 12
  284. #define MCLK_DIV 17
  285. #else
  286. #error "This CPU Clock Speed is not supported by the Audio library";
  287. #endif
  288. #ifndef MCLK_SRC
  289. #if F_CPU >= 20000000
  290. #define MCLK_SRC 3 // the PLL
  291. #else
  292. #define MCLK_SRC 0 // system clock
  293. #endif
  294. #endif
  295. #endif
  296. PROGMEM
  297. void AudioOutputSPDIF::config_SPDIF(void)
  298. {
  299. #if defined(KINETISK)
  300. SIM_SCGC6 |= SIM_SCGC6_I2S;
  301. SIM_SCGC7 |= SIM_SCGC7_DMA;
  302. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  303. // enable MCLK output
  304. I2S0_MCR = I2S_MCR_MICS(MCLK_SRC) | I2S_MCR_MOE;
  305. while (I2S0_MCR & I2S_MCR_DUF) ;
  306. I2S0_MDR = I2S_MDR_FRACT((MCLK_MULT-1)) | I2S_MDR_DIVIDE((MCLK_DIV-1));
  307. // configure transmitter
  308. I2S0_TMR = 0;
  309. I2S0_TCR1 = I2S_TCR1_TFW(1); // watermark
  310. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_MSEL(1) | I2S_TCR2_BCD | I2S_TCR2_DIV(0);
  311. I2S0_TCR3 = I2S_TCR3_TCE;
  312. //4 Words per Frame 32 Bit Word-Length -> 128 Bit Frame-Length, MSB First:
  313. I2S0_TCR4 = I2S_TCR4_FRSZ(3) | I2S_TCR4_SYWD(0) | I2S_TCR4_MF | I2S_TCR4_FSP | I2S_TCR4_FSD;
  314. I2S0_TCR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31);
  315. I2S0_RCSR = 0;
  316. #if 0
  317. // configure pin mux for 3 clock signals (debug only)
  318. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK) 44.1kHz
  319. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK 5.6 MHz
  320. CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK 11.43MHz
  321. #endif
  322. #elif defined(__IMXRT1052__) || defined(__IMXRT1062__)
  323. CCM_CCGR5 |= CCM_CCGR5_SAI1(CCM_CCGR_ON);
  324. //PLL:
  325. int fs = AUDIO_SAMPLE_RATE_EXACT;
  326. // PLL between 27*24 = 648MHz und 54*24=1296MHz
  327. int n1 = 4; //SAI prescaler 4 => (n1*n2) = multiple of 4
  328. int n2 = 1 + (24000000 * 27) / (fs * 256 * n1);
  329. double C = ((double)fs * 256 * n1 * n2) / 24000000;
  330. int c0 = C;
  331. int c2 = 10000;
  332. int c1 = C * c2 - (c0 * c2);
  333. set_audioClock(c0, c1, c2);
  334. CCM_CSCMR1 = (CCM_CSCMR1 & ~(CCM_CSCMR1_SAI1_CLK_SEL_MASK))
  335. | CCM_CSCMR1_SAI1_CLK_SEL(2); // &0x03 // (0,1,2): PLL3PFD0, PLL5, PLL4
  336. CCM_CS1CDR = (CCM_CS1CDR & ~(CCM_CS1CDR_SAI1_CLK_PRED_MASK | CCM_CS1CDR_SAI1_CLK_PODF_MASK))
  337. | CCM_CS1CDR_SAI1_CLK_PRED(n1-1) // &0x07
  338. | CCM_CS1CDR_SAI1_CLK_PODF(n2-1); // &0x3f
  339. IOMUXC_GPR_GPR1 = (IOMUXC_GPR_GPR1 & ~(IOMUXC_GPR_GPR1_SAI1_MCLK1_SEL_MASK))
  340. | (IOMUXC_GPR_GPR1_SAI1_MCLK_DIR | IOMUXC_GPR_GPR1_SAI1_MCLK1_SEL(0)); //Select MCLK
  341. int rsync = 0;
  342. int tsync = 1;
  343. // configure transmitter
  344. I2S1_TMR = 0;
  345. I2S1_TCR1 = I2S_TCR1_RFW(0); // watermark
  346. I2S1_TCR2 = I2S_TCR2_SYNC(tsync) | I2S_TCR2_MSEL(1) | I2S_TCR2_BCD | I2S_TCR2_DIV(0);
  347. I2S1_TCR3 = I2S_TCR3_TCE;
  348. //4 Words per Frame 32 Bit Word-Length -> 128 Bit Frame-Length, MSB First:
  349. I2S1_TCR4 = I2S_TCR4_FRSZ(3) | I2S_TCR4_SYWD(0) | I2S_TCR4_MF | I2S_TCR4_FSP | I2S_TCR4_FSD;
  350. I2S1_TCR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31);
  351. //I2S1_RCSR = 0;
  352. I2S1_RMR = 0;
  353. //I2S1_RCSR = (1<<25); //Reset
  354. I2S1_RCR1 = I2S_RCR1_RFW(0);
  355. I2S1_RCR2 = I2S_RCR2_SYNC(rsync) | I2S_TCR2_MSEL(1) | I2S_TCR2_BCD | I2S_TCR2_DIV(0);
  356. I2S1_RCR3 = I2S_RCR3_RCE;
  357. I2S1_RCR4 = I2S_TCR4_FRSZ(3) | I2S_TCR4_SYWD(0) | I2S_TCR4_MF | I2S_TCR4_FSP | I2S_TCR4_FSD;
  358. I2S1_RCR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31);
  359. #if 0
  360. //debug only:
  361. CORE_PIN23_CONFIG = 3; //1:MCLK 11.43MHz
  362. CORE_PIN21_CONFIG = 3; //1:RX_BCLK 5.6 MHz
  363. CORE_PIN20_CONFIG = 3; //1:RX_SYNC 44.1 KHz
  364. // CORE_PIN6_CONFIG = 3; //1:TX_DATA0
  365. // CORE_PIN7_CONFIG = 3; //1:RX_DATA0
  366. #endif
  367. #endif
  368. }
  369. #if defined(KINETISL)
  370. void AudioOutputSPDIF::update(void)
  371. {
  372. audio_block_t *block;
  373. block = receiveReadOnly(0); // input 0 = left channel
  374. if (block) release(block);
  375. block = receiveReadOnly(1); // input 1 = right channel
  376. if (block) release(block);
  377. }
  378. #endif