ADC GDMA Mode

This sample code guide is designed to help users easily and comprehensively understand ADC sample. This sample demonstrates how ADC samples data in continuous mode by GDMA. This sample uses continuous mode of the ADC peripheral to measure voltage on P0_0 and P0_1.

Requirements

For hardware requirements, please refer to the Requirements.

Wiring

Connect P0_0 and P0_1 of EVB to external DC voltage source. Input voltage of P0_0 and P0_1 must range from 0 to 3.3V.

Configurations

  1. The entry function is as follows, call this function in main() to run this sample code. For more details, please refer to the Initialization.

    adc_gdma_demo();
    

Building and Downloading

For building and downloading, please refer to the Building and Downloading.

Experimental Verification

Press the Reset button on the EVB, ADC starts continuous sampling, and sample rawdata will be stored in array ADC_Buffer.

Code Overview

This section introduces the code and process description for initialization and corresponding function implementation in the sample.

Note

To use the mode, please turn off Charger auto enable and Battery detection support on the MCUConfig Tool. As shown in Turn Off Charger on the MCUConfig Tool.

../../../_images/Turn_Off_Charger_on_the_McuConfig_Tool.jpg

Turn Off Charger on the MCUConfig Tool

Source Code Directory

  • For project directory, please refer to Source Code Directory.

  • Source code directory: sdk\src\sample\io_demo\gdma\adc_dma\adc_gdma_demo.c.

ADC Initialization

The initialization flow for peripherals can refer to Initialization Flow.

ADC continuous mode initialization flow is shown in the following figure.

../../../_images/ADC_Continuous_Sampling_by_DMA_Mode.png

ADC Continuous Mode Initialization Flow Chart

  1. Call Pad_Config() and Pinmux_Config() to initialize the pin.

    static void board_adc_init(void)
    {
       Pad_Config(ADC_0, PAD_SW_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_DISABLE, PAD_OUT_LOW);
       Pad_Config(ADC_1, PAD_SW_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_DISABLE, PAD_OUT_LOW);
       Pinmux_Config(ADC_0, IDLE_MODE);
       Pinmux_Config(ADC_1, IDLE_MODE);
    }
    
  2. Call RCC_PeriphClockCmd() to enable the ADC clock and function.

  3. Initialize the ADC peripheral:

    1. Define the ADC_InitTypeDef type adcInitStruct, and call ADC_StructInit() to pre-fill adcInitStruct with default values.

    2. Modify the adcInitStruct parameters as needed. The ADC initialization parameter configuration is shown in the table below.

    3. Call ADC_Init() to initialize the ADC peripheral.

    ADC Initialization Parameters

    ADC Hardware Parameters

    Setting in the adcInitStruct Variables

    ADC

    Bit Map

    ADC_InitTypeDef::bitmap

    0x03

    Schedule Index

    ADC_InitTypeDef::schIndex

    Index 0 is set to EXT_SINGLE_ENDED(0).

    Index 1 is set to EXT_SINGLE_ENDED(1).

    Burst Size

    ADC_InitTypeDef::adcBurstSize

    1

    Sample Time

    ADC_InitTypeDef::adcClock

    ADC_CLK_39K

    FIFO Threshold Level

    ADC_InitTypeDef::adcFifoThd

    5

  4. Call ADC_INTConfig() to enable ADC FIFO read error interrupt ADC_INT_FIFO_RD_ERR.

  5. Call NVIC_Init() to enable NVIC of ADC.

GDMA Initialization

GDMA initialization flow is shown in the following figure.

../../../_images/ADC_GDMA_Initialization_Flow_Chart.png

GDMA Initialization Flow Chart

  1. Call RCC_PeriphClockCmd() to enable the GDMA clock and function.

  2. Call GDMA_channel_request to request an unused GDMA channel.

  3. Initialize the GDMA peripheral:

    1. Define the GDMA_InitTypeDef type GDMA_InitStruct, and call GDMA_StructInit() to pre-fill GDMA_InitStruct with default values.

    2. Modify the GDMA_InitStruct parameters as needed. The GDMA initialization parameter configuration is shown in the table below.

    3. Call GDMA_Init() to initialize the GDMA peripheral.

    GDMA Initialization Parameters

    GDMA Hardware Parameters

    Setting in the GDMA_InitStruct Variables

    GDMA

    Channel Num

    GDMA_InitTypeDef::GDMA_ChannelNum

    ADC_DMA_CHANNEL_NUM

    Transfer Direction

    GDMA_InitTypeDef::GDMA_DIR

    GDMA_DIR_PeripheralToMemory

    Buffer Size

    GDMA_InitTypeDef::GDMA_BufferSize

    200

    Source Address Increment or Fix

    GDMA_InitTypeDef::GDMA_SourceInc

    DMA_SourceInc_Fix

    Destination Address Increment or Fix

    GDMA_InitTypeDef::GDMA_DestinationInc

    DMA_DestinationInc_Inc

    Source Data Size

    GDMA_InitTypeDef::GDMA_SourceDataSize

    GDMA_DataSize_HalfWord

    Destination Data Size

    GDMA_InitTypeDef::GDMA_DestinationDataSize

    GDMA_DataSize_HalfWord

    Source Burst Transaction Length

    GDMA_InitTypeDef::GDMA_SourceMsize

    GDMA_Msize_1

    Destination Burst Transaction Length

    GDMA_InitTypeDef::GDMA_DestinationMsize

    GDMA_Msize_1

    Source Address

    GDMA_InitTypeDef::GDMA_SourceAddr

    (uint32_t)(&(ADC->FIFO))

    Destination Address

    GDMA_InitTypeDef::GDMA_DestinationAddr

    (uint32_t)ADC_Buffer

    Source Handshake

    GDMA_InitTypeDef::GDMA_SourceHandshake

    GDMA_Handshake_ADC

  4. Call NVIC_Init() to enable NVIC of GDMA.

  5. Call GDMA_INTConfig() to enable GDMA transfer complete interrupt GDMA_INT_Transfer.

Functional Implementation

Enable ADC Contiunous Sampling

  1. Call GDMA_Cmd() to enable GDMA.

  2. Call ADC_Cmd() to enable ADC contiunous sampling.

ADC Interrupt Handle

When read the empty FIFO, ADC FIFO read error interrupt is triggered:

  1. Call ADC_GetIntFlagStatus() to check ADC_INT_FIFO_RD_ERR interrupt status flag.

  2. Call ADC_ClearINTPendingBit() to clear ADC_INT_FIFO_RD_ERR interrupt.

GDMA Interrupt Handle

When GDMA transfer is completed, transfer complete interrupt is triggered:

  1. Call GDMA_ClearINTPendingBit() to clear GDMA_INT_Transfer interrupt.

  2. Call ADC_Cmd() to disable ADC.

  3. Call GDMA_channel_release() to release the GDMA channel used by ADC.