Skip to content

Instantly share code, notes, and snippets.

@abobija
Last active April 13, 2024 04:57
Show Gist options
  • Star 55 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save abobija/2f11d1b2c7cb079bec4df6e2348d969f to your computer and use it in GitHub Desktop.
Save abobija/2f11d1b2c7cb079bec4df6e2348d969f to your computer and use it in GitHub Desktop.
ESP-IDF on WSL2 - Build, Flash and Monitor

ESP-IDF on WSL2 - Build, Flash and Monitor ⚡

Demo

How to setup ESP-IDF on WSL2 Ubuntu 20.04 - Config, Build and Flash

Intro

WSL2 still does not support USB devices, but with a little effort we can make possible to flash and monitor ESP device from WSL2.

Info:
Tested on Ubuntu 20.04 LTS and Debian distributions.

For flashing and monitoring over the serial COM port, I've wrote this compact idfx shell script.

Note:
As a prerequisite for using idfx, Python 🐍 needs to be installed on the Windows.

More about idfx you can find in official repository.

Ok, that was short intro, now you can open WSL and start to execute next commands.

Commands 📜

Installation 🚀

  • Update Linux
    sudo apt update && sudo apt upgrade -y

  • Install tools required for esp-idf
    sudo apt install -y git wget curl flex bison gperf python3 python3-pip python3-venv cmake ninja-build ccache libffi-dev libssl-dev dfu-util libusb-1.0-0

  • Make python3 as default python
    sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 1

  • Make pip3 as default pip
    sudo update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 1

  • Make esp directory and go inside
    cd ~ && mkdir esp && cd esp

  • Clone esp-idf repository
    git clone --recursive https://github.com/espressif/esp-idf.git

  • Modify esp-idf installation script to work with WSL
    cd esp-idf && ! grep -q "dirname --" install.sh; [ $? -eq 0 ] && sed -i 's/dirname/dirname --/g' install.sh

  • Run esp-idf installation script
    . ./install.sh || true

  • Add esp-idf export script to the user profile script in order to make isp-idf tools visible on the PATH for every session
    echo -e '\n\n. $HOME/esp/esp-idf/export.sh > /dev/null 2>&1 || true' >> ~/.profile

  • Install idfx ( ? )
    curl https://git.io/JyBgj --create-dirs -L -o $HOME/bin/idfx && chmod u+x $HOME/bin/idfx

  • Source profile script to add all necessary tools to the PATH
    . ~/.profile || true

Create (a copy of hello_world) project 📄

  • Make a copy of hello_world example project in home directory
    cd ~ && cp -r $IDF_PATH/examples/get-started/hello_world .

Build, flash and monitor ⚡

  • Go inside of project
    cd hello_world

  • Set target
    idf.py set-target esp32

  • Configure
    idf.py menuconfig

  • Build
    idf.py build

  • Flash
    (open Device Manager on Windows to find COM port of your ESP, mine is COM2)
    idfx flash COM2

  • Monitor
    (change COM2 with your port)
    idfx monitor COM2
    (To exit monitor press CTRL+] or CTRL+T,X)

Tip:
Flash and monitor with single command:
idfx flash COM2 monitor


That's it.
Happy coding and flashing! ⚡

idfx preview

Author

abobija - abobija.com

@akauppi
Copy link

akauppi commented Jan 1, 2024

Just letting you know:

I've made a similar repo https://github.com/lure23/ESP32-WSL which uses USBIP for communicating with the device, from WSL2. It requires minimum installations on the host (Windows) side, which was one of my design criteria. This is where it differs from the work in this repo. Perhaps someone who has problems finds it a way further. cc @hetal-mpc

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