SDIO Filesystem Write Read

This sample code demonstrates SD card read and write function by filesystem. FatFS_DirectoyDemo() demonstrates how to write the data under the specified directory file of the SD card. FatFS_Demo() demonstrates how to write and read data directly to the file of SD card. The default implementation is FatFS_DirectoyDemo(), which can be modified to implement FatFS_Demo().

Requirements

For hardware requirements, please refer to the Requirements.

Wiring

The wiring between SD card and EVB can refer to Wiring.

Configurations

  1. The following T_SD_CONFIG structure can be configured to modify card type, group pins, bus width, clock and power pin releated.

    static const T_SD_CONFIG    sd_card_cfg =
    {
       .sd_if_type = SD_IF_SD_CARD,
       .sdh_group = GROUP_0,
       .sdh_bus_width = SD_BUS_WIDTH_1B,
       .sd_bus_clk_sel = SD_BUS_CLK_20M,
       .sd_power_en = 1,
       .sd_power_high_active = 1,
       .sd_power_pin = P5_6
    };
    
  2. The entry function is as follows, call this function in main() to run this sample code. For more details, please refer to the Initialization.

    SD_DemoCode();
    

Building and Downloading

For building and downloading, please refer to the Building and Downloading.

Experimental Verification

  1. FatFS_DirectoyDemo():

    1. Press the Reset button on the EVB, observe the SD card initialization status in the Debug Analyzer. If the card is successfully recognized, the following string will be printed.

      SD_InitCard: status: SD_OK
      
    2. Mount the file system, create directories and files under the specified path (0:/BBPro/test.txt).

    3. Open the specified directory, traverse all files in the directory, open file and write the specified data, and close the file when completed. If write success, the following string will be printed.

      FatFS_DirectoyDemo: write OK
      FatFS_DirectoyDemo: exit, res 0
      
    4. User can check that the files in the BBpro directory of the SD card are the written data. The expected characters to be displayed are: the first 1021 characters are 0 to 8 in a loop, and the last 3 characters are e, n, and d.

  2. FatFS_Demo():

    1. Press the Reset button on the EVB, observe the SD card initialization status in the Debug Analyzer. If the card is successfully recognized, the following string will be printed.

      SD_InitCard: status: SD_OK
      
    2. Mount the file system, create and open a file (Data.txt). Then write the specified data (loop 0~8 and end) and close the file.

    3. Open and read the data in the file (Data.txt), store it in buffer and close file. If read success, print log of the read length and data. The expected data are: the first 1021 characters are 0x30 to 0x38 in a loop, and the last 3 characters are 0x65, 0x6E, and 0x64.

      FatFS_Demo: read_len 1024
      sd_print_binary_data: buf
      
    4. User can check that ‘Data.txt’ in the SD card is the written data. The expected characters to be displayed are: the first 1021 characters are 0 to 8 in a loop, and the last 3 characters are e, n, and d.

Code Overview

Source Code Directory

  • For project directory, please refer to Source Code Directory.

  • Source code directory: sdk\src\sample\io_demo\sdio\demo\sdio_fs_demo.c .

Initialization

  1. The initialization flow for SD card can refer to Initialization.

  2. Call f_mount to to initialize and attach the file system object to the logical drive, preparing for file operations in the FatFS library.

    uint32_t res = 0;
    const TCHAR *driver_num = (const TCHAR *)_T("0:");
    
    /* Create workspace */
    res = f_mount(&fs, driver_num, 1);
    if (res != 0)
    {
       IO_PRINT_ERROR1("FatFS_Demo: f_mount fail, res %d", res);
       return;
    }
    

Functional Implementation

SDIO Filesystem Write Read

  1. Call f_open to create and open file on SD card.

  2. Call f_write to write to the file and f_read to read from the file on SD card.

  3. Call f_close to close the file.

    /* Open file */
    res = f_open(&fdst, (const TCHAR *)_T("Data.txt"), FA_CREATE_ALWAYS | FA_WRITE);
    if (res != FR_OK)
    {
       //Open file failure, add error handle code here
       f_close(&fdst);
       return ;
    }
    
    /* File operation */
    for (uint32_t i = 0; i < 1024; i++)
    {
       buffer[i] = i % 0x09 + 0x30;
    }
    buffer[1021] = 0x65; //e
    buffer[1022] = 0x6E; //n
    buffer[1023] = 0x64; //d
    f_write(&fdst, buffer, 1024, &a);
    
    memset(buffer, 0, 1024);
    f_read(&fdst, buffer, 1024, &a);
    
    /* Close file */
    f_close(&fdst);