Browse Source

Convert AudioInputAnalog to use DMAChannel.h

dds
PaulStoffregen 10 years ago
parent
commit
d951eef420
3 changed files with 26 additions and 29 deletions
  1. +20
    -21
      input_adc.cpp
  2. +2
    -8
      input_adc.h
  3. +4
    -0
      utility/dma_chan.h

+ 20
- 21
input_adc.cpp View File

audio_block_t * AudioInputAnalog::block_left = NULL; audio_block_t * AudioInputAnalog::block_left = NULL;
uint16_t AudioInputAnalog::block_offset = 0; uint16_t AudioInputAnalog::block_offset = 0;
bool AudioInputAnalog::update_responsibility = false; bool AudioInputAnalog::update_responsibility = false;
DMAChannel AudioInputAnalog::dma;


// #define PDB_CONFIG (PDB_SC_TRGSEL(15) | PDB_SC_PDBEN | PDB_SC_CONT) // #define PDB_CONFIG (PDB_SC_TRGSEL(15) | PDB_SC_PDBEN | PDB_SC_CONT)
// #define PDB_PERIOD 1087 // 48e6 / 44100 // #define PDB_PERIOD 1087 // 48e6 / 44100
// constants A10-A13 are actually 34 to 37 // constants A10-A13 are actually 34 to 37
if (pin > 23 && !(pin >= 34 && pin <= 37)) return; if (pin > 23 && !(pin >= 34 && pin <= 37)) return;


dma.begin(true); // Allocate the DMA channel first

//pinMode(2, OUTPUT); //pinMode(2, OUTPUT);
//pinMode(3, OUTPUT); //pinMode(3, OUTPUT);
//digitalWriteFast(3, HIGH); //digitalWriteFast(3, HIGH);
ADC0_SC2 |= ADC_SC2_ADTRG | ADC_SC2_DMAEN; ADC0_SC2 |= ADC_SC2_ADTRG | ADC_SC2_DMAEN;


// set up a DMA channel to store the ADC data // set up a DMA channel to store the ADC data
SIM_SCGC7 |= SIM_SCGC7_DMA;
SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
DMA_CR = 0;
DMA_TCD_SADDR(AUDIO_IN_ADC_DMA_CHANNEL) = &ADC0_RA;
DMA_TCD_SOFF(AUDIO_IN_ADC_DMA_CHANNEL) = 0;
DMA_TCD_ATTR(AUDIO_IN_ADC_DMA_CHANNEL) = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
DMA_TCD_NBYTES_MLNO(AUDIO_IN_ADC_DMA_CHANNEL) = 2;
DMA_TCD_SLAST(AUDIO_IN_ADC_DMA_CHANNEL) = 0;
DMA_TCD_DADDR(AUDIO_IN_ADC_DMA_CHANNEL) = analog_rx_buffer;
DMA_TCD_DOFF(AUDIO_IN_ADC_DMA_CHANNEL) = 2;
DMA_TCD_CITER_ELINKNO(AUDIO_IN_ADC_DMA_CHANNEL) = sizeof(analog_rx_buffer) / 2;
DMA_TCD_DLASTSGA(AUDIO_IN_ADC_DMA_CHANNEL) = -sizeof(analog_rx_buffer);
DMA_TCD_BITER_ELINKNO(AUDIO_IN_ADC_DMA_CHANNEL) = sizeof(analog_rx_buffer) / 2;
DMA_TCD_CSR(AUDIO_IN_ADC_DMA_CHANNEL) = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
DMAMUX0_CHCFG(AUDIO_IN_ADC_DMA_CHANNEL) = DMAMUX_DISABLE;
DMAMUX0_CHCFG(AUDIO_IN_ADC_DMA_CHANNEL) = DMAMUX_SOURCE_ADC0 | DMAMUX_ENABLE;
dma.TCD->SADDR = &ADC0_RA;
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 = analog_rx_buffer;
dma.TCD->DOFF = 2;
dma.TCD->CITER_ELINKNO = sizeof(analog_rx_buffer) / 2;
dma.TCD->DLASTSGA = -sizeof(analog_rx_buffer);
dma.TCD->BITER_ELINKNO = sizeof(analog_rx_buffer) / 2;
dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
dma.triggerAtHardwareEvent(DMAMUX_SOURCE_ADC0);
update_responsibility = update_setup(); update_responsibility = update_setup();
DMA_SERQ = AUDIO_IN_ADC_DMA_CHANNEL;
NVIC_ENABLE_IRQ(IRQ_DMA_CH(AUDIO_IN_ADC_DMA_CHANNEL));
dma.enable();
dma.attachInterrupt(isr);
} }


void DMA_ISR(AUDIO_IN_ADC_DMA_CHANNEL)(void)
void AudioInputAnalog::isr(void)
{ {
uint32_t daddr, offset; uint32_t daddr, offset;
const uint16_t *src, *end; const uint16_t *src, *end;
audio_block_t *left; audio_block_t *left;


//digitalWriteFast(3, HIGH); //digitalWriteFast(3, HIGH);
daddr = (uint32_t)(DMA_TCD_DADDR(AUDIO_IN_ADC_DMA_CHANNEL));
DMA_CINT = AUDIO_IN_ADC_DMA_CHANNEL;
daddr = (uint32_t)(dma.TCD->DADDR);
dma.clearInterrupt();


if (daddr < (uint32_t)analog_rx_buffer + sizeof(analog_rx_buffer) / 2) { if (daddr < (uint32_t)analog_rx_buffer + sizeof(analog_rx_buffer) / 2) {
// DMA is receiving to the first half of the buffer // DMA is receiving to the first half of the buffer

+ 2
- 8
input_adc.h View File



#include "AudioStream.h" #include "AudioStream.h"
#include "DMAChannel.h" #include "DMAChannel.h"
#include "utility/dma_chan.h"

#ifdef __MK20DX128__
#define AUDIO_IN_ADC_DMA_CHANNEL 2
#else
#define AUDIO_IN_ADC_DMA_CHANNEL 6
#endif


class AudioInputAnalog : public AudioStream class AudioInputAnalog : public AudioStream
{ {
AudioInputAnalog(unsigned int pin) : AudioStream(0, NULL) { begin(pin); } AudioInputAnalog(unsigned int pin) : AudioStream(0, NULL) { begin(pin); }
virtual void update(void); virtual void update(void);
void begin(unsigned int pin); void begin(unsigned int pin);
friend void DMA_ISR(AUDIO_IN_ADC_DMA_CHANNEL)(void);
private: private:
static audio_block_t *block_left; static audio_block_t *block_left;
static uint16_t block_offset; static uint16_t block_offset;
uint16_t dc_average; uint16_t dc_average;
static bool update_responsibility; static bool update_responsibility;
static DMAChannel dma;
static void isr(void);
}; };


#endif #endif

+ 4
- 0
utility/dma_chan.h View File

#ifndef dma_chan_h_ #ifndef dma_chan_h_
#define dma_chan_h_ #define dma_chan_h_


/*
TODO: remove this file - made obsolete by DMAChannel.h

#define DMA_ISR(ch) DMA_ISR_EXP(ch) #define DMA_ISR(ch) DMA_ISR_EXP(ch)
#define DMA_ISR_EXP(ch) dma_ch ## ch ## _isr #define DMA_ISR_EXP(ch) dma_ch ## ch ## _isr




#define DMA_TCD_CSR(ch) DMA_TCD_CSR_EXP(ch) #define DMA_TCD_CSR(ch) DMA_TCD_CSR_EXP(ch)
#define DMA_TCD_CSR_EXP(ch) DMA_TCD ## ch ## _CSR #define DMA_TCD_CSR_EXP(ch) DMA_TCD ## ch ## _CSR
*/


#endif #endif

Loading…
Cancel
Save