cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f103 ADC multichanel DMA STM32Cube_FW_F1_V1.0.0

Denis Krasutski
Associate III
Posted on April 03, 2015 at 03:00

Hello

everyone,

I have trouble with getting ADC values over DMA.

I saw example :STM3210E_EVAL\Examples\ADC\ADC_Sequencer and create project in MXCube with 13-ADC channels after generate code and launch project I am got only ch0 value other values set as zero.

*Interrupt HAL_ADC_ConvCpltCallbackhas been occurred.

My initialization code:


void
MX_DMA_Init(
void
)

{

/* DMA controller clock enable */

__HAL_RCC_DMA1_CLK_ENABLE();


/* DMA interrupt init */

HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 5, 0);

HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);

}


/* ADC1 init function */

void
MX_ADC1_Init(
void
)

{

ADC_ChannelConfTypeDef sConfig;



/**Common config 

*/

hadc1.Instance = ADC1;

hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;

hadc1.Init.ContinuousConvMode = ENABLE;

hadc1.Init.DiscontinuousConvMode = DISABLE;

hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;

hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;

hadc1.Init.NbrOfConversion = 13;

hadc1.Init.NbrOfDiscConversion = 13;

HAL_ADC_Init(&hadc1);



/**Configure Regular Channel 

*/

sConfig.Channel = ADC_CHANNEL_0;

sConfig.Rank = 1;

sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;

HAL_ADC_ConfigChannel(&hadc1, &sConfig);



/**Configure Regular Channel 

*/

sConfig.Channel = ADC_CHANNEL_1;

sConfig.Rank = 2;

HAL_ADC_ConfigChannel(&hadc1, &sConfig);



/**Configure Regular Channel 

*/

sConfig.Channel = ADC_CHANNEL_3;

sConfig.Rank = 3;

HAL_ADC_ConfigChannel(&hadc1, &sConfig);



/**Configure Regular Channel 

*/

sConfig.Channel = ADC_CHANNEL_6;

sConfig.Rank = 4;

HAL_ADC_ConfigChannel(&hadc1, &sConfig);



/**Configure Regular Channel 

*/

sConfig.Channel = ADC_CHANNEL_7;

sConfig.Rank = 5;

HAL_ADC_ConfigChannel(&hadc1, &sConfig);



/**Configure Regular Channel 

*/

sConfig.Channel = ADC_CHANNEL_8;

sConfig.Rank = 6;

HAL_ADC_ConfigChannel(&hadc1, &sConfig);



/**Configure Regular Channel 

*/

sConfig.Channel = ADC_CHANNEL_9;

sConfig.Rank = 7;

HAL_ADC_ConfigChannel(&hadc1, &sConfig);



/**Configure Regular Channel 

*/

sConfig.Channel = ADC_CHANNEL_10;

sConfig.Rank = 8;

HAL_ADC_ConfigChannel(&hadc1, &sConfig);



/**Configure Regular Channel 

*/

sConfig.Channel = ADC_CHANNEL_11;

sConfig.Rank = 9;

HAL_ADC_ConfigChannel(&hadc1, &sConfig);



/**Configure Regular Channel 

*/

sConfig.Channel = ADC_CHANNEL_12;

sConfig.Rank = 10;

HAL_ADC_ConfigChannel(&hadc1, &sConfig);



/**Configure Regular Channel 

*/

sConfig.Channel = ADC_CHANNEL_13;

sConfig.Rank = 11;

HAL_ADC_ConfigChannel(&hadc1, &sConfig);



/**Configure Regular Channel 

*/

sConfig.Channel = ADC_CHANNEL_14;

sConfig.Rank = 12;

HAL_ADC_ConfigChannel(&hadc1, &sConfig);



/**Configure Regular Channel 

*/

sConfig.Channel = ADC_CHANNEL_15;

sConfig.Rank = 13;

HAL_ADC_ConfigChannel(&hadc1, &sConfig);



}




void
HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)

{

RCC_PeriphCLKInitTypeDef PeriphClkInit;

GPIO_InitTypeDef GPIO_InitStruct;

if
(hadc->Instance==ADC1)

{

/* USER CODE BEGIN ADC1_MspInit 0 */



/* USER CODE END ADC1_MspInit 0 */

/* Peripheral clock enable */

__ADC1_CLK_ENABLE();


PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;

PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;

HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit); 


/**ADC1 GPIO Configuration 

PC0 ------> ADC1_IN10

PC1 ------> ADC1_IN11

PC2 ------> ADC1_IN12

PC3 ------> ADC1_IN13

PA0-WKUP ------> ADC1_IN0

PA1 ------> ADC1_IN1

PA3 ------> ADC1_IN3

PA6 ------> ADC1_IN6

PA7 ------> ADC1_IN7

PC4 ------> ADC1_IN14

PC5 ------> ADC1_IN15

PB0 ------> ADC1_IN8

PB1 ------> ADC1_IN9 

*/

GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3 

|GPIO_PIN_4|GPIO_PIN_5;

GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;

GPIO_InitStruct.Pull = GPIO_NOPULL;

HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);



GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_3|GPIO_PIN_6 

|GPIO_PIN_7;

GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);



GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;

GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;

HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);



/* Peripheral DMA init*/


hdma_adc1.Instance = DMA1_Channel1;

hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;

hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;

hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;

hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;

hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;

hdma_adc1.Init.Mode = DMA_CIRCULAR;

hdma_adc1.Init.Priority = DMA_PRIORITY_VERY_HIGH;


HAL_DMA_DeInit(&hdma_adc1);

HAL_DMA_Init(&hdma_adc1);



__HAL_LINKDMA(hadc,DMA_Handle,hdma_adc1);



/* System interrupt init*/

HAL_NVIC_SetPriority(ADC1_2_IRQn, 5, 0);

HAL_NVIC_EnableIRQ(ADC1_2_IRQn);

/* USER CODE BEGIN ADC1_MspInit 1 */



/* USER CODE END ADC1_MspInit 1 */

}

}


void
DMA1_Channel1_IRQHandler(
void
)

{

/* USER CODE BEGIN DMA1_Channel1_IRQn 0 */



/* USER CODE END DMA1_Channel1_IRQn 0 */

HAL_DMA_IRQHandler(&hdma_adc1);

/* USER CODE BEGIN DMA1_Channel1_IRQn 1 */



/* USER CODE END DMA1_Channel1_IRQn 1 */

}


void
HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc){ 

}


void
HAL_ADC_ConvCpltCallback( ADC_HandleTypeDef * hadc){

xSemaphoreGiveFromISR(_dma_ready_sem, NULL);

}


Start:

HAL_ADC_Start_DMA(&hadc1, (uint32_t*)table, 13);

//wait complete

//result:167, 0, 0, 0 ,0, 0, 0, 0 ,0, 0, 0, 0, 0

What else is required to run the ADC?

Or this problem in library?

Best Regards,

Krasutski Denis

#stm32f103-cubemx-dma-adc
1 REPLY 1
Denis Krasutski
Associate III
Posted on April 03, 2015 at 07:56

I has been try launch

ADC_Sequencer on my board.

I got some result after launch:

if
(HAL_ADC_Start_DMA(&AdcHandle,
(uint32_t *)aADCxConvertedValues,
ADCCONVERTEDVALUES_BUFFER_SIZE
) != HAL_OK)
{
/* Start Error */
Error_Handler();
}

aADCxConvertedValues contain only one values, other zero, dma interrupts not occurred!

But this array filled after run 3 times:

HAL_ADC_Start(&AdcHandle);
/* Wait for conversion completion before conditional check hereafter */
HAL_ADC_PollForConversion(&AdcHandle, 1);

It is normally? for get all ADC values I shuld run

HAL_ADC_PollForConversion several times?