|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
-
-
- #include <Arduino.h>
- #include "output_dac.h"
- #include "utility/pdb.h"
-
- #if defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
-
- DMAMEM __attribute__((aligned(32))) static uint16_t dac_buffer[AUDIO_BLOCK_SAMPLES*2];
- audio_block_t * AudioOutputAnalog::block_left_1st = NULL;
- audio_block_t * AudioOutputAnalog::block_left_2nd = NULL;
- bool AudioOutputAnalog::update_responsibility = false;
- DMAChannel AudioOutputAnalog::dma(false);
-
- void AudioOutputAnalog::begin(void)
- {
- dma.begin(true);
-
- SIM_SCGC2 |= SIM_SCGC2_DAC0;
- DAC0_C0 = DAC_C0_DACEN;
-
- for (int16_t i=0; i<=2048; i+=8) {
- *(int16_t *)&(DAC0_DAT0L) = i;
- delay(1);
- }
-
-
- 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;
- }
-
- dma.TCD->SADDR = dac_buffer;
- dma.TCD->SOFF = 2;
- dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
- dma.TCD->NBYTES_MLNO = 2;
- dma.TCD->SLAST = -sizeof(dac_buffer);
- dma.TCD->DADDR = &DAC0_DAT0L;
- dma.TCD->DOFF = 0;
- dma.TCD->CITER_ELINKNO = sizeof(dac_buffer) / 2;
- dma.TCD->DLASTSGA = 0;
- dma.TCD->BITER_ELINKNO = sizeof(dac_buffer) / 2;
- dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
- dma.triggerAtHardwareEvent(DMAMUX_SOURCE_PDB);
- update_responsibility = update_setup();
- dma.enable();
- dma.attachInterrupt(isr);
- }
-
- void AudioOutputAnalog::analogReference(int ref)
- {
-
- if (ref == INTERNAL) {
- DAC0_C0 &= ~DAC_C0_DACRFS;
- } else {
- DAC0_C0 |= DAC_C0_DACRFS;
- }
- }
-
-
- void AudioOutputAnalog::update(void)
- {
- audio_block_t *block;
- block = receiveReadOnly(0);
- if (block) {
- __disable_irq();
- if (block_left_1st == NULL) {
- block_left_1st = block;
- __enable_irq();
- } else if (block_left_2nd == NULL) {
- block_left_2nd = block;
- __enable_irq();
- } else {
- audio_block_t *tmp = block_left_1st;
- block_left_1st = block_left_2nd;
- block_left_2nd = block;
- __enable_irq();
- release(tmp);
- }
- }
- }
-
-
-
-
- void AudioOutputAnalog::isr(void)
- {
- const int16_t *src, *end;
- int16_t *dest;
- audio_block_t *block;
- uint32_t saddr;
-
- saddr = (uint32_t)(dma.TCD->SADDR);
- dma.clearInterrupt();
- if (saddr < (uint32_t)dac_buffer + sizeof(dac_buffer) / 2) {
-
-
- dest = (int16_t *)&dac_buffer[AUDIO_BLOCK_SAMPLES];
- end = (int16_t *)&dac_buffer[AUDIO_BLOCK_SAMPLES*2];
- } else {
-
-
- dest = (int16_t *)dac_buffer;
- end = (int16_t *)&dac_buffer[AUDIO_BLOCK_SAMPLES];
- }
- block = AudioOutputAnalog::block_left_1st;
- if (block) {
- src = block->data;
- do {
-
- *dest++ = ((*src++) + 32768) >> 4;
- } while (dest < end);
- AudioStream::release(block);
- AudioOutputAnalog::block_left_1st = AudioOutputAnalog::block_left_2nd;
- AudioOutputAnalog::block_left_2nd = NULL;
- } else {
- do {
- *dest++ = 2048;
- } while (dest < end);
- }
- if (AudioOutputAnalog::update_responsibility) AudioStream::update_all();
- }
-
-
-
- #elif defined (__MKL26Z64__)
-
- DMAMEM static uint16_t dac_buffer1[AUDIO_BLOCK_SAMPLES];
- DMAMEM static uint16_t dac_buffer2[AUDIO_BLOCK_SAMPLES];
- audio_block_t * AudioOutputAnalog::block_left_1st = NULL;
- audio_block_t * AudioOutputAnalog::block_left_2nd = NULL;
- bool AudioOutputAnalog::update_responsibility = false;
- DMAChannel AudioOutputAnalog::dma1(false);
-
- void AudioOutputAnalog::begin(void)
- {
- dma1.begin(true);
-
-
-
- delay(10);
-
- SIM_SCGC6 |= SIM_SCGC6_DAC0;
- DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS;
-
- for (int16_t i=0; i<2048; i+=8) {
- *(int16_t *)&(DAC0_DAT0L) = i;
- delay(1);
- }
-
-
- FTM1_SC = 0;
- FTM1_CNT = 0;
- FTM1_MOD = (uint32_t)((F_PLL/2) / 44117.64706 + 0.5);
- FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_DMA;
-
- dma1.sourceBuffer(dac_buffer1, sizeof(dac_buffer1));
- dma1.destination(*(int16_t *)&DAC0_DAT0L);
- dma1.interruptAtCompletion();
- dma1.disableOnCompletion();
- dma1.triggerAtHardwareEvent(DMAMUX_SOURCE_FTM1_OV);
- dma1.attachInterrupt(isr1);
-
- update_responsibility = update_setup();
-
- dma1.enable();
- }
-
- void AudioOutputAnalog::isr1(void)
- {
- if(!dma1.complete()) return;
- dma1.clearInterrupt();
-
- bool finishedFirst = (dma1.sourceAddress() == (void *)&dac_buffer1[AUDIO_BLOCK_SAMPLES]);
- if(finishedFirst) {
-
- dma1.sourceBuffer(dac_buffer2, sizeof(dac_buffer2));
- } else {
-
- dma1.sourceBuffer(dac_buffer1, sizeof(dac_buffer1));
- }
-
- dma1.enable();
-
- int16_t *dest;
- const int16_t * end;
- if(finishedFirst) {
- dest = (int16_t *)dac_buffer1;
- end = (int16_t *)&dac_buffer1[AUDIO_BLOCK_SAMPLES];
- } else {
- dest = (int16_t *)dac_buffer2;
- end = (int16_t *)&dac_buffer2[AUDIO_BLOCK_SAMPLES];
- }
- const int16_t *src;
- audio_block_t *block;
- block = AudioOutputAnalog::block_left_1st;
- if (block) {
- src = block->data;
- do {
- *dest++ = ((*src++) >> 4) + 0x800;
- } while (dest < end);
- AudioStream::release(block);
- AudioOutputAnalog::block_left_1st = AudioOutputAnalog::block_left_2nd;
- AudioOutputAnalog::block_left_2nd = NULL;
- } else {
- do {
- *dest++ = 0x800;
- } while (dest < end);
- }
- if (AudioOutputAnalog::update_responsibility) AudioStream::update_all();
- }
-
- void AudioOutputAnalog::update(void)
- {
- audio_block_t *block;
- block = receiveReadOnly();
- if (block) {
- __disable_irq();
- if (block_left_1st == NULL) {
- block_left_1st = block;
- __enable_irq();
- } else if (block_left_2nd == NULL) {
- block_left_2nd = block;
- __enable_irq();
- } else {
- audio_block_t *tmp = block_left_1st;
- block_left_1st = block_left_2nd;
- block_left_2nd = block;
- __enable_irq();
- release(tmp);
- }
- }
- }
-
-
-
-
- #else
-
- void AudioOutputAnalog::begin(void)
- {
- }
-
- void AudioOutputAnalog::update(void)
- {
- audio_block_t *block;
- block = receiveReadOnly(0);
- if (block) release(block);
- }
-
- #endif
|