Robotdyn STM32F303CCT6 ADC conversion time

Post here first, or if you can't find a relevant section!
Post Reply
leogomide07
Posts: 5
Joined: Thu Jan 30, 2020 12:07 pm

Robotdyn STM32F303CCT6 ADC conversion time

Post by leogomide07 »

Im getting too higher ADC conversion time on the STM32F303CCT6 Black Pill Board, around 200-220 us

It can be some clock configurations?
There is any other function other than AnalogRead() that have a better conversion time?
There is some configuration of number of cycles for this reading?

Just for comparison, with STM32F103C8T6 i get about 65-68us of conversion time using the same code:

Code: Select all


float temp1 , temp2, temp3 = 0;
void setup()
{
  pinMode(PA0, INPUT);
  analogReadResolution(12);
  SerialUSB.begin();
 }
 
 void loop()
{

  temp1 = micros();

  temp3 = analogRead(PA0);

  temp2 = micros();

  SerialUSB.print(static_cast<double>(temp2 - temp1), 4);
  SerialUSB.println("us");

  delay(500);
}
This is my Platformio.ini:

Code: Select all

[env:robotdyn_blackpill_f303cc]
platform = ststm32
board = robotdyn_blackpill_f303cc
framework = arduino
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Robotdyn STM32F303CCT6 ADC conversion time

Post by fpiSTM »

ADC_SAMPLINGTIME and ADC_CLOCK_DIV could also be redefined by the variant or using build_opt.h.

See https://github.com/stm32duino/wiki/wiki ... l-channels
leogomide07
Posts: 5
Joined: Thu Jan 30, 2020 12:07 pm

Re: Robotdyn STM32F303CCT6 ADC conversion time

Post by leogomide07 »

fpiSTM wrote: Thu Jan 30, 2020 5:36 pm ADC_SAMPLINGTIME and ADC_CLOCK_DIV could also be redefined by the variant or using build_opt.h.

See https://github.com/stm32duino/wiki/wiki ... l-channels
as shown in the link, i added #include "stm32f3xx_ll_adc.h" and tried this two ways, but none of them worked:

Code: Select all

#if defined(STM32F3xx)
#define ADC_SAMPLINGTIME  LL_ADC_SAMPLINGTIME_19CYCLES_5
#define ADC_CLOCK_DIV LL_ADC_CLOCK_SYNC_PCLK_DIV2
#endif

Code: Select all

//added this line on setup()
LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_0, LL_ADC_SAMPLINGTIME_19CYCLES_5);
how can i change the sampling time and clock division?
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Robotdyn STM32F303CCT6 ADC conversion time

Post by fpiSTM »

You go on the wrong way.
What I mean is ADC_SAMPLINGTIME can be redefined at sketch level. Do not use HAL or LL function as analogWrite do the full configuration.

Here how it is defined by default:
https://github.com/stm32duino/Arduino_C ... pp#L57-L71

And here the possible value for F3 series:
https://github.com/stm32duino/Arduino_C ... 1377-L1384

So, in our case the default value is :
ADC_SAMPLETIME_13CYCLES_5
To redefine int you can create a build_opt.h file and define the desired value:

Code: Select all

-DADC_SAMPLINGTIME=ADC_SAMPLETIME_1CYCLE_5
Or add hal_conf_extra.h with:

Code: Select all

#define ADC_SAMPLINGTIME ADC_SAMPLETIME_1CYCLE_5
This is the same for

Code: Select all

ADC_CLOCK_DIV
Anyway, analogRead() do a full configuration each time it is called. The fact you get a lesser time on a STM32F103 is not really relevant as it is not the same series nor the same HAL ADC.
leogomide07
Posts: 5
Joined: Thu Jan 30, 2020 12:07 pm

Re: Robotdyn STM32F303CCT6 ADC conversion time

Post by leogomide07 »

fpiSTM wrote: Fri Jan 31, 2020 7:13 am
To redefine int you can create a build_opt.h file and define the desired value:

Code: Select all

-DADC_SAMPLINGTIME=ADC_SAMPLETIME_1CYCLE_5
I tried to creat a file called build_opt.h with the line you suggested and added #include "build_opt.h", but i get the following error:
In file included from src\main.cpp:3:0:
src\build_opt.h:1:1: error: expected unqualified-id before '-' token
-DADC_SAMPLINGTIME=ADC_SAMPLETIME_1CYCLE_5
^
*** [.pio\build\robotdyn_blackpill_f303cc\src\main.cpp.o] Error 1
Then i tried to include this lines in Platformio.ini:

Code: Select all

build_flags = 
    -D ADC_SAMPLINGTIME=ADC_SAMPLETIME_1CYCLE_5
    -D ADC_CLOCK_DIV=ADC_CLOCK_SYNC_PCLK_DIV1
The sketch compiled but nothing changed

For last i created the file hal_conf_extra.h with this lines and added #include "hal_conf_extra.h":

Code: Select all

#define ADC_SAMPLINGTIME ADC_SAMPLETIME_1CYCLE_5
#define ADC_CLOCK_DIV ADC_CLOCK_SYNC_PCLK_DIV4
The sketch compiles too, i tried to play with the clock divider and sampling time, but nothing changed again :(

I want to have 12 bit resolution at the adc, so i have analogReadResolution(12) at the setup(), this can mess with the other configurations?

Do you recommend any sampling and clock configuration for the best 12bit readings with the lower time conversion?

Sorry to ask so many questions, im new to platformio
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Robotdyn STM32F303CCT6 ADC conversion time

Post by fpiSTM »

I didn't support PIO as I do not use it.
leogomide07 wrote: Fri Jan 31, 2020 12:09 pm I tried to creat a file called build_opt.h with the line you suggested and added #include "build_opt.h", but i get the following error:
I never told to include it, just create it, it is a dedicated file for the GCC option... See the Wiki.
Same for the hal_conf_extra.h , this is managed dynamically.

As It told analogRead is not for high performance ADC. You could crosscheck the clock settings maybe the ADC clock can be increased.

One other way, you can use the HAL or LL and even use the DMA but you will have to do this on your own.
I've provided an example how to use HAL on the code snipplets:
viewtopic.php?f=41&t=110
Post Reply

Return to “General discussion”