|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
-
-
- #if defined(__IMXRT1062__)
- #include <Arduino.h>
- #include "input_tdm2.h"
- #include "output_tdm2.h"
- #include "utility/imxrt_hw.h"
-
- DMAMEM __attribute__((aligned(32)))
- static uint32_t tdm_rx_buffer[AUDIO_BLOCK_SAMPLES*16];
- audio_block_t * AudioInputTDM2::block_incoming[16] = {
- nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
- nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr
- };
- bool AudioInputTDM2::update_responsibility = false;
- DMAChannel AudioInputTDM2::dma(false);
-
-
- void AudioInputTDM2::begin(void)
- {
- dma.begin(true);
-
-
- AudioOutputTDM2::config_tdm();
-
- CORE_PIN5_CONFIG = 2;
- IOMUXC_SAI2_RX_DATA0_SELECT_INPUT = 0;
- dma.TCD->SADDR = &I2S2_RDR0;
- dma.TCD->SOFF = 0;
- dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(2) | DMA_TCD_ATTR_DSIZE(2);
- dma.TCD->NBYTES_MLNO = 4;
- dma.TCD->SLAST = 0;
- dma.TCD->DADDR = tdm_rx_buffer;
- dma.TCD->DOFF = 4;
- dma.TCD->CITER_ELINKNO = sizeof(tdm_rx_buffer) / 4;
- dma.TCD->DLASTSGA = -sizeof(tdm_rx_buffer);
- dma.TCD->BITER_ELINKNO = sizeof(tdm_rx_buffer) / 4;
- dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
- dma.triggerAtHardwareEvent(DMAMUX_SOURCE_SAI2_RX);
- update_responsibility = update_setup();
- dma.enable();
-
- I2S2_RCSR |= I2S_RCSR_RE | I2S_RCSR_BCE | I2S_RCSR_FRDE | I2S_RCSR_FR;
- I2S2_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE;
- dma.attachInterrupt(isr);
-
- }
-
-
- static void memcpy_tdm_rx(uint32_t *dest1, uint32_t *dest2, const uint32_t *src)
- {
- uint32_t i, in1, in2;
-
- for (i=0; i < AUDIO_BLOCK_SAMPLES/2; i++) {
- in1 = *src;
- in2 = *(src+8);
- src += 16;
- *dest1++ = (in1 >> 16) | (in2 & 0xFFFF0000);
- *dest2++ = (in1 << 16) | (in2 & 0x0000FFFF);
- }
- }
-
- void AudioInputTDM2::isr(void)
- {
- uint32_t daddr;
- const uint32_t *src;
- unsigned int i;
-
- daddr = (uint32_t)(dma.TCD->DADDR);
- dma.clearInterrupt();
-
- if (daddr < (uint32_t)tdm_rx_buffer + sizeof(tdm_rx_buffer) / 2) {
-
-
- src = &tdm_rx_buffer[AUDIO_BLOCK_SAMPLES*8];
- } else {
-
-
- src = &tdm_rx_buffer[0];
- }
- if (block_incoming[0] != nullptr) {
- #if IMXRT_CACHE_ENABLED >=1
- arm_dcache_delete((void*)src, sizeof(tdm_rx_buffer) / 2);
- #endif
- for (i=0; i < 16; i += 2) {
- uint32_t *dest1 = (uint32_t *)(block_incoming[i]->data);
- uint32_t *dest2 = (uint32_t *)(block_incoming[i+1]->data);
- memcpy_tdm_rx(dest1, dest2, src);
- src++;
- }
- }
- if (update_responsibility) update_all();
- }
-
-
- void AudioInputTDM2::update(void)
- {
- unsigned int i, j;
- audio_block_t *new_block[16];
- audio_block_t *out_block[16];
-
-
- for (i=0; i < 16; i++) {
- new_block[i] = allocate();
- if (new_block[i] == nullptr) {
- for (j=0; j < i; j++) {
- release(new_block[j]);
- }
- memset(new_block, 0, sizeof(new_block));
- break;
- }
- }
- __disable_irq();
- memcpy(out_block, block_incoming, sizeof(out_block));
- memcpy(block_incoming, new_block, sizeof(block_incoming));
- __enable_irq();
- if (out_block[0] != nullptr) {
-
- for (i=0; i < 16; i++) {
- transmit(out_block[i], i);
- release(out_block[i]);
- }
- }
- }
-
-
- #endif
|