SDIO Card Detection
This sample code demonstrates the SD card detection function. Simulate SD card insertion and removal through GPIO.
Requirements
For hardware requirements, please refer to the Requirements.
Wiring
Configurations
The following macros can be configured to adjust test parameters for SD card operations, including block numbers, the starting address, and card detect pin.
#define BLOCK_NUM 1
#define OPER_SD_CARD_ADDR 0x8000
#define SD_CARD_DETECT_PIN P0_3
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 };
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_card_detect_demo();
Building and Downloading
For building and downloading, please refer to the Building and Downloading.
Experimental Verification
Press the Reset button on the EVB, GPIO low edge interrupt function has been enabled.
Connect P0_3 to GND on EVB to simulate card insertion, trigger gpio interrupt and print log. Then change trigger condition, initialize and read the card.
gpio_isr_cb: card insert!
Remove P0_3 from GND to simulate card removal, trigger gpio interrupt and print log. Then change trigger condition, and deinitialize the card.
gpio_isr_cb: card remove!
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_card_detect_demo.c
.
Initialization
The initialization flow for SD card can refer to Initialization.
Functional Implementation
SDIO Card Detection
The card detection is realized through the gpio interrupt function, the falling edge is used to simulate the card insertion, and the rising edge is used to simulate the card removal.
static void gpio_isr_cb(uint32_t context) { uint8_t event = 0; uint8_t pin_index = (uint32_t)context; T_GPIO_LEVEL gpio_level = hal_gpio_get_input_level(pin_index); if (gpio_level == GPIO_LEVEL_LOW) { event = SD_EVENT_CARD_INSERT; IO_PRINT_INFO0("gpio_isr_cb: card insert!"); hal_gpio_irq_change_polarity(pin_index, GPIO_IRQ_ACTIVE_HIGH); if (os_msg_send(sd_evt_queue_handle, &event, 0) == false) { IO_PRINT_ERROR0("gpio_isr_cb: send card insert msg fail"); } } else { event = SD_EVENT_CARD_REMOVE; IO_PRINT_INFO0("gpio_isr_cb: card remove!"); hal_gpio_irq_change_polarity(pin_index, GPIO_IRQ_ACTIVE_LOW); if (os_msg_send(sd_evt_queue_handle, &event, 0) == false) { IO_PRINT_ERROR0("gpio_isr_cb: send card remove msg fail"); } } }
After the SD card is inserted, it is initialized using
sd_init()
and read usingsd_read()
. Once the card is removed, it is deinitialized by callingsd_deinit()
.static void sd_cd_test_task(void *param) { uint8_t event; while (true) { if (os_msg_recv(sd_evt_queue_handle, &event, 0xFFFFFFFF) == true) { if (event == SD_EVENT_CARD_INSERT) { sd_init(SDIO_CD_ID); sd_card_read(); } else if (event == SD_EVENT_CARD_REMOVE) { sd_deinit(SDIO_CD_ID); } } } }