|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
-
-
-
- #if defined(__IMXRT1062__)
- #include <Arduino.h>
- #include "input_i2s2.h"
- #include "output_i2s2.h"
-
- DMAMEM __attribute__((aligned(32))) static uint32_t i2s2_rx_buffer[AUDIO_BLOCK_SAMPLES];
- audio_block_t * AudioInputI2S2::block_left = NULL;
- audio_block_t * AudioInputI2S2::block_right = NULL;
- uint16_t AudioInputI2S2::block_offset = 0;
- bool AudioInputI2S2::update_responsibility = false;
- DMAChannel AudioInputI2S2::dma(false);
-
-
- void AudioInputI2S2::begin(void)
- {
- dma.begin(true);
-
-
-
-
-
- AudioOutputI2S2::config_i2s();
-
- CORE_PIN5_CONFIG = 2;
- IOMUXC_SAI2_RX_DATA0_SELECT_INPUT = 0;
-
- dma.TCD->SADDR = (void *)((uint32_t)&I2S2_RDR0+2);
- dma.TCD->SOFF = 0;
- dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
- dma.TCD->NBYTES_MLNO = 2;
- dma.TCD->SLAST = 0;
- dma.TCD->DADDR = i2s2_rx_buffer;
- dma.TCD->DOFF = 2;
- dma.TCD->CITER_ELINKNO = sizeof(i2s2_rx_buffer) / 2;
- dma.TCD->DLASTSGA = -sizeof(i2s2_rx_buffer);
- dma.TCD->BITER_ELINKNO = sizeof(i2s2_rx_buffer) / 2;
- dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
- dma.triggerAtHardwareEvent(DMAMUX_SOURCE_SAI2_RX);
- dma.enable();
-
- I2S2_RCSR = I2S_RCSR_RE | I2S_RCSR_BCE | I2S_RCSR_FRDE | I2S_RCSR_FR;
- I2S2_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE;
-
- update_responsibility = update_setup();
- dma.attachInterrupt(isr);
- }
-
- void AudioInputI2S2::isr(void)
- {
- uint32_t daddr, offset;
- const int16_t *src, *end;
- int16_t *dest_left, *dest_right;
- audio_block_t *left, *right;
-
- daddr = (uint32_t)(dma.TCD->DADDR);
- dma.clearInterrupt();
-
- if (daddr < (uint32_t)i2s2_rx_buffer + sizeof(i2s2_rx_buffer) / 2) {
-
-
- src = (int16_t *)&i2s2_rx_buffer[AUDIO_BLOCK_SAMPLES/2];
- end = (int16_t *)&i2s2_rx_buffer[AUDIO_BLOCK_SAMPLES];
- if (AudioInputI2S2::update_responsibility) AudioStream::update_all();
- } else {
-
-
- src = (int16_t *)&i2s2_rx_buffer[0];
- end = (int16_t *)&i2s2_rx_buffer[AUDIO_BLOCK_SAMPLES/2];
- }
- left = AudioInputI2S2::block_left;
- right = AudioInputI2S2::block_right;
- if (left != NULL && right != NULL) {
- offset = AudioInputI2S2::block_offset;
- if (offset <= AUDIO_BLOCK_SAMPLES/2) {
- dest_left = &(left->data[offset]);
- dest_right = &(right->data[offset]);
- AudioInputI2S2::block_offset = offset + AUDIO_BLOCK_SAMPLES/2;
-
- arm_dcache_delete((void*)src, sizeof(i2s2_rx_buffer) / 2);
-
- do {
- *dest_left++ = *src++;
- *dest_right++ = *src++;
- } while (src < end);
- }
- }
- }
-
-
-
- void AudioInputI2S2::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();
- }
- }
-
-
-
-
- #if 0
- void AudioInputI2S2slave::begin(void)
- {
- dma.begin(true);
-
-
-
-
- AudioOutputI2S2slave::config_i2s();
-
- CORE_PIN33_CONFIG = 2;
- IOMUXC_SAI2_RX_DATA0_SELECT_INPUT = 0;
-
- dma.TCD->SADDR = (void *)((uint32_t)&I2S2_RDR0 + 2);
- dma.TCD->SOFF = 0;
- dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
- dma.TCD->NBYTES_MLNO = 2;
- dma.TCD->SLAST = 0;
- dma.TCD->DADDR = i2s2_rx_buffer;
- dma.TCD->DOFF = 2;
- dma.TCD->CITER_ELINKNO = sizeof(i2s2_rx_buffer) / 2;
- dma.TCD->DLASTSGA = -sizeof(i2s2_rx_buffer);
- dma.TCD->BITER_ELINKNO = sizeof(i2s2_rx_buffer) / 2;
- 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);
-
- }
- #endif
- #endif
|