Raspberry Pi Pico: how to access the filesystem

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Raspberry Pi Pico: how to access the filesystem

Post by pythoncoder » Tue Jan 26, 2021 1:58 pm

I'm probably missing something obvious but the docs say there is a 1600KB filesystem. This doesn't seem to be mounted and available to MicroPython by default. This seems surprising. I'm using the firmware from the Pi site.

Code: Select all

MPY: soft reboot
MicroPython v1.13-290-g556ae7914 on 2021-01-21; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>> import os
>>> os.listdir('/')
[]
>>> 
Is there a magic uos.umount or uos.VfsLfs1 invocation I should be using?
Peter Hinch
Index to my micropython libraries.

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

Re: Raspberry Pi Pico: how to access the filesystem

Post by Roberthh » Tue Jan 26, 2021 3:01 pm

I just used it. Initially it is empty. I did not look for a way to access it from the PC. For file transfer I used mpr. But rshell should work as well.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Raspberry Pi Pico: how to access the filesystem

Post by pythoncoder » Tue Jan 26, 2021 3:23 pm

I now see what's going on. I was expecting the filesystem to be mounted on /flash or /pyboard. Unfortunately it is mounted at '/' which confuses rshell. I found the source to _boot.py which is now a frozen module:

Code: Select all

import os
import machine, rp2

# Try to mount the filesystem, and format the flash if it doesn't exist.
# Note: the flash requires the programming size to be aligned to 256 bytes.
bdev = rp2.Flash()
try:
    vfs = os.VfsLfs2(bdev, progsize=256)
except:
    os.VfsLfs2.mkfs(bdev, progsize=256)
    vfs = os.VfsLfs2(bdev, progsize=256)
os.mount(vfs, "/")

del os, bdev, vfs
So I guess I need to change the last line and rebuild. I'll point Dave Hylands at this for his comments.
Peter Hinch
Index to my micropython libraries.

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

Re: Raspberry Pi Pico: how to access the filesystem

Post by Roberthh » Tue Jan 26, 2021 3:57 pm

After writing my last reply I tried rshell and it worked. Another nice option of mpr is mount a PC directory at the micro and access it from there.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Raspberry Pi Pico: how to access the filesystem

Post by pythoncoder » Tue Jan 26, 2021 4:45 pm

Hmm. I'm still struggling. I edited _boot.py and rebuilt. I now have the flash mounted at /flash with the contents as I left it. Python sees it but rshell doesn't. What am I doing wrong?

rshell reports version 0.0.28 and Git seems to say it's up to date.

Code: Select all

/mnt/qnap2/data/Projects/MicroPython/rshell> ls /flash
Cannot access '/flash': No such file or directory
/mnt/qnap2/data/Projects/MicroPython/rshell> repl
Entering REPL. Use Control-X to exit.
>
MicroPython de1239b6a-dirty on 2021-01-26; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>> 
>>> import os
>>> os.listdir('/')
['flash/']
>>> os.listdir('/flash/')
['rats.py']
>>> 
Peter Hinch
Index to my micropython libraries.

User avatar
aivarannamaa
Posts: 171
Joined: Fri Sep 22, 2017 3:19 pm
Location: Estonia
Contact:

Re: Raspberry Pi Pico: how to access the filesystem

Post by aivarannamaa » Tue Jan 26, 2021 4:51 pm

pythoncoder wrote:
Tue Jan 26, 2021 3:23 pm
I was expecting the filesystem to be mounted on /flash or /pyboard. Unfortunately it is mounted at '/' which confuses rshell.
Default builds for ESP8266, ESP32 and CircuitPython also mount at '/'. I was thinking of PyBoard and some ESP builds (eg M5Stack) with their /flash to be exceptions :)
Aivar Annamaa
https://thonny.org

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Raspberry Pi Pico: how to access the filesystem

Post by pythoncoder » Tue Jan 26, 2021 4:54 pm

My diagnosis was evidently wrong. See above - something else is confusing either me or rshell ;)
[EDIT]
My diagnosis was sort of right - my attempt at a fix was not.

See below: rshell is now working with the mount at /flash. I'm still baffled by its behaviour with the mount at '/'.
Peter Hinch
Index to my micropython libraries.

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

Re: Raspberry Pi Pico: how to access the filesystem

Post by Roberthh » Tue Jan 26, 2021 4:57 pm

I have rshell 0.0.26. Starting with:
rshell -p /dev/ttyACM0
detects the board. As with all boards with / as root it gets a little bit confused considering files as directories.

Code: Select all

robert@hh3:~$ rshell -p /dev/ttyACM0 
Using buffer-size of 32
Connecting to /dev/ttyACM0 (buffer-size 32)...
Trying to connect to REPL  connected
Testing if ubinascii.unhexlify exists ... Y
Retrieving root directories ... /main.py/ /runtest.py/
Setting time ... Jan 26, 2021 17:57:00
Evaluating board_name ... pyboard
Retrieving time epoch ... Jan 01, 1970
Welcome to rshell. Use Control-D (or the exit command) to exit rshell.
Edit: But I mus say it does not work well. The edit command for instance failed on writing back.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

OK, I need an rshell lesson...

Post by pythoncoder » Tue Jan 26, 2021 5:14 pm

Ah, fixed it. I had a trailing slash in my _boot.py mount command. Everything now works as I'd expect.

With the stock firmware, at the rshell prompt if I issued:

Code: Select all

ls /
I got a listing of the root directory on my PC (rather as I'd expect). It's not obvious to me how to use rshell in that state. How would I copy a file from the current directory to the target? How would I list the target's contents?
Peter Hinch
Index to my micropython libraries.

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

Re: Raspberry Pi Pico: how to access the filesystem

Post by Roberthh » Tue Jan 26, 2021 5:19 pm

The target appears under the directory /pyboard

Post Reply