PlatformIO

May 23, 2024 · View on GitHub

PlatformIO is an open-source ecosystem for embedded development. It has a built-in library manager and is Arduino-compatible. It supports most operating systems; Windows, MacOS, Linux 32 and 64-bit, ARM, and X86.
And best of all, MegaCore is supported!

MegaCore + PlatformIO

MegaCore and PlatformIO go great together. You can do serial uploads and upload using a dedicated programmer, but you can also let PlatformIO calculate the fuses and load the correct bootloader file, just like Arduino IDE does!

PlatformIO uses the information provided in platformio.ini to calculate what fuse bits and what bootloader file to load.
Provide enough information and run the following commands:

; Only set fuses
pio run -t fuses -e fuses_bootloader
; Set fuses and burn bootloader
pio run -t bootloader -e fuses_bootloader
; (where "fuses_bootloader" can be replaced with a different environment to match your build configuration)

You can find a platformio.ini template you can use when creating a project for a MegaCore-compatible device below.
The most common functionality is available in this template. As you can see, the template is divided into multiple environments.

  • The default build environment is defined under [platformio].
  • All parameters common for all environments are defined under [env].
  • Use the [env:Upload_UART] or [env:Upload_ISP] to upload to your target.
  • Use [env:fuses_bootloader] to set the fuses or burn the bootloader.

More information on what each line means can be found further down on this page.

platformio.ini template

; PlatformIO template configuration file for MegaCore
; https://github.com/MCUdude/MegaCore/
;
;   Build options: build flags, source filter
;   Hardware options: oscillator type, BOD, UART number, EEPROM retain
;   Upload options: custom upload port, speed, and extra flags
;   Library options: dependencies, extra library storage
;   Advanced options: extra scripting
;
; Please visit the documentation for the other options
; https://github.com/MCUdude/MegaCore/blob/master/PlatformIO.md
; https://docs.platformio.org/page/projectconf.html
; https://docs.platformio.org/en/latest/platforms/atmelavr.html


[platformio]
default_envs = Upload_UART ; Default build target


; Common settings for all environments
[env]
platform = atmelavr
framework = arduino

; TARGET SETTINGS
; Chip in use
board = ATmega128
; Clock frequency in [Hz]
board_build.f_cpu = 16000000L

; BUILD OPTIONS
; Comment out to enable LTO (this line unflags it)
build_unflags = -flto
; Extra build flags
build_flags = 

; SERIAL MONITOR OPTIONS
; Serial monitor port defined in the Upload_UART environment
monitor_port = ${env:Upload_UART.upload_port}
; Serial monitor baud rate
monitor_speed = 9600


; Run the following command to upload with this environment
; pio run -e Upload_UART -t upload
[env:Upload_UART]
; Serial bootloader protocol
upload_protocol = urclock
; Serial upload port
upload_port = /dev/cu.usbserial*
; Set upload baudrate. Can be changed on the fly if using Urboot
board_upload.speed = ${env:fuses_bootloader.board_bootloader.speed}


; Run the following command to upload with this environment
; pio run -e Upload_ISP -t upload
[env:Upload_ISP]
; Custom upload procedure
upload_protocol = custom
; Avrdude upload flags
upload_flags =
  -C$PROJECT_PACKAGES_DIR/tool-avrdude/avrdude.conf
  -p$BOARD_MCU
  -PUSB
  -cusbasp
; Avrdude upload command
upload_command = avrdude $UPLOAD_FLAGS -U flash:w:$SOURCE:i


; Run the following command to set fuses
; pio run -e fuses_bootloader -t fuses
; Run the following command to set fuses + burn bootloader
; pio run -e fuses_bootloader -t bootloader
[env:fuses_bootloader]
board_hardware.oscillator = external ; Oscillator type
board_bootloader.type = urboot       ; urboot, optiboot or no_bootloader
board_bootloader.speed = 115200      ; Bootloader baud rate
board_hardware.uart = uart0          ; Set UART to use for serial upload
board_hardware.bod = 2.7v            ; Set brown-out detection
board_hardware.eesave = yes          ; Preserve EEPROM when uploading using programmer
upload_protocol = usbasp             ; Use the USBasp as programmer
upload_flags =                       ; Select USB as upload port and divide the SPI clock by 8
  -PUSB
  -B8

board

PlatformIO requires the board parameter to be present. The table below shows what board name should be used for each target

TargetBoard name
ATmega6490P/PAATmega6490P
ATmega6490/AATmega6490
ATmega6450PATmega6450P
ATmega6450/AATmega6450
ATmega3290P/PAATmega3290P
ATmega3290/AATmega3290
ATmega3250PATmega3250P
ATmega3250/AATmega3250
ATmega2561/VATmega2561
ATmega2560/VATmega2560
ATmega1281/VATmega1281
ATmega1280/VATmega1280
ATmega649P/PAATmega649P
ATmega649/AATmega649
ATmega645PATmega645P
ATmega645/AATmega645
ATmega640/VATmega640
ATmega329P/PAATmega329P
ATmega329/AATmega329
ATmega325PATmega325P
ATmega325/AATmega325
ATmega169P/PAATmega169P
ATmega169/AATmega169A
ATmega165PATmega165P
ATmega165/AATmega165
ATmega128/AATmega128
ATmega64/AATmega64
AT90CAN128AT90CAN128
AT90CAN64AT90CAN64
AT90CAN32AT90CAN32

board_build.f_cpu

Specifies the clock frequency in [Hz]. Used to determine what oscillator option to choose. A capital L has to be added to the end of the frequency number. Below is a table with supported clocks. Defaults to 16 MHz if not specified.

Clock speedOscillatorboard_build.f_cpu
20 MHzExternal20000000L
18.432 MHzExternal18432000L
16 MHzExternal16000000L (default)
14.7456 MHzExternal14745600L
12 MHzExternal12000000L
11.0592 MHzExternal11059200L
9.216 MHzExternal9216000L
8 MHzExternal8000000L
7.3728 MHzExternal7372800L
6 MHzExternal6000000L
4 MHzExternal4000000L
3.6864 MHzExternal3686400L
2 MHzExternal2000000L
1.8432 MHzExternal1843200L
1 MHzExternal1000000L
8 MHzInternal8000000L
1 MHzInternal1000000L

board_hardware.oscillator

Specifies to use the internal or an external oscillator.
Internal oscillator only works with board_build.f_cpu values 8000000L and 1000000L.

Oscillator option
external (default)
internal

board_bootloader.type

Specifies which bootloader type to burn.

Bootloader type
urboot (default)
optiboot (deprecated)
no_bootlaoder

board_hardware.uart

Specifies the hardware UART port used for serial upload. Use no_bootloader if you’re using a dedicated programmer, i.e. not using a bootloader for serial upload.

Upload serial port option
no_bootloader
uart0 (default)
uart1
uart2
uart3

board_hardware.bod

Specifies the hardware brown-out detection. Use disabled to disable.

ATmega640/1280/1281/2560/2561ATmega64/128AT90CAN32/64/128
4.3v4.0v4.1v
2.7v (default)2.7v (default)4.0v
1.8v3.9v
disableddisabled3.8v
2.7v (default)
2.6v
2.5v
disabled

board_hardware.eesave

Specifies if the EEPROM memory should be retained when uploading using a programmer. Use no to disable.

EEPROM retain
yes (default)
no

board_hardware.ckout

Enable clock output on targets that support it.
Pin PE7 on ATmega1280/1281/2560/2561, ATmega165/169/325/329/645/649 and ATmega3250/3290/6450/6490 Pin PC7 on AT90CAN32/64/128

Clock output enable
yes
no (default)

board_hardware.jtagen

Enable the JTAG port.

JTAG enable
yes
no (default)

board_upload.speed / board_bootloader.speed

Specifies the upload baud rate. Valid baud rates are shown in the table below. The Urboot bootloader has auto baud support, so the upload baud rate can be changed without re-flashing the bootloader. The only exception to this rule is UART2 and UART3 on the ATmega640/1280/2560, which does not support auto baud. Optiboot on the other hand is compiled to support a specific baud rate, and a different bootloader binary needs to be flashed to support a different baud rate.

Note that if you're using a programmer that communicates with Avrdude with a serial port (Arduino as ISP, STK500, etc.) the board_upload.speed field will interfere with the programmer's baud rate.
In this case, use board_bootloader.speed to set the bootloader baud rate, and board_upload.speed to set the baud rate for the programmer.

Suggested baud rates for a particular clock speed are in bold text.

10000005000004608002500002304001152005760038400192009600
20000000LXXXX
18432000LXXXXXXX
16000000LXXXXXXX
14745600LXXXXXXX
12000000LXXXXX
11059200LXXXXXXX
9216000LXXXXXX
8000000LXXXXXXXX
7372800LXXXXXXX
6000000LXXXXX
4000000LXXX
3686400LXXXXXXX
2000000LXXX
1843200LXXXXXX
1000000LX

board_build.variant

Holds the current pinout in use. The alternative pinout is only available for ATmega640/1280/2560. See pinout pics for more info.

TargetDefault pinoutAlternative pinout
ATmega2560/V
ATmega1280/V
ATmega640/V
100-pin-arduino-mega100-pin-avr
ATmega2561/V
ATmega1281/V
ATmega128/A
ATmega64/A
AT90CAN128
AT90CAN64
AT90CAN32
64-pin-avr

build_unflags

This parameter is used to unflag. Since LTO is enabled by default in PlatformIO we may disable it by unflagging -flto.

build_flags

This parameter is used to set compiler flags. This is useful if you want to, for instance, charge the serial RX or TX buffer. Here's a list of the currently available core file flags:

FlagDefault sizeDescription
-lprintf_fltLets you print floats with printf (occupies ~1.5 kB)
-Wall -WextraShow all compiler warnings
-DSERIAL_RX_BUFFER_SIZE=12864 bytesSets the serial RX buffer to 128 bytes
-DSERIAL_TX_BUFFER_SIZE=12864 bytesSets the serial TX buffer to 128 bytes
-DTWI_BUFFER_SIZE=6432 bytesSets the TWI (i2c) buffer to 64 bytes
-DWIRE_TIMEOUTEnable timeout for the Wire and Wire1 library

Example: build_flags = -DSERIAL_RX_BUFFER_SIZE=128 -DSERIAL_TX_BUFFER_SIZE=128

upload_port

Holds the serial port used for uploading. PlatformIO automatically detects the serial port. However, if you want to override this you can uncomment upload_port. Use /dev/[port] on Unix-compatible systems, and use COMx on Windows.

board_upload.speed

Upload baud rate. See board_build.f_cpu for more details.

upload_protocol

Used when using a programmer rather than using a USB to serial adapter.
Supports all Avrdude compatible programmers such as usbasp, usbtiny and stk500v1.

upload_flags

Used to pass extra flags to Avrdude when uploading using a programmer.
Typical parameters are -PUSB, -B[clock divider] and -b[baudrate].
Note that every flag has to be on its own line, and they have to be indented with two spaces:

upload_flags =
  -PUSB
  -B32
  -v

monitor_port

PlatformIO detects serial ports automatically. However, if you want to override this you can uncomment monitor_port. Use /dev/[port] on Unix-compatible systems, and use COMx on Windows.

monitor_speed

Sets the serial monitor baud rate. Defaults to 9600 if not defined.