PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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