Take your micropython game with the PSoC6 from Infineon to the next level by storing data on an SD card! In this section, we'll walk you through the process of mounting an SD card using two popular filesystem options: FAT and LFS2. You'll learn how to create files and store data on your SD card, giving your project a whole new dimension.
Crash Course FAT and LFS2 FilesystemNew to the world of FAT and LFS2 filesystems? No worries, we've got you covered!
The FAT (File Allocation Table) filesystem is a widely used, simple, and compatible file system found on various storage devices like SD cards, USB drives, and older hard drives. However, it has limitations on file size and partition size, and may not be the most efficient in terms of storage utilization. In contrast, LFS2 (Log-structured File System 2) is a modern, innovative file system designed to overcome these limitations, using a log-structured approach to improve performance, extend storage lifespan, and minimize write amplification. LFS2 is particularly well-suited for solid-state drives (SSDs) and other flash-based storage devices.
Hardware SetupAll you must do is carefully insert the MicroSD card into the MicroSD card slot on your PSoC board, as shown in the image below.
Your SD card is interfaced with the PSoC6 board like this:
- Slot: any integer.
- Width: 4 (indicating a 4-bit wide data bus)
- CD (Card Detect): Pin P13_5
- CMD (Command Line): Pin P12_4
- CLK (Clock Line): Pin P12_5
- DAT0-DAT3 (Data Lines): Pins P13_0 to P13_3
New to MicroPython on PSoC? Make sure you're set up for success! Follow the link to check out the getting started guide!
SD Card with LFS2 filesystemNow your ready to start! The first example shows how to handle a SD Card with LFS2 filesystem.
Steps to Mount and Use SD Card with LFS2 filesystem:
1. Import necessary modules:
import machine
import os #operating system
2. Create an SD Card object:
Initialize the SD card to represent the SDHC block on your microcontroller.
bdev = machine.SDCard(slot=0, width=4, cd="P13_5", cmd="P12_4", clk="P12_5", dat0="P13_0", dat1="P13_1", dat2="P13_2", dat3="P13_3")
3. Mount or in case of an OS error, format the SD card before mounting:
try:
vfs = os.VfsLfs2(bdev, progsize=512, readsize=512)
os.mount(vfs, "/SDCardLfs2")
except OSError:
os.VfsLfs2.mkfs(bdev, progsize=512, readsize=512)
vfs = os.VfsLfs2(bdev, progsize=512, readsize=512)
os.mount(vfs, "/SDCardLfs2")
4. Create a text file and write some data into it:
TEST_STRING = "This is a test string."
with open("/SDCardLfs2/test_sd_lfs2.txt", "w") as f:
f.write(TEST_STRING)
5. Open the file and read the data:
with open("/SDCardLfs2/test_sd_lfs2.txt", "r") as f:
read_data = f.read()
print(read_data)
6. Deinitialize the SD card:
bdev.deinit()
SD Card with FAT filesystemSteps to Mount and Use SD Card with FAT filesystem:
1. Import necessary modules:
import machine
import os
2. Initialize the SD card:
bdev = machine.SDCard(slot=0, width=4, cd="P13_5", cmd="P12_4", clk="P12_5", dat0="P13_0", dat1="P13_1", dat2="P13_2", dat3="P13_3")
3. Mount or in case of an OS error, format the SD card before mounting:
try:
vfs = os.VfsFat(bdev)
os.mount(vfs, "/SDCardFat")
except OSError:
os.VfsFat.mkfs(bdev)
vfs = os.VfsFat(bdev)
os.mount(vfs, "/SDCardFat")
4. Create a text file and write some data into it:
with open("/SDCardFat/test_sd_fat.txt", "w") as f:
f.write(TEST_STRING)
5. Open the file and read the data:
with open("/SDCardFat/test_sd_fat.txt", "r") as f:
read_data = f.read()
print(read_data)
6. Deinitialize the SD card:
bdev.deinit()
By following these steps, you can easily mount an SD card using either the FAT filesystem or the LFS2 filesystem, create files, and read from them, enhancing the data storage capabilities of your microcontroller project.
FarewellWith the understanding gained from this guide, you are now equipped to embark on your next innovative project. Don't forget to refer back to the guidance in this Protip and the "Micropython on PSoC6" Protip for technical support. Happy coding, and here's to bringing many more innovative projects to life!
Comments