Search code examples
filesystemsesp32micropython

esp32 micropython littlefs


Using a Expressif dev-board and standard micropython.bin I was able to create a littlefs2 partition, mount it and write data into a file:

 # ESP8266 and ESP32
import os
os.umount('/')
os.VfsLfs2.mkfs(bdev)
os.mount(bdev, '/'))

with open("myfile.txt", "a") as f:
  f.write("myData")

Once I have done this os.listdir() will show the myfile.txt file but I can't see a way to update my code on the ESP anymore.
I assume I need to get rid of the littlefs2 file system first but could not find an example for that.
The only way I found for changing my code was erasing all flash, load a new copy of micropython.bin and then flash my code modules again onto the chip - which is kind of a long process for every change in my code?
Any shorter/faster way to update my code files on the ESP?


Solution

  • A - Revert to fat

    if reformat with vfsFat is close enough to 'getting rid of' you can do the following:

    import os
    os.umount('/')
    os.VfsFat.mkfs(bdev)
    os.mount(bdev, '/')
    
    import _boot
    

    this should re-format the flash as fat, and re-init using the post_flash _boot module

    B- Use an IDE to write code for/to the MCU

    Assuming you also have a serialport ( or telnet) connection to your MCU another approach is to use an IDE ( VSCode + Pymakr works for sure, or likely Thonny works as well) to connect to your MCU. Pymakr will happily upload / download files from whatever filesystem you are using, as long as the files are not TOO HUGE

    C - use a cmdline tool to download files from your MCU

    there are a few tools for this

    • rshell advantage: can download complete folders, but may have same issues on windows
    • pyboard.py simple to use for single file up/download not for more complex operations. Do NOT use the stale version from PyPi
    • mpremote newest tool with more functionality , not supported against older firmware versions.