Can preprocessors know the type of ESP32 board?

Hi. How can preprocessors know the type of ESP32 board that is selected? For example: ESP32, ESP32 C3, ESP32 S3, ...

Thank you

A preprocessor can act according to symbols that are defined.

If you define a symbol that identifies the CPU type, then conditional compilations for that CPU can be carried out. Example

#define ESP32

#ifdef ESP32
<code to be compiled>
#endif

Normally I can just select a board I'm using. I'm sure there are already some symbols available to the preprocessor since different boards use different definitions.
I wouldn't like to make additional definitions in my code for the same purpose. I would rather use existing ones.

Yes, and you can look those up in the board definition file.

Do you mean this:
esp32c2.build.tarch=riscv32
esp32c2.build.target=esp
esp32c2.build.mcu=esp32c2
esp32c2.build.core=esp32
esp32c2.build.variant=esp32c2
esp32c2.build.board=ESP32C2_DEV
esp32c2.build.bootloader_addr=0x0

How can I use this in my code?

if you compile with the IDE and look at the verbose console output while compiling, you'll see the compiler will set the equivalent of a #define on the compile lines (-D option) ➜ check it out and you'll know what's used

for example on an "ESP3S3" ➜ you'll see

 -DARDUINO=10819 -DARDUINO_ESP32S3_DEV -DARDUINO_ARCH_ESP32 "-DARDUINO_BOARD=\"ESP32S3_DEV\"" "-DARDUINO_VARIANT=\"esp32s3\""

being part of the compilation command line

so in your code you can do

#ifdef ARDUINO_ESP32S3_DEV
// here you have code specifically for the S3
#endif

For each MCU type you have a different sdk with a different sdkconfig file where this are defined


#if CONFIG_IDF_TARGET_ESP32
.............
#elif CONFIG_IDF_TARGET_ESP32S2
...............
#elif CONFIG_IDF_TARGET_ESP32C2
.................
#elif CONFIG_IDF_TARGET_ESP32C3
.................
#elif CONFIG_IDF_TARGET_ESP32S3
.................
#elif CONFIG_IDF_TARGET_ESP32C6
.................
#elif CONFIG_IDF_TARGET_ESP32H2
.................
#else
#error Target CONFIG_IDF_TARGET is not supported
#endif

This is it! THank you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.