OpenEdv-开源电子网

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

折腾文件系统两天了,也看了很多例程,但是最基本的读取文件都没有成功

[复制链接]

20

主题

120

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
230
金钱
230
注册时间
2015-6-4
在线时间
5 小时
发表于 2015-6-28 14:43:16 | 显示全部楼层 |阅读模式
5金钱
在SD卡下面建了一个my_data的TXT文件,文件内容是hello!但是始终无法读出来!
下面是代码,希望大神们帮忙看看,哪里出现问题了!
diskio.c
#include "diskio.h" /* FatFs lower layer API */
#include "sdio_sdcard.h" /* Example: Header file of existing MMC/SDC contorl module */

/* Definitions of physical drive number for each drive */
#define ATA 0 /* Example: Map ATA harddisk to physical drive 0 */
#define MMC 1 /* Example: Map MMC/SD card to physical drive 1 */
#define USB 2 /* Example: Map USB MSD to physical drive 2 */


/*-----------------------------------------------------------------------*/
/* Get Drive Status                                                      */
/*-----------------------------------------------------------------------*/

DSTATUS disk_status (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
   
return 0;
}



/*-----------------------------------------------------------------------*/
/* Inidialize a Drive                                                    */
/*-----------------------------------------------------------------------*/

DSTATUS disk_initialize (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
    u8 state;
state=SD_Init();
if(state==0)
 return RES_OK;
    else
      return 1;
}



/*-----------------------------------------------------------------------*/
/* Read Sector(s)                                                        */
/*-----------------------------------------------------------------------*/

DRESULT disk_read (
BYTE pdrv, /* Physical drive nmuber to identify the drive */
BYTE *buff, /* Data buffer to store read data */
DWORD sector, /* Sector address in LBA */
UINT count /* Number of sectors to read */
)
{
u8 res;    

if (count == 1) 
{
res = SD_ReadBlock(buff, sector ,512);

else
{
res = SD_ReadMultiBlocks(buff, sector ,512, count);
}

if (res == SD_OK) 
{
return RES_OK;

return RES_ERROR;
}



/*-----------------------------------------------------------------------*/
/* Write Sector(s)                                                       */
/*-----------------------------------------------------------------------*/

#if _USE_WRITE
DRESULT disk_write (
BYTE pdrv, /* Physical drive nmuber to identify the drive */
const BYTE *buff, /* Data to be written */
DWORD sector, /* Sector address in LBA */
UINT count /* Number of sectors to write */
)
{
u8 res;
    
if (count == 1) 
{
res = SD_WriteBlock((uint8_t *)buff, sector ,512);

else
{
res = SD_WriteMultiBlocks((uint8_t *)buff, sector  ,512, count);
}
    
   if (res == SD_OK) 
{
return RES_OK;
}
    else    
   return RES_ERROR;
}
#endif


/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                               */
/*-----------------------------------------------------------------------*/

#if _USE_IOCTL
DRESULT disk_ioctl (
BYTE pdrv, /* Physical drive nmuber (0..) */
BYTE cmd, /* Control code */
void *buff /* Buffer to send/receive control data */
)
{   
return RES_OK;
}
#endif
DWORD get_fattime (void)
{
    DWORD date=0;
    return date;
}
main.c
#include "pbdata.h"
#include "ff.h"
#include "diskio.h"
#include "integer.h"
#include "string.h"

void RCC_Configuration(void);
void GPIO_COM_Configuration(void);
void NVIC_Configuration(void);
void USART_Configuration(void);

SD_Error Status=SD_OK;
extern SD_CardInfo SDCardInfo;

int fputc(int ch,FILE *f)
{
USART_SendData(USART1,(u8)ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
return ch;
}
u8 state;

FATFS fs; //逻辑驱动器的工作区(文件系统对象)
FRESULT res;//FATFS函数公共结果代码
FILINFO fileInfo;
UINT br; //文件读/写字节计数
u16 i;
DIR dir;
FIL file;//文件对象
char data[512];
char buffer[512];

int main(void)
{
 
   RCC_Configuration(); //系统时钟初始化
   GPIO_COM_Configuration();//端口初始化
   USART_Configuration();
   NVIC_Configuration();
   
   Status=SD_Init();

   if(Status==SD_OK)
   {
    printf("SD卡初始化成功!\r\n");
   }
   else
   {
    printf("SD卡初始化失败!\r\n");
   }

printf("制造商ID号: %d\r\n",SDCardInfo.SD_cid.ManufacturerID);
printf("卡的类型: %d\r\n",SDCardInfo.CardType);
printf("卡的容量: %d\r\n",SDCardInfo.CardCapacity);
printf("块的大小: %d\r\n",SDCardInfo.CardBlockSize);

   if(disk_initialize(0)==0)
    printf("SD卡准备就绪!\r\n");
   //为逻辑驱动器注册工作区
   res = f_mount(&fs,0,1);
   res = f_open(&file, "my_data.txt", FA_OPEN_EXISTING | FA_READ);
    if(res!=FR_OK)
    {
while(1);
    }
   for (;;) 
    { 
     res = f_read(&file, buffer, sizeof(buffer), &br); 
     if (res || br == 0) break;   // error or eof 
     for( i = 0; i < br; ++i ) 
     printf("%c",buffer);
     printf("\n");
   }

    f_close(&file);
    f_mount(&fs,0,0);
   printf("操作完成!\r\n");
    
   while(1)
   {
    GPIO_SetBits(GPIOB,GPIO_Pin_5);
    delay_ms(1000);
    GPIO_ResetBits(GPIOB,GPIO_Pin_5);
    delay_ms(1000);
   }       
}
大神们,帮帮忙吧!谢谢1

最佳答案

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

OK!解决啦!原来是挂载SD卡是出错啦!    if( f_mount(&fs,"0:",1)==FR_OK)    printf(" f_mount OK\r\n"); 改为这样就OK啦!
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

20

主题

120

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
230
金钱
230
注册时间
2015-6-4
在线时间
5 小时
 楼主| 发表于 2015-6-28 14:43:17 | 显示全部楼层
OK!解决啦!原来是挂载SD卡是出错啦!
   if( f_mount(&fs,"0:",1)==FR_OK)
   printf(" f_mount OK\r\n");
改为这样就OK啦!
回复

使用道具 举报

20

主题

120

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
230
金钱
230
注册时间
2015-6-4
在线时间
5 小时
 楼主| 发表于 2015-6-28 15:14:34 | 显示全部楼层
用的是SDIO访问SD卡!
回复

使用道具 举报

20

主题

120

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
230
金钱
230
注册时间
2015-6-4
在线时间
5 小时
 楼主| 发表于 2015-6-28 15:15:00 | 显示全部楼层
求大神们帮帮忙!!!谢谢啦!!!
回复

使用道具 举报

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

本版积分规则



关闭

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

正点原子公众号

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

GMT+8, 2025-6-20 19:46

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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