Bläddra i källkod

Add AudioInputTDM

dds
PaulStoffregen 7 år sedan
förälder
incheckning
7673a0dbdf
6 ändrade filer med 203 tillägg och 10 borttagningar
  1. +1
    -0
      Audio.h
  2. +145
    -0
      input_tdm.cpp
  3. +48
    -0
      input_tdm.h
  4. +1
    -0
      keywords.txt
  5. +7
    -9
      output_tdm.cpp
  6. +1
    -1
      output_tdm.h

+ 1
- 0
Audio.h Visa fil

#include "input_adcs.h" #include "input_adcs.h"
#include "input_i2s.h" #include "input_i2s.h"
#include "input_i2s_quad.h" #include "input_i2s_quad.h"
#include "input_tdm.h"
#include "mixer.h" #include "mixer.h"
#include "output_dac.h" #include "output_dac.h"
#include "output_dacs.h" #include "output_dacs.h"

+ 145
- 0
input_tdm.cpp Visa fil

/* 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.
*/

#include "input_tdm.h"
#include "output_tdm.h"
#if defined(KINETISK)

DMAMEM static uint32_t tdm_rx_buffer[AUDIO_BLOCK_SAMPLES*16];
audio_block_t * AudioInputTDM::block_incoming[16] = {
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
};
bool AudioInputTDM::update_responsibility = false;
DMAChannel AudioInputTDM::dma(false);


void AudioInputTDM::begin(void)
{
dma.begin(true); // Allocate the DMA channel first

// TODO: should we set & clear the I2S_RCSR_SR bit here?
AudioOutputTDM::config_tdm();

CORE_PIN13_CONFIG = PORT_PCR_MUX(4); // pin 13, PTC5, I2S0_RXD0
dma.TCD->SADDR = &I2S0_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_I2S0_RX);
update_responsibility = update_setup();
dma.enable();

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
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 AudioInputTDM::isr(void)
{
uint32_t daddr;
const uint32_t *src;
unsigned int i;

//digitalWriteFast(3, HIGH);
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] != NULL) {
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();
//digitalWriteFast(3, LOW);
}


void AudioInputTDM::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] == NULL) {
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] != NULL) {
// 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 // KINETISK

+ 48
- 0
input_tdm.h Visa fil

/* 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.
*/

#ifndef _input_tdm_h_
#define _input_tdm_h_

#include "Arduino.h"
#include "AudioStream.h"
#include "DMAChannel.h"

class AudioInputTDM : public AudioStream
{
public:
AudioInputTDM(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

+ 1
- 0
keywords.txt Visa fil

AudioInputI2S KEYWORD2 AudioInputI2S KEYWORD2
AudioInputI2SQuad KEYWORD2 AudioInputI2SQuad KEYWORD2
AudioInputI2Sslave KEYWORD2 AudioInputI2Sslave KEYWORD2
AudioInputTDM KEYWORD2
AudioInputUSB KEYWORD2 AudioInputUSB KEYWORD2
AudioOutputI2S KEYWORD2 AudioOutputI2S KEYWORD2
AudioOutputI2SQuad KEYWORD2 AudioOutputI2SQuad KEYWORD2

+ 7
- 9
output_tdm.cpp Visa fil

} }


// TODO: needs optimization... // TODO: needs optimization...
static void memcpy_tdm(uint32_t *dest, const uint32_t *src1, const uint32_t *src2)
static void memcpy_tdm_tx(uint32_t *dest, const uint32_t *src1, const uint32_t *src2)
{ {
uint32_t i, in1, in2, out1, out2; uint32_t i, in1, in2, out1, out2;


for (i=0; i < 16; i += 2) { for (i=0; i < 16; i += 2) {
src1 = block_input[i] ? (uint32_t *)(block_input[i]->data) : zeros; src1 = block_input[i] ? (uint32_t *)(block_input[i]->data) : zeros;
src2 = block_input[i+1] ? (uint32_t *)(block_input[i+1]->data) : zeros; src2 = block_input[i+1] ? (uint32_t *)(block_input[i+1]->data) : zeros;
memcpy_tdm(dest, src1, src2);
memcpy_tdm_tx(dest, src1, src2);
dest++; dest++;
} }
for (i=0; i < 16; i++) { for (i=0; i < 16; i++) {
| I2S_TCR4_FSE | I2S_TCR4_FSD; | I2S_TCR4_FSE | I2S_TCR4_FSD;
I2S0_TCR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31); I2S0_TCR5 = I2S_TCR5_WNW(31) | I2S_TCR5_W0W(31) | I2S_TCR5_FBT(31);


#if 0
// configure receiver (sync'd to transmitter clocks) // configure receiver (sync'd to transmitter clocks)
I2S0_RMR = 0; I2S0_RMR = 0;
I2S0_RCR1 = I2S_RCR1_RFW(1);
I2S0_RCR1 = I2S_RCR1_RFW(4);
I2S0_RCR2 = I2S_RCR2_SYNC(1) | I2S_TCR2_BCP | I2S_RCR2_MSEL(1) I2S0_RCR2 = I2S_RCR2_SYNC(1) | I2S_TCR2_BCP | I2S_RCR2_MSEL(1)
| I2S_RCR2_BCD | I2S_RCR2_DIV(3);
| I2S_RCR2_BCD | I2S_RCR2_DIV(0);
I2S0_RCR3 = I2S_RCR3_RCE; I2S0_RCR3 = I2S_RCR3_RCE;
I2S0_RCR4 = I2S_RCR4_FRSZ(1) | I2S_RCR4_SYWD(15) | I2S_RCR4_MF
| I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
I2S0_RCR5 = I2S_RCR5_WNW(15) | I2S_RCR5_W0W(15) | I2S_RCR5_FBT(15);
#endif
I2S0_RCR4 = I2S_RCR4_FRSZ(7) | I2S_RCR4_SYWD(0) | I2S_RCR4_MF
| I2S_RCR4_FSE | I2S_RCR4_FSD;
I2S0_RCR5 = I2S_RCR5_WNW(31) | I2S_RCR5_W0W(31) | I2S_RCR5_FBT(31);


// configure pin mux for 3 clock signals // configure pin mux for 3 clock signals
CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK) CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)

+ 1
- 1
output_tdm.h Visa fil

AudioOutputTDM(void) : AudioStream(16, inputQueueArray) { begin(); } AudioOutputTDM(void) : AudioStream(16, inputQueueArray) { begin(); }
virtual void update(void); virtual void update(void);
void begin(void); void begin(void);
friend class AudioInputI2S;
friend class AudioInputTDM;
protected: protected:
static void config_tdm(void); static void config_tdm(void);
static audio_block_t *block_input[16]; static audio_block_t *block_input[16];

Laddar…
Avbryt
Spara