cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L0 ADC DMA Continuous

Nigel Meijer
Associate II
Posted on May 18, 2018 at 10:40

I am trying to set up adc with dma in the so it runs in the background.

I Used keil to generate the code.

When calling: HAL_ADC_Start_DMA(&hadc, (uint32_t*) ADCReadings, 2); A single conversion is done and written to ADCReadings. However i expected it to run continuously untilHAL_ADC_Stop_DMA() is called.

Is it possible run it continuously or should this be done using a timer to call

HAL_ADC_Start_DMA?

Added my code for reference:

static void MX_ADC_Init(void)
{
 ADC_ChannelConfTypeDef sConfig;
 /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) 
 */
 hadc.Instance = ADC1;
 hadc.Init.OversamplingMode = DISABLE;
 hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
 hadc.Init.Resolution = ADC_RESOLUTION_12B;
 hadc.Init.SamplingTime = ADC_SAMPLETIME_79CYCLES_5;
 hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
 hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
 hadc.Init.ContinuousConvMode = ENABLE;
 hadc.Init.DiscontinuousConvMode = DISABLE;
 hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
 hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
 hadc.Init.DMAContinuousRequests = DISABLE;
 hadc.Init.EOCSelection = ADC_EOC_SEQ_CONV;
 hadc.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
 hadc.Init.LowPowerAutoWait = DISABLE;
 hadc.Init.LowPowerFrequencyMode = DISABLE;
 hadc.Init.LowPowerAutoPowerOff = DISABLE;
 if (HAL_ADC_Init(&hadc) != HAL_OK)
 {
 Error_Handler();
 }
 /**Configure for the selected ADC regular channel to be converted. 
 */
 sConfig.Channel = ADC_CHANNEL_0;
 sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
 if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
 {
 Error_Handler();
 }
 /**Configure for the selected ADC regular channel to be converted. 
 */
 sConfig.Channel = ADC_CHANNEL_1;
 if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
 {
 Error_Handler();
 }
}
/** 
 * Enable DMA controller clock
 */
static void MX_DMA_Init(void) 
{
 /* DMA controller clock enable */
 __HAL_RCC_DMA1_CLK_ENABLE();
 /* DMA interrupt init */
 /* DMA1_Channel1_IRQn interrupt configuration */
 HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
 HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
}
/** Pinout Configuration
*/
static void MX_GPIO_Init(void)
{
 /* GPIO Ports Clock Enable */
 __HAL_RCC_GPIOA_CLK_ENABLE();
}
void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
{
 GPIO_InitTypeDef GPIO_InitStruct;
 if(hadc->Instance==ADC1)
 {
 /* USER CODE BEGIN ADC1_MspInit 0 */
 /* USER CODE END ADC1_MspInit 0 */
 /* Peripheral clock enable */
 __HAL_RCC_ADC1_CLK_ENABLE();
 
 /**ADC GPIO Configuration 
 PA0 ------> ADC_IN0
 PA1 ------> ADC_IN1 
 */
 GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 /* Peripheral DMA init*/
 
 hdma_adc.Instance = DMA1_Channel1;
 hdma_adc.Init.Request = DMA_REQUEST_0;
 hdma_adc.Init.Direction = DMA_PERIPH_TO_MEMORY;
 hdma_adc.Init.PeriphInc = DMA_PINC_DISABLE;
 hdma_adc.Init.MemInc = DMA_MINC_ENABLE;
 hdma_adc.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
 hdma_adc.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
 hdma_adc.Init.Mode = DMA_CIRCULAR;
 hdma_adc.Init.Priority = DMA_PRIORITY_LOW;
 if (HAL_DMA_Init(&hdma_adc) != HAL_OK)
 {
 Error_Handler();
 }
 __HAL_LINKDMA(hadc,DMA_Handle,hdma_adc);
 /* USER CODE BEGIN ADC1_MspInit 1 */
 /* USER CODE END ADC1_MspInit 1 */
 }
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

0 REPLIES 0