You can find a minimal example of my ADC code below (running the ADC at 128 kHz). I would still prefer using Paul's high-level functions for the DMA.
File pdb.h
Code:
/* Audio Library for Teensy 3.X
* Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
* Modified 2015, Ferdinand Keil, ferdinandkeil@gmail.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 pdb_h_
#define pdb_h_
#include "kinetis.h"
// Multiple input & output objects use the Programmable Delay Block
// to set their sample rate. They must all configure the same
// period to avoid chaos.
#define PDB_CONFIG (PDB_SC_TRGSEL(15) | PDB_SC_PDBEN | PDB_SC_CONT | PDB_SC_PDBIE | PDB_SC_DMAEN)
#if F_BUS == 60000000
#define PDB_PERIOD (468-1)
#elif F_BUS == 56000000
#define PDB_PERIOD (437-1)
#elif F_BUS == 48000000
#define PDB_PERIOD (375-1)
#elif F_BUS == 36000000
#define PDB_PERIOD (281-1)
#elif F_BUS == 24000000
#define PDB_PERIOD (187-1)
#elif F_BUS == 16000000
#define PDB_PERIOD (125-1)
#else
#error "Unsupported F_BUS speed"
#endif
#endif
File adc_test.ino
Code:
#include "pdb.h"
#include <DMAChannel.h>
DMAChannel dma(false);
DMAMEM static int16_t adcbuffer[128];
void setup() {
// Configure the ADC and run at least one software-triggered
// conversion. This completes the self calibration stuff and
// leaves the ADC in a state that's mostly ready to use
analogReadRes(16);
analogReference(EXTERNAL); // range 0 to 1.2 volts
analogReadAveraging(1);
analogRead(A10);
// set the programmable delay block to trigger DMA requests
SIM_SCGC6 |= SIM_SCGC6_PDB; // enable PDB clock
PDB0_IDLY = 0; // interrupt delay register
PDB0_MOD = PDB_PERIOD; // modulus register, sets period
PDB0_SC = PDB_CONFIG | PDB_SC_LDOK; // load registers from buffers
PDB0_SC = PDB_CONFIG | PDB_SC_SWTRIG; // reset and restart
PDB0_CH0C1 = 0x0101; // channel n control register?
dma.begin(true); // allocate the DMA channel first
dma.TCD->SADDR = &ADC0_RA;
dma.TCD->SOFF = 0;
dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1);
dma.TCD->NBYTES_MLNO = 2;
dma.TCD->SLAST = 0;
dma.destinationBuffer(adcbuffer, sizeof(adcbuffer));
dma.triggerAtHardwareEvent(DMAMUX_SOURCE_ADC0);
dma.enable();
}
void loop() {
// do nothing here
}