TrueSTUDIO ADC clock problem

Discussions concerning TrueSTUDIO for STM32 9.0.0 and later versions.

Moderators: Markus Girdland, Mattias Norlander

Posts: 18
Joined: Fri Mar 10, 2017 5:32 am

TrueSTUDIO ADC clock problem

Postby diamondjimkoehler » Fri Dec 14, 2018 12:07 am

I'm running Linux Mint 19 which is based on Ubuntu 18.02. Using TrueSTUDIO version 9.1, I have been investigating the ADC process in a "blue pill" which has a STM32F103C6 processor. This board has a 8 MHz crystal as the main clock and the PLL is set to multiply this by 9. The ADC clock was set to be 12 MHz. The A/D process should take about 12.5 clock cycles of the ADC clock and I have set the sampling time to 7.5 cycles thus the process should take about 21 clock cycles corresponding to about 1.5 microS. I have written a brief routine to sample 1000 cycles of channel 0; the source here is a DDS-based triangular waveform generator set to 2350 Hz. In this tight loop, I would expect the time needed to index the pointer and store the data would be a small fraction of a microS. The code is shown below:

Code: Select all

      ptr = 0;
          ADC1->CR2 |= ADC_CR2_ADON;   // turn on the ADC_SR
          while (ptr < total_sample_number)
             ADC1->CR2 |= ADC_CR2_ADON;   // set the ADON bit in the ADC_SR
             while ((ADC1->SR & ADC_SR_JEOC) != 0);  // wait till the EOC bit is set in the ADC_CR2 register
             data[ptr++] = (uint16_t) ADC1->DR;

The clock setup coder, as generated by STM32CubeMX is shown below:

Code: Select all

 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /**Initializes the CPU, AHB and APB busses clocks
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  /**Initializes the CPU, AHB and APB busses clocks
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;

If I plot the data array using gnuplot, I get:
As you can see, there is about one cycle of the 2350 Hz data making up the 1000 samples so, if you work out the time between samples (1/(2350 * 1000)), you get a total ADC conversion time of about 0.43 microS This is far too fast and implies an ADC clock of about 48 MHz. The 'noise' on the signal shows that the A/D converter is not completing the conversion before starting again.
If I use STM32CubeMX to generate code to set the ADC clock to 3 MHz, I get the following data set (again plotted with Gnuplot):
Here the conversion time between samples is about 1.5 microS and the A/D conversion looks much cleaner implying that the conversions are completing properly. But, with a 3 MHz ADC clock, it should take about 6 microS for a conversion, not 1.5 microS.
So, in summary, it seems to me that TrueSTUDIO is not setting the ADC clock correctly.

Posts: 3
Joined: Tue Jan 01, 2019 7:49 am

Re: TrueSTUDIO ADC clock problem

Postby Mechi » Wed Jan 02, 2019 2:31 pm

I wonder if this is similar to my problem that I just posted - the clocks are much faster than they are supposed to be (in code converted from IAR).
I hope there's a solution to this...

Return to “TrueSTUDIO for STM32 discussions”

Who is online

Users browsing this forum: No registered users and 1 guest