Josverl / vfs_merge

Merge a MicroPython firmware and with source files into a single binary that can be flashed to a device.
Other
4 stars 1 forks source link

How is this tool actually used? #5

Open steoj opened 5 months ago

steoj commented 5 months ago

Question from a noob here:

This looks exactly what I need to make sure my firmware is bundled with my boot.py and my main.py when flashing my ESP32. Its a python program, I get that, but what command line actions are actually needed to make this happen?

I can open the repo in VSCode and it starts up a container environment for me. Something goes wrong and I@m not sure how to actually make the merge with src and firmware. Screenshot 2024-05-07 at 06 44 40

Running Mac M3

steoj commented 5 months ago

Got a little further by understanding and discovering poetry! Managed to create a distribution file dist/vfs_merge-0.1.0-py3-none-any.whl and may install this locally by running:

pipx install dist/vfs_merge-0.1.0-py3-none-any.whl

All well and good. Moving to my project I run:

vfsmerge --port esp32 --firmware ./boards/CUSTOM_ESP32/build/micropython.bin

but this causes the whole thing to exit by:

sten.johnsen$ vfsmerge --port esp32 --firmware ./boards/CUSTOM_ESP32/build/micropython.bin
Traceback (most recent call last):
  File "/Users/sten.johnsen/.local/bin/vfsmerge", line 5, in <module>
    from vfs_merge.__main__ import cli
  File "/Users/sten.johnsen/.local/pipx/venvs/vfs-merge/lib/python3.12/site-packages/vfs_merge/__main__.py", line 11, in <module>
    from .create_littlefs import folder_to_lfs
  File "/Users/sten.johnsen/.local/pipx/venvs/vfs-merge/lib/python3.12/site-packages/vfs_merge/create_littlefs.py", line 9, in <module>
    from littlefs import LittleFS
  File "/Users/sten.johnsen/.local/pipx/venvs/vfs-merge/lib/python3.12/site-packages/littlefs/__init__.py", line 5, in <module>
    from pkg_resources import DistributionNotFound, get_distribution
ModuleNotFoundError: No module named 'pkg_resources'

I am missing something, but what?

Josverl commented 5 months ago

I never tried pipx from a wheel , but it appears it is missing one of the dependencies did you do a poetry build before ?

background: I pulled this tool out of another project - but never published it to PyPI. until just now - but it still needs some fit an finish to setup the folder and file structure correctly. you could try if the package from PyPI works better - and without needing to build from a clone

If you need/want to build from a clone for now the quick-steps are :

bootstrap toolchain ( one time)

clone and install

to run

steoj commented 5 months ago

Thanks for your quick and helpful reply. Helped a lot that you published it! Using vfs_merge as part of my build pipeline and it seems vfs_merge expects esptool to be callable not esptool.py This task:

    - name: Merge with src files
      run: |
        pip install vfs_merge
        mkdir build
        vfsmerge --port esp32 --firmware ./boards/CUSTOM_ESP32/build/micropython.bin

causes Screenshot 2024-05-08 at 06 47 10

Note that I also have to create the build folder as writing the littlefs.img otherwise fails.

I can reproduce this locally and running

esptool.py --chip esp32 merge_bin -o build/firmware_lfs.bin --flash_mode dio --flash_size 4MB 0x1000 boards/CUSTOM_ESP32/build/micropython.bin 0x00200000 build/littlefs.img

produces a binary in build/firmware_lfs.bin

Josverl commented 5 months ago

I can update the code to create a ./build folder if it does not exists.

I think i've seen the behaviour on subprocess.run with some other tools as well. it is caused by the now process not inherting the python environment and I think I know how to fix that by using subprocess.run(f"{sys.executable} -m esptool ....) rather than hoping that the OS already knows where to fid the python script for the environment

Josverl commented 5 months ago

✅ Publishing vfs-merge (0.1.2) to PyPI

Once you have it working I'd love a copy/example of your workflow to add to the documentation

steoj commented 5 months ago

Thanks for following up. Although I can create a micropython.bin, load it to the ESP32 dev board and run, loading the binary created by vfd_merge will not fly. Checking the binaries with mp-image-tool-esp32 reveals something a little worrying to me: Screenshot 2024-05-10 at 13 42 13 Invalid firmware header might be linked to the fact that my esp32 refuses to boot after loading. Flashing

esptool.py --chip esp32 -p $(PORT) -b $(BAUD) --before default_reset --after hard_reset write_flash --flash_mode=dio
--flash_size=4MB    --flash_freq 40m 0x1000  build/bootloader/bootloader.bin 0x8000  build/partition_table/partition-table.bin 0x10000 build/micropython.bin

makes REPL start and everyone is happy (less the python files in the SRC directory that is). I'm also noticing from the debug info that vfs_merge is running esptool v4.5, while I locally (installed with homebrew) have v4.7. Might that have anything to do with my unfortunate result?