2. May 2023

How to build single flashable binary for ESP32 with esptool.py

Build of ESP-IDF project produces several files, like bootloader, application binary or partition table.

Having several files makes it harder to ship the application outside of build computer.

Solution to the problem is merging binaries into single flashable file.

Build your project with idf.py as always:

cd project
idf.py build

Merge binaries into single file. At the end of build process the tool will display command for flashing. This can be used to compose command like this:

esptool.py --chip ESP32 merge_bin -o merged-flash.bin --flash_mode dio ^
  --flash_size ^
  2MB 0x0 build\bootloader\bootloader.bin ^
  0x8000 build\partition_table\partition-table.bin ^
  0x10000 build\my_app.bin

Luckily there is a simpler way, because all those arguments are stored in build/flash_args file:

Example for Bash

(cd build; esptool.py --chip esp32 merge_bin -o merged-flash.bin @flash_args)

Example for PowerShell

cd build
esptool.py --chip esp32 merge_bin -o merged-flash.bin "@flash_args"

17. April 2023

Matter: chip-tool pairing ble-wifi failing with Abort trap: 6 on macOS

chip-tool can be used for commissioning ESP32 with Matter.

macOS users may face the following error with iTerm:

chip-tool interactive start

pairing ble-wifi 0x7283 SSID PASS PIN 3840
..[FP] Validating NOC chain
..[FP] NOC chain validation successful
..[FP] Added new fabric at index: 0x1
...
Abort trap: 6

It’s necessary to grant access to Bluetooth to apps launched from iTerm.

Click System SettingsPrivacy & Security. Select Bluetooth. Click +, Select Applications, and find iTerm.

Close and open iTerm. The problem with “trap 6” should be gone.

Just do not forget to install Bluetooth Central Matter Client Developer mode profile. Otherwise the provisioning will fail with UUID problem.

14. April 2023

Setting up esp-matter development environtment on openSUSE

When setting up development environment for esp-matter on OpenSUSE you may encouter following error:

./install.sh
...
Error
uilding wheels for collected packages: gevent
  Building wheel for gevent (pyproject.toml): started
  error: subprocess-exited-with-error
  
  × Building wheel for gevent (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> See above for output.
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  Building wheel for gevent (pyproject.toml): finished with status 'error'
  ERROR: Failed building wheel for gevent
Failed to build gevent
ERROR: Could not build wheels for gevent, which is required to install pyproject.toml-based projects
['esp-matter/connectedhomeip/connectedhomeip/.environment/pigweed-venv/bin/python', '-m', 'pip', 'install', '--log', 'esp-matter/connectedhomeip/connectedhomeip/.environment/pigweed-venv/pip-requirements.log', '--requirement=esp-matter/connectedhomeip/connectedhomeip/scripts/setup/requirements.txt', '--constraint=esp-matter/connectedhomeip/connectedhomeip/scripts/setup/constraints.txt'] {'stdout': <_io.TextIOWrapper name=3 mode='w+' encoding='UTF-8'>, 'stderr': -2}

Problem is caused by gdbgui dependency in ESP-IDF. The dependency is not necessary to build the project.

Edit file esp-matter/connectedhomeip/connectedhomeip/scripts/setup/requirements.esp32.txt .

Disable the line with gdbgui:

#gdbgui==0.13.2.0 ; platform_machine != 'aarch64' and sys_platform == 'linux'

Continue with `./install.sh` script.

9. February 2023

How to call Rust functions from Python, Ruby and Zig

Rust language is great for creating libraries and drivers which can be consumed by other languages like Python, Ruby or Zig.

Let’s consider following Rust application Cargo.toml:

[package]
name = "rustlib"
version = "0.1.0"
edition = "2021"

[lib]
name = "rust_lib"
crate-type = ["dylib"]

File src/lib.rs:

#[no_mangle]
pub extern "C" fn add(left: usize, right: usize) -> usize {
    left + right
}

The next step is to build the library:

cargo build --release

The output library is stored in ./target/release/librust_lib.so

Python

Invoking the add function from Python is very easy:

import ctypes
lib = ctypes.CDLL("./target/release/librust_lib.so")
lib.add(1,2)

Ruby

In case of Ruby we will need ffi gem.
Installation on OpenSuse:

sudo zypper install ruby3.1-rubygem-ffi

Ruby code:

require 'ffi'

module MyLib
  extend FFI::Library
  ffi_lib './target/release/librust_lib.so'
  attach_function :add, [ :int, :int ], :int
end

puts MyLib.add 1, 2

Zig

Zig will require little bit more stuff. We need to generate C headers from Rust, which then can be loaded to Zig. Install cbindgen for the conversion:

cargo install cbindgen

Generate header file from the library.

cbindgen --lang c --output rustlib.h

Create Zig application in zig2rust:

const std = @import("std");
const rustlib = @cImport(@cInclude("rustlib.h"));

pub fn main() !void {
    const stdout = std.io.getStdOut().writer();
    const result = rustlib.add(1,2);
    try stdout.print("Result is {d}.\n", .{result});
}

Compile Zig application:

zig build-exe zig2rust.zig -I. -l target/release/librust_lib.so

Run the application:

./zig2rust

21. December 2022

Rust Bare Metal application for ESP32, desktop, Android and iOS

Rust language and tooling are very powerful. It makes it easy to build for different platforms and architectures and still have performance close to C language.

It’s possible to write the same application for ESP32 with Xtensa architecture or ESP32-C3 RISC-V architecture. The portability of Rust code is high. Changing the target and adding a few wrapper functions makes it possible to build the same application for the desktop or web browser with WebAssembly. Once the WebAssembly is ready, it’s possible to turn the application into Progressive Web App (PWA) which can be installed on Android or iOS.

ESP32 Spooky Maze game is example of application which can work on ESP32 and also on mobile device. The shared core code contains the main part of the implementation, and each platform has its tiny wrapper.

The example Rust application uses Embedded Graphics to draw images. When the app is running on real HW, it’s transferred to the display via SPI using ESP-HAL. The desktop version is using SDL2 to interact with Linux, macOS, or Windows. The web version is using WASM, and framebuffer is serialized to HTML5 canvas. PWA application for Android also supports access to Accelerometer, so it can simulate similar behavior like IMU on ESP32-C3-DevKit-RUST or ESP32-S3-BOX where user can tilt the device to move the character in the maze.

Web version of the application with PWA support: Spooky (full-screen mode)

Embedded version of the application: ESP32-S3-BOX, M5CORE-FIRE (M5Stack) and many more.

Thanks to cloud development IDEs, it’s possible to build the application on GitPod.io or GitHub Codespaces and run it even without real HW using Wokwi simulator.