OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 2686|回复: 2

STM32F103ZET6+4GSDCard+FATFS

[复制链接]

28

主题

133

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
323
金钱
323
注册时间
2016-11-22
在线时间
66 小时
发表于 2016-11-22 11:31:33 | 显示全部楼层 |阅读模式
1金钱
#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------\r\n\r\n");

res = f_mount(&g_fatfs,"1:",1);//Mount immediately.
printf("mount res = %d.\r\n\r\n",res);

res = f_mkfs("1:",0,1024);//FDISK Format
printf("mkfs res = %d.\r\n\r\n",res);

res = f_mount(&g_fatfs,"1:",1);//Mount immediately.
printf("mount res = %d.\r\n\r\n",res);

res = f_setlabel("1:FATFS YL");
printf("setlabel res = %d.\r\n\r\n",res);

res = f_getlabel("1:",str,0);
printf("getlabel res = %d.\r\n\r\n",res);
printf("label:%s\r\n\r\n",str);

disk_ioctl(1,GET_SECTOR_COUNT,&count);
printf("sector count:%luMByte\r\n\r\n",((count*512)/1024)/1024);


res = f_open(&fil,"1:message.txt",FA_CREATE_ALWAYS|FA_WRITE);
printf("open res = %d.\r\n\r\n",res);

res = f_write(&fil,"hello world\r\nestablish in 2016.11.12",36,&bw);
printf("write res = %d.\r\n\r\n",res);

res = f_close(&fil);
printf("close res = %d.\r\n\r\n",res);

res = f_open(&fil,"1:message.txt",FA_OPEN_EXISTING|FA_READ);
printf("open1 res = %d.\r\n\r\n",res);

res = f_read (&fil,buf,36,&bw);
printf("read res = %d.\r\n\r\n",res);

res = f_close(&fil);
printf("close1 res = %d.\r\n\r\n",res);

printf("message.tex:%s\r\n\r\n",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协议的卡,这是怎么回事呢。

最佳答案

查看完整内容[请看2#楼]

R0.12这个版本的文件系统内核有点问题,我换到R0.12a就行了。还是感谢原子的支持。
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

28

主题

133

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
323
金钱
323
注册时间
2016-11-22
在线时间
66 小时
 楼主| 发表于 2016-11-22 11:31:34 | 显示全部楼层
正点原子 发表于 2016-11-27 14:38
直接电脑格式化了,再插开发板试试

R0.12这个版本的文件系统内核有点问题,我换到R0.12a就行了。还是感谢原子的支持。
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165524
金钱
165524
注册时间
2010-12-1
在线时间
2116 小时
发表于 2016-11-27 14:38:32 | 显示全部楼层
直接电脑格式化了,再插开发板试试
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-5-25 21:03

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表