Make megaTinyCore work for PlatformIO

The megaTinyCore work great when using with Arduino IDE, but not so with PlatformIO. This is my quest on making the megaTinyCore work for PlatformIO.

megaTinyCore

The megaTinyCore created by Spencer Konde is an Arduino Core platform for tinyAVR 0, 1 and 2 Series MCU (collectively I called them Modern AVR) that were released by Microchip between 2017 - 2021. The megaTinyCore allows user to use those Modern AVR chips like an Arduino under the Arduino IDE.

Ever since I discovered ATtiny3217 in 2020, I had graduately phase-out all my ATmrga328-based boards and building my own development board based ATtiny Series 1 and 2 chips for my projects and never look back. As those chips are smaller (both in the physical packaging size and the density of the the silicon chip), faster and consumed less power and cheaper than the classic AVR chip, and sometime has more flash and RAM memory that the ATmega328 used in the Arduino Uno. Along the same time, I graduately shifted my Arduino development to PlatformIO with VSCode instead of using the Arduino IDE.

A hack to make it work with PlatformIO

Mr. Konde develops the megaTinyCore with the assumptions of using it under the Arduino IDE, Mr. Konde admitted that he has never use PlatformIO and doesn't seems to have any interest or time to use it in foreseeable future. Although there is a page about PlatformIO for megaTinyCore, and it suggests a configuration of platformio.ini as:

[env:ATtiny1614]
platform = atmelmegaavr
board = ATtiny1614
framework = arduino
upload_protocol = custom
upload_command = C:\Users\(your username)\AppData\Local\Arduino15\packages\megaTinyCore\tools\python3\3.7.2-post1/python3 -u C:\Users\(your username)AppData\Local\Arduino15\packages\megaTinyCore\hardware\megaavr\2.4.2/tools/prog.py -t uart -u COM18 -b 460800 -d attiny1614 --fuses 2:0x01 6:0x04 8:0x00 -f$SOURCE -a write

This configuration is based on some user’s hack trying to make the megaTinyCore work under PlatformIO, this hack has several issues:

  • it requires to have the Arduino IDE installed on your computer even you don’t use it;
  • The path for the directory is based on a particular version of the megaTinyCore installed on your computer(in this case, v2.4.2), if in near future (which for sure will happen), your megaTinyCore version changed to a newer version, it will stop working and you will have to change the platformio.ini.
  • It also tie to a particular version of python 3 bundled by megaTinyCore, even if you already has python3 installed on your computer, although this can be solved easily.

To solve the issues, we need to understand a little bit of what is the prog.py and what does it do. In short, the prog.py is a Serial UPDI programmer implementation tightly integrated in the megaTinyCore since release of 2.2.0.

The concept of Serial UPDI is simple in hardware that requires only a resistor (and optionally a diode), plus a readily available USB-UART adaptor, the TX/RX pins are wired together during the programming mode for a rapid duplex communication. Microchip developed a python software called pymcuprog that implemented Serial UPDI among other Microchip/Atmel programming protocols. Instead of using Microchip’s implementation, Mr Konde is not impressed by the speed and performance of the pumcuprog and decided to come out his own version of the programmer prog.py (It does use part of pymcuprog implementation in the backend). The prog.py allows user to program the modern AVR chips at baud rate up to 921600bps (it actually only works for CH-340-based USB-UART adaptor).

The prog.py is tightly integrated with the megaTinyCore and works well under Arduino IDE, and it is not packaged as a standard alone python application, nor as a git submodule that can be installed separately, which explain why it needs Arduino IDE to be installed. In order to run prog.py, instead of making the prog.py executable, and allow user to pick the python distribution that a user might already installed on its operating system, megaTineyCore also bundled the python package in the megaTinyCore.

A better solution

One solution that I came out take a few steps to set it up. It still looks a lit like a hack but it remove the dependency of the bundled python as well as the requirement of having Arduino IDE installed. First, some pre-requistion, since we are not going to rely on the installation of Arduino IDE, so it will need the computer to install Python3 and set it up so that it is able to be accessable globally. It probably need to have pySerial installed or whatever python packages you were prompt when running prog.py.

Step 1

Create a PlatformIO project as usal for megaTinyCore.

Step 2

Download megaTinyCore from megaTinyCore github, unzip the download repositotry if necessary, and go into megatinycore-master/megaavr/ folder of the downloaded repository, drag the tools folder into your PlatformIO project folder, you can delete the rest of the downloaded repository.

Step 3

Goto your PlatformIO project folder, run the following CLI command to make prog.py executable.

sudo chmod +x tools/prog.py

Step 4

Configure the platformIO to use the megaTinyCore prog.py tool. This is an example of platformio.ini for ATtiny824 running at 20MHz clock.

[env:ATtiny824]
board = ATtiny824

platform = atmelmegaavr
framework = arduino
board_build.f_cpu = 20000000L
monitor_speed = 115200
monitor_rts = 0

upload_protocol = custom
upload_port = /dev/cu.usbserial* ; for MacOS
upload_speed = 230400

; for using MegaTinyCore SerialUPDI
upload_flags =
  --fuses
  ;2:0x01  ;if board_build.f_cpu=16000000L
  2:0x02  ;if board_build.f_cpu=20000000L
  -d
  $BOARD_MCU
  -b
  $UPLOAD_SPEED
  -u
  $UPLOAD_PORT
upload_command = ./tools/prog.py $UPLOAD_FLAGS -f $SOURCE -a write

This is a better solution than the hack suggested on megaTinyCore github. There is no longer need to have Arduino IDE installed, there is no more dependency on the python bundled by megaTinyCore, and there is no longer tie to a specific installed version of megaTinyCore. It is however still need a littble bit of work to download the repository and set the file to be executable each time when you create a PlatformIO project.

An Even Better Solution

The pymcuprog developed by Microchip actually work well with megaTinyCore, it just that Mr. Konde think it is slow and not tune for the performance. However in my opinion, it is a better to used pymcuprog, with all things considered, for several reasons. First, it is developed and supported by Microchip as we all know that "standing on the shoulders of a giant" ensure the scalebility and reliability in long run. It makes it easier to upgrade both the pymcuprog and megaTinyCore independently in future, and more importantly it make the creating of a PlatformIO project easier too.

The pymcuprog can be download and install easily via pymcuprog · PyPI. Once it is installed, whenever you want to create a PlatformIO project, simply create a PlatformIO project and configure the platformio.ini and that all! Here is an example of platformio.ini configuration for ATtiny824 running at 20MHz clock.

[env:ATtiny824]
board = ATtiny824

platform = atmelmegaavr
framework = arduino
board_build.f_cpu = 20000000L
monitor_speed = 115200
monitor_rts = 0

upload_protocol = custom
upload_port = /dev/cu.usbserial* ; for MacOS
upload_speed = 230400

; for using pymcuprog
upload_flags =
    --tool
    uart
    --device
    $BOARD
    --uart
    $UPLOAD_PORT
    --clk
    $UPLOAD_SPEED
upload_command = pymcuprog write --erase $UPLOAD_FLAGS --filename $SOURCE

The recommend upload speed is 115200 but in most of the case, at least for me, using a Silicon Labs CP2102N-based Serial Adaptor at 230400bps work just fine.

This configuration make it very easy to setup, but it is not perfect, for example, you can specify the settting such as BOD voltage, or external clock source, but for some megaTinyCore specific configurations, it simply not available with pymcuprog and if you really need to configure those parameters, you'll have to use the previous solution.

Conclusion

Microchip pymcuprog is the easier way to make megaTinyCore work for PlatformIO than using the internal SerialUPDI implementation of megaTinyCore prog.py. No doubt it is slower than the build-in SerialUPDI implementation, but it make much easier to create PlatformIO project in my opinion is more important than obsess about programming and upload speed.

One comment by reader

  1. Thank you, was finally able to get platform.io to upload via UPDI thanks to you!

    Now just need to figure out how to get latest megatinycore on Platform.io, it’s 2.6.7, while latest is 2.6.10.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.