AnsweredAssumed Answered

STM32F4 Dual ADC Simultaneous Sampling PWM Trigger

Question asked by hamed.zaid on Oct 29, 2015
My application requires random simultaneous sampling of two signals. To achieve that I used TIM1 as a trigger for ADC1&2. I configured the DMA and it was working, but I want to change the PWM pulsewidth after each sample, tried to do that in the DMA interrupt handler but the ADC kept hitting an overrun (because of the ongoing trigger I gues). So I stopped initializing the DMA and change the pulse width in the ADC interrupt handler, I read the values directly from the CDR register. The timer is set to 43 KHz period. I had an overrun problem but now things to work well if I put a delay after I start the PWM. I want to collect 1000 samples and process them afterwards.

Theoretically, things seem to be working but the values I get from the CDR register do not correspond to voltages on the pins, I tried to put a DC value on each pin. If anyone has any insights to why the values I am getting are wrong (not related to the pins), perhaps there is something wrong going on that I fail to diagnose.

Here is a snap of my code. 
#include "stm32f4xx_hal.h"
 
/* USER CODE BEGIN Includes */
 
/* USER CODE END Includes */
 
/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;
ADC_HandleTypeDef hadc2;
DMA_HandleTypeDef hdma_adc1;
 
RNG_HandleTypeDef hrng;
 
TIM_HandleTypeDef htim1;
 
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
__IO uint32_t ADCValues;
extern float s1[],s2[];
extern uint32_t counter;
uint16_t rd[1000]={0};
 
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_ADC1_Init(void);
static void MX_ADC2_Init(void);
static void MX_RNG_Init(void);
static void MX_TIM1_Init(void);
 
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
static void Error_Handler(void);
/* USER CODE END PFP */
 
/* USER CODE BEGIN 0 */
 
/* USER CODE END 0 */
 
int main(void)
{
 
  /* USER CODE BEGIN 1 */
    // Initializing variables
       uint i=0,j=0,itr=0;
       float gain[2]={0};
       float rr=0;
       uint32_t *r=0;
 
  /* USER CODE END 1 */
 
  /* MCU Configuration----------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
 // MX_DMA_Init();
  MX_ADC1_Init();
  MX_ADC2_Init();
  MX_RNG_Init();
  MX_TIM1_Init();
 
  /* USER CODE BEGIN 2 */
  /* USER CODE BEGIN 2 */
 
while(itr<2){
  // Generate 1000 random numbers for pulse width (Random Sampling)
     int c=0;
     for (c=0; c<1000;c++){
     HAL_RNG_GenerateRandomNumber(&hrng,r);
     rr=(float)HAL_RNG_ReadLastRandomNumber(&hrng)/4294967296;
     rd[c]=(uint16_t)(rr*2400); //changing the 32 bit random # to 16 bit between 0 - PWM period
     }
    if(HAL_ADCEx_MultiModeStart_DMA(&hadc1, &ADCValues,1)!= HAL_OK || HAL_ADC_Start_IT(&hadc2)!= HAL_OK){
        Error_Handler();
    }
    HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
    HAL_Delay(280);
        HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_12);// yellow LED
                            if (HAL_ADC_GetError(&hadc1)!=HAL_ADC_ERROR_NONE ||HAL_ADC_GetError(&hadc2)!= HAL_ADC_ERROR_NONE
                                                            || __HAL_ADC_GET_FLAG(&hadc1,ADC_FLAG_OVR)|| __HAL_ADC_GET_FLAG(&hadc2,ADC_FLAG_OVR))
                                        {
                                  HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13); //red LED
                                        }
itr++;
}
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_12);// yellow LED
}
 
/** System Clock Configuration
*/
void SystemClock_Config(void)
{
 
  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
 
  __PWR_CLK_ENABLE();
 
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
 
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = 16;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 13;
  RCC_OscInitStruct.PLL.PLLN = 195;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 5;
  HAL_RCC_OscConfig(&RCC_OscInitStruct);
 
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1
                              |RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3);
 
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
 
  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
 
  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
 
/* ADC1 init function */
void MX_ADC1_Init(void)
{
 
  ADC_MultiModeTypeDef multimode;
  ADC_ChannelConfTypeDef sConfig;
 
    /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
    */
  hadc1.Instance = ADC1;
  hadc1.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
  hadc1.Init.Resolution = ADC_RESOLUTION12b;
  hadc1.Init.ScanConvMode = DISABLE;
  hadc1.Init.ContinuousConvMode = DISABLE;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_FALLING;
  hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.NbrOfConversion = 1;
  hadc1.Init.DMAContinuousRequests = ENABLE;
  hadc1.Init.EOCSelection = ENABLE;
  HAL_ADC_Init(&hadc1);
 
    /**Configure the ADC multi-mode
    */
  multimode.Mode = ADC_DUALMODE_REGSIMULT;
  multimode.DMAAccessMode = ADC_DMAACCESSMODE_2;
  multimode.TwoSamplingDelay = ADC_TWOSAMPLINGDELAY_5CYCLES;
  HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode);
 
 
    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
    */
  sConfig.Channel = ADC_CHANNEL_0;
  sConfig.Rank = 1;
  sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig);
  /* Peripheral interrupt init*/
    HAL_NVIC_SetPriority(ADC_IRQn, 1, 0);
    HAL_NVIC_EnableIRQ(ADC_IRQn);
}
 
static void Error_Handler(void)
{
    HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13); // Red LED
  while(1)
  {
  }
}
 
/* ADC2 init function */
void MX_ADC2_Init(void)
{
 
  ADC_MultiModeTypeDef multimode;
  ADC_ChannelConfTypeDef sConfig;
 
    /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
    */
  hadc2.Instance = ADC2;
  hadc2.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
  hadc2.Init.Resolution = ADC_RESOLUTION12b;
  hadc2.Init.ScanConvMode = DISABLE;
  hadc2.Init.ContinuousConvMode = DISABLE;
  hadc2.Init.DiscontinuousConvMode = DISABLE;
  hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc2.Init.NbrOfConversion = 1;
  hadc2.Init.DMAContinuousRequests = ENABLE;
  hadc2.Init.EOCSelection = ENABLE;
  hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_FALLING;
  hadc2.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
  HAL_ADC_Init(&hadc2);
 
 
    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
    */
  sConfig.Channel = ADC_CHANNEL_1;
  sConfig.Rank = 1;
  sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES;
  HAL_ADC_ConfigChannel(&hadc2, &sConfig);
  /* Peripheral interrupt init*/
    HAL_NVIC_SetPriority(ADC_IRQn, 1, 0);
    HAL_NVIC_EnableIRQ(ADC_IRQn);
}
 
/* RNG init function */
void MX_RNG_Init(void)
{
 
  hrng.Instance = RNG;
  HAL_RNG_Init(&hrng);
 
}
 
/* TIM1 init function */
void MX_TIM1_Init(void)
{
 
  TIM_ClockConfigTypeDef sClockSourceConfig;
  TIM_MasterConfigTypeDef sMasterConfig;
  TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;
  TIM_OC_InitTypeDef sConfigOC;
 
  htim1.Instance = TIM1;
  htim1.Init.Prescaler = 0;
  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim1.Init.Period = 2800;
  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim1.Init.RepetitionCounter = 0;
  HAL_TIM_Base_Init(&htim1);
 
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig);
 
  HAL_TIM_PWM_Init(&htim1);
 
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_OC1;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);
 
  sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
  sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
  sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
  sBreakDeadTimeConfig.DeadTime = 0;
  sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
  sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
  sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
  HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig);
 
  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = 1200;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_ENABLE;
  sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
  sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1);
 
}
 
/**
  * Enable DMA controller clock
  */
void MX_DMA_Init(void)
{
  /* DMA controller clock enable */
    __DMA2_CLK_ENABLE();
 
  /* DMA interrupt init */
    HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, 0, 1);
    HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn);
 
}
 
/** Configure pins as
        * Analog
        * Input
        * Output
        * EVENT_OUT
        * EXTI
     PA9   ------> USB_OTG_FS_VBUS
     PA10   ------> USB_OTG_FS_ID
     PA11   ------> USB_OTG_FS_DM
     PA12   ------> USB_OTG_FS_DP
*/
void MX_GPIO_Init(void)
{
 
  GPIO_InitTypeDef GPIO_InitStruct;
 
  /* GPIO Ports Clock Enable */
  __GPIOC_CLK_ENABLE();
  __GPIOH_CLK_ENABLE();
  __GPIOA_CLK_ENABLE();
 
  /*Configure GPIO pins : PC13 PC12 */
  GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_12;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
 
  /*Configure GPIO pin : PA9 */
  GPIO_InitStruct.Pin = GPIO_PIN_9;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
  /*Configure GPIO pins : PA10 PA11 PA12 */
  GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
}
 
/* USER CODE BEGIN 4 */
 
/* USER CODE END 4 */
 
#ifdef USE_FULL_ASSERT
 
/**
   * @brief Reports the name of the source file and the source line number
   * where the assert_param error has occurred.
   * @param file: pointer to the source file name
   * @param line: assert_param error line source number
   * @retval None
   */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
    ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
 
}
 
#endif
 
/**
  * @}
  */
 
/**
  * @}
*/
 
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

And The Interrupt Handler of The ADC is as shown below:
void ADC_IRQHandler(void)
{
  /* USER CODE BEGIN ADC_IRQn 0 */
 
  /* USER CODE END ADC_IRQn 0 */
  HAL_ADC_IRQHandler(&hadc1);
  HAL_ADC_IRQHandler(&hadc2);
  /* USER CODE BEGIN ADC_IRQn 1 */
  if (HAL_ADC_GetError(&hadc1)!=HAL_ADC_ERROR_NONE ||HAL_ADC_GetError(&hadc2)!= HAL_ADC_ERROR_NONE
                                || __HAL_ADC_GET_FLAG(&hadc1,ADC_FLAG_OVR)|| __HAL_ADC_GET_FLAG(&hadc2,ADC_FLAG_OVR))
            {
      HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13); //red LED
            }
  if(counter<1000)
        {
                    temp=(uint16_t)(ADC->CDR);
                    s1[counter]=(float)temp/(float)4095*(float)3300; // reading value from first adc in mV 3.3Vref/12bits
                    temp=(uint16_t)((ADC->CDR)>>16);
                    s2[counter]=(float)temp/(float)4095*(float)3300; // reading value from second adc in mV
                    if (s1[counter]>max1)
                        max1=s1[counter];
                    if (s2[counter]>max2)
                        max2=s2[counter];
                    TIM1->CCR1=rd[counter];
                    counter++;
                 // __HAL_ADC_CLEAR_FLAG(&hadc1,ADC_FLAG_EOC);
        }
 
  if (HAL_ADC_GetError(&hadc1)!=HAL_ADC_ERROR_NONE ||HAL_ADC_GetError(&hadc2)!= HAL_ADC_ERROR_NONE
                                || __HAL_ADC_GET_FLAG(&hadc1,ADC_FLAG_OVR)|| __HAL_ADC_GET_FLAG(&hadc2,ADC_FLAG_OVR))
            {
      HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13); //red LED
            }
  if (counter>=1000){
      HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_1);
      if(HAL_ADCEx_MultiModeStop_DMA(&hadc1)!= HAL_OK || HAL_ADC_Stop_IT(&hadc2)!= HAL_OK){
          HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13); //red LED
          }
      HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_12); // LED
      counter=0;
      max1=0; max2=0;
  }
  if (HAL_ADC_GetError(&hadc1)!=HAL_ADC_ERROR_NONE ||HAL_ADC_GetError(&hadc2)!= HAL_ADC_ERROR_NONE
                                || __HAL_ADC_GET_FLAG(&hadc1,ADC_FLAG_OVR)|| __HAL_ADC_GET_FLAG(&hadc2,ADC_FLAG_OVR))
            {
      HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13); //red LED
            }
  /* USER CODE END ADC_IRQn 1 */
}



Outcomes