OpenEdv-开源电子网

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

SD卡读取一个扇区,512byte,读出来的数据超过了512byte,请问是什么原因造成的?

[复制链接]

12

主题

30

帖子

0

精华

初级会员

Rank: 2

积分
107
金钱
107
注册时间
2013-9-4
在线时间
8 小时
发表于 2015-4-25 21:33:31 | 显示全部楼层 |阅读模式
5金钱
 res=f_read(fftemp,tmp,512,(UINT *)&bread);
SD卡读一个扇区512,发现读出来的数据会超过,怎么回事?



watch1是tmp的数据。 

[mw_shl_code=c,true]/*-----------------------------------------------------------------------*/ /* Read File */ /*-----------------------------------------------------------------------*/ FRESULT f_read ( FIL *fp, /* Pointer to the file object */ void *buff, /* Pointer to data buffer */ UINT btr, /* Number of bytes to read */ UINT *br /* Pointer to number of bytes read */ ) { FRESULT res; DWORD clst, sect, remain; UINT rcnt, cc; BYTE csect, *rbuff = buff; *br = 0; /* Initialize byte counter */ res = validate(fp->fs, fp->id); /* Check validity */ if (res != FR_OK) LEAVE_FF(fp->fs, res); if (fp->flag & FA__ERROR) /* Aborted file? */ LEAVE_FF(fp->fs, FR_INT_ERR); if (!(fp->flag & FA_READ)) /* Check access mode */ LEAVE_FF(fp->fs, FR_DENIED); remain = fp->fsize - fp->fptr; if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */ for ( ; btr; /* Repeat until all data read */ rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) { if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */ csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1)); /* Sector offset in the cluster */ if (!csect) { /* On the cluster boundary? */ if (fp->fptr == 0) { /* On the top of the file? */ clst = fp->sclust; /* Follow from the origin */ } else { /* Middle or end of the file */ #if _USE_FASTSEEK if (fp->cltbl) clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */ else #endif clst = get_fat(fp->fs, fp->clust); /* Follow cluster chain on the FAT */ } if (clst < 2) ABORT(fp->fs, FR_INT_ERR); if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR); fp->clust = clst; /* Update current cluster */ } sect = clust2sect(fp->fs, fp->clust); /* Get current sector */ if (!sect) ABORT(fp->fs, FR_INT_ERR); sect += csect; cc = btr / SS(fp->fs); /* When remaining bytes >= sector size, */ if (cc) { /* Read maximum contiguous sectors directly */ if (csect + cc > fp->fs->csize) /* Clip at cluster boundary */ cc = fp->fs->csize - csect; if (disk_read(fp->fs->drv, rbuff, sect, (BYTE)cc) != RES_OK) ABORT(fp->fs, FR_DISK_ERR); #if !_FS_READONLY && _FS_MINIMIZE <= 2 /* Replace one of the read sectors with cached data if it contains a dirty sector */ #if _FS_TINY if (fp->fs->wflag && fp->fs->winsect - sect < cc) mem_cpy(rbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), fp->fs->win, SS(fp->fs)); #else if ((fp->flag & FA__DIRTY) && fp->dsect - sect < cc) mem_cpy(rbuff + ((fp->dsect - sect) * SS(fp->fs)), fp->buf, SS(fp->fs)); #endif #endif rcnt = SS(fp->fs) * cc; /* Number of bytes transferred */ continue; } #if !_FS_TINY if (fp->dsect != sect) { /* Load data sector if not in cache */ #if !_FS_READONLY if (fp->flag & FA__DIRTY) { /* Write-back dirty sector cache */ if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK) ABORT(fp->fs, FR_DISK_ERR); fp->flag &= ~FA__DIRTY; } #endif if (disk_read(fp->fs->drv, fp->buf, sect, 1) != RES_OK) /* Fill sector cache */ ABORT(fp->fs, FR_DISK_ERR); } #endif fp->dsect = sect; } rcnt = SS(fp->fs) - (fp->fptr % SS(fp->fs)); /* Get partial sector data from sector buffer */ if (rcnt > btr) rcnt = btr; #if _FS_TINY if (move_window(fp->fs, fp->dsect)) /* Move sector window */ ABORT(fp->fs, FR_DISK_ERR); mem_cpy(rbuff, &fp->fs->win[fp->fptr % SS(fp->fs)], rcnt); /* Pick partial sector */ #else mem_cpy(rbuff, &fp->buf[fp->fptr % SS(fp->fs)], rcnt); /* Pick partial sector */ #endif } LEAVE_FF(fp->fs, FR_OK); }[/mw_shl_code]

最佳答案

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

超过512的数据,做不得准。
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165371
金钱
165371
注册时间
2010-12-1
在线时间
2110 小时
发表于 2015-4-25 21:33:32 | 显示全部楼层
超过512的数据,做不得准。
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复

使用道具 举报

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

本版积分规则



关闭

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

正点原子公众号

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

GMT+8, 2025-2-24 12:09

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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