|Raspberry Pi Pico File System & SD Card Reader|
|Written by Mike James & Harry Fairhead|
|Monday, 29 May 2023|
Page 1 of 3
The Raspberry Pi Pico doesn't have any removable storage, but you can use its internal flash memory to read and write files and its easy to add an SD card reader. We show how to do it in MicroPython.
This is new material that extends our recent book:
Programming the Raspberry Pi Pico/W In MicroPython Second Edition
By Harry Fairhead & Mike James
Buy from Amazon.
Also of interest:
The Pico has 2MB of flash memory which is used to store the MicroPython interpreter. The rest of the memory is converted into a file system that you can make use of from within your own MicroPython programs.
The flash memory is divided into a number of partitions that hold the system and data. Unlike some other implementations of MicroPython, only the data partition,1.6MB, is available and it is automatically mounted as the root when the system boots up. You can gain access to this partition using the rp2.Flash() function which returns a Partition object representing the data partition.
Once you have an instance of Partition you can use any of the following methods:
part.readblocks(block_num, buf, offset) part.writeblocks(block_num, buf, offset) part.ioctl(cmd, arg)
These methods implement both the simple and extended the block protocols defined by os.AbstractBlockDev. Only a subset of ioctl commands are implemented for the Pico:
Generally these commands are too low level to work with a partition and instead you want to install a file system so that you can work in terms of files. But if you really want to reinvent the wheel then you can work directly in terms of raw blocks.
The Flash object is set up so that it isolates you from the other partitions and its block numbers start at zero. So to write some data to block zero and read it back we can use:
from rp2 import Flash
First we unmount the file system so that it can't be used. If the partition was left mounted the file system could use the block we are about to use and overwrite it. Next we get the number of blocks and the block size. Before we can write new data to a block we have to erase it using ioctl command 6. After this we can write any number of bytes up to the block size. Reading the data back is just a matter of setting the length of the buffer to specify the number of bytes to read in. If you try this out you will find that we have stored "Hello World":
The bytes beyond "Hello World" are whatever was already stored in the block.
|Last Updated ( Thursday, 08 June 2023 )|