No USB filesystem after installing MIcroPython bootloader

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
sgordon777
Posts: 1
Joined: Fri Jul 15, 2022 3:07 am

No USB filesystem after installing MIcroPython bootloader

Post by sgordon777 » Fri Jul 15, 2022 3:16 am

I just got some Pi Picos... I've been playing around with CircuitPython (which works just fine BTW) and decided to try flashing MicroPython....


I did the following:

1-Hook new Pico board up to USB port
2-Hold BootSel button
-Plug USB port into PC
-Copy rp2-pico-20220618-v1.19.1.uf2 (uPython bootloader) to D: (drive mapped to Pico bootloader FS) drive

D: drive goes away, but I never get a new drive mapped to MicroPython USB drive never appears.

The above procedure works just fine with the CircuitPython uf2.... I get a USB drive with a Lib folder and a code.py file.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: No USB filesystem after installing MIcroPython bootloader

Post by Roberthh » Fri Jul 15, 2022 7:22 am

MicroPython does not have MSC support by intention for most ports. The reason: it requires to use a FAT file system on the boards, which i not designed to be used on flash devices. And using it either has the high probability of file system corruption or requires dedicated switch between write access by the PC or by the board. Instead, MicroPython favors to use the Littlefs files system and at the PC the mpremote tool in combination with it's mount command.

In any case: MSC can can be enabled for the RPI Pico by a compile switch. It is enabled for instance for the Arduino Nano Connect 2040. And it can be enabled at the PyBoard series by a API call during boot.

P.S.: For Linux system, Littlefs support is available as a fuse file system.

JumpZero
Posts: 54
Joined: Mon Oct 30, 2017 5:54 am
Location: Arcachon - France

Re: No USB filesystem after installing MIcroPython bootloader

Post by JumpZero » Sat Jul 16, 2022 3:29 pm

Roberthh wrote:
Fri Jul 15, 2022 7:22 am
P.S.: For Linux system, Littlefs support is available as a fuse file system
Oh! This caught my attention.
I would love to be able to access the micropython filesystem from Linux.
I searched the documentation and actually found here this:

Code: Select all

A littlefs filesystem can be still be accessed on a PC over USB MSC using the littlefs FUSE driver. Note that you must specify both the --block_size and --block_count options to override the defaults. For example (after building the littlefs-fuse executable):
Getting and building littlefs fuse driver should be ok (not yet done).
But the example given in the doc to get block-size and block_count parameters is for the pyboard.
I tried uos.statvfs('/') on an esp32. Would the obtained f_frsize and f_blocks be ok to pass to lfs parameters?

Also in the doc the example to mount the filesystem on linux is:

Code: Select all

$ ./lfs --block_size=4096 --block_count=512 -o allow_other /dev/sdb1 mnt
Right now (but I haven't tried anything yet) I cannot see how I would get /dev/sdb1 to appear in Linux.
Does these micropython commands given in the doc:

Code: Select all

import pyb
f = pyb.Flash(start=0)
f.ioctl(1, 1)  # initialise flash in littlefs raw-block mode
make a USB MSC appears in Linux?
I must have missed something!

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: No USB filesystem after installing MIcroPython bootloader

Post by Roberthh » Sat Jul 16, 2022 3:49 pm

The block-size is indeed 4096. The block-count is file-system-size/4096, which should be 352. If you have chosen the wrong number, you'll get an error message with the right number.
To enable file system access, the port must support it. The PyBoard port does, and you have to enable it e.g. with pyb.usb_mode('VCP+MSC') (see: https://docs.micropython.org/en/latest/ ... hlight=msc). Once MSC is enabled, you will see another /dev/sd* device in Linux. It does not have to be /dev/sdb1, could be as well /dev/sdbc ..., depending on how many mass storage devices are already attached to your computer.
Raspberry Pi support MSC at the moment only for the Arduino nano 2040 connect board. You can enable it as well for the other boards, but you have to build a new firmware, by uncommenting the line

#define MICROPY_HW_USB_MSC (1)

in the board's mpyconfigboard.h. I did not try RPI Pico MSC with fuse yet, but can do so.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: No USB filesystem after installing MIcroPython bootloader

Post by Roberthh » Sat Jul 16, 2022 4:03 pm

Edit: Looking through the RP2 implementation, using MSC currently enforces a FAT file system for the board. That's done in main.c by executing _boot_fat.py instead of _boot.py. So you would have to change that as well.
Tested it and it worked. My mount command was:

sudo ./lfs --block_size=4096 --block_count=352 -o allow_other -o nonempty /dev/sdc mount

with mount being my mount point. But be careful. The PC is the master, and changes made by the PC are not always visible at the board. And do not write at the board while it is mounted. There is no protection against trouble.
Unmounting does not work from the PC file manager, if you mounted using sudo. You have to do that in the command line.

JumpZero
Posts: 54
Joined: Mon Oct 30, 2017 5:54 am
Location: Arcachon - France

Re: No USB filesystem after installing MIcroPython bootloader

Post by JumpZero » Sun Jul 17, 2022 11:21 am

Thanks Roberthh I understand now
I'm gonna try that. I'll report here.
Have a good day

JumpZero
Posts: 54
Joined: Mon Oct 30, 2017 5:54 am
Location: Arcachon - France

Re: No USB filesystem after installing MIcroPython bootloader

Post by JumpZero » Mon Jul 18, 2022 9:38 am

I have tested it for the rp2/PICO and it works very well.
I have tested both FAT and littleFS.
As you mentioned to enable mass storage I have uncommented the line
#define MICROPY_HW_USB_MSC (1)
in the board's mpyconfigboard.h then build a new firmware. This build defaults to FAT filesystem.
To build a littleFS firmware I set MICROPY_VFS_FAT to 0 in mpconfigport.h (then main.c runs _boot.py instead of _boot_fat.py)

Code: Select all

// #define MICROPY_VFS_FAT                         (1)
#define MICROPY_VFS_FAT                         (0) 
Then the lfs mount command is working as expected.
As you mentioned entering wrong number for block_count brings an error message with the right number.
A good trick to get it!

MicroPython keeps surprising me with features that I discover. I love it!
With this mass storage (FAT or littleFS) I'm able to configure my favourite editor: geany with 3 windows:
- the mounted file system
- the edit window
- a terminal window where I run rshell
I get a "Thonny" like app where I can edit and run code on the pico. Awesome!
Here under is a screenshot

As you said:
But be careful. The PC is the master, and changes made by the PC are not always visible at the board. And do not write at the board while it is mounted. There is no protection against trouble.
Yes, changes are not always visible in the MicroPython REPL when done on the PC. I have to exit and open again the REPL to see the changes.
I did not try to write to the filesystem from the REPL while mounted on the PC, I usely don't need, I take note of being careful.

I must try now:
Doing the same for the PICO_W (should be easy only the block_count will change I think)
Doing the same for ESP8266 and ESP32 (I already check and it seems different)

Thanks again
Attachments
ScreenshotGeanyPicoFatRshell.png
ScreenshotGeanyPicoFatRshell.png (140.67 KiB) Viewed 4895 times

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: No USB filesystem after installing MIcroPython bootloader

Post by Roberthh » Mon Jul 18, 2022 10:01 am

It os not possible for ESP8266 and ESP32, because they connect through a usb/uart bridge. Only ESP32-C3 may be possible, because it has built-in USB. But someone has to port the code for MSC support.

JumpZero
Posts: 54
Joined: Mon Oct 30, 2017 5:54 am
Location: Arcachon - France

Re: No USB filesystem after installing MIcroPython bootloader

Post by JumpZero » Mon Jul 18, 2022 11:39 am

Roberthh wrote:
Mon Jul 18, 2022 10:01 am
It os not possible for ESP8266 and ESP32, because they connect through a usb/uart bridge. Only ESP32-C3 may be possible, because it has built-in USB. But someone has to port the code for MSC support.
But yes of course, I had forgotten this CH340/CH341 bridge. Of course it's hardware limitation.

dschwert
Posts: 12
Joined: Mon Sep 11, 2017 4:30 pm

Re: No USB filesystem after installing MIcroPython bootloader

Post by dschwert » Tue Jul 19, 2022 8:35 pm

Roberthh wrote:
Mon Jul 18, 2022 10:01 am
Only ESP32-C3 may be possible, because it has built-in USB. But someone has to port the code for MSC support.
The ESP32-C3 has the bridge built in, but it's still a bridge, no full USB.

See here:
https://docs.espressif.com/projects/esp ... nsole.html
"the USB Serial/JTAG Controller is a fixed function device, implemented entirely in hardware. This means it cannot be reconfigured to perform any function other than to provide a serial channel and JTAG debugging functionality."

A big fail, IMHO.

I have built Micropython for Pico with MSC, but I would appreciate if that was made available as download option.

Regards,
Dietmar

Post Reply