|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
-
-
-
- #if defined(__IMXRT1052__) || defined(__IMXRT1062__)
- #include <Arduino.h>
- #include "input_spdif3.h"
- #include "output_spdif3.h"
- #include "utility/imxrt_hw.h"
-
- DMAMEM __attribute__((aligned(32)))
- static uint32_t spdif_rx_buffer[AUDIO_BLOCK_SAMPLES * 4];
- audio_block_t * AudioInputSPDIF3::block_left = NULL;
- audio_block_t * AudioInputSPDIF3::block_right = NULL;
- uint16_t AudioInputSPDIF3::block_offset = 0;
- bool AudioInputSPDIF3::update_responsibility = false;
- DMAChannel AudioInputSPDIF3::dma(false);
-
- FLASHMEM
- void AudioInputSPDIF3::begin(void)
- {
- dma.begin(true);
-
- AudioOutputSPDIF3::config_spdif3();
-
- const int nbytes_mlno = 2 * 4;
- dma.TCD->SADDR = &SPDIF_SRL;
- dma.TCD->SOFF = 4;
- dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(2) | DMA_TCD_ATTR_DSIZE(2);
- dma.TCD->NBYTES_MLNO = DMA_TCD_NBYTES_MLOFFYES_NBYTES(nbytes_mlno) | DMA_TCD_NBYTES_SMLOE |
- DMA_TCD_NBYTES_MLOFFYES_MLOFF(-8);
- dma.TCD->SLAST = -8;
- dma.TCD->DADDR = spdif_rx_buffer;
- dma.TCD->DOFF = 4;
- dma.TCD->DLASTSGA = -sizeof(spdif_rx_buffer);
- dma.TCD->CITER_ELINKNO = sizeof(spdif_rx_buffer) / nbytes_mlno;
- dma.TCD->BITER_ELINKNO = sizeof(spdif_rx_buffer) / nbytes_mlno;
- dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
-
- dma.triggerAtHardwareEvent(DMAMUX_SOURCE_SPDIF_RX);
- update_responsibility = update_setup();
- dma.attachInterrupt(isr);
- dma.enable();
-
- SPDIF_SRCD = 0;
- SPDIF_SCR |= SPDIF_SCR_DMA_RX_EN;
- CORE_PIN15_CONFIG = 3;
- IOMUXC_SPDIF_IN_SELECT_INPUT = 0;
-
- }
-
- void AudioInputSPDIF3::isr(void)
- {
- uint32_t daddr, offset;
- const int32_t *src, *end;
- int16_t *dest_left, *dest_right;
- audio_block_t *left, *right;
-
- dma.clearInterrupt();
-
- if (AudioInputSPDIF3::update_responsibility) AudioStream::update_all();
-
- daddr = (uint32_t)(dma.TCD->DADDR);
-
- if (daddr < (uint32_t)spdif_rx_buffer + sizeof(spdif_rx_buffer) / 2) {
-
-
- src = (int32_t *)&spdif_rx_buffer[AUDIO_BLOCK_SAMPLES * 2];
- end = (int32_t *)&spdif_rx_buffer[AUDIO_BLOCK_SAMPLES * 4];
- } else {
-
-
- src = (int32_t *)&spdif_rx_buffer[0];
- end = (int32_t *)&spdif_rx_buffer[AUDIO_BLOCK_SAMPLES*2];
- }
-
- left = AudioInputSPDIF3::block_left;
- right = AudioInputSPDIF3::block_right;
-
- if (left != NULL && right != NULL) {
- offset = AudioInputSPDIF3::block_offset;
- if (offset <= AUDIO_BLOCK_SAMPLES*2) {
- dest_left = &(left->data[offset]);
- dest_right = &(right->data[offset]);
- AudioInputSPDIF3::block_offset = offset + AUDIO_BLOCK_SAMPLES*2;
-
- do {
- #if IMXRT_CACHE_ENABLED >=1
- SCB_CACHE_DCIMVAC = (uintptr_t)src;
- asm("dsb":::"memory");
- #endif
-
- *dest_left++ = (*src++) >> 8;
- *dest_right++ = (*src++) >> 8;
-
- *dest_left++ = (*src++) >> 8;
- *dest_right++ = (*src++) >> 8;
-
- *dest_left++ = (*src++) >> 8;
- *dest_right++ = (*src++) >> 8;
-
- *dest_left++ = (*src++) >> 8;
- *dest_right++ = (*src++) >> 8;
-
- } while (src < end);
- }
- }
- else if (left != NULL) {
- offset = AudioInputSPDIF3::block_offset;
- if (offset <= AUDIO_BLOCK_SAMPLES*2) {
- dest_left = &(left->data[offset]);
- AudioInputSPDIF3::block_offset = offset + AUDIO_BLOCK_SAMPLES*2;
-
- do {
- #if IMXRT_CACHE_ENABLED >=1
- SCB_CACHE_DCIMVAC = (uintptr_t)src;
- asm("dsb":::"memory");
- #endif
-
- *dest_left++ = (*src++) >> 8;
- src++;
-
- *dest_left++ = (*src++) >> 8;
- src++;
-
- *dest_left++ = (*src++) >> 8;
- src++;
-
- *dest_left++ = (*src++) >> 8;
- src++;
-
- } while (src < end);
- }
- }
- else if (right != NULL) {
- offset = AudioInputSPDIF3::block_offset;
- if (offset <= AUDIO_BLOCK_SAMPLES*2) {
- dest_right = &(right->data[offset]);
- AudioInputSPDIF3::block_offset = offset + AUDIO_BLOCK_SAMPLES*2;
-
- do {
- #if IMXRT_CACHE_ENABLED >=1
- SCB_CACHE_DCIMVAC = (uintptr_t)src;
- asm("dsb":::"memory");
- #endif
-
- src++;
- *dest_right++ = (*src++) >> 8;
-
- src++;
- *dest_right++ = (*src++) >> 8;
-
- src++;
- *dest_right++ = (*src++) >> 8;
-
- src++;
- *dest_right++ = (*src++) >> 8;
-
- } while (src < end);
- }
- }
-
- }
-
-
- void AudioInputSPDIF3::update(void)
- {
- audio_block_t *new_left=NULL, *new_right=NULL, *out_left=NULL, *out_right=NULL;
-
-
- new_left = allocate();
- if (new_left != NULL) {
- new_right = allocate();
- if (new_right == NULL) {
- release(new_left);
- new_left = NULL;
- }
- }
- __disable_irq();
- if (block_offset >= AUDIO_BLOCK_SAMPLES) {
-
-
- out_left = block_left;
- block_left = new_left;
- out_right = block_right;
- block_right = new_right;
- block_offset = 0;
- __enable_irq();
-
- transmit(out_left, 0);
- release(out_left);
- transmit(out_right, 1);
- release(out_right);
-
- } else if (new_left != NULL) {
-
- if (block_left == NULL) {
-
-
- block_left = new_left;
- block_right = new_right;
- block_offset = 0;
- __enable_irq();
- } else {
-
- __enable_irq();
- release(new_left);
- release(new_right);
- }
- } else {
-
-
-
- __enable_irq();
- }
- }
-
- bool AudioInputSPDIF3::pllLocked(void)
- {
- return (SPDIF_SRPC & SPDIF_SRPC_LOCK) == SPDIF_SRPC_LOCK ? true:false;
- }
-
- unsigned int AudioInputSPDIF3::sampleRate(void) {
- if (!pllLocked()) return 0;
- return (float)((uint64_t)F_BUS_ACTUAL * SPDIF_SRFM) / (0x8000000ULL * AudioOutputSPDIF3::dpll_Gain()) + 0.5F;
- }
-
- #endif
|