You can have one project with all your source files. The
platformio.ini file can then have multiple environments (
[env:xyz]) in which you tell PlatformIO for what target platform, board, framework etc to build the source files too.
All of this is documented in
So you can just have to environments in your project, one for the Due, one for ATMega2560, as in
platform = atmelavr
board = megaatmega2560
framework = arduino
platform = atmelsam
board = due
framework = arduino
Note that as soon as you have multiple environments in your project, you have to use the project environment switcher to switch between the environments you want to work in – you can only have one active at a time. You also have the “Build all” (default) and the build / project tasks for each environment available there.
So I can have a single project with a single
src/main.cpp and build it for both a Due and a ATMega2560 without having to create a new project.
Note that you get the “platform” and “board” values by looking into the documentation or seraching for your board in the registry (example).
Also note that even though you now have the same code files for both target processors, you can still write the code in a way that one code is active for one environment and another piece of code is active for a different environment. The C-Preprocessor’s
#ifdef checks for example come in handy here coupled with the fact that different macros are active for different processors / boards / architectures.
In this simplest possible project, you can distinguish the Atmel SAM based Due from the Atmel AVR based Mega2560 by using the
ARDUINO_ARCH_AVR macros respectively.
You can see how VSCode visually “greys out” the unactivated pieces of code when I have the Due environment active.
You can use this technique to e.g. include different libraries for different platforms, like
EEPROM.h for AVR-based devices and
DueFlashStorage.h for the Due, by placing the
#include’s to these libraries in an
#ifdef protected block that only activates for the needed platform.
This enables you to have a single project that is truly cross-platform, supporting being built for multiple target boards of whatever architectures and boards you chose to support.