SDIO Write Read

This sample code demonstrates the read and write functions of SD card. Read the data at the specified address of the SD card, write the specified data to the address, and then read the data at the address for comparison.

Requirements

For hardware requirements, please refer to the Requirements.

Wiring

Take RTL87x3D group1 as an example, connect P4_2 ~ P4_7 on EVB to CLK, CMD, DAT0, DAT1, DAT2 and DAT3 of SD card respectively. And the VCC and GND pins on EVB are connected with the VDD and GND of the SD card. The specific group pins of different ICs are shown in SDIO Group Pins in RTL87x3D and SDIO Group Pins in RTL87x3E and RTL87x3EP.

The hardware connection of SDIO demo code is shown in the figure below.

../../../_images/SDIO_Hardware_Connection_Diagram.png

SDIO Demo Code Hardware Connection Diagram

Configurations

  1. The following macros can be configured to adjust test parameters for SD card operations, including block numbers, and the starting address.

    • #define BLOCK_NUM            1

    • #define OPER_SD_CARD_ADDR    0x8000

  2. 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
    };
    
  3. The entry function is as follows, call this function in main() to run this sample code. For more details, please refer to the Initialization.

    sdio_demo();
    

Building and Downloading

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

Experimental Verification

  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. Read 512 bytes of data from the SD card and prints the initial card data at address 0x8000. If read success, the following string will be printed.

    sd_print_binary_data: buf[0]
    ...
    sd_print_binary_data: buf[4]
    
  3. Write 512 bytes of data to the SD card address 0x8000, the data is 0~0xff + 0~0xff, and will prints the write data.

    sd_print_binary_data: buf[0] 00 01 ... 76 77
    ...
    sd_print_binary_data: buf[4] E0 EE ... FE FF
    
  4. Read 512 bytes data from the card address 0x8000. If read success, prints the data of 0~0xff + 0~0xff.

    sd_print_binary_data: buf[0] 00 01 ... 76 77
    ...
    sd_print_binary_data: buf[4] E0 EE ... FE FF
    

Code Overview

Source Code Directory

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

  • Source code directory: sdk\src\sample\io_demo\sdio\sdcard\sdio_demo.c .

Initialization

SDIO initialization flow is shown in the following figure.

../../../_images/SDIO_Initialization_Flow_Chart.png

SDIO Initialization Flow Chart

The codes below demonstrate the SDIO initialization flow.

  1. Call sd_config_init() to config the SD card initialization information. The initialization parameters for SDIO are configured as follows:

    SDIO Initialization Parameters

    SDIO Hardware Parameters

    Setting in the sd_cfg

    SDIO

    Card Type

    T_SD_CONFIG::sd_if_type

    SD_IF_SD_CARD

    Pin Group

    T_SD_CONFIG::sdh_group

    GROUP_0

    Bus Width

    T_SD_CONFIG::sdh_bus_width

    SD_BUS_WIDTH_1B

    Clock

    T_SD_CONFIG::sd_bus_clk_sel

    SD_BUS_CLK_20M

    Power Pin

    T_SD_CONFIG::sd_power_pin

    P5_6

    Power Pin High Active (high/low active)

    T_SD_CONFIG::sd_power_high_active

    1

    Power Pin Enable

    T_SD_CONFIG::sd_power_en

    1

  2. Call sd_board_init() to config the PAD, PINMUX and pin group selection of SD card.

  3. Call sd_card_init() to initialize the card recognition, and tune delay to increase stability for high-speed data transfer.

Functional Implementation

SDIO Write Read

  1. Call sd_write() to write the prepared data in test_buf to the SD card.

    for (uint32_t i = 0; i < BLOCK_DATA_SIZE; i++)
    {
       test_buf[i] = i & 0xff;
    }
    
    sd_status = sd_write(SD_HOST_ID, OPER_SD_CARD_ADDR, (uint32_t)test_buf, SINGLE_BLOCK_SIZE,
                         BLOCK_NUM);
    
    if (sd_status != 0)
    {
       return ;
    }
    
  2. Call sd_read() to read the data in SD card, then stored in test_buf.

    memset(test_buf, 0, BLOCK_DATA_SIZE);
    
    sd_status = sd_read(SD_HOST_ID, OPER_SD_CARD_ADDR, (uint32_t)test_buf, SINGLE_BLOCK_SIZE,
                         BLOCK_NUM);
    
    if (sd_status != 0)
    {
       return ;
    }