1. April 2022

How to develop for ESP32-C3 with Rust on macOS with Lima using Dev Container in VS Code

Lima is a solution for macOS for managing Linux VM on macOS which plays well with Dev Containers. It has several advantages over Docker Desktop for macOS or Podman.

Let’s see how it can be used for the development of software for embedded hardware like ESP32-C3 using source code from Ferrous Systems training:

git clone https://github.com/ferrous-systems/espressif-trainings.git

Install Lima and Docker-CLI:

brew install lima docker

Create Linux VM with Dockerd (following instructions from Kevin O’Brien – Utilizing Docker CLI without Docker Desktop):

curl https://raw.githubusercontent.com/lima-vm/lima/master/examples/docker.yaml -O
limactl start ./docker.yaml
limactl shell docker
sudo systemctl enable ssh.service

There is one important tweak in the Lima configuration. It’s necessary to enable write operation otherwise, the workspace mounted from VS Code is read-only. Open file ~/.lima/docker/lima.yaml and add writable flag to desired folder:

mounts:
- location: "~"
  writable: true

Restart Lima to apply changes.

limactl stop docker
limactl start docker

Create context for Docker-CLI to connect to dockerd running in the VM:

docker context create lima --docker "host=unix://${HOME}/.lima/docker/sock/docker.sock"
docker context use lima

Open VS Code with the installed Remote Container plugin and click Re-Open in Container:

cd espressif-trainings
code .

Open terminal in VS Code and build the example:

cd intro/hardware-check
cp cfg.toml.example cfg.toml
cargo build

Flashing of the resulting file could be done by espflash and mounting the device to Lima or using a tool like Adafruit WebSerial ESPTool. The file for flashing is located in the directory target/release.

31. March 2022

How to develop for ESP32-C3 with Rust on macOS with Podman using Dev Container in VS Code

Development in Dev Containers using VS Code greatly simplifies bootstrapping of the development environment. The developer does not need to install toolchains locally and spends a lot of time composing the development environment.

The default installation of VS Code is configured to work with Docker. It requires some small additional steps to switch to Podman.

Let’s begin with development using examples from Ferrous Systems training:

git clone https://github.com/ferrous-systems/espressif-trainings.git

Install Podman and check version:

brew install podman
podman --version

The version should be at least 4.0. If you have a previous version, consider an upgrade.

Following step might not be obvious to Docker users. Docker creates VM for managing containers in the background without asking the user. In the case of Podman, this is more versatile and you can define what kind of machine do you want to create. Here are a few options recommended for development, when you omit them you’ll get smaller defaults.

podman machine init --disk-size 20 --cpus 8 -m 4096 -v ${HOME}/espressif-trainings:${HOME}/espressif-trainings
podman machine start

Please, notice also -v option which mounts the development directory to Podman VM, without this mount you’ll get:

Error: statfs espressif-trainings: no such file or directory

Now the Podman VM should be ready and we can spin up containers. Go to the project directory and open Visual Studio Code:

cd ${HOME}/espressif-trainings
code .

It’s necessary to install one additional dependency for Podman: podman-compose

pip3 install podman-compose

It’s necessary to tell VS Code to use podman instead of docker commands. Go into Settings and search for keyword docker. Replace docker by podman and docker-compose by podman-compose.

VS Code is ready and click Reopen in Container.

Pulling the base image might take a while.

Open terminal and build the first ESP32-C3 example:

cd intro/hardware-check
cp cfg.toml.example cfg.toml
cargo build

Note: If VS Code is complaining about existing vscode volume, it’s possible to remove it by command

podman volume rm vscode

Note 2: If the remove is blocked by the existing terminated container, it’s possible to clean the reference by command

podman container prune

Flashing of the resulting file could be done by espflash and mounting device to Podman or using the tool like Adafruit WebSerial ESPTool. The file for flashing is located in the directory target/release.

21. February 2022

Podman: Could not open ‘edk2-aarch64-code.fd’

It’s possible to use brew to install Podman on Apple Silicon (M1). The installation gets slightly more complicated when the user wants to use Homebrew installed in user’s home directory.

Problem #1 – gvproxy

Command:

podman machine start

Error:

Error: unable to start host networking: "could not find \"gvproxy\" in one of ....

Solution: add path to Podman’s helper binaries stored in bin and libexec to ~/.config/containers/containers.conf

[engine]
  helper_binaries_dir=["/Users/georgik.rocks/brew/Cellar/podman/4.0.3/bin","/Users/georgik.rocks/brew/Cellar/podman/4.0.3/libexec"]

Problem #2 – edk2-aarch64-code.fd

Command:

podman machine start

Error:

INFO[0000] new connection from  to /tmp/podman/qemu_podman-machine-default.sock
Waiting for VM ...
qemu-system-aarch64: -drive file=edk2-aarch64-code.fd,if=pflash,format=raw,readonly=on: Could not open 'edk2-aarch64-code.fd': No such file or directory
Error: dial unix /tmp/podman/podman-machine-default_ready.sock: connect: connection refused
ERRO[0003] cannot receive packets from , disconnecting: cannot read size from socket: EOF
ERRO[0003] cannot read size from socket: EOF

Solution: Open file ~/.config/containers/podman/machine/qemu/podman-machine-default.json and change to /Users/USERNAME/brew/Cellar/qemu/6.2.0_1/share/qemu/edk2-aarch64-code.fd

"file=/Users/georgik.rocks/brew/Cellar/qemu/6.2.0_1/share/qemu/edk2-aarch64-code.fd,if=pflash,format=raw,readonly=on"

After these changes Podman should start without problem.

1. December 2021

How to connect ESP32 as USB serial device to Linux in WSL2 on Windows 11

There is a nice article and video explaining how to connect USB serial to Linux in WSL2.

Just few details are missing. Here is the full list of steps necessary to flash ESP32 with FTDI from WSL2:

  • Install Windows 11, open Windows Update – join Windows Insider Program (Beta channel) – install updates, reboot machine
  • Windows Update – Advanced Options – check the option “Receive updates for other Microsoft products” – Back – Check for updates
  • Reboot or shutdown WSL2 images
  • start a new WSL2 image e.g. with Ubuntu 20 LTS, check that you have kernel 5.10: uname -a. It does not work on 4.x kernel from normal WSL2
  • install https://github.com/dorssel/usbipd-win/releases on Windows
  • in Linux – sudo apt install linux-tools-5.4.0-77-generic hwdata
  • in Linux – visudo
  • in Linux – prepend path Defaults secure_path=”/usr/lib/linux-tools/5.4.0-77-generic:
  • connect ESP device with FTDI in Windows PowerShell (administrator) type: usbipd wsl list
  • search for 5-3 USB Serial Converter A, USB Serial Converter B Not attached
  • type in Windows: usbipd wsl attach -b 5-3 -d Ubuntu
  • type in Linux:cd examples/get-started/blink; idf.py build flash monitor

Result:

I (263) example: Example configured to blink addressable LED!
I (263) example: Turning the LED OFF!
I (1273) example: Turning the LED ON!

3. November 2021

Pipenv on Windows fails AttributeError: ‘NoneType’ object has no attribute ‘version_sort’

There is an issue with Python Pipenv on Windows.

The command to open shell with isolated pipenv you can use command:

python -m pipenv shell

The command might fail on Windows with a strange error like this:

Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2032.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2032.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pipenv\__main__.py", line 5, in <module>
    cli()
  File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pipenv\vendor\click\core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pipenv\vendor\click\core.py", line 782, in main
    rv = self.invoke(ctx)
  File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pipenv\vendor\click\core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pipenv\vendor\click\core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pipenv\vendor\click\core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pipenv\vendor\click\decorators.py", line 73, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pipenv\vendor\click\core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pipenv\cli\command.py", line 429, in shell
    do_shell(
  File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pipenv\core.py", line 2356, in do_shell
    ensure_project(
  File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pipenv\core.py", line 576, in ensure_project
    ensure_virtualenv(
  File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pipenv\core.py", line 498, in ensure_virtualenv
    python = ensure_python(three=three, python=python)
  File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pipenv\core.py", line 388, in ensure_python
    path_to_python = find_a_system_python(python)
  File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pipenv\core.py", line 350, in find_a_system_python
    return next(iter(finder.find_all_python_versions()), None)
  File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pipenv\vendor\pythonfinder\pythonfinder.py", line 328, in find_all_python_versions
    path_list = sorted(versions, key=version_sort, reverse=True)
AttributeError: 'NoneType' object has no attribute 'version_sort'

The problem is caused by pythonfinder.py which is trying to locate a Python.

You can override the pythonfinder.py by explicitly defining the version of the Python.

Determine the version of installed Python:

python --version

Specify the Python version when starting the shell:

python -m pipenv shell --python 3.9.7

The Python environment should start correctly.