Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

289 linhas
8.8KB

  1. /* Hardware-SPDIF for Teensy 4
  2. * Copyright (c) 2019, Frank Bösing, f.boesing@gmx.de
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice, development funding notice, and this permission
  12. * notice shall be included in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. /*
  23. http://www.hardwarebook.info/S/PDIF
  24. https://www.mikrocontroller.net/articles/S/PDIF
  25. https://en.wikipedia.org/wiki/S/PDIF
  26. */
  27. #if defined(__IMXRT1052__) || defined(__IMXRT1062__)
  28. #include <Arduino.h>
  29. #include "output_spdif3.h"
  30. #include "utility/imxrt_hw.h"
  31. #include "memcpy_audio.h"
  32. #include <math.h>
  33. audio_block_t * AudioOutputSPDIF3::block_left_1st = nullptr;
  34. audio_block_t * AudioOutputSPDIF3::block_right_1st = nullptr;
  35. audio_block_t * AudioOutputSPDIF3::block_left_2nd = nullptr;
  36. audio_block_t * AudioOutputSPDIF3::block_right_2nd = nullptr;
  37. bool AudioOutputSPDIF3::update_responsibility = false;
  38. DMAChannel AudioOutputSPDIF3::dma(false);
  39. DMAMEM __attribute__((aligned(32)))
  40. static int32_t SPDIF_tx_buffer[AUDIO_BLOCK_SAMPLES * 4];
  41. DMAMEM __attribute__((aligned(32)))
  42. audio_block_t AudioOutputSPDIF3::block_silent;
  43. #define SPDIF_DPLL_GAIN24 0
  44. #define SPDIF_DPLL_GAIN16 1
  45. #define SPDIF_DPLL_GAIN12 2
  46. #define SPDIF_DPLL_GAIN8 3
  47. #define SPDIF_DPLL_GAIN6 4
  48. #define SPDIF_DPLL_GAIN4 5
  49. #define SPDIF_DPLL_GAIN3 6
  50. #define SPDIF_DPLL_GAIN1 7
  51. #define SPDIF_DPLL_GAIN SPDIF_DPLL_GAIN8 //Actual Gain
  52. static const uint8_t spdif_gain[8] = {24, 16, 12, 8, 6, 4, 3, 1};
  53. FLASHMEM
  54. void AudioOutputSPDIF3::begin(void)
  55. {
  56. dma.begin(true); // Allocate the DMA channel first
  57. block_left_1st = nullptr;
  58. block_right_1st = nullptr;
  59. memset(&block_silent, 0, sizeof(block_silent));
  60. config_spdif3();
  61. const int nbytes_mlno = 2 * 4; // 8 Bytes per minor loop
  62. dma.TCD->SADDR = SPDIF_tx_buffer;
  63. dma.TCD->SOFF = 4;
  64. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(2) | DMA_TCD_ATTR_DSIZE(2);
  65. dma.TCD->NBYTES_MLNO = DMA_TCD_NBYTES_MLOFFYES_NBYTES(nbytes_mlno) | DMA_TCD_NBYTES_DMLOE |
  66. DMA_TCD_NBYTES_MLOFFYES_MLOFF(-8);
  67. dma.TCD->SLAST = -sizeof(SPDIF_tx_buffer);
  68. dma.TCD->DADDR = &SPDIF_STL;
  69. dma.TCD->DOFF = 4;
  70. dma.TCD->DLASTSGA = -8;
  71. //dma.TCD->ATTR_DST = ((31 - __builtin_clz(8)) << 3);
  72. dma.TCD->CITER_ELINKNO = sizeof(SPDIF_tx_buffer) / nbytes_mlno;
  73. dma.TCD->BITER_ELINKNO = sizeof(SPDIF_tx_buffer) / nbytes_mlno;
  74. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  75. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_SPDIF_TX);
  76. update_responsibility = update_setup();
  77. dma.enable();
  78. dma.attachInterrupt(isr);
  79. CORE_PIN14_CONFIG = 3; //3:SPDIF_OUT
  80. SPDIF_SCR |= SPDIF_SCR_DMA_TX_EN;
  81. SPDIF_STC |= SPDIF_STC_TX_ALL_CLK_EN;
  82. // pinMode(13, OUTPUT);
  83. }
  84. void AudioOutputSPDIF3::isr(void)
  85. {
  86. const int16_t *src_left, *src_right;
  87. const int32_t *end;
  88. int32_t *dest;
  89. audio_block_t *block_left, *block_right;
  90. uint32_t saddr;
  91. saddr = (uint32_t)(dma.TCD->SADDR);
  92. dma.clearInterrupt();
  93. if (saddr < (uint32_t)SPDIF_tx_buffer + sizeof(SPDIF_tx_buffer) / 2) {
  94. // DMA is transmitting the first half of the buffer
  95. // so we must fill the second half
  96. dest = SPDIF_tx_buffer + AUDIO_BLOCK_SAMPLES*2;
  97. end = SPDIF_tx_buffer + AUDIO_BLOCK_SAMPLES*4;
  98. } else {
  99. // DMA is transmitting the second half of the buffer
  100. // so we must fill the first half
  101. dest = SPDIF_tx_buffer;
  102. end = SPDIF_tx_buffer + AUDIO_BLOCK_SAMPLES*2;
  103. }
  104. block_left = block_left_1st;
  105. if (!block_left) block_left = &block_silent;
  106. block_right = block_right_1st;
  107. if (!block_right) block_right = &block_silent;
  108. src_left = (const int16_t *)(block_left->data);
  109. src_right = (const int16_t *)(block_right->data);
  110. do {
  111. #if IMXRT_CACHE_ENABLED >= 2
  112. SCB_CACHE_DCCIMVAC = (uintptr_t) dest;
  113. asm volatile("dsb");
  114. #endif
  115. *dest++ = (*src_left++) << 8;
  116. *dest++ = (*src_right++) << 8;
  117. *dest++ = (*src_left++) << 8;
  118. *dest++ = (*src_right++) << 8;
  119. *dest++ = (*src_left++) << 8;
  120. *dest++ = (*src_right++) << 8;
  121. *dest++ = (*src_left++) << 8;
  122. *dest++ = (*src_right++) << 8;
  123. } while (dest < end);
  124. if (block_left != &block_silent) {
  125. release(block_left);
  126. block_left_1st = block_left_2nd;
  127. block_left_2nd = nullptr;
  128. }
  129. if (block_right != &block_silent) {
  130. release(block_right);
  131. block_right_1st = block_right_2nd;
  132. block_right_2nd = nullptr;
  133. }
  134. if (update_responsibility) update_all();
  135. //digitalWriteFast(13,!digitalReadFast(13));
  136. }
  137. void AudioOutputSPDIF3::update(void)
  138. {
  139. audio_block_t *block_left, *block_right;
  140. block_left = receiveReadOnly(0); // input 0
  141. block_right = receiveReadOnly(1); // input 1
  142. __disable_irq();
  143. if (block_left) {
  144. if (block_left_1st == nullptr) {
  145. block_left_1st = block_left;
  146. block_left = nullptr;
  147. } else if (block_left_2nd == nullptr) {
  148. block_left_2nd = block_left;
  149. block_left = nullptr;
  150. } else {
  151. audio_block_t *tmp = block_left_1st;
  152. block_left_1st = block_left_2nd;
  153. block_left_2nd = block_left;
  154. block_left = tmp;
  155. }
  156. }
  157. if (block_right) {
  158. if (block_right_1st == nullptr) {
  159. block_right_1st = block_right;
  160. block_right = nullptr;
  161. } else if (block_right_2nd == nullptr) {
  162. block_right_2nd = block_right;
  163. block_right = nullptr;
  164. } else {
  165. audio_block_t *tmp = block_right_1st;
  166. block_right_1st = block_right_2nd;
  167. block_right_2nd = block_right;
  168. block_right = tmp;
  169. }
  170. }
  171. __enable_irq();
  172. if (block_left) {
  173. release(block_left);
  174. }
  175. if (block_right) {
  176. release(block_right);
  177. }
  178. }
  179. void AudioOutputSPDIF3::mute_PCM(const bool mute)
  180. {
  181. if (mute)
  182. SPDIF_SCR |= SPDIF_SCR_VALCTRL;
  183. else
  184. SPDIF_SCR &= ~SPDIF_SCR_VALCTRL;
  185. }
  186. uint32_t AudioOutputSPDIF3::dpll_Gain(void)
  187. {
  188. return spdif_gain[SPDIF_DPLL_GAIN];
  189. }
  190. PROGMEM
  191. void AudioOutputSPDIF3::config_spdif3(void)
  192. {
  193. delay(1); //WHY IS THIS NEEDED?
  194. uint32_t fs = AUDIO_SAMPLE_RATE_EXACT;
  195. // PLL between 27*24 = 648MHz und 54*24=1296MHz
  196. // n1, n2 choosen for compatibility with I2S (same PLL frequency) :
  197. int n1 = 4; //SAI prescaler 4 => (n1*n2) = multiple of 4
  198. int n2 = 1 + (24000000 * 27) / (fs * 256 * n1);
  199. double C = ((double)fs * 256 * n1 * n2) / 24000000;
  200. int c0 = C;
  201. int c2 = 10000;
  202. int c1 = C * c2 - (c0 * c2);
  203. set_audioClock(c0, c1, c2);
  204. //use new pred/podf values
  205. n1 = 7; //0: divide by 1 (do not use with high input frequencies), 1:/2, 2: /3, 7:/8
  206. n2 = 0; //0: divide by 1, 7: divide by 8
  207. CCM_CCGR5 &= ~CCM_CCGR5_SPDIF(CCM_CCGR_ON); //Clock gate off
  208. CCM_CDCDR = (CCM_CDCDR & ~(CCM_CDCDR_SPDIF0_CLK_SEL_MASK | CCM_CDCDR_SPDIF0_CLK_PRED_MASK | CCM_CDCDR_SPDIF0_CLK_PODF_MASK))
  209. | CCM_CDCDR_SPDIF0_CLK_SEL(0) // 0 PLL4, 1 PLL3 PFD2, 2 PLL5, 3 pll3_sw_clk
  210. | CCM_CDCDR_SPDIF0_CLK_PRED(n1)
  211. | CCM_CDCDR_SPDIF0_CLK_PODF(n2);
  212. CCM_CCGR5 |= CCM_CCGR5_SPDIF(CCM_CCGR_ON); //Clock gate on
  213. if (!(SPDIF_SCR & (SPDIF_SCR_DMA_RX_EN | SPDIF_SCR_DMA_TX_EN))) {
  214. //Serial.print("Reset SPDIF3");
  215. SPDIF_SCR = SPDIF_SCR_SOFT_RESET; //Reset SPDIF
  216. while (SPDIF_SCR & SPDIF_SCR_SOFT_RESET) {;} //Wait for Reset (takes 8 cycles)
  217. } else return;
  218. SPDIF_SCR =
  219. SPDIF_SCR_RXFIFOFULL_SEL(0) | // Full interrupt if at least 1 sample in Rx left and right FIFOs
  220. SPDIF_SCR_RXAUTOSYNC |
  221. SPDIF_SCR_TXAUTOSYNC |
  222. SPDIF_SCR_TXFIFOEMPTY_SEL(2) | // Empty interrupt if at most 8 samples in Tx left and right FIFOs
  223. SPDIF_SCR_TXFIFO_CTRL(1) | // 0:Send zeros 1: normal operation
  224. SPDIF_SCR_VALCTRL | // Outgoing Validity always clear
  225. SPDIF_SCR_TXSEL(5) | // 0:off and output 0, 1:Feed-though SPDIFIN, 5:Tx Normal operation
  226. SPDIF_SCR_USRC_SEL(3);
  227. SPDIF_SRPC =
  228. SPDIF_SRPC_CLKSRC_SEL(1) | //if (DPLL Locked) SPDIF_RxClk else tx_clk (SPDIF0_CLK_ROOT)
  229. SPDIF_SRPC_GAINSEL(SPDIF_DPLL_GAIN);
  230. uint32_t pllclock = (c0 + (float)c1 / c2) * 24000000ULL; //677376000 Hz
  231. uint32_t clock = pllclock / (1 + n1) / (1 + n2);
  232. uint32_t clkdiv = clock / (fs * 64); // 1 .. 128
  233. uint32_t mod = clock % (fs * 64);
  234. if (mod > ((fs * 64) / 2)) clkdiv += 1; //nearest divider
  235. #if 0
  236. Serial.printf("PLL: %d\n", pllclock);
  237. Serial.printf("clock: %d\n", clock);
  238. Serial.printf("clkdiv: %d\n", clkdiv);
  239. #endif
  240. SPDIF_STC =
  241. SPDIF_STC_TXCLK_SOURCE(1) | //tx_clk input (from SPDIF0_CLK_ROOT)
  242. SPDIF_STC_TXCLK_DF(clkdiv - 1);
  243. }
  244. #endif