| #include "input_i2s2.h" | #include "input_i2s2.h" | ||||
| #include "input_i2s_quad.h" | #include "input_i2s_quad.h" | ||||
| #include "input_tdm.h" | #include "input_tdm.h" | ||||
| #include "input_tdm2.h" | |||||
| #include "input_pdm.h" | #include "input_pdm.h" | ||||
| #include "mixer.h" | #include "mixer.h" | ||||
| #include "output_dac.h" | #include "output_dac.h" |
| #include <Arduino.h> | #include <Arduino.h> | ||||
| #include "input_tdm.h" | #include "input_tdm.h" | ||||
| #include "output_tdm.h" | #include "output_tdm.h" | ||||
| #if defined(KINETISK) | |||||
| #if defined(KINETISK) || defined(__IMXRT1052__) || defined(__IMXRT1062__) | |||||
| #include "utility/imxrt_hw.h" | |||||
| DMAMEM static uint32_t tdm_rx_buffer[AUDIO_BLOCK_SAMPLES*16]; | |||||
| DMAMEM __attribute__((aligned(32))) | |||||
| static uint32_t tdm_rx_buffer[AUDIO_BLOCK_SAMPLES*16]; | |||||
| audio_block_t * AudioInputTDM::block_incoming[16] = { | audio_block_t * AudioInputTDM::block_incoming[16] = { | ||||
| NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |||||
| NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL | |||||
| nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, | |||||
| nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr | |||||
| }; | }; | ||||
| bool AudioInputTDM::update_responsibility = false; | bool AudioInputTDM::update_responsibility = false; | ||||
| DMAChannel AudioInputTDM::dma(false); | DMAChannel AudioInputTDM::dma(false); | ||||
| // TODO: should we set & clear the I2S_RCSR_SR bit here? | // TODO: should we set & clear the I2S_RCSR_SR bit here? | ||||
| AudioOutputTDM::config_tdm(); | AudioOutputTDM::config_tdm(); | ||||
| #if defined(KINETISK) | |||||
| CORE_PIN13_CONFIG = PORT_PCR_MUX(4); // pin 13, PTC5, I2S0_RXD0 | CORE_PIN13_CONFIG = PORT_PCR_MUX(4); // pin 13, PTC5, I2S0_RXD0 | ||||
| dma.TCD->SADDR = &I2S0_RDR0; | dma.TCD->SADDR = &I2S0_RDR0; | ||||
| dma.TCD->SOFF = 0; | dma.TCD->SOFF = 0; | ||||
| I2S0_RCSR |= I2S_RCSR_RE | I2S_RCSR_BCE | I2S_RCSR_FRDE | I2S_RCSR_FR; | I2S0_RCSR |= I2S_RCSR_RE | I2S_RCSR_BCE | I2S_RCSR_FRDE | I2S_RCSR_FR; | ||||
| I2S0_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE; // TX clock enable, because sync'd to TX | I2S0_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE; // TX clock enable, because sync'd to TX | ||||
| dma.attachInterrupt(isr); | dma.attachInterrupt(isr); | ||||
| #else | |||||
| CORE_PIN7_CONFIG = 3; //RX_DATA0 | |||||
| IOMUXC_SAI1_RX_DATA0_SELECT_INPUT = 2; | |||||
| dma.TCD->SADDR = &I2S1_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_SAI1_RX); | |||||
| update_responsibility = update_setup(); | |||||
| dma.enable(); | |||||
| I2S1_RCSR |= I2S_RCSR_RE | I2S_RCSR_BCE | I2S_RCSR_FRDE | I2S_RCSR_FR; | |||||
| I2S1_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE; | |||||
| dma.attachInterrupt(isr); | |||||
| #endif | |||||
| } | } | ||||
| // TODO: needs optimization... | // TODO: needs optimization... | ||||
| const uint32_t *src; | const uint32_t *src; | ||||
| unsigned int i; | unsigned int i; | ||||
| //digitalWriteFast(3, HIGH); | |||||
| daddr = (uint32_t)(dma.TCD->DADDR); | daddr = (uint32_t)(dma.TCD->DADDR); | ||||
| dma.clearInterrupt(); | dma.clearInterrupt(); | ||||
| // need to remove data from the first half | // need to remove data from the first half | ||||
| src = &tdm_rx_buffer[0]; | src = &tdm_rx_buffer[0]; | ||||
| } | } | ||||
| if (block_incoming[0] != NULL) { | |||||
| 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) { | for (i=0; i < 16; i += 2) { | ||||
| uint32_t *dest1 = (uint32_t *)(block_incoming[i]->data); | uint32_t *dest1 = (uint32_t *)(block_incoming[i]->data); | ||||
| uint32_t *dest2 = (uint32_t *)(block_incoming[i+1]->data); | uint32_t *dest2 = (uint32_t *)(block_incoming[i+1]->data); | ||||
| } | } | ||||
| } | } | ||||
| if (update_responsibility) update_all(); | if (update_responsibility) update_all(); | ||||
| //digitalWriteFast(3, LOW); | |||||
| } | } | ||||
| // allocate 16 new blocks. If any fails, allocate none | // allocate 16 new blocks. If any fails, allocate none | ||||
| for (i=0; i < 16; i++) { | for (i=0; i < 16; i++) { | ||||
| new_block[i] = allocate(); | new_block[i] = allocate(); | ||||
| if (new_block[i] == NULL) { | |||||
| if (new_block[i] == nullptr) { | |||||
| for (j=0; j < i; j++) { | for (j=0; j < i; j++) { | ||||
| release(new_block[j]); | release(new_block[j]); | ||||
| } | } | ||||
| memcpy(out_block, block_incoming, sizeof(out_block)); | memcpy(out_block, block_incoming, sizeof(out_block)); | ||||
| memcpy(block_incoming, new_block, sizeof(block_incoming)); | memcpy(block_incoming, new_block, sizeof(block_incoming)); | ||||
| __enable_irq(); | __enable_irq(); | ||||
| if (out_block[0] != NULL) { | |||||
| if (out_block[0] != nullptr) { | |||||
| // if we got 1 block, all 16 are filled | // if we got 1 block, all 16 are filled | ||||
| for (i=0; i < 16; i++) { | for (i=0; i < 16; i++) { | ||||
| transmit(out_block[i], i); | transmit(out_block[i], i); | ||||
| } | } | ||||
| #endif // KINETISK | |||||
| #endif |
| /* Audio Library for Teensy 3.X | |||||
| * Copyright (c) 2017, Paul Stoffregen, paul@pjrc.com | |||||
| * | |||||
| * Development of this audio library was funded by PJRC.COM, LLC by sales of | |||||
| * Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop | |||||
| * open source software by purchasing Teensy or other PJRC products. | |||||
| * | |||||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | |||||
| * of this software and associated documentation files (the "Software"), to deal | |||||
| * in the Software without restriction, including without limitation the rights | |||||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |||||
| * copies of the Software, and to permit persons to whom the Software is | |||||
| * furnished to do so, subject to the following conditions: | |||||
| * | |||||
| * The above copyright notice, development funding notice, and this permission | |||||
| * notice shall be included in all copies or substantial portions of the Software. | |||||
| * | |||||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |||||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |||||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |||||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |||||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |||||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |||||
| * THE SOFTWARE. | |||||
| */ | |||||
| #if defined(__IMXRT1052__) || 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); // Allocate the DMA channel first | |||||
| // TODO: should we set & clear the I2S_RCSR_SR bit here? | |||||
| AudioOutputTDM2::config_tdm(); | |||||
| CORE_PIN33_CONFIG = 2; //2:RX_DATA0 | |||||
| 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); | |||||
| } | |||||
| // TODO: needs optimization... | |||||
| 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) { | |||||
| // DMA is receiving to the first half of the buffer | |||||
| // need to remove data from the second half | |||||
| src = &tdm_rx_buffer[AUDIO_BLOCK_SAMPLES*8]; | |||||
| } else { | |||||
| // DMA is receiving to the second half of the buffer | |||||
| // need to remove data from the first half | |||||
| 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]; | |||||
| // allocate 16 new blocks. If any fails, allocate none | |||||
| 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) { | |||||
| // if we got 1 block, all 16 are filled | |||||
| for (i=0; i < 16; i++) { | |||||
| transmit(out_block[i], i); | |||||
| release(out_block[i]); | |||||
| } | |||||
| } | |||||
| } | |||||
| #endif |
| /* Audio Library for Teensy 3.X | |||||
| * Copyright (c) 2017, Paul Stoffregen, paul@pjrc.com | |||||
| * | |||||
| * Development of this audio library was funded by PJRC.COM, LLC by sales of | |||||
| * Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop | |||||
| * open source software by purchasing Teensy or other PJRC products. | |||||
| * | |||||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | |||||
| * of this software and associated documentation files (the "Software"), to deal | |||||
| * in the Software without restriction, including without limitation the rights | |||||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |||||
| * copies of the Software, and to permit persons to whom the Software is | |||||
| * furnished to do so, subject to the following conditions: | |||||
| * | |||||
| * The above copyright notice, development funding notice, and this permission | |||||
| * notice shall be included in all copies or substantial portions of the Software. | |||||
| * | |||||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |||||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |||||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |||||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |||||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |||||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |||||
| * THE SOFTWARE. | |||||
| */ | |||||
| #if defined(__IMXRT1052__) || defined(__IMXRT1062__) | |||||
| #ifndef _input_tdm2_h_ | |||||
| #define _input_tdm2_h_ | |||||
| #include "Arduino.h" | |||||
| #include "AudioStream.h" | |||||
| #include "DMAChannel.h" | |||||
| class AudioInputTDM2 : public AudioStream | |||||
| { | |||||
| public: | |||||
| AudioInputTDM2(void) : AudioStream(0, NULL) { begin(); } | |||||
| virtual void update(void); | |||||
| void begin(void); | |||||
| protected: | |||||
| static bool update_responsibility; | |||||
| static DMAChannel dma; | |||||
| static void isr(void); | |||||
| private: | |||||
| static audio_block_t *block_incoming[16]; | |||||
| }; | |||||
| #endif | |||||
| #endif |
| AudioInputI2SQuad KEYWORD2 | AudioInputI2SQuad KEYWORD2 | ||||
| AudioInputI2Sslave KEYWORD2 | AudioInputI2Sslave KEYWORD2 | ||||
| AudioInputTDM KEYWORD2 | AudioInputTDM KEYWORD2 | ||||
| AudioInputTDM2 KEYWORD2 | |||||
| AudioInputPDM KEYWORD2 | AudioInputPDM KEYWORD2 | ||||
| AudioInputUSB KEYWORD2 | AudioInputUSB KEYWORD2 | ||||
| AudioOutputI2S KEYWORD2 | AudioOutputI2S KEYWORD2 |
| #include <Arduino.h> | #include <Arduino.h> | ||||
| #include "output_i2s2.h" | #include "output_i2s2.h" | ||||
| #include "memcpy_audio.h" | #include "memcpy_audio.h" | ||||
| #include "utility/imxrt_hw.h" | |||||
| audio_block_t * AudioOutputI2S2::block_left_1st = NULL; | audio_block_t * AudioOutputI2S2::block_left_1st = NULL; | ||||
| audio_block_t * AudioOutputI2S2::block_right_1st = NULL; | audio_block_t * AudioOutputI2S2::block_right_1st = NULL; | ||||
| void AudioOutputI2S2::isr(void) | void AudioOutputI2S2::isr(void) | ||||
| { | { | ||||
| int16_t *dest; | |||||
| int16_t *dest, *dc; | |||||
| audio_block_t *blockL, *blockR; | audio_block_t *blockL, *blockR; | ||||
| uint32_t saddr, offsetL, offsetR; | uint32_t saddr, offsetL, offsetR; | ||||
| * THE SOFTWARE. | * THE SOFTWARE. | ||||
| */ | */ | ||||
| #if defined(__IMXRT1052__) || defined(__IMXRT1062__) | |||||
| #if defined(__IMXRT1052__) || defined(__IMXRT1062__) | |||||
| #include <Arduino.h> | #include <Arduino.h> | ||||
| #include "output_tdm2.h" | #include "output_tdm2.h" | ||||
| #include "memcpy_audio.h" | #include "memcpy_audio.h" | ||||
| CCM_CCGR5 |= CCM_CCGR5_SAI2(CCM_CCGR_ON); | CCM_CCGR5 |= CCM_CCGR5_SAI2(CCM_CCGR_ON); | ||||
| //PLL: | //PLL: | ||||
| int fs = AUDIO_SAMPLE_RATE_EXACT*2; //176.4 khZ | |||||
| int fs = AUDIO_SAMPLE_RATE_EXACT; //176.4 khZ | |||||
| // PLL between 27*24 = 648MHz und 54*24=1296MHz | // PLL between 27*24 = 648MHz und 54*24=1296MHz | ||||
| int n1 = 4; //SAI prescaler 4 => (n1*n2) = multiple of 4 | int n1 = 4; //SAI prescaler 4 => (n1*n2) = multiple of 4 | ||||
| int n2 = 1 + (24000000 * 27) / (fs * 256 * n1); | int n2 = 1 + (24000000 * 27) / (fs * 256 * n1); | ||||
| CCM_CSCMR1 = (CCM_CSCMR1 & ~(CCM_CSCMR1_SAI2_CLK_SEL_MASK)) | CCM_CSCMR1 = (CCM_CSCMR1 & ~(CCM_CSCMR1_SAI2_CLK_SEL_MASK)) | ||||
| | CCM_CSCMR1_SAI2_CLK_SEL(2); // &0x03 // (0,1,2): PLL3PFD0, PLL5, PLL4 | | CCM_CSCMR1_SAI2_CLK_SEL(2); // &0x03 // (0,1,2): PLL3PFD0, PLL5, PLL4 | ||||
| //n1 = n1 / 2; //Double Speed for TDM | |||||
| n1 = n1 / 2; //Double Speed for TDM | |||||
| CCM_CS2CDR = (CCM_CS2CDR & ~(CCM_CS2CDR_SAI2_CLK_PRED_MASK | CCM_CS2CDR_SAI2_CLK_PODF_MASK)) | CCM_CS2CDR = (CCM_CS2CDR & ~(CCM_CS2CDR_SAI2_CLK_PRED_MASK | CCM_CS2CDR_SAI2_CLK_PODF_MASK)) | ||||
| | CCM_CS2CDR_SAI2_CLK_PRED(n1-1) // &0x07 | | CCM_CS2CDR_SAI2_CLK_PRED(n1-1) // &0x07 |