Skip to content

Instantly share code, notes, and snippets.

@Rumidom
Last active March 28, 2024 14:56
Show Gist options
  • Select an option

  • Save Rumidom/cbd62875bf52b8d26bde6864ee4a5402 to your computer and use it in GitHub Desktop.

Select an option

Save Rumidom/cbd62875bf52b8d26bde6864ee4a5402 to your computer and use it in GitHub Desktop.
Building Micropython for ESP32 on Windows
  • 1 - Open cmd or powershell and run: wsl --install to install WSL (Windows Subsystem for linux)

  • 2 - Enable Windows Subsystem for linux on windows features and reboot

2.1 - Alternativaly run as administrator on powershell/cmd:

PS C:\WINDOWS\system32> Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
  • 3 - search for ubuntu on windowns search bar, run the ubuntu aplication, this will give you acess to a linux terminal

  • 4 - on the new linux terminal run (you need to install everything even if you are already setup on windowns):

sudo apt update && sudo apt upgrade -y
sudo apt-get install build-essential libffi-dev git pkg-config
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

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

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

4.3 - install esptool : sudo -H pip install esptool

4.4 - install rshell (you need this to connect to your micropython device over the terminal): sudo pip3 install rshell

  • 5 - create a "esp" folder and clone the latest version of ESP-IDF suported by micropython:
cd ~ && mkdir esp && cd esp
git clone -b v5.0.4 --recursive https://github.com/espressif/esp-idf.git
  • 6 - Modify install.sh to work with WSL:
cd esp-idf && ! grep -q "dirname --" install.sh; [ $? -eq 0 ] && sed -i 's/dirname/dirname --/g' install.sh
. ./install.sh || true

6.1 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
. ~/.profile || true
  • 7 - clone micropython repo to you $HOME directory, if you intend on contributing the docs sugest you fork the repo:
cd ~ && git clone https://github.com/micropython/micropython.git
  • 8 - now that everything is set-up, any time you need to build micropython you just open the ubuntu terminal and build it: (no need to run export.sh )
cd /micropython/ports/esp32
make submodule
make

the bin file will be located at /micropython/ports/esp32 under build-... folder

8.1 for the esp32 S3 with octal spiram, more info on BOARD and BOARD_VARIANTS here:

make BOARD=ESP32_GENERIC_S3 BOARD_VARIANT=SPIRAM_OCT

8.2 to modify flash size Modify the sdkconfig.board file:

Under CONFIG_COMPILER_OPTIMIZATION_SIZE=y add the following and save:
CONFIG_FLASHMODE_QIO=y
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
CONFIG_ESPTOOLPY_FLASHSIZE_DETECT=y
CONFIG_ESPTOOLPY_AFTER_NORESET=y
CONFIG_SPIRAM_MEMTEST=
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=
CONFIG_ESPTOOLPY_FLASHSIZE_8MB=
CONFIG_ESPTOOLPY_FLASHSIZE_16MB=y
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions-16MiB.csv"

8.3 you could also modify the .bin file using this tool: https://github.com/glenn20/mp-image-tool-esp32 (on windows rename mp-image-tool-esp32 to mp-image-tool-esp32.py and run python3 mp-image-tool-esp32.py)

  • 9 - turns out that WLS doesnt have acess to hardware(COM port), but there is a workaround: (steps 9.3 to 9.6 need to be done every time you open the terminal)

9.0 - with the Ubuntu terminal open run: ls /dev/tty* this will be useful later

9.1 - install: https://github.com/dorssel/usbipd-win/releases

9.2 - Open Powershell as admin

9.3 - usbipd list (this will list your usb devices)

9.4 - Search for your USB device and bind the usb to share usbipd bind --busid 2-3 (for me it was 2-3 Serial USB (COM6))

9.5 - usbipd list Verify using usbip list that its shared

9.6 - Attach to wsl usbipd attach --wsl --busid 2-3

9.7 - run lsusb on the Ubuntu terminal, your USB device should be there

  • 9 - Another tricky thing is finding the actual COM port in linux, running ls /dev/tty* will list all ports, its one of those compare with the list from step 7.0, the com port should apear on the latest list but not in the one from step 7.0, for me it was /dev/ttyACM0, but might be diferent for you, Linux might use ttySx for a serial port device name. For example, COM1 (DOS/Windows name) is ttyS0, COM2 is ttyS1, and so on.

9.1 to test try connecting to a already flashed board: rshell --buffer-size 32 -a -p /dev/ttyACM0

References:
https://docs.micropython.org/en/latest/develop/gettingstarted.html
https://www.wemos.cc/en/latest/tutorials/others/build_micropython_esp32.html
https://github.com/micropython/micropython/blob/master/ports/esp32/README.md
https://abobija.com/blog/electronics/esp-idf-on-wsl2/
https://github.com/orgs/micropython/discussions/11612 \ https://github.com/glenn20/mp-image-tool-esp32

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