AnsweredAssumed Answered

stm32f103 adc dma hangs mcu

Question asked by Sarunaszx on Jul 26, 2016
Latest reply on Jul 26, 2016 by Clive One
Hello,

I ran into a problem when trying to configure ADC DMA on STM32F103 microcontroller. 
I am able to get the data from ADC, however I am unable to get into the main loop of the program. I tried changing ADC sample cycles and clock frequency, however, it did not help. If I stop dma interrupts, it works normally (not ADC of course). If I break the program it is always at void HAL_DMA_IRQHandler(DMA_HandleTypeDef *hdma). I guess it is always getting called and overloads the cpu.

Here is the code of mine:
001./* Includes ------------------------------------------------------------------*/
002.#include "stm32f1xx_hal.h"
003. 
004./* USER CODE BEGIN Includes */
005./* USER CODE END Includes */
006. 
007./* Private variables ---------------------------------------------------------*/
008.ADC_HandleTypeDef hadc1;
009.DMA_HandleTypeDef hdma_adc1;
010. 
011./* USER CODE BEGIN PV */
012./* USER CODE END PV */
013. 
014./* Private function prototypes -----------------------------------------------*/
015.void SystemClock_Config(void);
016.static void MX_GPIO_Init(void);
017.static void MX_DMA_Init(void);
018.static void MX_ADC1_Init(void);
019. 
020./* USER CODE BEGIN PFP */
021./* USER CODE END PFP */
022. 
023./* USER CODE BEGIN 0 */
024.__IO uint32_t ADC_val[2];
025.void  HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc);
026.int irl = 0;
027./* USER CODE END 0 */
028. 
029.int main(void)
030.{
031. 
032.  /* USER CODE BEGIN 1 */
033.  /* USER CODE END 1 */
034. 
035.  /* MCU Configuration----------------------------------------------------------*/
036. 
037.  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
038.  HAL_Init();
039. 
040.  /* Configure the system clock */
041.  SystemClock_Config();
042. 
043.  /* Initialize all configured peripherals */
044.  MX_GPIO_Init();
045.  MX_DMA_Init();
046.  MX_ADC1_Init();
047. 
048.  /* USER CODE BEGIN 2 */
049.  HAL_ADC_Start_DMA(&hadc1, (uint32_t*)ADC_val, 4);
050.  HAL_ADC_Start_IT(&hadc1);
051.  /* USER CODE END 2 */
052. 
053.  /* Infinite loop */
054.  /* USER CODE BEGIN WHILE */
055.  while(1){   
056.      HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_7);
057.      HAL_Delay(100);
058.      HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_7);
059.      HAL_Delay(100);
060.  }
061.  /* USER CODE END WHILE */
062. 
063.  /* USER CODE BEGIN 3 */
064.  /* USER CODE END 3 */
065. 
066.}
067. 
068./** System Clock Configuration
069.*/
070.void SystemClock_Config(void)
071.{
072. 
073.  RCC_OscInitTypeDef RCC_OscInitStruct;
074.  RCC_ClkInitTypeDef RCC_ClkInitStruct;
075.  RCC_PeriphCLKInitTypeDef PeriphClkInit;
076. 
077.  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
078.  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
079.  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
080.  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
081.  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
082.  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
083.  HAL_RCC_OscConfig(&RCC_OscInitStruct);
084. 
085.  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
086.                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
087.  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
088.  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
089.  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
090.  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
091.  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
092. 
093.  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
094.  PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
095.  HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
096. 
097.  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
098. 
099.  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
100. 
101.  /* SysTick_IRQn interrupt configuration */
102.  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
103.}
104. 
105./* ADC1 init function */
106.void MX_ADC1_Init(void)
107.{
108. 
109.  ADC_ChannelConfTypeDef sConfig;
110. 
111.    /**Common config
112.    */
113.  hadc1.Instance = ADC1;
114.  hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
115.  hadc1.Init.ContinuousConvMode = ENABLE;
116.  hadc1.Init.DiscontinuousConvMode = DISABLE;
117.  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
118.  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
119.  hadc1.Init.NbrOfConversion = 2;
120.  HAL_ADC_Init(&hadc1);
121. 
122.    /**Configure Regular Channel
123.    */
124.  sConfig.Channel = ADC_CHANNEL_1;
125.  sConfig.Rank = 1;
126.  sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
127.  HAL_ADC_ConfigChannel(&hadc1, &sConfig);
128. 
129.    /**Configure Regular Channel
130.    */
131.  sConfig.Channel = ADC_CHANNEL_2;
132.  sConfig.Rank = 2;
133.  HAL_ADC_ConfigChannel(&hadc1, &sConfig);
134. 
135.}
136. 
137./**
138.  * Enable DMA controller clock
139.  */
140.void MX_DMA_Init(void)
141.{
142.  /* DMA controller clock enable */
143.  __HAL_RCC_DMA1_CLK_ENABLE();
144. 
145.  /* DMA interrupt init */
146.  HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
147.  HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
148. 
149.}
150. 
151./** Configure pins as
152.        * Analog
153.        * Input
154.        * Output
155.        * EVENT_OUT
156.        * EXTI
157.*/
158.void MX_GPIO_Init(void)
159.{
160. 
161.  GPIO_InitTypeDef GPIO_InitStruct;
162. 
163.  /* GPIO Ports Clock Enable */
164.  __GPIOD_CLK_ENABLE();
165.  __GPIOA_CLK_ENABLE();
166.  __GPIOB_CLK_ENABLE();
167. 
168.  /*Configure GPIO pins : BUTTON1_Pin BUTTON2_Pin */
169.  GPIO_InitStruct.Pin = BUTTON1_Pin|BUTTON2_Pin;
170.  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
171.  GPIO_InitStruct.Pull = GPIO_PULLUP;
172.  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
173. 
174.  /*Configure GPIO pin : FREE_INPUT_Pin */
175.  GPIO_InitStruct.Pin = FREE_INPUT_Pin;
176.  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
177.  GPIO_InitStruct.Pull = GPIO_NOPULL;
178.  HAL_GPIO_Init(FREE_INPUT_GPIO_Port, &GPIO_InitStruct);
179. 
180.  /*Configure GPIO pins : LED1_Pin LED2_Pin */
181.  GPIO_InitStruct.Pin = LED1_Pin|LED2_Pin;
182.  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
183.  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
184.  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
185. 
186.  /*Configure GPIO pins : BUZZER_Pin FREE_OUTPUT_Pin */
187.  GPIO_InitStruct.Pin = BUZZER_Pin|FREE_OUTPUT_Pin;
188.  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
189.  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
190.  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
191. 
192.  /*Configure GPIO pin Output Level */
193.  HAL_GPIO_WritePin(GPIOB, LED1_Pin|LED2_Pin|BUZZER_Pin|FREE_OUTPUT_Pin, GPIO_PIN_RESET);
194. 
195.}
196. 
197./* USER CODE BEGIN 4 */
198.void  HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc){
199.   
200.  
201.   
202.}
203./* USER CODE END 4 */
204. 
205.#ifdef USE_FULL_ASSERT
206. 
207./**
208.   * @brief Reports the name of the source file and the source line number
209.   * where the assert_param error has occurred.
210.   * @param file: pointer to the source file name
211.   * @param line: assert_param error line source number
212.   * @retval None
213.   */
214.void assert_failed(uint8_t* file, uint32_t line)
215.{
216.  /* USER CODE BEGIN 6 */
217.  /* USER CODE END 6 */
218. 
219.}
220. 
221.#endif

and I include stm32cubemx picture of Clock configuration. I did only minor changes and also only in User Code space.

Attachments

Outcomes