There is no easy way to change the partitions in MicroPython. You will need to modify the partition
table and compile MicroPython from source. Here is how you can set up a MicroPython build
environment using Ubuntu 20.04.2. You can use the Windows Subsystem for Linux version 2's Ubuntu
20.04.2 as well.
Update and upgrade Ubuntu using apt-get if you are using a new install of Ubuntu or the Windows
Subsystem for Linux.
sudo apt-get -y update
sudo apt-get -y upgrade
Use apt-get to install the required build tools.
sudo apt-get -y install build-essential libffi-dev git pkg-config cmake virtualenv python3-pip python3-virtualenv
Use git to clone the esp-idf SDK repo & install -- this will take a while.
git clone --recursive https://github.com/espressif/esp-idf.git
cd esp-idf/
./install.sh
Source the esp-idf export.sh script to set the required environment variables. It's important that
you source the file and not run it using ./export.sh. You will need to source this file before
compiling MicroPython.
source export.sh
cd ..
Use git to clone the MicroPython repo.
git clone https://github.com/micropython/micropython.git
Update the git submodules and compile the MicroPython cross-compiler
cd micropython/
git submodule update --init
cd mpy-cross/
make
cd ..
cd ports/esp32
You can change the ESP32 partition sizes by editing the partitions.csv file in ports/esp32. There
are a few things you should be aware of when changing the partitions.csv file. The factory
partition is where the MicroPython application resides. This partition must be large enough for the
MicroPython firmware and it must be placed at offsets aligned to 0x10000 (64K). The vfs partition
is where the virtual file system is located.
The generic partitions.csv file is configured for a 4MB flash device and looks like this:
Default partitions.csv file contents
# Notes: the offset of the partition table itself is set in
# $IDF_PATH/components/partition_table/Kconfig.projbuild.
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x9000, 0x6000,
phy_init, data, phy, 0xf000, 0x1000,
factory, app, factory, 0x10000, 0x180000,
vfs, data, fat, 0x200000, 0x200000,
The factory partition starts at 0x10000 (aligned to 64K as required) and has a size of 0x180000.
This places the end of the factory partition at (0x10000 + 0x180000) or 0x190000. The next
partition is the vfs partition, it starts at 0x200000 (2MB) and has a size of 0x200000 (2MB). This
leaves 0x200000 - 0x190000 = 0x70000 or 448K bytes of unused flash between the end of the factory partition and the start of the vfs partition. Adding the unused flash to the factory partition
results in a new factory partition size of 0x180000 + 0x70000 = 0x1F0000 or 64K less than 2MB.
Modified partitions.csv file contents
# Notes: the offset of the partition table itself is set in
# $IDF_PATH/components/partition_table/Kconfig.projbuild.
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x9000, 0x6000,
phy_init, data, phy, 0xf000, 0x1000,
factory, app, factory, 0x10000, 0x1F0000,
vfs, data, fat, 0x200000, 0x200000,
If more 'factory' flash space is required, you can decrease the size of the vfs partition and
increase it's offset by the same amount then add the newly freed space to the factory partition.
Modified partitions.csv file contents for a 3MB factory partition and a 960K virtual file system
partition.
# Notes: the offset of the partition table itself is set in
# $IDF_PATH/components/partition_table/Kconfig.projbuild.
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x9000, 0x6000,
phy_init, data, phy, 0xf000, 0x1000,
factory, app, factory, 0x10000, 0x300000,
vfs, data, fat, 0x310000, 0xF0000,
Once you have your partition sizes worked out, build the MicroPython firmware.
make all
The firmware.bin file will be in the build-GENERIC directory. If you have been running other
firmware or have modified the partition table you must erase before you flash the new firmware to
your device. Set PORT= to the ESP32's usb serial port.
make PORT=/dev/ttyUSB0 erase
make PORT=/dev/ttyUSB0 deploy
At this point you should have MicroPython running on your ESP32 device using the new partition sizes.
You can connect to the REPL over the usb port using:
make PORT=/dev/ttyUSB0 monitor