PlatformIO
Home Up HD44780 I2C Address I2C Connector Crystals Logic Translate DC Supply Compatibility Example PlatformIO Load Cell Arduino Arduino 2

 

Visual Studio Code and PlatformIO

Visual Studio Code is an open-source IDE. It appears a lot like the Microsoft Visual Studio IDE. I do not know if they have code in common or just a name and appearance.

PlatformIO describes itself as "Your Gateway to Embedded Software Development Excellence" and appears to be a suite of command line tools to support embedded projects. PlatformIO is available as a plugin for the Visual Studio IDE, and the combination forms a quite attractive alternative to the Arduino IDE for embedded projects on AVR processors and some ARM platforms.

Installation:

The PlatformIO homepage explains the process https://platformio.org/install/ide?install=vscode

Install VS Code

Find the Plugin manager

Find and install the PlatformIO extension

After that PlatformIO is supposed to take care of automatically downloading the resources needed to build a project.

The process isn't completely automatic, the "platformio.ini" file serves as a project file, but it does need to be edited manually to add certain project options.

This is an example of a platformio.ini file for a Digispark project:

; 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

[env:digispark-tiny]
platform = atmelavr
board = digispark-tiny
framework = arduino

The file can have multiple [env:<platform>] entries for different boards or different builds for the same board. The PlatformIO "Create Project" function makes a minimal platformio.ini file.

This is a file for a attiny85 standalone project. Unlike the Digispark this is just the microcontroller, and a programmer will be required.

; 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

[env:attiny85]
platform = atmelavr
board = attiny85
framework = arduino
; change microcontroller
board_build.mcu = attiny85

; change MCU frequency
board_build.f_cpu = 8000000L
upload_protocol = custom
upload_port = COM3
upload_speed = 19200
upload_flags =
-C
; use "tool-avrdude-megaavr" for the atmelmegaavr platform
${platformio.packages_dir}/tool-avrdude/avrdude.conf
-p
$BOARD_MCU
-P
$UPLOAD_PORT
-b
$UPLOAD_SPEED
-c
stk500v1
upload_command = avrdude $UPLOAD_FLAGS -U flash:w:$SOURCE:i

Caution: at this time I am not clear if this will work or not.

Turns out that as I'm using an Arduino-based programmer the programmer type needs to be "arduino". Who knew?

Also f_cpu is the frequency used by the Arduino framework for calculating delays and does not set the actual CPU speed, which appears to default to 1MHz. There's some evidence that the Arduino IDE is setting a clock of 8MHz as a program outputting an "analogue" PWM signal is using 60Hz in VSCode and 500Hz in Arduino.

Also it has the programmer COM port value embedded in it, and this will quite likely change. I am using source control and if the port setting is stored with the source then it will have to be changed to move between computers. The simplest solution is probably to comment out the setting and set it using an environment variable instead.

I believe it is possible to specify the programmer's port in a Windows environment variable

 https://docs.platformio.org/en/latest/envvars.html#envvar-PLATFORMIO_UPLOAD_PORT

but I am not clear on the details.

Converting an INO file to a CPP file

VScode appears to open and edit Arduino "ino" files but it complains as some IDE features expect CPP syntax.

The main changes you will need to make are:

  • Rename the file
  • Add "#include <arduino.h>" at the top.
  • For each function you may need to add a "forward declaration" or "prototype" at the top underneath the include.

A declaration looks like the first line of the function up to just before the first curly bracket and ends in a semicolon.

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

These match the datasheet defaults and would be expected to give an 8MHz clock frequency.

Datasheet section: 6.2.7 Default Clock Source states that the oscillator clock of 8 MHz with longest start-up time and an initial system clock prescaling of 8, resulting in 1.0 MHz system clock.