How to use stm32duino (official STM32 core) build_opt.h in PlatformIO?

Hello,
I would need to customize adc clock divider and sampling rate parameters and, as written in the official wiki, this should be done creating a “special” file named build_opt.h and putting there something like this:

-DADC_CLOCK_DIV=ADC_CLOCK_DIV_4 
-DADC_SAMPLETIME=ADC_SAMPLETIME_3CYCLES

Info about this is here:
https://github.com/stm32duino/Arduino_Core_STM32/issues/558#issuecomment-508743510

and here, in the official wiki:
https://github.com/stm32duino/wiki/wiki/Customize-build-options-using-build_opt.h

Now, as described in the wiki, when using Arduino IDE, put the file build_opt.h in the sketch folder and you should be done…but…what if one need to do the same thing in PlatformIO?

Any hint/help is very welcome!

The Arduino build system simply reads extra GCC option flags from this file and substitutes them in. See

Meaning this is 100% equivalent to what is already possible in PlatformIO via the build_flags. Your example would translate in

build_flags = 
    -DADC_CLOCK_DIV=ADC_CLOCK_DIV_4 
    -DADC_SAMPLETIME=ADC_SAMPLETIME_3CYCLES

added to the platformio.ini.

If you still do want to have it in a file, then as seen here, you can pass a -include <file> as a GCC flag and it will include it as the first line of every compilation unit. Thus you can exactly replicate the build_opt.h behaviour, alas more indirectly.

1 Like

Thank you @maxgerhardt, I already use build_flags for other definitions, I was not sure it could work also for this case, still, it did work a charm, thank you!

1 Like