|
-
-
- #include "input_adc.h"
- #include "utility/pdb.h"
- #include "utility/dspinst.h"
-
- DMAMEM static uint16_t analog_rx_buffer[AUDIO_BLOCK_SAMPLES];
- audio_block_t * AudioInputAnalog::block_left = NULL;
- uint16_t AudioInputAnalog::block_offset = 0;
- uint16_t AudioInputAnalog::dc_average = 0;
- bool AudioInputAnalog::update_responsibility = false;
- DMAChannel AudioInputAnalog::dma(false);
-
-
- void AudioInputAnalog::init(uint8_t pin)
- {
- uint32_t i, sum=0;
-
-
-
-
- analogReadRes(16);
- analogReference(INTERNAL);
- analogReadAveraging(8);
-
- for (i=0; i < 1024; i++) {
- sum += analogRead(pin);
- }
- dc_average = sum >> 10;
-
-
- #if defined(KINETISK)
- if (!(SIM_SCGC6 & SIM_SCGC6_PDB)
- || (PDB0_SC & PDB_CONFIG) != PDB_CONFIG
- || PDB0_MOD != PDB_PERIOD
- || PDB0_IDLY != 1
- || PDB0_CH0C1 != 0x0101) {
- SIM_SCGC6 |= SIM_SCGC6_PDB;
- PDB0_IDLY = 1;
- PDB0_MOD = PDB_PERIOD;
- PDB0_SC = PDB_CONFIG | PDB_SC_LDOK;
- PDB0_SC = PDB_CONFIG | PDB_SC_SWTRIG;
- PDB0_CH0C1 = 0x0101;
- }
- #endif
-
- ADC0_SC2 |= ADC_SC2_ADTRG | ADC_SC2_DMAEN;
-
-
- dma.begin(true);
- #if defined(KINETISK)
- 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;
- #endif
- dma.triggerAtHardwareEvent(DMAMUX_SOURCE_ADC0);
- update_responsibility = update_setup();
- dma.enable();
- dma.attachInterrupt(isr);
- }
-
-
- void AudioInputAnalog::isr(void)
- {
- uint32_t daddr, offset;
- const uint16_t *src, *end;
- uint16_t *dest_left;
- audio_block_t *left;
-
- #if defined(KINETISK)
- daddr = (uint32_t)(dma.TCD->DADDR);
- #endif
- dma.clearInterrupt();
-
- if (daddr < (uint32_t)analog_rx_buffer + sizeof(analog_rx_buffer) / 2) {
-
-
- src = (uint16_t *)&analog_rx_buffer[AUDIO_BLOCK_SAMPLES/2];
- end = (uint16_t *)&analog_rx_buffer[AUDIO_BLOCK_SAMPLES];
- if (update_responsibility) AudioStream::update_all();
- } else {
-
-
- src = (uint16_t *)&analog_rx_buffer[0];
- end = (uint16_t *)&analog_rx_buffer[AUDIO_BLOCK_SAMPLES/2];
- }
- left = block_left;
- if (left != NULL) {
- offset = block_offset;
- if (offset > AUDIO_BLOCK_SAMPLES/2) offset = AUDIO_BLOCK_SAMPLES/2;
- dest_left = (uint16_t *)&(left->data[offset]);
- block_offset = offset + AUDIO_BLOCK_SAMPLES/2;
- do {
- *dest_left++ = *src++;
- } while (src < end);
- }
- }
-
-
-
- void AudioInputAnalog::update(void)
- {
- audio_block_t *new_left=NULL, *out_left=NULL;
- unsigned int dc, offset;
- int32_t tmp;
- int16_t s, *p, *end;
-
-
-
-
- new_left = allocate();
-
- __disable_irq();
- offset = block_offset;
- if (offset < AUDIO_BLOCK_SAMPLES) {
-
- if (new_left != NULL) {
-
- if (block_left == NULL) {
-
-
- block_left = new_left;
- block_offset = 0;
- __enable_irq();
-
- } else {
-
- __enable_irq();
- release(new_left);
-
-
- }
- } else {
-
-
-
- __enable_irq();
-
- }
- return;
- }
-
-
- out_left = block_left;
- block_left = new_left;
- block_offset = 0;
- __enable_irq();
-
-
-
- dc = dc_average;
- p = out_left->data;
- end = p + AUDIO_BLOCK_SAMPLES;
- do {
- tmp = (uint16_t)(*p) - (int32_t)dc;
- s = signed_saturate_rshift(tmp, 16, 0);
- *p++ = s;
- dc += s / 12000;
- } while (p < end);
- dc_average = dc;
-
-
- transmit(out_left);
- release(out_left);
- }
-
-
|