[STM32F103] ADC & DMA continuous sampling 1 channel

Post your cool example code here.
Post Reply
luca_stm32
Posts: 19
Joined: Tue Feb 18, 2020 3:37 pm

[STM32F103] ADC & DMA continuous sampling 1 channel

Post by luca_stm32 »

Hi everybody.
I'm writing a little piece of code in order to acquire one analog channel (PA1) in DMA mode.

I followed viewtopic.php?t=110 and using STM32CUBEMX I wrote this code:

Code: Select all

uint32_t adc_val[1];

DMA_HandleTypeDef hdma_adc1;
ADC_HandleTypeDef hadc1;

/**
* @brief ADC MSP Initialization
* This function configures the hardware resources used in this example
* @param hadc: ADC handle pointer
* @retval None
*/
extern "C" void MY_HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(hadc->Instance==ADC1)
  {
    /* Peripheral clock enable */
    __HAL_RCC_ADC1_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**ADC1 GPIO Configuration
    PA1     ------> ADC1_IN1
    */
    GPIO_InitStruct.Pin = GPIO_PIN_1;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* ADC1 DMA Init */
    /* ADC1 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_WORD;
    hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
    hdma_adc1.Init.Mode = DMA_CIRCULAR;
    hdma_adc1.Init.Priority = DMA_PRIORITY_LOW;
    if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
    {
      Error_Handler();
    }

    __HAL_LINKDMA(hadc,DMA_Handle,hdma_adc1);
  }
}

/**
* @brief ADC MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param hadc: ADC handle pointer
* @retval None
*/
extern "C" void MY_HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
{
  if(hadc->Instance==ADC1)
  {
    /* Peripheral clock disable */
    __HAL_RCC_ADC1_CLK_DISABLE();

    /**ADC1 GPIO Configuration
    PA1     ------> ADC1_IN1
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_1);

    /* ADC1 DMA DeInit */
    HAL_DMA_DeInit(hadc->DMA_Handle);
  }
}

/**
  * @brief This function handles DMA1 channel1 global interrupt.
  */
extern "C" void DMA1_Channel1_IRQHandler(void)
{
  HAL_DMA_IRQHandler(&hdma_adc1);
}


/**
  * @brief ADC1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_ADC1_Init(void)
{
  ADC_ChannelConfTypeDef sConfig = {0};

  /** Common config
  */
  hadc1.Instance = ADC1;
  hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  hadc1.Init.ContinuousConvMode = ENABLE;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.NbrOfConversion = 1;
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure Regular Channel
  */
  sConfig.Channel = ADC_CHANNEL_1;
  sConfig.Rank = ADC_REGULAR_RANK_1;
  sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
  if (HAL_ADC_ConfigChannel(&hadc1, &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);
}

void setup() {

  pinMode (PC13, OUTPUT);
  digitalWrite(PC13,HIGH);
  
  MX_DMA_Init();
  MX_ADC1_Init();

    /* Run the ADC calibration */
  if (HAL_ADCEx_Calibration_Start(&hadc1) != HAL_OK) {
    /* Calibration Error */
    Error_Handler();
  }

  MY_HAL_ADC_MspInit(&hadc1);

  HAL_ADC_Start_DMA(&hadc1, adc_val, 1);

  Serial.begin(115200);
  
}

void loop() {

  Serial.print("Analog Value = ");
  Serial.println(adc_val[0]);
  delay(500);
  digitalToggle(PC13);

}
In order to make it compile, I had to define a MY_HAL_ADC_MspDeInit function and call it after MX_ADC1_Init, because HAL_ADC_MspDeInit is already defined in .arduino15/packages/STMicroelectronics/hardware/stm32/2.5.0/libraries/SrcWrapper/src/stm32/ and is not a WEAK function.

Is there a way to force the compilator to "ignore" HAL_ADC_MspDeInit defined in analog.cpp?

Thanks.
Regards.
Luca
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: [STM32F103] ADC & DMA continuous sampling 1 channel

Post by fpiSTM »

Yes as stated in the topic you mentioned :
To achieve this I simply disable the HAL ADC usage by the arduino API by defining -DHAL_ADC_MODULE_ONLY in build_opt.h file.
luca_stm32
Posts: 19
Joined: Tue Feb 18, 2020 3:37 pm

Re: [STM32F103] ADC & DMA continuous sampling 1 channel

Post by luca_stm32 »

Thanks fpiSTM!
Adding -DHAL_ADC_MODULE_ONLY in build_opt.h resolved the situation.

Yesterday I added -DHAL_ADC_MODULE_ONLY option in hal_conf_custom.h and a lot of errors appared.

Thanks again.
Luca
walterwhite
Posts: 1
Joined: Tue Feb 27, 2024 8:32 am
Location: https://fnfgo.org

Re: [STM32F103] ADC & DMA continuous sampling 1 channel

Post by walterwhite »

I recommend using preprocessor directives to conditionally include or exclude code based on certain conditions.

Code: Select all

#ifndef HAL_ADC_MspDeInit
extern "C" void MY_HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc) {
  // Your custom de-initialization code here
}
#endif
Post Reply

Return to “Code snippets”