Skip to content

Instantly share code, notes, and snippets.

@weygoldt
Last active April 4, 2023 12:34
Show Gist options
  • Select an option

  • Save weygoldt/9b1bb4d459500441a4739b1894a4076d to your computer and use it in GitHub Desktop.

Select an option

Save weygoldt/9b1bb4d459500441a4739b1894a4076d to your computer and use it in GitHub Desktop.
Everything I need to interface with MCUs on the command line.

PlatformIO - Lazy hardware development on the command line

From the pio website:

PlatformIO is a cross-platform, cross-architecture, multiple framework, professional tool for embedded systems engineers and for software developers who write applications for embedded products.

It is mainly advertised to be a VSCode plugin, that turns VSCode into an ArduinoIDE like thing. But that is too mainstream for us. PlatformIO is based on a simple command line toolbox, so lets use that instead. Everything I mention here are just notes I took while reading the official doc. Refer to that for more info.

Installation

Its just a python package, so you can install it from PyPI, e.g. into a virtual environment. But the pio docs highly recommend to use their own install scrip instead. So lets do that.

curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core-installer/master/get-platformio.py -o get-platformio.py
python3 get-platformio.py

During installation, pio creates its own virtual environment, which is quite handy, since this means that it will not interfere with system packages. The drawback to this virtualization is that our standard shell does not know where the executables are. To fix this, we need to create symbolic links to them. To do that, run this:

ln -s ~/.platformio/penv/bin/platformio ~/.local/bin/platformio
ln -s ~/.platformio/penv/bin/pio ~/.local/bin/pio
ln -s ~/.platformio/penv/bin/piodebuggdb ~/.local/bin/piodebuggdb

On Linux system, we also need to add udev rules. The following snippet downloads them, places them into the right location, reloads the udev service and adds the current user to appropriate groups.

curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/system/99-platformio-udev.rules | sudo tee /etc/udev/rules.d/99-platformio-udev.rules

## restart udev
# i think this is the debian way
# sudo service udev restart
# on arch based systems:
sudo udevadm control --reload-rules
sudo udevadm trigger

## add user to some groups
# on debian base 
# sudo usermod -a -G dialout $USER
# sudo usermod -a -G plugdev $USER
# on arch based systems 
sudo usermod -a -G uucp $USER
sudo usermod -a -G lock $USER

Now log out and back in for the changes to take effect.

PlatformIO enables telemetry by default so lets disable that.

pio settings set enable_telemetry No 
pio settings get 

Now lets initialize pio in our directory. Create an example project directory, e.g. mkdir pio_test && cd pio_test. Now, list all the available boards (platforms) by running pio boards. Choose your board and run

pio project init --ide vim --board teensy41

This is pretty much all you need. You can now edit the source code using your editor of choice. To test if the code compiles, you can simply run

pio run 

To flash an attached board, add the following:

pio run --target upload

For convenience, I use to shell aliases comp for compilation and flash to upload. If you let your device access the serial port to print some debug information, you can monitor this by running

pio device monitor

If this throws an error and you want to check which devices are available, use

pio device list

This is basically all I need for everyday use. Have fun!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment