Flash File System Using FAT
In this page, we will learn how to use the FAT file system on the flash memory with FAT::Flash class.
Link and Include
Add the following lines to your CMakeLists.txt.
target_link_libraries(your-project jxglib_FAT_Flash)
jxglib_configure_FAT(your-project FF_VOLUMES 1)
Modify the number of FF_VOLUMES according to the number of FAT volumes you want to use.
Add the following line to your source file.
Declaring a File System
Creating a FAT::Flash instance declares that the specified region of the flash memory is reserved for the FAT file system.
driveName: A string name for the drive, can contain any charactersaddrXIP: The starting address of the flash memory region for the FAT file systembytesXIP: The number of bytes to reserve from the end of the flash memory for the FAT file system
The construction below allocates a region from the end of the flash memory.
driveName: A string name for the drive, can contain any charactersbytesXIP: The number of bytes to reserve from the end of the flash memory for the FAT file system
Note that the constructor is just a declaration of the drive and does not modify the flash memory. The file system is created only when format command is executed on the shell or FS::Format() is called in the program for the declared drive.
Sample Program
In this sample program, we create a FAT::Flash instance on the flash memory. The instance is assigned a unique drive name and a specific region of the flash memory as follows:
| Drive Name | File System Type | Flash Memory Region | Size |
|---|---|---|---|
| FAT: | FAT | 0x10100000-0x10200000 | 1MB |
Build and Flash the Program
Create a new Pico SDK project named fs-flash-fat.
Create Pico SDK Project
- In VSCode, run
>Raspberry Pi Pico: New Pico Projectin the command palette. -
In the dialog below, select
C/C++.
-
Create a project with the following settings:
- Name ... Enter the project name.
- Board type ... Select your board type.
- Location ... Select the parent directory where the project directory will be created.
- Stdio support ... Leave
Console over USBunchecked when you use LABOPlatform or other USB features because they conflict with each other. You can enable it by editing theCMakeLists.txtfile later. - Code generation options ... Check
Generate C++ code.

Open Existing Pico SDK Project
Open the project folder in VSCode using one of the following methods:
- In a command prompt, change the current directory to the project folder and execute
code .. -
In a Explorer, choose the project folder, push
Alt+Dto focus the address bar, and executecode ..
If the folder is already prepared as a Pico SDK project, just proceed with editing and building the project.
If not, you will see the following message at the bottom right corner of VSCode.

Click Yes and you will see the following window.

Click Import and the project will be prepared as a Pico SDK project.
When the Do you want to import this project as Raspberry Pi Pico project? message disappears before you click Yes, you can reveal it by clicking the icon
at the bottom right corner of VSCode.
Clone the pico-jxglib repository from GitHub so the direcory structure looks like this:
Clone the Repository
Change the current directory to the parent directory where you want to clone the pico-jxglib repository and run the following commands:
$ git clone https://github.com/ypsitau/pico-jxglib.git
$ cd pico-jxglib
$ git submodule update --init --recursive
pico-jxglib is updated almost daily. If you've already cloned it, run the following command in the pico-jxglib directory to get the latest version:
A directory from a Git repository can safely be moved to another location even after cloning.
Add the following lines to the end of CMakeLists.txt:
| CMakeLists.txt | |
|---|---|
Enable UART or USB stdio as described below.
Enable UART and/or USB stdio
Find the following lines in CMakeLists.txt:
You can set the value to 1 to enable stdio or 0 to disable it.
pico_enable_stdio_uart()enables UART stdio that uses GPIO0 (UART0 TX) and GPIO1 (UART0 RX).pico_enable_stdio_usb()enables USB stdio that uses the USB interface. Make sure to disable USB stdio when you use USB features because they conflict with each other.
Edit fs-flash-fat.cpp as follows:
Build and flash the program to the board.
Build and Flash
The simplest way to build and flash the program is to use the UF2 file generated by the build process. Pico board, a USB cable, and a computer are all you need to get started. Here are the steps to build and flash the program:
-
Pressing
F7on VSCode will build the project. If this is the first time you build the project, you will see the dialog shown below. SelectPico Using compilers: ...and the build process will start.
-
After building, you can find the generated UF2 file in the
builddirectory. - Connect your Pico to the computer using a USB cable while holding the BOOTSEL button, and it will appear as a mass storage device. Copy the generated UF2 file to this device to flash it. No need to mind the destination directory, just copy it to the root directory of the device.
If you have a debug probe like this, you can also flash the program using OpenOCD and GDB. This is the recommended method for development, as it allows you to debug the program while running it on the board!
Running the Program
Open a terminal emulator to connect the board.
Setup Tera Term for LABOPlatform
Tera Term is available here.
From the menu bar, select [File] - [New Connection...] to open the dialog below:

pico-jxgLABO or a firmware that links jxglib_LABOPlatform provides two USB serial ports: one for terminal use and the other for applications such as logic analyzers and plotters. Select one of the ports and press Enter key in the terminal. When successfully connected, you will see a prompt in the terminal.
A prompt will appear like this:
FAT is the driver name specified by the FAT::Flash instance in the program. The current directory is now ?, indicating the drive is not formatted.
ls-drive shows the drive information:
Executing format command with the drive name formats the drive:
FAT:?>format fat:
drive fat: formatted in FAT12
FAT:/>ls-drive -r
Drive Format Total Remarks
FAT: FAT12 1048576 FAT::Flash 0x10100000-0x10200000 1024kB
The prompt changes to FAT:/>, indicating the drive is now formatted and ready to use. You can create files and directories as usual: