AnsweredAssumed Answered

Trigger ADC with timer problem

Question asked by Bugfighter on Mar 17, 2015
Latest reply on Mar 17, 2015 by Bugfighter
Dear all.
I'm trying to set up the ADC to be triggered by timer 3 and updating the conversion result using DMA. Seems like the ADC callback is called only once. Timer 3 is running fine. I'm running the code on Discovery STM32f429 using the HAL config functions.
Here is my code:
001./* Includes ------------------------------------------------------------------*/
002.#include "main.h"
003.#include "WM.h"
004.#include "stm32f4xx_hal_conf.h"
005.#include "stm32f4xx_hal_adc.h"
006. 
007./* Private typedef -----------------------------------------------------------*/
008./* Private define ------------------------------------------------------------*/
009./* Private macro -------------------------------------------------------------*/
010./* Private variables ---------------------------------------------------------*/
011.uint8_t GUI_Initialized = 0;
012.uint8_t pulsState=0;
013.uint8_t firstPulse=0;
014.uint8_t pulseDelay=0;
015.uint8_t secondPulse=0;
016.uint8_t firstPulseDuration=8;
017.uint8_t pulseDelayDuration=3;
018.uint8_t secondPulseDuration=12;
019. 
020.TIM_HandleTypeDef TimHandle;
021.TIM_HandleTypeDef Tim4Handle;
022.TIM_MasterConfigTypeDef Tim3MasterConfigHandle;
023.uint32_t uwPrescalerValue = 0;
024. 
025./* ADC handler declaration */
026.ADC_HandleTypeDef    AdcHandle;
027. 
028./* Variable used to get converted value */
029.__IO uint16_t uhADCxConvertedValue = 50;
030. 
031./* Private function prototypes -----------------------------------------------*/
032.static void BSP_Config(void);
033.static void SystemClock_Config(void);
034.void BSP_Pointer_Update(void);
035.void BSP_Background(void);
036.static void Error_Handler(void);
037.static void EXTILine0_Config(void);
038. 
039.extern void MainTask(void);
040.void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin);
041. 
042./* Private functions ---------------------------------------------------------*/
043. 
044./**
045.  * @brief  Main program
046.  * @param  None
047.  * @retval None
048.  */
049.int main(void)
050.{
051.  ADC_ChannelConfTypeDef sConfig;
052.  /* STM32F4xx HAL library initialization:
053.       - Configure the Flash prefetch, instruction and Data caches
054.       - Configure the Systick to generate an interrupt each 1 msec
055.       - Set NVIC Group Priority to 4
056.       - Global MSP (MCU Support Package) initialization
057.     */
058.  HAL_Init();
059. 
060.  /* Initialize LCD and LEDs */
061.  BSP_Config();
062. 
063.  /* Configure the system clock to 180 MHz */
064.  SystemClock_Config();
065. 
066.   /* Configure EXTI Line0 (connected to PA0 pin) in interrupt mode */
067.  EXTILine0_Config();
068. 
069.    /*##-1- Configure the ADC peripheral #######################################*/
070.  AdcHandle.Instance          = ADCx;
071. 
072.  AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4;
073.  AdcHandle.Init.Resolution = ADC_RESOLUTION_12B;
074.  AdcHandle.Init.ScanConvMode = DISABLE;
075.  AdcHandle.Init.ContinuousConvMode = DISABLE;
076.  AdcHandle.Init.DiscontinuousConvMode = DISABLE;
077.  AdcHandle.Init.NbrOfDiscConversion = 1;
078.  AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
079.  AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T3_TRGO;
080.  AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
081.  AdcHandle.Init.NbrOfConversion = 1;
082.  AdcHandle.Init.DMAContinuousRequests = ENABLE;
083.  AdcHandle.Init.EOCSelection = DISABLE;
084. 
085.  if(HAL_ADC_Init(&AdcHandle) != HAL_OK)
086.  {
087.    /* Initialization Error */
088.    Error_Handler();
089.  }
090. 
091.    /*##-2- Configure ADC regular channel ######################################*/
092.  sConfig.Channel = ADCx_CHANNEL;
093.  sConfig.Rank = 1;
094.  sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
095.  //sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
096.  sConfig.Offset = 0;
097. 
098.  if(HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK)
099.  {
100.    /* Channel Configuration Error */
101.    Error_Handler();
102.  }
103. 
104.  /*##-3- Start the conversion process and enable interrupt ##################*/
105.  if(HAL_ADC_Start_DMA(&AdcHandle, (uint32_t*)&uhADCxConvertedValue, 1) != HAL_OK)
106.  {
107.    /* Start Conversation Error */
108.    Error_Handler();
109.  }
110. 
111. 
112.  /***********************************************************/
113.   /* Compute the prescaler value to have TIM3 counter clock equal to 10 KHz */
114.  uwPrescalerValue = (uint32_t) ((SystemCoreClock /2) / 10000) - 1;
115. 
116.  /* Set TIMx instance */
117.  TimHandle.Instance = TIM3;
118. 
119.  /* Initialize TIM3 peripheral as follows:
120.       + Period = 500 - 1
121.       + Prescaler = ((SystemCoreClock/2)/10000) - 1
122.       + ClockDivision = 0
123.       + Counter direction = Up
124.  */
125.  TimHandle.Init.Period = 500 - 1;
126.  TimHandle.Init.Prescaler = uwPrescalerValue;
127.  TimHandle.Init.ClockDivision = 0;
128.  TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
129.  if(HAL_TIM_Base_Init(&TimHandle) != HAL_OK)
130.  {
131.    Error_Handler();
132.  }
133. 
134.     //  /* TIM3 TRGO selection */
135.  Tim3MasterConfigHandle.MasterOutputTrigger=TIM_TRGO_ENABLE;
136.//  Tim3MasterConfigHandle.MasterOutputTrigger=TIM_TRGO_RESET;
137.  Tim3MasterConfigHandle.MasterSlaveMode=TIM_MASTERSLAVEMODE_DISABLE;
138.  if(HAL_TIMEx_MasterConfigSynchronization(&TimHandle, &Tim3MasterConfigHandle)!=HAL_OK)
139.  {
140.    Error_Handler();
141.  }
142. 
143.  /*##-2- Start the TIM Base generation in interrupt mode ####################*/
144.  /* Start Channel1 */
145.  if(HAL_TIM_Base_Start_IT(&TimHandle) != HAL_OK)
146.  {
147.     Error_Handler();
148.  }
149. 
150. 
151. 
152.  /***********************************************************/
153. 
154.   /***********************************************************/
155.   /* Compute the prescaler value to have TIM4 counter clock equal to 10 KHz */
156.  uwPrescalerValue = (uint32_t) ((SystemCoreClock /2) / 10000) - 1;
157. 
158.  /* Set TIMx instance */
159.  Tim4Handle.Instance = TIM4;
160. 
161.  /* Initialize TIM3 peripheral as follows:
162.       + Period = 10 - 1
163.       + Prescaler = ((SystemCoreClock/2)/10000) - 1
164.       + ClockDivision = 0
165.       + Counter direction = Up
166.  */
167.  Tim4Handle.Init.Period = 10 - 1;
168.  Tim4Handle.Init.Prescaler = uwPrescalerValue;
169.  Tim4Handle.Init.ClockDivision = 0;
170.  Tim4Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
171.  if(HAL_TIM_Base_Init(&Tim4Handle) != HAL_OK)
172.  {
173.    Error_Handler();
174.  }
175.  /* This timer will be started when we push the switch */
176. 
177.  /***********************************************************/
178. 
179.  /* Init the STemWin GUI Library */
180.  GUI_Init();
181.  GUI_Initialized = 1;
182. 
183.  /* Activate the use of memory device feature */
184.  WM_SetCreateFlags(WM_CF_MEMDEV);
185. 
186.  MainTask();
187. 
188.  /* Infinite loop */
189.  while (1)
190.  {
191.  }
192.}
193. 
194./**
195.  * @brief  Configures EXTI Line0 (connected to PA0 pin) in interrupt mode
196.  * @param  None
197.  * @retval None
198.  */
199.static void EXTILine0_Config(void)
200.{
201.  GPIO_InitTypeDef   GPIO_InitStructure;
202. 
203.  /* Enable GPIOA clock */
204.  __HAL_RCC_GPIOA_CLK_ENABLE();
205. 
206.  /* Configure PA0 pin as input floating */
207.  GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING;
208.  GPIO_InitStructure.Pull = GPIO_NOPULL;
209.  GPIO_InitStructure.Pin = GPIO_PIN_0;
210.  HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
211. 
212.  /* Enable and set EXTI Line0 Interrupt to the lowest priority */
213.  HAL_NVIC_SetPriority(EXTI0_IRQn, 6, 7);
214.  HAL_NVIC_EnableIRQ(EXTI0_IRQn);
215.}
216. 
217./**
218.  * @brief  Conversion complete callback in non blocking mode
219.  * @param  AdcHandle : AdcHandle handle
220.  * @note   This example shows a simple way to report end of conversion, and
221.  *         you can add your own implementation.
222.  * @retval None
223.  */
224.void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* AdcHandle)
225.{
226.  /* Turn LED3 on: Transfer process is correct */
227.  BSP_LED_On(LED3);
228.  BSP_LED_Toggle(LED4);
229.}
230. 
231./**
232.  * @brief EXTI line detection callbacks
233.  * @param GPIO_Pin: Specifies the pins connected EXTI line
234.  * @retval None
235.  */
236.void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
237.{
238.  if((GPIO_Pin == KEY_BUTTON_PIN)&&(pulsState==0))
239.  {
240.    /* Start timer 4 to couhnt ms */
241.  /*##-2- Start the TIM Base generation in interrupt mode ####################*/
242.  /* Start Channel1 */
243.  if(HAL_TIM_Base_Start_IT(&Tim4Handle) != HAL_OK)
244.  {
245.     Error_Handler();
246.  }
247. 
248.  }
249.}
250./**
251.  * @brief  Period elapsed callback in non blocking mode
252.  * @param  htim: TIM handle
253.  * @retval None
254.  */
255.void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
256.{
257.  if(htim==&TimHandle)
258.  {
259.   BSP_Background();
260.  }
261. 
262.  if(htim==&Tim4Handle)
263.  {
264. 
265.    switch (pulsState)
266.    {
267.     case 0:
268.       BSP_LED_On(LED4);
269.       pulsState=1;
270.      break;
271.     case 1:
272. 
273.        if(firstPulse++>=firstPulseDuration)
274.        {
275.          BSP_LED_Off(LED4);
276.          pulsState=2;
277.        }
278.        break;
279. 
280.     case 2:
281. 
282.      if(pulseDelay++>=pulseDelayDuration)
283.      {
284.       BSP_LED_On(LED4);
285.       pulsState=3;
286.      }
287.      break;
288.     case 3:
289.      if(secondPulse++>=secondPulseDuration)
290.      {
291.       BSP_LED_Off(LED4);
292.       firstPulse=0;
293.       pulseDelay=0;
294.       secondPulse=0;
295.       pulsState=0;
296.       if(HAL_TIM_Base_Stop_IT(&Tim4Handle) != HAL_OK)
297.        {
298.          Error_Handler();
299.        }
300.      }
301.      break;
302.    }
303.  }
304.}
305. 
306./**
307.  * @brief TIM MSP Initialization
308.  *        This function configures the hardware resources used in This application:
309.  *           - Peripheral's clock enable
310.  *           - Peripheral's GPIO Configuration
311.  * @param htim: TIM handle pointer
312.  * @retval None
313.  */
314.void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim)
315.{
316. 
317.  if(htim==&TimHandle)
318.  {
319.      /*##-1- Enable peripherals and GPIO Clocks #################################*/
320.  /* TIMx Peripheral clock enable */
321.  __HAL_RCC_TIM3_CLK_ENABLE();
322. 
323.  /*##-2- Configure the NVIC for TIMx ########################################*/
324.  /* Set the TIMx priority */
325.  HAL_NVIC_SetPriority(TIM3_IRQn, 2, 2);
326. 
327.  /* Enable the TIMx global Interrupt */
328.  HAL_NVIC_EnableIRQ(TIM3_IRQn);
329.  }
330.  else
331.    if(htim==&Tim4Handle)
332.    {
333.    /*##-1- Enable peripherals and GPIO Clocks #################################*/
334.  /* TIMx Peripheral clock enable */
335.  __HAL_RCC_TIM4_CLK_ENABLE();
336. 
337.  /*##-2- Configure the NVIC for TIMx ########################################*/
338.  /* Set the TIMx priority */
339.  HAL_NVIC_SetPriority(TIM4_IRQn, 0, 1);
340. 
341.  /* Enable the TIMx global Interrupt */
342.  HAL_NVIC_EnableIRQ(TIM4_IRQn);
343.    }
344.}
345. 
346./**
347.  * @brief  Initializes the STM32F429I-DISCO's LCD and LEDs resources.
348.  * @param  None
349.  * @retval None
350.  */
351.static void BSP_Config(void)
352.{
353.  /* Initialize STM32F429I-DISCO's LEDs */
354.  BSP_LED_Init(LED3);
355.  BSP_LED_Init(LED4);
356. 
357.  /* Initializes the SDRAM device */
358.  BSP_SDRAM_Init();
359. 
360.  /* Initialize the Touch screen */
361.  BSP_TS_Init(240, 320);
362. 
363.  /* Enable the CRC Module */
364.  __HAL_RCC_CRC_CLK_ENABLE();
365.}
366. 
367./**
368.  * @brief  BSP_Background.
369.  * @param  None
370.  * @retval None
371.  */
372.void BSP_Background(void)
373.{
374.  BSP_LED_Toggle(LED3);
375. 
376. 
377.  /* Capture input event and update cursor */
378.  if(GUI_Initialized == 1)
379.  {
380.    BSP_Pointer_Update();
381. 
382.  }
383.}
384. 
385./**
386.  * @brief  Provide the GUI with current state of the touch screen
387.  * @param  None
388.  * @retval None
389.  */
390.void BSP_Pointer_Update(void)
391.{
392.  GUI_PID_STATE TS_State;
393.  static TS_StateTypeDef prev_state;
394.  TS_StateTypeDef  ts;
395.  uint16_t xDiff, yDiff;
396. 
397.  BSP_TS_GetState(&ts);
398. 
399.  TS_State.Pressed = ts.TouchDetected;
400. 
401.  xDiff = (prev_state.X > ts.X) ? (prev_state.X - ts.X) : (ts.X - prev_state.X);
402.  yDiff = (prev_state.Y > ts.Y) ? (prev_state.Y - ts.Y) : (ts.Y - prev_state.Y);
403. 
404.  if(ts.TouchDetected)
405.  {
406.    if((prev_state.TouchDetected != ts.TouchDetected )||
407.       (xDiff > 3 )||
408.         (yDiff > 3))
409.    {
410.      prev_state = ts;
411. 
412.      TS_State.Layer = 0;
413.      TS_State.x = ts.X;
414.      TS_State.y = ts.Y;
415. 
416.      GUI_TOUCH_StoreStateEx(&TS_State);
417.      //BSP_LED_Toggle(LED4);
418.    }
419.  }
420.  else{
421.  TS_State.Layer = 0;
422.  TS_State.x = -1;
423.  TS_State.y = -1;
424.  prev_state.X=-1;
425.  prev_state.Y=-1;
426.  GUI_TOUCH_StoreStateEx(&TS_State);
427.  }
428.}
429. 
430./**
431.  * @brief  System Clock Configuration
432.  *         The system Clock is configured as follow :
433.  *            System Clock source            = PLL (HSE)
434.  *            SYSCLK(Hz)                     = 180000000
435.  *            HCLK(Hz)                       = 180000000
436.  *            AHB Prescaler                  = 1
437.  *            APB1 Prescaler                 = 4
438.  *            APB2 Prescaler                 = 2
439.  *            HSE Frequency(Hz)              = 8000000
440.  *            PLL_M                          = 8
441.  *            PLL_N                          = 360
442.  *            PLL_P                          = 2
443.  *            PLL_Q                          = 7
444.  *            VDD(V)                         = 3.3
445.  *            Main regulator output voltage  = Scale1 mode
446.  *            Flash Latency(WS)              = 5
447.  * @param  None
448.  * @retval None
449.  */
450.static void SystemClock_Config(void)
451.{
452.  RCC_ClkInitTypeDef RCC_ClkInitStruct;
453.  RCC_OscInitTypeDef RCC_OscInitStruct;
454. 
455.  /* Enable Power Control clock */
456.  __HAL_RCC_PWR_CLK_ENABLE();
457. 
458.  /* The voltage scaling allows optimizing the power consumption when the device is
459.     clocked below the maximum system frequency, to update the voltage scaling value
460.     regarding system frequency refer to product datasheet.  */
461.  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
462. 
463.  /* Enable HSE Oscillator and activate PLL with HSE as source */
464.  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
465.  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
466.  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
467.  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
468.  RCC_OscInitStruct.PLL.PLLM = 8;
469.  RCC_OscInitStruct.PLL.PLLN = 360;
470.  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
471.  RCC_OscInitStruct.PLL.PLLQ = 7;
472.  HAL_RCC_OscConfig(&RCC_OscInitStruct);
473.  if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
474.  {
475.    Error_Handler();
476.  }
477.  /* Activate the Over-Drive mode */
478.  HAL_PWREx_EnableOverDrive();
479. 
480.  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
481.  clocks dividers */
482.  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
483.  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
484.  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
485.  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
486.  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
487.  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
488.  if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
489.  {
490.    Error_Handler();
491.  }
492.}
493. 
494./**
495.  * @brief  This function is executed in case of error occurrence.
496.  * @param  None
497.  * @retval None
498.  */
499.static void Error_Handler(void)
500.{
501.  /* Turn LED4 on */
502.  BSP_LED_On(LED4);
503.  while(1)
504.  {
505.  }
506.}
507. 
508.#ifdef  USE_FULL_ASSERT
509./**
510.  * @brief  Reports the name of the source file and the source line number
511.  *   where the assert_param error has occurred.
512.  * @param  file: pointer to the source file name
513.  * @param  line: assert_param error line source number
514.  * @retval None
515.  */
516.void assert_failed(uint8_t* file, uint32_t line)
517.{
518.  /* User can add his own implementation to report the file name and line number,
519.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
520. 
521.  /* Infinite loop */
522.  while (1)
523.  {
524.  }
525.}
526.#endif

Outcomes