1
votes

I have been using STM32 IDE which incorporates the CUBE MX.

Using the HAL code I am able to read on three pins using a separate ADC for each pin. I have started all the ADC's at the same time and then I poll for completion. I am I correct in thinking these ADC reads should be practically simultaneous (i.e. they all read in at a very similar time)?

Code fragment below. Using a NUCLEO-STM32 F446RE btw.

  MX_ADC1_Init();
  MX_ADC2_Init();
  MX_ADC3_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
      static int flip,sysclk=0,old_sysclk=0,adc1,adc2,adc3;//adc3_0,adc3_1,adc3_2, adc_pstat0,
      int adc_pstat1, adc_pstat2, adc_pstat3;

      flip ^= 1;

          HAL_GPIO_WritePin(GPIOC,GPIO_PIN_0/*|GPIO_PIN_2|GPIO_PIN_6*/, flip);


           HAL_ADC_Start(&hadc3);
           HAL_ADC_Start(&hadc2);
           HAL_ADC_Start(&hadc1);
           adc_pstat1 = HAL_ADC_PollForConversion(&hadc1, 10);
          adc_pstat3 = HAL_ADC_PollForConversion(&hadc3, 10); // should already be done!
          adc_pstat2 = HAL_ADC_PollForConversion(&hadc2, 10); // should already be done!
          adc3 = HAL_ADC_GetValue(&hadc3);
          adc2 = HAL_ADC_GetValue(&hadc2);
          adc1 = HAL_ADC_GetValue(&hadc1);

                  if (adc_pstat2 ||adc_pstat3)
                     asm("\t nop");
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
          sysclk = HAL_GetTick();

          if ( (sysclk - 1000) > old_sysclk ){
              //printf("nucleo F446 0x%X adc3_0 0x%X adc3_1 0x%X adc3_2 0x%X\n",sysclk,adc3_0,adc3_1,adc3_2);
              printf("|->nucleo F446 sysclk=0x%X adc1=0x%X adc2=0x%X adc3=0x%X\n",sysclk,adc1,adc2,adc3);
              old_sysclk = sysclk;
          }
      }
      /* USER CODE END 3 */
    }
2
Yes, the ADC are sampled at a very similar time relative to human perception. No, they are not literally simultaneous. Consider what is nearly simultaneous enough for your application requirements. You could probably get closer to simultaneous by using a trigger signal to trigger the ADCs together. Or you could get more deterministic delta-sample-time by using one ADC to sample all three channels in quick succession.kkrambo
The HAL is not very efficient in terms of computing time (because it takes into account all possible use cases). So there will probably be several dozens of CPU cycles between the calls to HAL_ADC_Start. So if your CPU doesn't run at maximum frequency, say a few MHz to save power, the delay might very well be several ms, which is not negligeable at all in some applications.Guillaume Petitjean
@kkrambo please add your comment as an answerTarick Welling
There is now a "tripple" simultaneousmode in CubeMX.user50619
@Lundin I am afraid you are 100% wrong. This micro has 3 separate ADCs. Every ADC has many channels (up to 20 including internal). They can work autonomous or ADC can be a master one synchronizing another two. You can have 3 readings at the same time or triple the 2.4MSPS sampling rate to 7.2MSPS (or even more reducing the resolution).0___________

2 Answers

1
votes

Your code does not read it simultaneously as you do not start the ADCs at the sime moment.

You need to use the same external trigger for all of them or use ADCs in the double or tripple mode.

0
votes

CubeMX ADC setupI found a way to do this easily. I set up a timer and used to to update a trigger event. The ADC's then triggered off this event (TIM8_TRGO). Using this trigger I can get two or three ADCs to read simultaneously. The Dual Simultaneous mode seems to support ADCs reading at the same time