软件环境:Win7,Keil MDK 4.72a, IAR EWARM 7.2, GCC 4.2,Python 2.7 ,SCons 2.3.2
硬件环境:Armfly STM32F103ZE-EK v3.0开发板
参考文章:RT-Thread编程指南
由于SD卡可插拔的便捷性,使得其在嵌入式中的应用中广泛使用。
【1】修改底层驱动接口
(1)打开Armfly STM32F103ZE-EK v3.0开发板找到SD卡硬件接口部分,如下图:
从硬件接口可以看到,SD是SDIO的接口类型,amobbs论坛上给出官方范例及此驱动的区别,我们这个开发分支driver目录下就有现成的sdcard.c文件,sdio接口类型。下面打开文件sdcard.c,定位到2901行,对应上面硬件接口确认和下面代码一致:
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)然后定位到3168和3172行,这里PC7为插卡检测,所以要修改过来:
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)然后定位到int rt_hw_sdcard_init(void)末尾处3237行GPIO_Pin_6改成GPIO_Pin_7,然后定位3241行附近,打开INIT_DEVICE_EXPORT宏前面注释,修改如下:
__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);
修改完成后保存。
(4)打开application.c文件,定位到114行附近,加入下面代码:
/* mount SD Card as /dev/sd directory */ if (dfs_mount("sd0", "/dev", "elm", 0, 0) == 0) { rt_kprintf("sd0 mount to /dev.\n"); } else rt_kprintf("sd0 mount to /dev failed.\n");
(5)打开drivers目录下Sconscript文件,定位到21行,修改如下:
# 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‘]
然后保存并重新编译,如果SD卡没有问题,在终端中看到如下信息:
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 />
sd卡在系统中已经成功注册。但还不挂载成功,因为不能识别文件系统,需要重新格式化:
finsh />mkfs("elm","sd0")
0, 0x00000000
finsh />
格式化成功。
然后复位一次,终端显示如下:
\ | /
- 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 />
可以看到sd卡的容量信息,并且已经成功挂载的/dev。