AnsweredAssumed Answered

STM32F4 ADC/DMA always returns same value.

Question asked by John Hite on Apr 19, 2017
Latest reply on Apr 20, 2017 by John Hite

STM32F437

 

I want to use ADC3 with DMA to capture values and have them available when needed but I don't need an ISR. Regardless of changing the input the value never changes. Below is the code I am using. Please give a shout if you see something amiss.

 

Thanks,

JH

 

#define ADC_NUM_READINGS 1  

 

static uint16_t adc3_result;

 

static const DMA_InitTypeDef dma2 =
{
    DMA_Channel_2,
    ADC3_BASE + 0x4C,                 
    (uint32_t)&adc3_result,               
    DMA_DIR_PeripheralToMemory,
    ADC_NUM_READINGS,
    DMA_PeripheralInc_Disable,
    DMA_MemoryInc_Disable,
    DMA_PeripheralDataSize_HalfWord,
    DMA_MemoryDataSize_HalfWord,
    DMA_Mode_Circular,
    DMA_Priority_High,
    DMA_FIFOMode_Enable,
    DMA_FIFOThreshold_HalfFull,
    DMA_MemoryBurst_Single,
    DMA_PeripheralBurst_Single
};

 

void adcInit(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    ADC_CommonInitTypeDef cInit;
    ADC_InitTypeDef init;

 

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);

 

    GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA, &GPIO_InitStruct);

 

    DMA_DeInit(DMA2_Stream0);
    DMA_Init(DMA2_Stream0, (DMA_InitTypeDef*)&dma2);
    DMA_Cmd(DMA2_Stream0, ENABLE);

 

    cInit.ADC_Mode = ADC_Mode_Independent;
    cInit.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
    cInit.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
    cInit.ADC_Prescaler = ADC_Prescaler_Div2;
    ADC_CommonInit(&cInit);

 

    init.ADC_Resolution = ADC_Resolution_12b;
    init.ADC_ScanConvMode = DISABLE;
    init.ADC_ContinuousConvMode = ENABLE;
    init.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
    init.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_TRGO;
    init.ADC_DataAlign = ADC_DataAlign_Right;
    init.ADC_NbrOfConversion = 1;
    ADC_Init(ADC3, &init);

 

    ADC_RegularChannelConfig(ADC3, ADC_Channel_6, 1, ADC_SampleTime_15Cycles);
    ADC_DMARequestAfterLastTransferCmd(ADC3, ENABLE);
    ADC_DMACmd(ADC3, ENABLE);
    ADC_Cmd(ADC3, ENABLE);
    ADC_SoftwareStartConv(ADC3);
}

 

uint16_t readVoltage(void)
{
    return adc3_result;
}

Outcomes