ADC problem in STM32F207
Hi,
I have recently started working on a project which includes a STM32F207. I'm not able to use the ADC unit. I checked the code several times and I compared it with available examples, after debugging the code I realized that SWSTART flag in ADC1->CR2 register never turns on even after running "ADC1->CR2 |= (uint32_t)ADC_CR2_SWSTART;" the flag is zero.
I also checked datasheet, the only note says that this bit (SWSTART) can be set only if "ADON=1" which is valid in my case.
I wonder if anybody could assist me in correcting the problem.
Bellow is the code I have:
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
DMA_InitTypeDef DMA_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable ADC1, DMA2 and GPIO clocks ****************************************/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* DMA2 Stream0 channel2 configuration **************************************/
DMA_InitStructure.DMA_Channel = DMA_Channel_2;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_ADDRESS;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC1ConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0, &DMA_InitStructure);
DMA_Cmd(DMA2_Stream0, ENABLE);
/* Configure ADC1 Channel10-Channel15 pins as analog input ******************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* ADC Common Init **********************************************************/
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div8;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
/* ADC1 Init ****************************************************************/
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel configuration *************************************/
ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 1, ADC_SampleTime_3Cycles);
/* Enable DMA request after last transfer (Single-ADC mode) */
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/*SET SWSTART on ADC1*/
ADC_SoftwareStartConv(ADC1);
-
I'm not fond of the STL and prefer to bang-up the registers directly but this may point you in the right direction. The outer #defines and #includes aren't shown and some of the prep work is handled in a prior, general system initialization function. Also not the identical chip (an STM32F103) but I believe that the operation is the same as the 207.
-
How strongly do you know that conversion never starts?
You set that bit to request start-of-conversion. And then (according to the reference manual) the hardware clears that bit as soon as the conversion starts. So when debugging/single-stepping you might never see that bit as set.
I suggest you try to use the ADC _without_ DMA before trying with DMA. This way you can know if the problem is with the ADC or the DMA.
Hope this helps.
-
Thank you for your response Richard, I tried to use your code but unfortunately the compiler cannot detect these definitions :
RCC_CFGR_ADCPRE
RCC_CFGR_ADCPRE_DIV6
ADC_CR2_CAL
RCC_AHBENR_DMA1EN
DMA1_Channel1_IRQn
Dr Danish Ali, I disabled the DMA code and It did work :) . whenever I was enabling following command
ADC_DMACmd(ADC1, ENABLE);
ADC was converting only a single time (In ADC's Register) and DMA's destination register was zero all the time. Then I checked the DMA code again. I found another example for DMA Initialization and now DMA is also working fine.
I appreciate your help guys. I attach the code here for those who might be interested to see the final code.
Please sign in to leave a comment.
Comments
4 comments