Can I check the board/platform from the code?

Yes, every board implicitly has one or more macros that are given in the build process. When e.g. compiling for your board = nucleo_l432kc environment, you will see with the project task “Advanced → Verbose Build” that it has the compile flags

arm-none-eabi-gcc -o .pio\build\nucleo_l432kc\FrameworkArduino\pins_arduino.c.o -c -std=gnu11 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -Os -mcpu=cortex-m4 -mthumb -ffunction-sections -fdata-sections -Wall -nostdlib --param max-inline-insns-single=500 -DPLATFORMIO=50200 -DSTM32L432xx -DSTM32L4xx -DARDUINO=10808 -DARDUINO_ARCH_STM32 -DARDUINO_NUCLEO_L432KC -DBOARD_NAME="NUCLEO_L432KC" -DHAL_UART_MODULE_ENABLED […]

So you could use any of those for identification in the code.

#ifdef ARDUINO_NUCLEO_L432KC
/* board = nucleo_l432kc was selected .. */
#endif

#ifdef ARDUINO_ARCH_STM32 
/* general STM32-type board / arduino core selected */
#endif
//...

same for the LGT8F328P chip

avr-g++ -o .pio\build\LGT8F328P-SSOP20\src\main.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -Os -Wall -ffunction-sections -fdata-sections -flto -mmcu=atmega328p -DPLATFORMIO=50200 -DARDUINO_ARCH_AVR
-DARDUINO_AVR_LARDU_328P -DAVR_LARDU_328E -DF_CPU=32000000L -DCLOCK_SOURCE=1 -DARDUINO_ARCH_AVR -DARDUINO_ARCH_LGT -DARDUINO=10812 -Iinclude -Isrc -IC:\Users\Max.platformio\packages\framework-lgt8fx\cores\lgt8f -IC:\Users\Max.platformio\packages\framework-lgt8fx\variants\lgt8fx8ds20 src\main.cpp

So you can use ARDUINO_ARCH_AVR, ARDUINO_ARCH_LGT, ARDUINO_AVR_LARDU_328P etc.

1 Like