5inch_P4_IDF_08_SD_Card_Reading: ESP32-P4 SD Card File Read/Write¶
1. Course Introduction¶
This lesson uses the bsp_sd component to initialize the SD card and performs a file read/write verification through a FreeRTOS task. app_main first calls sd_init() to mount the SD card, then creates sd_task pinned to CPU1 with a stack size of 4096 and priority 5; the task writes to /sdcard/helloatao.txt, waits for the SD card to complete its internal operations, then reads and prints the file contents.
2. Learning Objectives¶
- Understand the role of
sd_init()in SD card mounting and underlying interface initialization. - Be able to use
write_string_file()to write text andread_string_file()to read it back. - Understand how FreeRTOS tasks, delays, and
vTaskDelete(NULL)work together in a one-shot test. - Be able to determine whether initialization, writing, or reading failed based on the
MAIN_ERRORlog.
3. Prerequisites¶
- Compatible development board: CrowPanel Advanced 5-inch ESP32-P4 HMI AI Display Development Board.
- The SD card must be formatted with a FAT file system; otherwise it may not be recognized correctly, and files on the card may fail to read.
- Software: VS Code, ESP-IDF Extension (ESP-IDF v5.5.4 and above).
- Project dependencies: keep the
main/main.cfile, theperipheral/bsp_sdcomponent, and thesdmmc_cmdandesp_vfs_fatmanaged components. - Configuration: target chip
esp32p4.
Code download link:
4. Software Operation Steps¶
In VS Code, open the ESP-IDF Extension panel, click Open ESP-IDF Project, and select the Lesson08-SD_Card_File_Reading folder.
You can also drag this project folder directly into VS Code.
First, select the runtime environment ESP-IDF v5.5.4, set the flashing method to UART, then select the serial port that corresponds to the actual development board. Next, in the ESP-IDF Extension panel, click Set Espressif Device Target and select esp32p4. After setup, the status bar should display ESP-IDF v5.5.4, UART, the required COM port, and ESP32-P4.
Click SDK Configuration Editor in the VS Code bottom status bar or the ESP-IDF extension panel, and wait for the configuration page to fully load before modifying any parameters. If the page is still loading, do not run Build immediately.
In the search box, type flash, and ensure Flash SPI mode: QIO; Flash Sampling Mode: STR Mode; Flash SPI speed: 80 MHz; Flash size: 16 MB. These parameters should match the on-board Flash of the Advance-P4.
If you need to read or write longer file names, enable long file name support for the FAT file system in the SDK Configuration Editor, and select the long file name working buffer method as required by the project; otherwise, read/write will fail when file names are too long.
Click Save in the top-right corner to save the Flash and FAT file system configuration, then proceed with compilation.
Click Full Clean to clear the cache left by the previous compilation. Perform this operation after the first compilation, when switching project configurations, or after modifying SDK parameters, so that old configurations do not affect the new compilation result.
Click Build to compile the project.
Confirm the development board is connected to the computer via USB, click Select Port to Use to select the serial port, then click Flash to flash the firmware.
After flashing, click Monitor to open the serial monitor; you should see card information and read/write logs. Press Ctrl + ] to exit the monitor.
After testing, you can insert the SD card into a computer and confirm that hello.txt appears in the root directory with the content hello world!.
Finally, you can use the one-click operation button in the ESP-IDF status bar to sequentially perform compilation, flashing, and opening the serial monitor. Use this only after the project configuration, serial port, and code have all been confirmed correct; if you need to locate problems, still follow the steps above one by one.
5. Hardware Operation Steps¶
With the power off, insert the MicroSD card into the SD card slot of the development board (or an adapter module). Pay attention to the card orientation—the gold contacts face down. 
Use a USB data cable to connect the ESP32-P4 development board to the computer; the board's power indicator lights up.
After flashing is complete and the board is reset, observe whether the serial port prints card information (Name, Type, Size, Speed).
Observe whether the serial port prints File written and Read a line from file: 'hello world!' in sequence, to confirm consistent read/write.
6. Key Code Explanation¶
const char *file_hello = SD_MOUNT_POINT "/helloatao.txt";
char *data = "hello world!peach";
err = write_string_file(file_hello, data);
vTaskDelay(200 / portTICK_PERIOD_MS);
err = read_string_file(file_hello);
SD_MOUNT_POINT places the test file on the mounted card. The task prints the card information, writes hello world!peach, waits 200 ms, and reads the same file back. A write failure immediately starts the next loop iteration; a read failure is logged.
Init() calls sd_init() and enters init_fail() if mounting fails. The test task is pinned to CPU1 with a 4096-byte stack. After one successful read/write cycle and a one-second wait, it deletes itself, making this a one-shot storage test.
7. Experimental Observations¶
After flashing and inserting the SD card, the serial port first outputs the SD card capacity, type, and other information, then writes helloatao.txt and reads back hello world!peach. After testing completes, SD card test completed appears and the task exits; if no card is inserted or file system initialization fails, the program continuously outputs SD card initialization errors.













