This tutorial contains the following goals:
1) How to use the main features of the eDMA component 2.4.0 (periodic trigger, peripheral request, linking, scatter-gather) in the Peripherals Configuration Tool.
2) How to set up periodic ADC autonomous (without CPU) queued measurement controlled by DMA.
The application is developed in MCUXpresso IDE 11.5.0 which can be downloaded from https://www.nxp.com/mcuxpresso/ide website. It integrates MCUXpresso Config Tools Version 11 that is used for application design and configuration.
The FRDM-K66F - Freedom Development Platform for Kinetis K66 SDK version 2.11.0 can be downloaded from the https://mcuxpresso.nxp.com/ website or using the MCUXpresso IDE.
The main goal of this application is to show the configuration of the DMA peripheral which can be used for the ADC channel to be measured and for collecting the results into the resulting output array. It demonstrates an approach for periodic measurement of multiple ADC channels without CPU intervention. It can be used on any MCU with a simple ADC peripheral (without a possibility to queue measured channels).
The application starts with the initialization of pins, clocks, and peripherals configured by the MCUXpresso Config Tool. After the initial state is printed to the console, it starts the PIT (periodic interrupt timer). The PIT channel named Trigger causes periodic transfer on the DMA channel named ADC, which configures the ADC peripheral’s channel to be measured. When the timer counting is completed, the ADC measures the analog signal of the DAC peripheral that has been already initialized. The interrupt routine of the PIT Trigger channel includes the actual DAC data reading sequence that will be printed at the end of the console.
After the end of the measurement loop, there is the ADC peripheral’s DMA request which initiates the DMA channel named OUTPUT to read the measured ADC data. The DMA channel OUTPUT links the DMA channel named DAC, which increases the buffer read index of the DAC peripheral and generates the next analog signal to be measured by the ADC. The sample number corresponds to the number of major loops of the DMA channel OUTPUT. The major count completion interrupt of the DMA channel OUTPUT stops the measurement trigger (PIT) and enables the DMA request of the DMA channel named PROCESS. The DMA channel PROCESS processes the whole dataset by sorting it into individual data buffers using the scatter-gather function. The major count completion interrupt of the DMA channel PROCESS sets the flag for printing the results and starts the delay timer (PIT second channel named Delay) that delays individual measurements. The post-delay interrupt routine stops the PIT Delay channel and restarts the PIT Trigger channel, which initiates the whole cycle again.
Figure 1. Application diagram
The project includes the initial configuration of the following peripherals by the Peripherals Tool:
The DAC peripheral is used for creating simulated data to be measured by the ADC.
DAC peripheral setup:
The DAC buffer works in swing mode, so it moves the buffer read index from the first item to the last, then back to the first, and so on. After the one loop is measured by the ADC nearly the same values are printed out to the console log, due to the same voltage reference selected in both the DAC and the ADC. The DAC interrupt is not used.
Figure 2. DAC Configuration
The ADC peripheral is used for measuring an analog signal (in this case, generated by the DAC peripheral) and converting it into a digital signal.
ADC peripheral setup:
The ADC channel selection is configured by the DMA. The adc_trigger[] array includes configurations of the SC1A register, which selects and triggers the ADC channel measurement.
The clock frequency for the ADC measurement is set to 3.75 MHz. The conversion time is calculated in the following way:
Conversion time = SFC adder + AverageNum * (BCT + LST adder + HSC adder)
Where:
The resulting conversion time is calculated as follows:
Conversion time = (5 ADCK + 5 bus clock cycles) + 8 * (20 ADCK + 0 ADCK + 0 ADCK)
= 165 * 16/60000000 + 5/60000000
= 44.083 us
Figure 3. ADC configuration
The PIT peripheral is used for setting time delays.
PIT peripheral setup:
PIT Channel 0 is configured to set the delay between each ADC measurement. It periodically triggers a DMA transfer of DMA channel 0 which writes to the ADC register SC1A.
Setup PIT channel 0:
Figure 4. PIT configuration channel 0 named Trigger
PIT Channel 1 is configured to set a delay for printing results. It starts at the end of the measurement loop.
PIT channel 1 setup:
Figure 5. DMA channel OUTPUT TCD configuration
Note: The error interrupt vector and channel error event for all DMA channels are enabled. It can improve the debugging process. The ISR is defined in the DMA_ERROR handler.
eDMA peripheral setup:
Figure 6. General eDMA configuration overview
The application example uses 4 DMA channels for control:
All used DMA channels are configured as Non-transactional (TCD structures) channel API mode, which allows you to configure all TCD (Transfer Control Descriptor) registers.
The eDMA channel ADC (Figure 7) is used for setting and triggering the ADC channel to measure the analog signal generated by the DAC peripheral. It demonstrates the use-case of a triggered always-on DMA request when the selected DMA channel (0) is triggered by the corresponding PIT channel (0). The peripheral request is enabled; therefore, the channel operates immediately after the PIT trigger timer starts and completes counting.
eDMA channel ADC setup:
Figure 7. DMA channel ADC configuration
The Transfer Control Descriptor for the DMA channel ADC (Figure
eDMA channel ADC TCD named ADC_trigger setup:
Source address configuration
Destination address configuration
Minor loop configuration
Major loop configuration
Scatter-gather configuration
Figure 8. DMA channel ADC TCD configuration
The DMA channel DAC (Figure 9) is used for setting and triggering the DAC peripheral to generate an analog signal for the ADC measurement. It demonstrates the DMA channel (1) configuration without using a DMA request. A channel without a DMA request can be enabled with a software trigger or with channel linking functionality. In this case, the DMA channel DAC is linked from the DMA channel OUTPUT.
Figure 9. DMA channel DAC configuration
eDMA channel DAC setup:
Channel API mode - Non-transactional (TCD structures)
The TCD for the DMA channel DAC (Figure 10) is configured as a memory-to-peripheral transfer, which configures the DAC peripheral for generating the next analog signal.
eDMA channel DAC TCD named DAC_trigger setup:
Source address configuration
Destination address configuration
Minor loop configuration
Major loop configuration
Scatter-gather configuration
Note: Instead of the DAC peripheral the DMA channel can send or receive settings to/from any other peripherals that trigger the ADC measurement (for example, a communication peripheral - UART).
Figure 10. DMA channel DAC TCD configuration
The DMA channel OUTPUT (Figure 11) is used for reading the ADC peripheral results and storing them in memory. It demonstrates the DMA channel (2) configuration using the DMA peripheral request (ADC). The peripheral request is enabled; therefore, the channel operates immediately after the ADC measurement is done.
eDMA channel OUTPUT setup:
Figure 11. DMA channel OUTPUT configuration
The TCD for the DMA channel OUTPUT (Figure 12) is configured as a peripheral-to-memory transfer which reads the measured values by the ADC peripheral.
eDMA channel OUTPUT TCD named ADC_RESULTS setup:
Source address configuration
Destination address configuration
Minor loop configuration
Major loop configuration
Scatter-gather configuration
Interrupt configuration
Figure 12. DMA channel OUTPUT TCD configuration
The DMA channel PROCESS (Figure 13) is used for processing the results and storing them in memory. It demonstrates the DMA channel (3) configuration using a DMA always-on request. The peripheral request is disabled; therefore, the channel operates after the request is enabled by the software. In the example application, the DMA request is enabled in the interrupt routine of the DMA channel OUTPUT which is invoked after all major loops transfers are completed (measurement finished). After this DMA transfer, there are two arrays of results, one for each ADC configuration, see configuration array adc_trigger[].
eDMA channel PROCESS setup:
Figure 13. DMA channel PROCESS configuration
The DMA channel PROCESS contains pre-configured TCDs as memory-to-memory transfers that process the measured data using scatter-gather functionality. In the example application, the data from one source data-array adc_results[] is scattered into two destination data-arrays, data0[], and data1[]. The DMA channel ADC contains two ADC configurations to be measured. The values measured by the first ADC channel configuration are stored into the data0, and after that, the second ADC channel configuration data into the data1.
eDMA channel TCD Data0 setup:
Source address configuration
Destination address configuration
Minor loop configuration
Major loop configuration
Scatter-gather configuration
Setup eDMA channel TCD Data1:
Source address configuration
Destination address configuration
Minor loop configuration
Major loop configuration
Scatter-gather configuration
Scatter-gather TCD address – Data0
Interrupt configuration
Figure 14. DMA channel PROCESS TCD configuration Data0
Figure 15. DMA channel PROCESS TCD configuration Data1
Global variables and definitions:
Definitions:
• SAMPLE_COUNT - number of the measured values from one measurement loop – one swing through DAC buffer (10 values) to top and back => 2 * 10
• DATA_COUNT - number of the data in each separated output array
• ADC_CONFIGURATIONS - number of values (channel settings) for ADC0_SC1A register
• DAC_TRIGGER_CONFIGURATIONS - number of values (SW trigger) for DAC0_C0 register
Main global variables:
• dac_output - Result buffer with DAC output, the array of DAC values filled from the actual pointed buffer index – used for printing purposes/* Result buffer with ADC measurement output - interleaved channels measured data, one by one */
AT_NONCACHEABLE_SECTION_INIT(uint32_t adc_results[SAMPLE_COUNT]) = {0};
/* Process data buffers – output separated measured data from the two selected ADC channels into these two arrays */
AT_NONCACHEABLE_SECTION_INIT(uint32_t data0[DATA_COUNT]) = {0};
AT_NONCACHEABLE_SECTION_INIT(uint32_t data1[DATA_COUNT]) = {0};
/* ADC channel selection: twice channel 23 configuration – 0x17; here you can specify your own channels numbers when you transform the tutorial to real application */
AT_NONCACHEABLE_SECTION_INIT(uint32_t adc_trigger[ADC_CONFIGURATIONS]) = {0x17, 0x17};
/* Set DAC software trigger configuration – 0xF0 */
AT_NONCACHEABLE_SECTION_INIT(uint8_t dac_trigger[DAC_TRIGGER_CONFIGURATIONS]) = {0xF0};
Functions and interrupt handler's description:
int main(void) {
/* Initialization of MCU (pins, clocks and peripherals) */
.
.
.
/* Start the DMA trigger */
PIT_StartTimer(PIT_PERIPHERAL, PIT_TRIGGER);
/* Inside infinite loop */
if(printFlag){
printBuffers();
}
}
/* PIT0_IRQn interrupt handler */
void PIT_TRIGGER_IRQHANDLER(void) {
/* After each ADC trigger */
/* Fill dac_output array by the actual value */
}
/* PIT1_IRQn interrupt handler */
void PIT_DELAY_IRQHANDLER(void) {
/* At the end of the measurement loop after printing out */
/* Stop delay timer */
PIT_StopTimer(PIT_PERIPHERAL, PIT_DELAY);
/* Start triggering in the next measurement loop */
PIT_StartTimer(PIT_PERIPHERAL, PIT_TRIGGER);
}
/* DMA2_DMA18_IRQn interrupt handler */
void DMA_OUTPUT(void) {
/* After all measurement results in actual loop is transferred by DMA to adc_results[] */
/* Stop DMA trigger */
PIT_StopTimer(PIT_PERIPHERAL, PIT_TRIGGER);
/* Enable data process DMA channel request */
EDMA_EnableChannelRequest(DMA_DMA_BASEADDR, DMA_PROCESS_DMA_CHANNEL);
}
/* DMA3_DMA19_IRQn interrupt handler */
void DMA_DATA_PROCESSED(void) {
/* After finishing separation DMA transfers from adc_results[] to the data0[] and data1[] */
/* Start delay next measurement and results printing */
PIT_StartTimer(PIT_PERIPHERAL, PIT_DELAY);
}
/* DMA_Error_IRQn interrupt handler */
void DMA_ERROR(void) {
/* Just for sure */
}
void printBuffers() {
/* Printed out the DAC values with ADC measured values for comparison */
}
The tutorial shows:
1) How to use the main abilities of the DMA component 2.4.0 (periodic trigger, peripheral request, linking, scatter-gather).
2) How to set up periodic ADC autonomous (without CPU) queued measurement controlled by DMA.