Easy way to change project processor?

Hello all, I hope you guys are having great days.

I am reaching out because I currently have a project with many different files and was wondering if there was a way to change the target processor without having to create a new project from scratch. Something seems odd with the idea of having to create a new project and copying over every file. But I am new to PlatformIO so it may very well be the solution.

More details: it is an Arduino DUE project being changed into an Arduino Mega2560 R3

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

https://docs.platformio.org/en/latest/projectconf/index.html

So you can just have to environments in your project, one for the Due, one for ATMega2560, as in

[env:megaatmega2560]
platform = atmelavr
board = megaatmega2560
framework = arduino

[env:due]
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_SAM and 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.

4 Likes

I really really really appreciate it. The name ‘maxgerhardt’ will forever be ingrained in my memory as the PlatformIO/Arduino master.

3 Likes

Yes, @maxgerhardt is the finding for the PlatformIO project and its Community. I always thank God for having Maximilian on this forum :pray:

Happy to hear that other people also value his professional support :rocket:

4 Likes