Skip to content

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 and read_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_ERROR log.

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.c file, the peripheral/bsp_sd component, and the sdmmc_cmd and esp_vfs_fat managed components.
  • Configuration: target chip esp32p4.

Code download link:

https://github.com/Elecrow-RD/-CrowPanel-Advanced-5inch-ESP32-P4-HMI-AI-Display-800x480-IPS-Touch-Screen/tree/master/example/V1.0

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.

5inch_P4_IDF 02

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.

5inch_P4_IDF 02

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.

5inch_P4_IDF 16

5inch_P4_IDF 03

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.

5inch_P4_IDF 05

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.

5inch_P4_IDF 06

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.

5inch_P4_IDF 06

Click Build to compile the project.

5inch_P4_IDF 07

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.

5inch_P4_IDF 08

After flashing, click Monitor to open the serial monitor; you should see card information and read/write logs. Press Ctrl + ] to exit the monitor.

5inch_P4_IDF 09

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!.

5inch_P4_IDF 10

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.

5inch_P4_IDF 11

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. image-20260729113202006

Use a USB data cable to connect the ESP32-P4 development board to the computer; the board's power indicator lights up.

5inch_P4_IDF 13

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.

5inch_P4_IDF 15

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.

sd_init();
xTaskCreatePinnedToCore(sd_task, "sd_task", 4096, NULL, 5,
                        &sd_task_handle, 1);

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.