Skip to main content
HWhit.1
Associate III
August 24, 2020
Question

Problem with porting CubeMX code for ADC with DMA to Arduino

  • August 24, 2020
  • 0 replies
  • 711 views

Hi,

I am trying to adapt CubeMX code for a single ADC with DMA readout. I and using a Nucleo-G431KB board and managed to port a single conversion without problems.

The code compiles OK and looks like many examples on the web. However it throws an error when HALADC_DMA_Start is called. It does not perform conversions and it seems the callbacks never called.

Any help is welcome.

 
/*
 Analog input, serial output
 
 Reads an analog input pin. Also prints the results to the Serial Monitor.
*/
 
#include "stm32g4xx_hal.h"
 
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
#define ADC_BUF_LEN 32
#define LED_PIN 13
 
ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;
 
// Variables
int a = 1;
uint16_t adc_buf[ADC_BUF_LEN];
 
 
void setup() {
 // initialize serial communications at 9600 bps:
 Serial.begin(9600);
 /* Initialize all configured peripherals */
//void SystemClock_Config(void);
 MX_DMA_Init();
 MX_ADC1_Init();
 
 int n = HAL_ADC_Start_DMA(&hadc1, (uint32_t*)adc_buf, ADC_BUF_LEN);
 if( n != HAL_OK)
 {
 Serial.println("DMA start error");
 Serial.println(n);
 Serial.println(ADC_BUF_LEN); 
 }
}
 
void loop() {
 // To test keep printing the ADC buffer to the Serial Monitor:
 for (int i = 0; i <= (ADC_BUF_LEN-1); i++) 
 {
Serial.print(i); Serial.print(" : "); Serial.println(adc_buf[i]);
 }
 delay(5);
}
 
 
///%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// Callback when Buffer half full
void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc1)
{
 Serial.println("### Half full ###");
 digitalWrite(LED_PIN, HIGH); // Turn the led on
}
///%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// Callback when Buffer full
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc1)
{
 Serial.println("### FULL ###");
 // digitalWrite(LED_PIN, LOW); // Turn the led on
}
///%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 
 
 
 
//#######################################################################
/* ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
 * All rights reserved.</center></h2>
 *
 * This software component is licensed by ST under BSD 3-Clause license,
 * the "License"; You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 * opensource.org/licenses/BSD-3-Clause
 *
 ******************************************************************************
*/
 
static void MX_ADC1_Init()
{
 
 /* USER CODE BEGIN ADC1_Init 0 */
 
 /* USER CODE END ADC1_Init 0 */
 
 ADC_MultiModeTypeDef multimode = {0};
 ADC_ChannelConfTypeDef sConfig = {0};
 
 /* USER CODE BEGIN ADC1_Init 1 */
 
 /* USER CODE END ADC1_Init 1 */
 /** Common config
 */
 hadc1.Instance = ADC1;
 hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
 hadc1.Init.Resolution = ADC_RESOLUTION_12B;
 hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
 hadc1.Init.GainCompensation = 0;
 hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
 hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
 hadc1.Init.LowPowerAutoWait = DISABLE;
 hadc1.Init.ContinuousConvMode = ENABLE;
 hadc1.Init.NbrOfConversion = 1;
 hadc1.Init.DiscontinuousConvMode = DISABLE;
 hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
 hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
 hadc1.Init.DMAContinuousRequests = ENABLE;
 hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
 hadc1.Init.OversamplingMode = DISABLE;
 if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
 {
 Serial.println("ADC setup channel fail");
 }
 /** Configure the ADC multi-mode
 */
 multimode.Mode = ADC_MODE_INDEPENDENT;
 if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK)
 {
 Serial.println("ADC multimode fail");
 }
 /** Configure Regular Channel
 */
 sConfig.Channel = ADC_CHANNEL_1;
 sConfig.Rank = ADC_REGULAR_RANK_1;
 sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
 sConfig.SingleDiff = ADC_SINGLE_ENDED;
 sConfig.OffsetNumber = ADC_OFFSET_NONE;
 sConfig.Offset = 0;
 if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
 {
 Serial.println("ADC config channel fail");
 }
 
}
 
/**
 * Enable DMA controller clock
 */
static void MX_DMA_Init()
{
 
 /* DMA controller clock enable */
__HAL_RCC_DMAMUX1_CLK_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) ;
}
 
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

This topic has been closed for replies.