Skip to content

Instantly share code, notes, and snippets.

@twolfson
Last active October 13, 2020 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twolfson/29158e13e3ed999aba1b1c4d0e05c1d7 to your computer and use it in GitHub Desktop.
Save twolfson/29158e13e3ed999aba1b1c4d0e05c1d7 to your computer and use it in GitHub Desktop.
Playground gist for exploring PlatformIO
.pio/
include/
lib/
src/
test/

gist-platformio-explore

Playground gist for exploring PlatformIO, now that we're equipped with more electronics, microcontroller, and C++ knowledge

Notes

We're going to be using PlatformIO Core to see how robust it is vs Arduino

I feel like it's more since we'll have controls for items like fuses at programming but not 100% certain

https://platformio.org/install/cli

We've used PlatformIO Core before in:

https://github.com/twolfson/arduino-playground

but it's been a loooong while


We're going to be using virtualenvwrapper to keep everything contained Although I believe Pipenv is the new hotness which does this as well, without maintaining requirements.txt by hand

# Create our virtual env
mkvirtualenv gist-platformio-explore
# `workon gist-platformio-explore` in future runs

# Install our dependencies
pip install platformio

# Init our project
#   DEV: Verified name via `platformio boards attiny`
#   https://docs.platformio.org/en/latest/core/quickstart.html
platformio project init --board attiny85

aaand damn, this requires folders...

Well... Let's work around that with symlinks I guess

ln -s ../main.cpp src/main.cpp

Aaaand build now

platformio run

Nice that it show how much RAM and Flash are used =D

Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   1.8% (used 9 bytes from 512 bytes)
Flash: [=         ]   8.3% (used 676 bytes from 8192 bytes)

Uploading via:

platformio run --target upload

Looks like it's running avrdude, neato =)

Uploading .pio/build/attiny85/firmware.hex

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e930b (probably t85)
avrdude: erasing chip
avrdude: reading input file ".pio/build/attiny85/firmware.hex"
avrdude: writing flash (676 bytes):

Writing | ################################################## | 100% 1.09s

avrdude: 676 bytes of flash written
avrdude: verifying flash memory against .pio/build/attiny85/firmware.hex:
avrdude: load data flash data from input file .pio/build/attiny85/firmware.hex:
avrdude: input file .pio/build/attiny85/firmware.hex contains 676 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 1.02s

avrdude: verifying ...
avrdude: 676 bytes of flash verified

avrdude: safemode: Fuses OK (E:FF, H:DF, L:62)

avrdude done.  Thank you.

Looks like it even has fuses listed out =D

Some boards seem to support external debug, https://docs.platformio.org/en/latest/frameworks/arduino.html#external-debug-tools

We can debug via simavr apparently? https://docs.platformio.org/en/latest/boards/atmelavr/attiny85.html DONE: Explore simavr debugging Ahh, seems to be local emulator. Not amazing but decent, https://github.com/buserror/simavr

DONE: Resume reading on debugging, https://docs.platformio.org/en/latest/plus/debugging.html Really digging these comprehensive docs so far =D

Build command for arduino_uno:

platformio run --target upload --environment arduino_uno

DONE: Explore debugging options for ATtiny85, since built-in serial monitor doesn't work (likely due to pins). Prob should find out "why" more first Yep, need a serial adapter in our programmer as well as a UART serial (for easier sending of packets, not really required -- as per SoftwareSerial.h, https://github.com/quitmeyer/ATtinyBareDemos/blob/0ec44ca7192d9a40a2277fae36fba533626ec149/ATTinyAnalogInputTOSerial/ATTinyAnalogInputTOSerial.ino) https://arduino.stackexchange.com/a/24932 So prob easiest to figure out some I2C screen solution then =/ Buying extra hardware to just send back to computer seems silly https://www.instructables.com/Easy-ATTiny-Serial-Communication-with-Tiny-AVR-Pro/ This one just passes through RX and TX to ATtiny which is pretty smart, I guess if they're both running on 5V USB then we're good DONE: Explore downclocking Arduino UNO to have same timing as ATtiny85 DONE: Figure out what the 8x fuse they're talking about is and how to program it

; https://forum.arduino.cc/index.php?topic=154843.msg1160871#msg1160871
board_build.f_cpu = 8000000L ; 8MHz

Currently messing with fuses, seeing that we can't set them via board_fuses as we expected Exploring why... Finding upload command is build + this avrdude invocation (via run --target upload --verbose)

avrdude -v -p attiny85 -C /home/todd/.platformio/packages/tool-avrdude/avrdude.conf -c usbtiny -e -D -U flash:w:.pio/build/attiny85/firmware.hex:i

https://www.ladyada.net/learn/avr/avrdude.html It looks like we set fuses via -U We tried to set -u via upload_flags just now to see if that would force it, no luck. Still no -U for fuses being sent, I guess we can write it by hand. I'm just kind of scared ._. Found the fix =D We use platformio run -t fuses instead of -t upload https://community.platformio.org/t/how-to-set-fuses-file/9403/8 Our LED did bug out when we set the fuse the first time but it works I guess =D The fuse is CKDIV8 fwiw, documented on p149 of ATtiny85 datasheet

Resuming on debugging, https://docs.platformio.org/en/latest/plus/debugging.html

  • There are IDE debuggers in VS Code, as well as Sublime Text and Atom but VS Code is preferred
  • Looks like there's also a debug CLI as well as lots of platforms

Linked Arduino tutorial: Use external device to connect to SWCLK, SWD, GND, RESET, and V_CC then debug in their IDE https://medium.com/@manuel.bl/arduino-in-circuit-debugging-with-platformio-9f699da57ddc

Most other tutorials listed are for wifi/bluetooth driven chips so easier setup/debugging

https://docs.platformio.org/en/latest/plus/debugging.html#boards List of boards says Uno is on board

https://docs.platformio.org/en/latest/boards/atmelavr/uno.html#debugging Only listing is simavr but maybe I'm misreading it? https://community.platformio.org/t/debugger-for-arduino-uno-mega-and-esp8266/5307/7 Nope, as of Oct 2019 (1 year ago), there's no debugging interface without using something external

Yep, set up the IDE. Default tool is simavr, nothing we can change to that I see (all lists are external tools) Extra sanity: Lights are still blinking on our board despite breakpoints

Running pio debug --interface gdb kind of works but doesn't start up program like VS Code Running pio debug --interface gdb -x .pioinit as noted does get us to the same point but visually there's a lot less feedback (e.g. we don't see the program as it's running)

Watching a video on gdb, https://www.youtube.com/watch?v=bWH-nL7v5F4 layout next is something we were missing That was a generally solid Getting "TUI mode not allowed" now =/ Should prob see if this is specific to simavr/PlatformIO or for all our C/C++ programs Huh, using it with a C++ program allows for layout next So maybe something is misconfigured with samavr or it's part of the whole PlatformIO set up Regardless, we can always use the IDE to debug I guess, and it's prob a lot faster than gdb setup over and over ... Yep it's pretty slick. Delay is a little laggy but that's prob simavr itself

Testing we've covered before Can do change up target test filtering with .ini setting which is handy, so do both embedded as well as CI testing https://www.thingforward.io/techblog/2017-08-08-embedded-testing-with-platformio-part-2.html Also works as CLI flag

Linters mentions for static code analysis, nice =D https://docs.platformio.org/en/latest/plus/pio-check.html

Skimming rest of content... remote dev, account, cloud/IDE, CI (run builds and tests as noted before)

// Taken from https://docs.platformio.org/en/latest/core/quickstart.html
// Load in our dependencies
#include "Arduino.h"
// Define our constants
#define BLINK_DELAY 300 // ms
// Define our platform specific constants
// https://github.com/arduino/ArduinoCore-avr/blob/1.8.3/cores/arduino/Arduino.h#L64-L68
#if defined(__AVR_ATtiny85__)
#ifndef LED_BUILTIN
#define LED_BUILTIN 3
#endif
#endif
#ifndef LED_BUILTIN
#error "Missing LED_BUILTIN"
#endif
void setup()
{
// initialize LED digital pin as an output.
pinMode(LED_BUILTIN, OUTPUT);
// Create a serial monitor
Serial.begin(9600);
}
void loop()
{
// turn the LED on (HIGH is the voltage level)
Serial.println("On");
digitalWrite(LED_BUILTIN, HIGH);
// wait for a second
delay(BLINK_DELAY);
// turn the LED off by making the voltage LOW
Serial.println("Off");
digitalWrite(LED_BUILTIN, LOW);
// wait for a second
delay(BLINK_DELAY);
}
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[platformio]
default_envs = attiny85
[env:attiny85]
platform = atmelavr
board = attiny85
framework = arduino
; Default fuses
; avrdude: safemode: Fuses OK (E:FF, H:DF, L:62)
; https://docs.platformio.org/en/latest/platforms/atmelavr.html#fuses-programming
; To update, run: platformio run -t fuses
; Default
; board_fuses.efuse = 0b11111111
; board_fuses.hfuse = 0b11011111
; board_fuses.lfuse = 0b01100010
board_fuses.efuse = 0b11111111
board_fuses.hfuse = 0b11011111
board_fuses.lfuse = 0b11100010 ; CKDIV8 disabled (now delay is same as an out of the box Arduino)
; Set same timing across boards (not quite perfectly in sync, likely RC issues but close enough)
board_build.f_cpu = 8000000L ; 8MHz
[env:arduino_uno]
platform = atmelavr
board = uno
framework = arduino
; Set same timing across boards (not quite perfectly in sync, likely RC issues but close enough)
board_build.f_cpu = 8000000L ; 8MHz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment