PlatformIO and Marlin – and how to have separate config files, for different 3Dprinter while sharing the same code.

I am using Marlin 2.0 to control my 3D printers, Marlin 2.0 can still be compiled and installed from the Arduino-IDE,although I use the supplied Makefile. To take advantage of other processors you have to use PlatformIO. In this article we will compile Marlin for my 3D-printer, just as we did in Marlin 2.0 using Makefile (and reclaiming io-pins on atmega1284p based mainboard), My 3D printers:

  • MKS GEN v1.3 using atmega2560 as used on Tevo Tarantula
  • Melzi 2.0 using atmega1284p as used on Tronxy X1
  • SKR v1.1 using lpc1768 on home made Prusa i3 Clone

PlatformIO already have a separate directory tree for object files and binaries.

We will have a subdir per board which contains the configuration files, and the platformio.ini, The platformio.ini is comparable to a Makefile, just different syntactic suger.

To do this I want a way to have per printer Configuration.h and Configuration_adv.h files. while sharing the rest. A very simple way to achieve that is to just add -Iprinter_conf_dir fot the C-preprocessor and have it be first in the include search path – but how do we do that in the PlatformIO world?

There is an option build_flags most options will make it into C*FLAGS, but you have no control of the order so no luck here.

With the option extra_scripts you can write your own code to change environment variables and how the system works. To figure out what we can do we create a simple platformio.ini file

src_dir = ../Marlin-git/Marlin
boards_dir = ../Marlin-git/buildroot/share/PlatformIO/boards

platform = atmelavr
framework = arduino
board = sanguino_atmega1284p
src_filter =  +<src/*> -<src/config> -<src/HAL> +<src/HAL/shared>

extra_scripts = is a small script which looks like this


When executing “pio run -e show_env” we will see something like this

Processing show_env (platform: atmelavr; framework: arduino; board: sanguino_atmega1284p)
Verbose mode can be enabled via `-v, --verbose` option
{ 'AR': 'ar',
  'ARCOM': '$AR $ARFLAGS $TARGET ${_long_sources_hook(__env__, SOURCES)}',
  'ARCOMSTR': 'Archiving $TARGET',
  'ARFLAGS': ['rc'],
  'AS': 'as',
  'ASCOMSTR': 'Compiling $TARGET',
  'ASFLAGS': [],
             '${_long_incflags_hook(__env__, _CPPINCFLAGS)} -c -o $TARGET '
  'ASPPCOMSTR': 'Compiling $TARGET',
  'BOARD': 'sanguino_atmega1284p',
  'BOARD_F_CPU': '16000000L',
  'BOARD_MCU': 'atmega1284p',
  'BUILDERS': { 'Library': ,
                'LoadableModule': ,
                'Object': ,
                'Program': ,
                'SharedLibrary': ,
                'SharedObject': ,
                'StaticLibrary': ,
                'StaticObject': },
  'BUILD_SCRIPT': '/t470/peter/.platformio/platforms/atmelavr/builder/',
  'CC': 'cc',
  'CCCOMSTR': 'Compiling $TARGET',
  'CCFLAGS': [],
  'CFILESUFFIX': '.c',
  'CFLAGS': [],
  'COMPILATIONDB_PATH': '$BUILD_DIR/compile_commands.json',
  'CONFIGUREDIR': '#/.sconf_temp',
  'CONFIGURELOG': '#/config.log',
  'CPPSUFFIXES': [ '.c',
  'CXX': 'c++',
  'CXXCOMSTR': 'Compiling $TARGET',
  'CXXFLAGS': [],
  'DSUFFIXES': ['.d'],
  'Dir': ,
  'Dirs': ,
  'ENV': ...
  'ESCAPE': ,
  'File': ,
  'HOST_ARCH': None,
  'HOST_OS': None,
  'IDLSUFFIXES': ['.idl', '.IDL'],
  'INCPREFIX': '-I',
  'INCSUFFIX': '',
                 '$__RPATH $SOURCES $_LIBDIRFLAGS $_LIBFLAGS',
  'LIBPREFIX': 'lib',
  'LIBSOURCE_DIRS': [ '/net/y/y/3D/Marlin-2.0.x-git/Tronxy_X1/test/lib',
  'LIBSUFFIX': '.a',
             '${_long_sources_hook(__env__, SOURCES)} $_LIBDIRFLAGS $_LIBFLAGS',
  'LINKCOMSTR': 'Linking $TARGET',
  'LINKFLAGS': [],
  'MAXLINELENGTH': 128072,
  'OBJPREFIX': '',
  'OBJSUFFIX': '.o',
  'PIOENV': 'show_env',
  'PIOFRAMEWORK': ['arduino'],
  'PIOPLATFORM': 'atmelavr',
  'PLATFORM': 'posix',
  'PLATFORM_MANIFEST': '/t470/peter/.platformio/platforms/atmelavr/platform.json',
  'PROGNAME': 'program',
  'PROJECTDATA_DIR': '/net/y/y/3D/Marlin-2.0.x-git/Tronxy_X1/test/data',
  'PROJECTSRC_DIR': '/net/y/y/3D/Marlin-2.0.x-git/Marlin',
  'PROJECT_BUILD_DIR': '/net/y/y/3D/Marlin-2.0.x-git/Tronxy_X1/test/.pio/build',
  'PROJECT_CONFIG': '/net/y/y/3D/Marlin-2.0.x-git/Tronxy_X1/test/platformio.ini',
  'PROJECT_CORE_DIR': '/t470/peter/.platformio',
  'PROJECT_DATA_DIR': '/net/y/y/3D/Marlin-2.0.x-git/Tronxy_X1/test/data',
  'PROJECT_DIR': '/net/y/y/3D/Marlin-2.0.x-git/Tronxy_X1/test',
  'PROJECT_INCLUDE_DIR': '/net/y/y/3D/Marlin-2.0.x-git/Tronxy_X1/test/include',
  'PROJECT_LIBDEPS_DIR': '/net/y/y/3D/Marlin-2.0.x-git/Tronxy_X1/test/.pio/libdeps',
  'PROJECT_PACKAGES_DIR': '/t470/peter/.platformio/packages',
  'PROJECT_SRC_DIR': '/net/y/y/3D/Marlin-2.0.x-git/Marlin',
  'PROJECT_TEST_DIR': '/net/y/y/3D/Marlin-2.0.x-git/Tronxy_X1/test/test',
  'PROJECT_WORKSPACE_DIR': '/net/y/y/3D/Marlin-2.0.x-git/Tronxy_X1/test/.pio',
  'PSPAWN': ,
  'PYTHONEXE': '/home/peter/.platformio/penv/bin/python',
  'RANLIB': 'ranlib',
  'RDirs': ,
  'SCANNERS': [],
  'SHCC': '$CC',
  'SHCXX': '$CXX',
  'SHELL': 'sh',
  'SHLIBSUFFIX': '.so',
  'SHLINK': '$LINK',
  'SHLINKFLAGS': ['$LINKFLAGS', '-shared'],
  'SHOBJSUFFIX': '.os',
  'SPAWN': ,
  'SRC_FILTER': [ '+ - - + '
  'TARGET_ARCH': None,
  'TARGET_OS': None,
  'TOOLS': [ 'ar',
  'UNIX_TIME': 1587916285,
  'UPLOAD_PROTOCOL': 'arduino',
  'UPLOAD_SPEED': 115200,
  '_CCCOMCOM': '$CPPFLAGS $_CPPDEFFLAGS ${_long_incflags_hook(__env__, '
  '_CPPINCFLAGS': '$( ${_concat(INCPREFIX, CPPPATH, INCSUFFIX, __env__, RDirs, '
                  'TARGET, SOURCE)} $)',
                  'RDirs, TARGET, SOURCE)} $)',
               'LIBSUFFIXES, __env__)}',
  '__DRPATH': '$_DRPATH',
  '__RPATH': '$_RPATH',
  '__SHLIBVERSIONFLAGS': '${__libversionflags(__env__,"SHLIBVERSION","_SHLIBVERSIONFLAGS")}',
  '__libversionflags': ,
  '_concat': ,
  '_defines': ,
  '_long_incflags_hook': ,
  '_long_sources_hook': ,
  '_stripixes': ,
  'toolpath': [ '/t470/peter/.platformio/penv/lib/python3.6/site-packages/platformio/builder/tools']}
PLATFORM: Atmel AVR 2.0.0 > Sanguino ATmega1284p (16MHz)
HARDWARE: ATMEGA1284P 16MHz, 16KB RAM, 127KB Flash
 - framework-arduino-avr 5.0.0 
 - toolchain-atmelavr 1.50400.190710 (5.4.0)
Converting Marlin.ino
LDF: Library Dependency Finder ->
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 5 compatible libraries
Scanning dependencies...
Dependency Graph
|--  1.0
|--  1.0
Building in release mode

Lots of variables, focusing on CPP variables we note that ‘_CPPINCFLAGS’ most likely have the include-file-search-path, if we could only prepend -Imy_conf_dir we would be all set. Another variable PIOENV hold the name of the current environment, we can use that to our advantage, and have per environment config-files.

The script below called lets the compiler look in a directory with the environment name first, or if it is not present to look in the dir where platformio.ini was located. So if we put our include files there, they will be found first – mission accompliced

import os
if os.path.isdir(env['PIOENV']):
    incdir = env['PIOENV']
    incdir = "."

env['_CPPINCFLAGS'] = "-I%s $( ${_concat(INCPREFIX, CPPPATH, INCSUFFIX, __env__, RDirs, TARGET, SOURCE)} $)" % (incdir)

With this we can create a small setup where it is convenient to have the firmware for all my 3D-printers based on the same source-code-tree. Below is how I have organized my directory layout.

 Makefile               # I like Makefile, it is like my README file
 Marlin-git/            # git clone -b 2.0.x Marlin-git
 platformio.ini         # to compile the default setup for the printers below.
 build/                 # Files generated by PlatformIO
 lib/                   # libraries put here will override those below
 libdeps/               # libraries downloaded by PlatformIO automagically

The content of the tronxy_x1/platformio.ini

# Marlin-2 via PlatformIO for Tronxy X1
# tronxy_x1: using NewLiquidCrystal over I2C via Wire
# tronxy_x1_tiny: using LCD_I2C_Tiny via SoftI2CMaster

top          = ..
hal_dir      = ${}/Marlin-git/Marlin/src/HAL
script_dir   = ${}/Marlin-git/buildroot/share/PlatformIO/scripts

libdeps_dir  = ${}/libdeps
build_dir    = ${}/build
lib_dir      = ${}/lib
src_dir      = ${}/Marlin-git/Marlin
boards_dir   = ${}/Marlin-git/buildroot/share/PlatformIO/boards
default_envs = tronxy_x1, tronxy_x1_tiny

default_src_filter = + - - + +
extra_scripts = pre:${my.script_dir}/
build_flags = -fmax-errors=5 -g -D__MARLIN_FIRMWARE__ -fmerge-all-constants -DUSE_MAKE
lib_deps      = LiquidCrystal

platform      = atmelavr
framework     = arduino
board         = sanguino_atmega1284p
build_flags   = ${common.build_flags}
lib_deps      = ${common.lib_deps}
monitor_speed = 250000
upload_speed  = 115200
src_filter    = ${common.default_src_filter}
extra_scripts = pre:../

extra_scripts = pre:../

build_flags   = ${common.build_flags}
  -DCUSTOM_MACHINE_NAME="\"Tronxy X1 24Apr2020\""
lib_deps      = ${common.lib_deps}

# with platformio saves text=246 data=8 bss=1
# Arduino the saving were text=642 data=18 bss=182
build_flags   = ${common.build_flags}
  -DCUSTOM_MACHINE_NAME="\"Tronxy x1 24Apr2020\""
lib_deps      =
lib_ignore    = Wire

The special thing here is that I can be working on a version using LCD_Tiny to save some flash which is the bottleneck on this CPU

The top platformio.ini have all that is needed to compile firmware for all my 3D-printers (envs), the file is below:

# Marlin Firmware for my 3D printers
# PlatformIO Configuration File
# For detailed documentation with EXAMPLES:

top          = .
hal_dir      = ${}/Marlin-git/Marlin/src/HAL
script_dir   = ${}/Marlin-git/buildroot/share/PlatformIO/scripts

libdeps_dir  = ${}/libdeps
build_dir    = ${}/build
lib_dir      = ${}/lib
src_dir      = ${}/Marlin-git/Marlin
boards_dir   = ${}/Marlin-git/buildroot/share/PlatformIO/boards
default_envs = tronxy_x1, tarantula

default_src_filter = +<src/*> -<src/config> -<src/HAL> +<src/HAL/shared>
extra_scripts = pre:${my.script_dir}/
build_flags = -fmax-errors=5 -g -D__MARLIN_FIRMWARE__ -fmerge-all-constants
lib_deps = LiquidCrystal

framework     = arduino
build_flags   = ${common.build_flags}
lib_deps      = ${common.lib_deps}
monitor_speed = 250000
upload_speed  = 115200
extra_scripts =

extra_scripts =

platform      = atmelavr
board         = megaatmega2560
src_filter    = ${common.default_src_filter} +<src/HAL/AVR>
build_flags   = ${common.build_flags}
  -DCUSTOM_MACHINE_NAME="\"Tarantula 26Apr2020\""

platform      = atmelavr
board         = sanguino_atmega1284p
src_filter    = ${common.default_src_filter} +<src/HAL/AVR>
build_flags   = ${common.build_flags}
  -DCUSTOM_MACHINE_NAME="\"Tronxy X1 24Apr2020\""

# NXP LPC176x ARM Cortex-M3
platform          =
board             = nxp_lpc1768
build_flags       = -DU8G_HAL_LINKS -I${my.hal_dir}/LPC1768/include -I${my.hal_dir}/LPC1768/u8g ${common.build_flags}
# debug options for backtrace
#  -funwind-tables
#  -mpoke-function-name
lib_ldf_mode      = off
lib_compat_mode   = strict
extra_scripts     = ${my.hal_dir}/LPC1768/
src_filter        = ${common.default_src_filter} +<src/HAL/LPC1768>
lib_deps          = LiquidCrystal

I haven’t made up my mind if I like PlatformIO yet.

My first reaction was: why bother, we have Makefiles, and OpenWRT and BSD-ports have showed us how to get/compile full systems long before PlatformIO was invented.

On the other hand it only took me a day or two to figure out a solution to my problem, and it only required a few lines of code, so it seems PlatformIO is highly flexible.

The real power with PlatformIO (and OpenWrt Makefiles) is that somebody already did most of the work and integrated everything for you, so you can create one of their precooked meals without much effort. I seem to always to end up with a desire to use it a little differently.

The good news is: if PlatformIO do not do work the way you think is should work – you can fix it, without too much effort !

If you like me prefer the commandline over the Arduino-IDE or other click-systems, you will be better of adopting the PlatformIO to compile Marlin, than try to get the supplied Makefile to do what you want.

Stay tuned, there might be more coming,

Print Friendly, PDF & Email
This entry was posted in 3D printer, Arduino. Bookmark the permalink.