How to build AVR code for the Digispark with PlatformIO
PlatformIO supports the Digispark USB development board, a compact board with the ATtiny85 AVR microcontroller. The canonical example code that lets the built-in LED blink looks like this:
digispark_blink_platformio/main.c (Source)
/* Atml AVR native blink example for the Digispark * * Copyright (C) 2021 Koen Vervloesem (koen@vervloesem.eu) * * SPDX-License-Identifier: MIT */ #include <avr/io.h> #include <util/delay.h> // Digispark built-in LED // Note: on some models the LED is connected to PB0 #define PIN_LED PB1 #define DELAY_MS 1000 int main(void) { // Initalize LED pin as output DDRB |= (1 << PIN_LED); while (1) { PORTB ^= (1 << PIN_LED); _delay_ms(DELAY_MS); } return 0; }
If you've bought your Digispark recently, the Micronucleus bootloader is a recent version that isn't supported by PlatformIO's older micronucleus
command.
If you've already upgraded the bootloader on your Digispark, you also have the newest version of the micronucleus
command. So the only thing you need is make PlatformIO use this version when uploading your code. You can do this with the following platformio.ini
:
[env:digispark-tiny] platform = atmelavr board = digispark-tiny upload_protocol = custom upload_command = micronucleus --run .pio/build/digispark-tiny/firmware.hex
The platform
and board
options are just the configuration options the PlatformIO documentation lists for the Digispark USB. By setting the upload_protocol
to custom
, you can supply your own upload command, and the micronucleus
in this command refers to the one you've installed globally with sudo make install
in /usr/local/bin/micronucleus
. [1]
After this, you can just build the code and upload it:
koan@tux:~/digispark_blink_platformio$ pio run -t upload Processing digispark-tiny (platform: atmelavr; board: digispark-tiny) --------------------------------------------------------------------------------------------------------- Verbose mode can be enabled via `-v, --verbose` option CONFIGURATION: https://docs.platformio.org/page/boards/atmelavr/digispark-tiny.html PLATFORM: Atmel AVR (3.1.0) > Digispark USB HARDWARE: ATTINY85 16MHz, 512B RAM, 5.87KB Flash DEBUG: Current (simavr) On-board (simavr) PACKAGES: - tool-avrdude 1.60300.200527 (6.3.0) - toolchain-atmelavr 1.50400.190710 (5.4.0) LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf LDF Modes: Finder ~ chain, Compatibility ~ soft Found 0 compatible libraries Scanning dependencies... No dependencies Building in release mode Checking size .pio/build/digispark-tiny/firmware.elf Advanced Memory Usage is available via "PlatformIO Home > Project Inspect" RAM: [ ] 0.0% (used 0 bytes from 512 bytes) Flash: [ ] 1.4% (used 82 bytes from 6012 bytes) Configuring upload protocol... AVAILABLE: custom CURRENT: upload_protocol = custom Uploading .pio/build/digispark-tiny/firmware.hex > Please plug in the device ... > Device is found! connecting: 33% complete > Device has firmware version 2.5 > Device signature: 0x1e930b > Available space for user applications: 6650 bytes > Suggested sleep time between sending pages: 7ms > Whole page count: 104 page size: 64 > Erase function sleep duration: 728ms parsing: 50% complete > Erasing the memory ... erasing: 66% complete > Starting to upload ... writing: 83% complete > Starting the user app ... running: 100% complete >> Micronucleus done. Thank you! ====================================== [SUCCESS] Took 3.99 seconds ======================================
I've created a GitHub project with this configuration, the example code, a Makefile and a GitHub Action to automatically check and build the code: koenvervloesem/digispark_blink_platformio. This can be used as a template for your own AVR code for the Digispark with PlatformIO.