Skip to content

Commit

Permalink
DM: fix DMA interrupt structure for DAC output, other SAMD51 updates
Browse files Browse the repository at this point in the history
  • Loading branch information
deanm1278 committed Sep 24, 2018
1 parent 691fe82 commit 8f43258
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 35 deletions.
10 changes: 10 additions & 0 deletions Audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,18 @@
#include "effect_multiply.h"
#include "effect_delay.h"
#include "effect_delay_ext.h"

#include "effect_midside.h"
#include "effect_reverb.h"
#include "effect_waveshaper.h"
#include "filter_biquad.h"
#include "filter_fir.h"
#include "filter_variable.h"
#include "input_adcs.h"

#if I2S_INTERFACES_COUNT > 0
#include "input_i2s.h"
#endif

#if 0
#include "input_adc.h"
Expand All @@ -99,7 +103,10 @@

#include "mixer.h"
#include "output_dacs.h"

#if I2S_INTERFACES_COUNT > 0
#include "output_i2s.h"
#endif

#if 0
#include "output_dac.h"
Expand All @@ -112,8 +119,11 @@

#include "play_memory.h"
#include "play_queue.h"

#if SPI_INTERFACES_COUNT > 0
#include "play_sd_raw.h"
#include "play_sd_wav.h"
#endif

#if 0
#include "play_serialflash_raw.h"
Expand Down
4 changes: 4 additions & 0 deletions effect_delay_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#define digitalWriteFast digitalWrite
#endif

#if SPI_INTERFACES_COUNT > 0

//#define INTERNAL_TEST

// While 20 MHz (Teensy actually uses 16 MHz in most cases) and even 24 MHz
Expand Down Expand Up @@ -295,3 +297,5 @@ void AudioEffectDelayExternal::write(uint32_t offset, uint32_t count, const int1
}
#endif
}

#endif
16 changes: 3 additions & 13 deletions examples/Synthesis/Guitar/Guitar.ino
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include <Audio.h>
#include <Wire.h>
#include <SD.h>
#include <SPI.h>
#include <SerialFlash.h>

#include "chords.h"

Expand All @@ -16,17 +14,16 @@ AudioSynthKarplusStrong string5;
AudioSynthKarplusStrong string6;
AudioMixer4 mixer1;
AudioMixer4 mixer2;
AudioOutputI2S i2s1;
AudioOutputAnalogStereo dacs;
AudioConnection patchCord1(string1, 0, mixer1, 0);
AudioConnection patchCord2(string2, 0, mixer1, 1);
AudioConnection patchCord3(string3, 0, mixer1, 2);
AudioConnection patchCord4(string4, 0, mixer1, 3);
AudioConnection patchCord5(mixer1, 0, mixer2, 0);
AudioConnection patchCord6(string5, 0, mixer2, 1);
AudioConnection patchCord7(string6, 0, mixer2, 2);
AudioConnection patchCord8(mixer2, 0, i2s1, 0);
AudioConnection patchCord9(mixer2, 0, i2s1, 1);
AudioControlSGTL5000 sgtl5000_1;
AudioConnection patchCord8(mixer2, 0, dacs, 0);
AudioConnection patchCord9(mixer2, 0, dacs, 1);

const int finger_delay = 5;
const int hand_delay = 220;
Expand All @@ -35,8 +32,6 @@ int chordnum=0;

void setup() {
AudioMemory(15);
sgtl5000_1.enable();
sgtl5000_1.volume(0.6);
mixer1.gain(0, 0.15);
mixer1.gain(1, 0.15);
mixer1.gain(2, 0.15);
Expand Down Expand Up @@ -100,10 +95,6 @@ void loop() {
delay(hand_delay);
strum_dn(chord, 0.7);
delay(hand_delay);

Serial.print("Max CPU Usage = ");
Serial.print(AudioProcessorUsageMax(), 1);
Serial.println("%");
}

void strum_up(const float *chord, float velocity)
Expand Down Expand Up @@ -139,4 +130,3 @@ void strum_dn(const float *chord, float velocity)
}



8 changes: 8 additions & 0 deletions input_i2s.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
* THE SOFTWARE.
*/

#if defined(ARDUINO_ARCH_SAMD)
#include <Arduino.h>
#endif

#if I2S_INTERFACES_COUNT > 0

#include "input_i2s.h"
#include "output_i2s.h"

Expand Down Expand Up @@ -264,5 +270,7 @@ void AudioInputI2Sslave::begin(void)
#endif
}

#endif



53 changes: 36 additions & 17 deletions output_dacs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ audio_block_t AudioOutputAnalogStereo::block_silent;
bool AudioOutputAnalogStereo::update_responsibility = false;
Adafruit_ZeroDMA *AudioOutputAnalogStereo::dma0;
Adafruit_ZeroDMA *AudioOutputAnalogStereo::dma1;
DmacDescriptor *AudioOutputAnalogStereo::desc0;
DmacDescriptor *AudioOutputAnalogStereo::desc1;
DmacDescriptor *AudioOutputAnalogStereo::desc;
static ZeroDMAstatus stat;
static uint32_t *saddr;

void dummyCallback(Adafruit_ZeroDMA *dma)
{
Expand Down Expand Up @@ -107,7 +107,7 @@ void AudioOutputAnalogStereo::begin(void)
dma0->setAction(DMA_TRIGGER_ACTON_BEAT);
dma1->setAction(DMA_TRIGGER_ACTON_BEAT);

desc0 = dma0->addDescriptor(
desc = dma0->addDescriptor(
dac_buffer, // move data from here
(void *)(&DAC->DATA[0]), // to here
AUDIO_BLOCK_SAMPLES, // this many...
Expand All @@ -116,8 +116,21 @@ void AudioOutputAnalogStereo::begin(void)
false,
DMA_ADDRESS_INCREMENT_STEP_SIZE_2,
DMA_STEPSEL_SRC);
desc->BTCTRL.bit.BLOCKACT = DMA_BLOCK_ACTION_INT;

desc = dma0->addDescriptor(
dac_buffer + AUDIO_BLOCK_SAMPLES, // move data from here
(void *)(&DAC->DATA[0]), // to here
AUDIO_BLOCK_SAMPLES, // this many...
DMA_BEAT_SIZE_HWORD, // bytes/hword/words
true, // increment source addr?
false,
DMA_ADDRESS_INCREMENT_STEP_SIZE_2,
DMA_STEPSEL_SRC);
desc->BTCTRL.bit.BLOCKACT = DMA_BLOCK_ACTION_INT;
dma0->loop(true);

desc1 = dma1->addDescriptor(
desc = dma1->addDescriptor(
((uint16_t *)dac_buffer) + 1, // move data from here
(void *)(&DAC->DATA[1]), // to here
AUDIO_BLOCK_SAMPLES, // this many...
Expand All @@ -126,7 +139,21 @@ void AudioOutputAnalogStereo::begin(void)
false,
DMA_ADDRESS_INCREMENT_STEP_SIZE_2,
DMA_STEPSEL_SRC); // increment dest addr?

desc->BTCTRL.bit.BLOCKACT = DMA_BLOCK_ACTION_INT;

desc = dma1->addDescriptor(
((uint16_t *)(dac_buffer + AUDIO_BLOCK_SAMPLES)) + 1, // move data from here
(void *)(&DAC->DATA[1]), // to here
AUDIO_BLOCK_SAMPLES, // this many...
DMA_BEAT_SIZE_HWORD, // bytes/hword/words
true, // increment source addr?
false,
DMA_ADDRESS_INCREMENT_STEP_SIZE_2,
DMA_STEPSEL_SRC); // increment dest addr?
desc->BTCTRL.bit.BLOCKACT = DMA_BLOCK_ACTION_INT;
dma1->loop(true);
saddr = dac_buffer;

update_responsibility = update_setup();
dma1->setCallback(AudioOutputAnalogStereo::isr);
dma0->setCallback(dummyCallback);
Expand Down Expand Up @@ -185,27 +212,19 @@ void AudioOutputAnalogStereo::isr(Adafruit_ZeroDMA *dma)
const uint32_t *src_left, *src_right, *end;
uint32_t *dest;
audio_block_t *block_left, *block_right;
uint32_t saddr;

//PORT[PORTA].Group->OUTTGL.reg |= (1UL << 21);
saddr = desc0->SRCADDR.reg;
saddr -= (AUDIO_BLOCK_SAMPLES) * 2 * (1<<1);

if (saddr == (uint32_t)dac_buffer) {
if (saddr == dac_buffer) {
//DMA has finished the first half
dma0->changeDescriptor(desc0, dac_buffer + AUDIO_BLOCK_SAMPLES);
dma1->changeDescriptor(desc1, ((uint16_t *)(dac_buffer + AUDIO_BLOCK_SAMPLES)) + 1);
dest = dac_buffer;
end = dac_buffer + AUDIO_BLOCK_SAMPLES;
saddr = (uint32_t *)end;
} else {
//DMA has finished the second half
dma0->changeDescriptor(desc0, dac_buffer);
dma1->changeDescriptor(desc1, ((uint16_t *)dac_buffer) + 1 );
dest = dac_buffer + AUDIO_BLOCK_SAMPLES;
end = dac_buffer + AUDIO_BLOCK_SAMPLES*2;
saddr = dac_buffer;
}
dma0->startJob();
dma1->startJob();

block_left = block_left_1st;
if (!block_left) block_left = &block_silent;
block_right = block_right_1st;
Expand Down
3 changes: 1 addition & 2 deletions output_dacs.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ class AudioOutputAnalogStereo : public AudioStream
audio_block_t *inputQueueArray[2];
static Adafruit_ZeroDMA *dma0;
static Adafruit_ZeroDMA *dma1;
static DmacDescriptor *desc0;
static DmacDescriptor *desc1;
static DmacDescriptor *desc;
static void isr(Adafruit_ZeroDMA *dma);
};

Expand Down
8 changes: 7 additions & 1 deletion output_i2s.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@
* THE SOFTWARE.
*/

#if defined(ARDUINO_ARCH_SAMD)
#include <Arduino.h>
#endif

#if I2S_INTERFACES_COUNT > 0

#include "output_i2s.h"
#include "memcpy_audio.h"


audio_block_t * AudioOutputI2S::block_left_1st = NULL;
audio_block_t * AudioOutputI2S::block_right_1st = NULL;
audio_block_t * AudioOutputI2S::block_left_2nd = NULL;
Expand Down Expand Up @@ -535,4 +540,5 @@ void AudioOutputI2Sslave::config_i2s(void)
#endif
}

#endif

9 changes: 8 additions & 1 deletion play_sd_raw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@
* THE SOFTWARE.
*/

#if defined(ARDUINO_ARCH_SAMD)
#include <Arduino.h>
#endif

#if SPI_INTERFACES_COUNT > 0

#include "play_sd_raw.h"
#include "spi_interrupt.h"


void AudioPlaySdRaw::begin(void)
{
playing = false;
Expand Down Expand Up @@ -124,3 +129,5 @@ uint32_t AudioPlaySdRaw::lengthMillis(void)
{
return ((uint64_t)file_size * B2M) >> 32;
}

#endif
8 changes: 7 additions & 1 deletion play_sd_wav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
* THE SOFTWARE.
*/

#if defined(ARDUINO_ARCH_SAMD)
#include <Arduino.h>
#endif

#if SPI_INTERFACES_COUNT > 0

#include "play_sd_wav.h"
#include "spi_interrupt.h"

Expand Down Expand Up @@ -563,7 +569,7 @@ uint32_t AudioPlaySdWav::lengthMillis(void)
return ((uint64_t)tlength * b2m) >> 32;
}


#endif



Expand Down

0 comments on commit 8f43258

Please sign in to comment.