Compile and Upload Arduino Sketch with PlatformIO in Arch Linux running on PC, Raspberry Pi 2 and 3

Posted by:

Preamble

PlatformIO is a python tool to build and upload sketches for multiple Hardware Platforms, at the moment of writing these are Arduino/AVR based boards. This is the only tool working with Raspberry Pi running Arch Linux so far, and I have been searching for some tool like this for a long time.

NB: The over-bearing minion is there because every time my little one sees it, she giggles.

platformio-logo.png

Installation

Install virtualenv first, detailed instruction here.

sudo pacman -S python2-virtualenv

Its recommended to use a virtual environment because platformio uses python2 because its based on SCons which does not support python3.

$ virtualenv2 --python=python2 venv #for python2
$ source ./venv/bin/activate
$ pip install platformio

You may need to install PlatformIO with python installer script for it to work.

sudo python2 -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio/master/scripts/get-platformio.py)"

Usage

  1. Ativate virtualenv

    source ~/venv/bin/activate
    
  2. Install platforms

    platformio platforms install atmelavr
    
  3. Start with a empty directory

    platformio init --board=uno --board=megaatmega2560
    #platformio boards
    
  4. Search for the board types using `platformio boards` command if needed.

    platformio boards |grep mega2560
    
  5. Modify platformio.ini file if needed.

    platform = atmelavr
    framework = arduino
    board = uno
    targets = upload
    
    [env:ArduinoMega2560]
    platform = atmelavr
    framework = arduino
    board = megaatmega2560   
    upload_port = /dev/ttyACM0 
    targets = upload
    
  6. Put your source code *.pde or *.ino files to src directory.
  7. Link rest of library files in lib directory, link the whole directory instead of individual .cpp and .h files.

    cd ./lib;ln -s ~/Arduino/libraries/LGDP4535/
    ln -s ~/Arduino/libraries/zTouchScreen/
    
  8. Change working directory to the project’s root where is located Project Configuration File (platformio.ini) and use these commands:

    # process/build project from the current directory
    platformio run
    # if you don't have enabled auto-uploading, then you can upload firmware manually
    platformio run --target upload
    # to clean cached/compiled files
    platformio run --target clean
    

    Output from platformio run:

    tan@taurus ~/proj/arduino/coordinate$ source ~/venv/bin/activate 
    (venv)tan@taurus ~/proj/arduino/coordinate$ platformio run 
    [Thu Oct 15 11:22:19 2015] Processing megaatmega2560 (platform: atmelavr, board: megaatmega2560, framework: arduino) 
    -------------------------------------------------------------------------------- 
    avr-g++ -o .pioenvs/megaatmega2560/src/tmp_ino_to.o -c -fno-exceptions -fno-threadsafe-statics -g -Os -Wall -ffunction-sections -fdata-sections -MMD -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO_AVR_MEGA2560 -DARDUINO=10605 -DPLATFORMIO=020302 -I.pioenvs/megaatmega2560/FrameworkArduino -I.pioenvs/megaatmega2560/FrameworkArduinoVariant -I.pioenvs/megaatmega2560/Adafruit_GFX -I.pioenvs/megaatmega2560/LGDP4535 -I.pioenvs/megaatmega2560/zTouchScreen src/tmp_ino_to.cpp 
    avr-g++ -o .pioenvs/megaatmega2560/firmware.elf -Os -mmcu=atmega2560 -Wl,--gc-sections,--relax .pioenvs/megaatmega2560/src/tmp_ino_to.o -L.pioenvs/megaatmega2560 -Wl,--start-group -lm .pioenvs/megaatmega2560/libFrameworkArduinoVariant.a .pioenvs/megaatmega2560/libFrameworkArduino.a .pioenvs/megaatmega2560/libAdafruit_GFX.a .pioenvs/megaatmega2560/libLGDP4535.a .pioenvs/megaatmega2560/libzTouchScreen.a -Wl,--end-group 
    avr-objcopy -O ihex -R .eeprom .pioenvs/megaatmega2560/firmware.elf .pioenvs/megaatmega2560/firmware.hex 
    "avr-size" --mcu=atmega2560 -C -d .pioenvs/megaatmega2560/firmware.elf 
    AVR Memory Usage 
    ---------------- 
    Device: atmega2560 
    
    Program:   15002 bytes (5.7% Full) 
    (.text + .data + .bootloader) 
    
    Data:        340 bytes (4.2% Full) 
    (.data + .bss + .noinit) 
    
    ========================= [SUCCESS] Took 1.50 seconds =========================
    

    Output for upload:

    (venv)tan@taurus ~/proj/arduino/coordinate$ platformio run --target upload
    [Thu Oct 15 11:22:26 2015] Processing megaatmega2560 (platform: atmelavr, board: megaatmega2560, framework: arduino)
    --------------------------------------------------------------------------------
    BeforeUpload(["upload"], [".pioenvs/megaatmega2560/firmware.hex"])
    Auto-detected UPLOAD_PORT/DISK: /dev/ttyACM0
    "/home/tan/.platformio/packages/tool-avrdude/avrdude" -v -p atmega2560 -C "/home/tan/.platformio/packages/tool-avrdude/avrdude.conf" -c wiring -b 115200 -D -P /dev/ttyACM0 -U flash:w:.pioenvs/megaatmega2560/firmware.hex:i
    
    avrdude: Version 6.1, compiled on Mar 24 2014 at 14:07:14
    Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
    Copyright (c) 2007-2014 Joerg Wunsch
    
    System wide configuration file is "/home/tan/.platformio/packages/tool-avrdude/avrdude.conf"
    User configuration file is "/home/tan/.avrduderc"
    User configuration file does not exist or is not a regular file, skipping
    
    Using Port                    : /dev/ttyACM0
    Using Programmer              : wiring
    Overriding Baud Rate          : 115200
    AVR Part                      : ATmega2560
    Chip Erase delay              : 9000 us
    PAGEL                         : PD7
    BS2                           : PA0
    RESET disposition             : dedicated
    RETRY pulse                   : SCK
    serial program mode           : yes
    parallel program mode         : yes
    Timeout                       : 200
    StabDelay                     : 100
    CmdexeDelay                   : 25
    SyncLoops                     : 32
    ByteDelay                     : 0
    PollIndex                     : 3
    PollValue                     : 0x53
    Memory Detail                 :
    
    Block Poll               Page                       Polled
    Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
    ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
    eeprom        65    10     8    0 no       4096    8      0  9000  9000 0x00 0x00
    flash         65    10   256    0 yes    262144  256   1024  4500  4500 0x00 0x00
    lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
    hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
    efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
    lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
    calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
    signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00
    
    Programmer Type : Wiring
    Description     : Wiring
    Programmer Model: AVRISP
    Hardware Version: 15
    Firmware Version Master : 2.10
    Vtarget         : 0.0 V
    SCK period      : 0.1 us
    
    avrdude: AVR device initialized and ready to accept instructions
    
    Reading | ################################################## | 100% 0.01s
    
    avrdude: Device signature = 0x1e9801
    avrdude: safemode: lfuse reads as FF
    avrdude: safemode: hfuse reads as D0
    avrdude: safemode: efuse reads as FF
    avrdude: reading input file ".pioenvs/megaatmega2560/firmware.hex"
    avrdude: writing flash (15002 bytes):
    
    Writing | ################################################## | 100% 2.42s
    
    avrdude: 15002 bytes of flash written
    avrdude: verifying flash memory against .pioenvs/megaatmega2560/firmware.hex:
    avrdude: load data flash data from input file .pioenvs/megaatmega2560/firmware.hex:
    avrdude: input file .pioenvs/megaatmega2560/firmware.hex contains 15002 bytes
    avrdude: reading on-chip flash data:
    
    Reading | ################################################## | 100% 1.93s
    
    avrdude: verifying ...
    avrdude: 15002 bytes of flash verified
    
    avrdude: safemode: lfuse reads as FF
    avrdude: safemode: hfuse reads as D0
    avrdude: safemode: efuse reads as FF
    avrdude: safemode: Fuses OK (E:FF, H:D0, L:FF)
    
    avrdude done.  Thank you.
    
    ========================= [SUCCESS] Took 6.12 seconds =========================
    
  9. Project Structure after processing

    ./.pioenvs/
    ├── ArduinoMega2560
    │   ├── FrameworkArduino
    │   ├── FrameworkArduinoVariant
    │   └── src
    ├── ArduinoUno
    │   ├── FrameworkArduino
    │   ├── FrameworkArduinoVariant
    │   └── src
    └── megaatmega2560
        ├── FrameworkArduino
        ├── FrameworkArduinoVariant
        └── src
    
  10. Deactivate virtualenv

    deactivate
    

Work with ArduinoJson

ArduinoJson works can be installed from Arduino IDE library manager. Issue tracker here.

The easy way

platformio lib --global install "ArduinoJson"

The hard way

  1. Install using the library without Arduino with instruction adapted from official wiki.
  2. Step 1: Download source code into platform project lib folder:

    git clone https://github.com/bblanchon/ArduinoJson.git
    
  3. Generate the Makefile for your environment

    cd ArduinoJson
    cmake .
    
  4. Step 3: Build

    make
    
  5. delete test folder, alternatively install with zip package.
  6. update library

    platformio lib update
    
  7. all set for platformio compile.

Notes

  1. If you are running Archlinux and running into this error: libncurses.so.5: cannot open shared object file, refer here.

    rm ~/.platformio/packages/tool-avrdude/avrdude
    ln -s /usr/bin/avrdude ~/.platformio/packages/tool-avrdude/avrdude
    
  2. For the following error: error while loading shared libraries: libreadline.so.6: cannot open shared object file: No such file or directory It seems libreadline.so.7 is backward compatible enough for most utilities to keep working after running:

    sudo ln -s /usr/lib/libreadline.so.7 /usr/lib/libreadline.so.6
    
  3. Works with Raspberry Pi running Arch Linux.
  4. Does not work without virtualenv, Scons error – got to do with the python3 issue.
  5. Does not work with Arduino IDE running.
  6. I often ran into this problem: the serial port will be disabled after a while, i.e. – disappeared from {php}ls /dev/{/php} and Arduino IDE port listing. My working solution is to unplug and then plug it into another USB port.
0
  Related Posts

Add a Comment