Skip to content

Instantly share code, notes, and snippets.

@ahsanu123
Forked from weshouman/Makefile
Created March 3, 2024 10:39
Show Gist options
  • Save ahsanu123/fdc85f1e909f9a7c2c196344347b57df to your computer and use it in GitHub Desktop.
Save ahsanu123/fdc85f1e909f9a7c2c196344347b57df to your computer and use it in GitHub Desktop.
arduino cli tips

Getting Started

Install

Use make install, root access will be needed to install into /usr/local/bin

Bonus

Add the following snippet into the .bashrc qq_arduino-cli() { arduino-cli $@ --config-file "${ARDCONFIG}"; } then resource .bashrc
source ~/.bashrc

Dump config

Set XDG_CONFIG_HOME=/home/${USER}/.config if not set
Run export ARDCONFIG=${XDG_CONFIG_HOME}/arduino-cli/config.yaml
Then qq_arduino-cli config init

Use config

Run qq_arduino-cli config dump -v

ESP32 Support

Use specific indices

Add the following 3rd party url to the configuration file:

  additional_urls:
    - https://dl.espressif.com/dl/package_esp32_index.json

Run qq_arduino-cli core update-index
Then qq_arduino-cli board list

Fetch and install the Core

To list all the supported cores run qq_arduino-cli core listall, an empty string shall be returned for a fresh install.
Which means we need to find the ESP32 core and install it.

  • Search for it using qq_arduino-cli core search esp
  • Install the found one using qq_arduino-cli core install esp32:esp32

Fetch the FQBN

Each board has Fully Qualified Board Name which, in normal conditions, shall be found by using qq_arduino-cli board list, however in my situation I always had unknown.
Thus I had to:

  • Check the installed boards using qq_arduino-cli board listall
  • In my situation I needed to use ESP32 Dev Module Board, thus the FQBN would be esp32:esp32:esp32

Compile and Upload

  • Compile using qq_arduino-cli compile --fqbn esp32:esp32:esp32 SKETCH_NAME
  • If the default was ok one should have been able to upload directly using sudo arduino-cli upload -p /dev/ttyUSB0 --fqbn esp32:esp32:esp32 SKETCH_NAME --config-file ${ARDCONFIG}, however this was not the case for me after closing Arduino GUI uploading worked :D
  • one needs to could also reverse engineer the property keys and values for the board then set them correctly according to the naming convention found in this issue.
    • For example: this should change the baud rate --fqbn esp32:esp32:esp32:FlashMode=qio,UploadSpeed=115200, interestingly, if used the --verbose option we would notice that the FlashMode got overwritten to dio :| Anyway, that won't make the upload work.

Notes:

  • An upload requires compiling first
  • Uploading requires sudo, thus the qq_arduino-cli shall be redefined for the root, thus I decided to just use the configfile
  • Uploading requires that the device is not connected to the Arduino GUI, otherwise the upload will be interrupted at any point check the following observations
Observation 1

If one used the esptool.py command generated from the arduino-cli while the Arduino GUI is connected to the board (may be even if just running), an interruption shall occur at any random time, also we could see in the serial monitor that a button event would spawn

Observation 2

If one used the esptool.py command generated from the Arduino GUI while the Arduino GUI is connected to the board, the following error would show up serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected or multiple access on port?)

Fetch Board Properties

To capture the board properties run

arduino-cli compile --fqbn FQBN --show-properties
# Examples
arduino-cli compile --fqbn esp32:esp32:esp32 --show-properties
arduino-cli compile --fqbn "esp32:esp32:esp32":DebugLevel=debug --show-properties

Sketch Data Upload

This issue disccusses sketch data upload automation.
That involves the following steps

  • Setting up an spiffs
  • Capturing the correct board properties
  • Compiling and uploading the spiffs to the board This guide explains those steps in details
Setting Up an SPIFFS

Run

git clone https://github.com/igrr/mkspiffs.git
cd ./mkspiffs
git submodule update --init
# customize the SPIFFS configuration
vim build_all_configs.sh
# and may add the CPPFLAG -DSPIFFS_OBJ_NAME_LEN=64 to allow for 64-char file name length instead of 32 
# Now build the spiffs into new folders using
./build_all_configs.sh
# Now try the generated mkspiffs
cd ./mkspiffs-0.2.3-7-gf248296-arduino-esp32-linux64
./mkspiffs --version
Capturing the Correct Board Properties

We could either manually capture the block size, page size, spiffs start and spiffs end by checking the ESP8266: boards.txt {This is NOT the case with ESP32: boards.txt} or we could use the attached file spiffs_board_info.mk which was captured from this comment

A major difference between capturing the information of ESP8266 and ESP32, is that, the desired info is not available in ESP32's boards.txt.
Block size and page size are standardized to 4096 and 256 respectively, based on the docs.
Spiffs start and spiffs end (to get spiffs size) are available in the partition table used to flash the board, which is located in a csv file as mentioned, for example, here by assuming we are using the default partition table, which is located in my case at /root/.arduino15/packages/esp32/hardware/esp32/1.0.6/tools/partitions/default.csv

# Name,   Type, SubType, Offset,  Size, Flags
nvs,      data, nvs,     0x9000,  0x5000,
otadata,  data, ota,     0xe000,  0x2000,
app0,     app,  ota_0,   0x10000, 0x140000,
app1,     app,  ota_1,   0x150000,0x140000,
spiffs,   data, spiffs,  0x290000,0x170000,

then spiffs start is 0x290000 and spiffs size is 0x170000

Compiling and Uploading the SPIFFS

This step is detailed in the aforementioned issue and even automated in spiffs_board_info.mk

References

VERSION=0.14.0
SPECIFIER=arduino-cli_${VERSION}_Linux_64bit
BINDIR=/usr/local/bin/
# - Avoid redownloading
# - Avoid overwriting
# - To overwrite, use make uninstall
install:
TMPDIR=/tmp/$(SPECIFIER) && \
mkdir -p $${TMPDIR} && \
if [ ! -f $${TMPDIR}/arduino-cli ]; then \
curl -L https://github.com/arduino/arduino-cli/releases/download/${VERSION}/$(SPECIFIER).tar.gz | tar -zxC $${TMPDIR}; \
fi && \
sudo cp $${TMPDIR}/arduino-cli $(BINDIR)/arduino-cli -vn
uninstall:
sudo rm ${BINDIR}/arduino-cli
download:
curl -LOJ https://github.com/arduino/arduino-cli/releases/download/${VERSION}/$(SPECIFIER).tar.gz
# SOURCE: https://github.com/esp8266/arduino-esp8266fs-plugin/issues/51#issuecomment-739433154
sketch := foo.ino
boardconfig := esp8266:esp8266:generic:eesz=4M1M
ARDUINO_CLI ?= arduino-cli
MKSPIFFS ?= mkspiffs
BC ?= bc
all: build
.PHONY: build
build:
$(ARDUINO_CLI) compile --fqbn $(boardconfig) $(sketch)
.ONESHELL:
filesystem.bin:
PROPS=$$($(ARDUINO_CLI) compile --fqbn $(boardconfig) --show-properties)
BUILD_SPIFFS_BLOCKSIZE=$$(echo "$$PROPS"|grep "^build.spiffs_blocksize"|cut -d= -f2)
BUILD_SPIFFS_END=$$(echo "$$PROPS"|grep "^build.spiffs_end"|cut -d= -f2)
BUILD_SPIFFS_PAGESIZE=$$(echo "$$PROPS"|grep "^build.spiffs_pagesize"|cut -d= -f2)
BUILD_SPIFFS_START=$$(echo "$$PROPS"|grep "^build.spiffs_start"|cut -d= -f2)
BUILD_SPIFFS_SIZE=$$(echo "ibase=16;$${BUILD_SPIFFS_END:2}-$${BUILD_SPIFFS_START:2}"|bc -q)
echo BUILD_SPIFFS_SIZE $$BUILD_SPIFFS_SIZE
$(MKSPIFFS) -c data -p $$BUILD_SPIFFS_PAGESIZE -b $$BUILD_SPIFFS_BLOCKSIZE -s $$BUILD_SPIFFS_SIZE $@
.PHONY: flash
flash: filesystem.bin
.PHONY: flash-fs
flash-fs:
.PHONY: clean
clean:
rm -rf build
rm -f filesystem.bin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment