#include "stm32f10x.h"
#include "app_usart_1.h"
#include "app_sd.h"
#include <stdio.h>
#include "ff.h"
#include "diskio.h"
int main(void)
{
static FATFS g_fatfs = {0};
static FIL fil = {0};
char str[24] = {0};
char buf[40] = {0};
int res = 0;
UINT bw = 0;
DWORD count = 0;
app_usart_1_init(38400);
printf("------SD Card Project + SPI Mode------
");
res = f_mount(&g_fatfs,"1:",1);//Mount immediately.
printf("mount res = %d.
",res);
res = f_mkfs("1:",0,1024);//FDISK Format
printf("mkfs res = %d.
",res);
res = f_mount(&g_fatfs,"1:",1);//Mount immediately.
printf("mount res = %d.
",res);
res = f_setlabel("1:FATFS YL");
printf("setlabel res = %d.
",res);
res = f_getlabel("1:",str,0);
printf("getlabel res = %d.
",res);
printf("label:%s
",str);
disk_ioctl(1,GET_SECTOR_COUNT,&count);
printf("sector count:%luMByte
",((count*512)/1024)/1024);
res = f_open(&fil,"1:message.txt",FA_CREATE_ALWAYS|FA_WRITE);
printf("open res = %d.
",res);
res = f_write(&fil,"hello world
establish in 2016.11.12",36,&bw);
printf("write res = %d.
",res);
res = f_close(&fil);
printf("close res = %d.
",res);
res = f_open(&fil,"1:message.txt",FA_OPEN_EXISTING|FA_READ);
printf("open1 res = %d.
",res);
res = f_read (&fil,buf,36,&bw);
printf("read res = %d.
",res);
res = f_close(&fil);
printf("close1 res = %d.
",res);
printf("message.tex:%s
",buf);
while(1);
}
DRESULT disk_ioctl (
BYTE pdrv, /* Physical drive nmuber (0..) */
BYTE cmd, /* Control code */
void *buff /* Buffer to send/receive control data */
)
{
int result = 0;
SD_CardInfo t_sd_cardinfo = {0};
switch (pdrv) {
case MMC :
result = SD_GetCardInfo(&t_sd_cardinfo);
switch(cmd)
{
case CTRL_SYNC:break;
case GET_SECTOR_SIZE:*(DWORD *)buff = 512;break;/* Get sector size(Byte) (needed at _MAX_SS != _MIN_SS) */
case GET_BLOCK_SIZE:*(DWORD *)buff = t_sd_cardinfo.CardBlockSize;break;//512Byte.
case GET_SECTOR_COUNT:*(DWORD *)buff = ((t_sd_cardinfo.CardCapacity*1024)/512);break;
default:result = SD_RESPONSE_FAILURE;break;
}
if(result == SD_RESPONSE_NO_ERROR)
{
return RES_OK;
}
}
return RES_PARERR;
}
SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo)
{
SD_Error status = SD_RESPONSE_FAILURE;
status = SD_GetCSDRegister(&(cardinfo->SD_csd));
status = SD_GetCIDRegister(&(cardinfo->SD_cid));
cardinfo->CardCapacity = (cardinfo->SD_csd.DeviceSize + 1) * 512;//KByte.
cardinfo->CardBlockSize = 512;//Byte.
/*!< Returns the reponse */
return status;
}
我目前出现的问题是,执行f_mount()函数总是提示"FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */",f_mkfs()之后再次执行f_mount()函数还是这个提示。但是我把disk_ioctl()函数下"GET_SECTOR_COUNT"命令的返回值改小一点,就没问题了。我的SD卡是4G容量2.0协议的卡,这是怎么回事呢。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
R0.12这个版本的文件系统内核有点问题,我换到R0.12a就行了。还是感谢原子的支持。
一周热门 更多>