Software Environment: Win7,keil MDK 4.72a, IAR ewarm 7.2, GCC 4.2,python 2.7, SCons 2.3.2
Hardware environment: Armfly Stm32f103ze-ek v3.0 Development Board
Reference article: Rt-thread Programming Guide
Due to the convenience of SD card pluggable, it is widely used in embedded applications.
"1" Modify the underlying driver interface
(1) Open the Armfly Stm32f103ze-ek v3.0 Development Board to find the SD Card hardware interface section, such as:
From the hardware interface can be seen, SD is Sdio interface type, Amobbs forum on the official example and the difference between this driver, our Development Branch driver directory has ready-made SDCARD.C file, Sdio interface type. Open the file sdcard.c below, navigate to line 2901, corresponding to the above hardware interface confirmation and the following code is consistent:
static void Gpio_configuration (void) { gpio_inittypedef gpio_initstructure; /* GPIOC and Gpiod periph clock enable * /rcc_apb2periphclockcmd (RCC_APB2PERIPH_GPIOC | Rcc_apb2periph_gpiod, ENABLE); /* Configure pc.08, pc.09, pc.10, pc.11, pc.12 pin:d0, D1, D2, D3, CLK pin */ Gpio_initstructure.gpio_pin = Gpio_pin_ 8 | Gpio_pin_9 | gpio_pin_10 | Gpio_pin_11 | Gpio_pin_12; Gpio_initstructure.gpio_speed = Gpio_speed_50mhz; Gpio_initstructure.gpio_mode = gpio_mode_af_pp; Gpio_init (GPIOC, &gpio_initstructure); /* Configure pd.02 CMD line */ gpio_initstructure.gpio_pin = gpio_pin_2; Gpio_init (Gpiod, &gpio_initstructure);}
(2) then locate to 3168 and 3172 lines, here PC7 for card detection, so to modify it come:
int Rt_hw_sdcard_init (void) {/ * SDIO POWER */ gpio_inittypedef gpio_initstructure; Rcc_apb2periphclockcmd (rcc_apb2periph_gpioc,enable); Gpio_initstructure.gpio_pin = gpio_pin_7; Gpio_initstructure.gpio_mode = gpio_mode_out_pp; Gpio_initstructure.gpio_speed = Gpio_speed_2mhz; Gpio_init (gpioc,&gpio_initstructure); Gpio_resetbits (gpioc,gpio_pin_7); /* SD card Power up * //Delay same time for SD card power up
(3) then locate the int rt_hw_sdcard_init (void) at the end of 3237 lines gpio_pin_6 change to gpio_pin_7, and then locate 3241 lines near, open the Init_device_export macro in front of the comments, modified as follows:
__return: rt_kprintf ("SDcard init failed\n"); Gpio_setbits (gpioc,gpio_pin_7); /* SD card Power down */ return 0;} Init_device_export (Rt_hw_sdcard_init);
Save after the modification is complete.
(4) Open the Application.c file, locate it near line 114, and add the following code:
/* Mount SD Card as/dev/sd directory */ if (Dfs_mount ("sd0", "/dev", "Elm", 0, 0) = = 0) { rt_kprintf ("Sd0 mou NT To/dev.\n "); } else rt_kprintf ("sd0 Mount To/dev failed.\n");
(5) Open the drivers directory under the Sconscript file, navigate to line 21, modify the following:
# Add DFS drvers.
If Getdepend (' Rt_using_dfs '):
src + = [' rt_spi_device.c ', ' rt_stm32f10x_spi.c ', ' spi_flash_w25qxx.c ', ' fs_test.c ',' SDCARD.C ']
Then save and recompile, if there is no problem with the SD card, see the following information in the terminal:
SD0 Mount To/dev failed.
Finsh/>list_device ()
Device type
-------- ----------
Flash0 Block Device
Spi12 SPI Device
Spi11 SPI Device
Spi1 SPI Bus
Sd0 Block Device
Uart3 Character Device
Uart2 Character Device
Uart1 Character Device
0, 0x00000000
Finsh/>
The SD card has been successfully registered in the system. However, it does not mount successfully because the file system is not recognized and needs to be reformatted:
Finsh/>mkfs ("Elm", "sd0")
0, 0x00000000
Finsh/>
Formatting succeeded.
Then reset it once and the terminal is displayed as follows:
\ | /
-Rt-thread Operating System
/ | \ 1.2.2 Build Mar 31 2015
2006-2013 Copyright by Rt-thread Team
Found Part[0], begin:219152384, SIZE:1.690GB
W25Q64BV or W25Q64CV detection
Finsh/>flash0 mount to/.
sd0 Mount To/dev.
Finsh/>
You can see the capacity information for the SD card and the/dev that has been successfully mounted.
Rt-thread Study notes (ix)---Open the Elm FatFS file system based on the SD card