新手上路
- 积分
- 46
- 金钱
- 46
- 注册时间
- 2015-2-3
- 在线时间
- 0 小时
|
5金钱
在正点原子的录音机实验中,往SD卡写录音文件时,出现如下问题:
res=f_write(f_rec,recbuf,512,&bw);//写入文件
返回值res为2,经检测,是在f_write()函数中的红色部分返回了。
FRESULT f_write (
FIL *fp, /* Pointer to the file object */
const void *buff, /* Pointer to the data to be written */
UINT btw, /* Number of bytes to write */
UINT *bw /* Pointer to number of bytes written */
)
{
FRESULT res;
DWORD clst, sect;
UINT wcnt, cc;
const BYTE *wbuff = buff;
*bw = 0; /* Initialize bytes written */
res = validate(fp->fs, fp->id); /* Check validity of the object */
if (res != FR_OK) LEAVE_FF(fp->fs, res);
if (fp->flag & FA__ERROR) /* Check abort flag */
{ printf("\n222222222:%0x\n",fp->flag);
LEAVE_FF(fp->fs, FR_INT_ERR);
}
if (!(fp->flag & FA_WRITE)) /* Check access mode */
{printf("\n\1333331n");
LEAVE_FF(fp->fs, FR_DENIED);
}
if (fp->fsize + btw < fp->fsize) btw = 0; /* File size cannot reach 4GB */
for ( ; btw; /* Repeat until all data transferred */
wbuff += wcnt, fp->fptr += wcnt, *bw += wcnt, btw -= wcnt) {
if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */
if (fp->csect >= fp->fs->csize) { /* On the cluster boundary? */
if (fp->fptr == 0) { /* On the top of the file? */
clst = fp->org_clust; /* Follow from the origin */
if (clst == 0) /* When there is no cluster chain, */
fp->org_clust = clst = create_chain(fp->fs, 0); /* Create a new cluster chain */
} else { /* Middle or end of the file */
clst = create_chain(fp->fs, fp->curr_clust); /* Follow or streach cluster chain */
}
if (clst == 0) break; /* Could not allocate a new cluster (disk full) */
if (clst == 1) {printf("\n\444444n");ABORT(fp->fs, FR_INT_ERR); }
if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
fp->curr_clust = clst; /* Update current cluster */
fp->csect = 0; /* Reset sector address in the cluster */
}
#if _FS_TINY
if (fp->fs->winsect == fp->dsect && move_window(fp->fs, 0)) /* Write back data buffer prior to following direct transfer */
ABORT(fp->fs, FR_DISK_ERR);
#else
if (fp->flag & FA__DIRTY) { /* Write back data buffer prior to following direct transfer */
if (disk_write(fp->fs->drive, fp->buf, fp->dsect, 1) != RES_OK)
ABORT(fp->fs, FR_DISK_ERR);
fp->flag &= ~FA__DIRTY;
}
#endif
sect = clust2sect(fp->fs, fp->curr_clust); /* Get current sector */
if (!sect) {printf("\n\55555555n");ABORT(fp->fs, FR_INT_ERR); }
sect += fp->csect;
cc = btw / SS(fp->fs); /* When remaining bytes >= sector size, */
if (cc) { /* Write maximum contiguous sectors directly */
if (fp->csect + cc > fp->fs->csize) /* Clip at cluster boundary */
cc = fp->fs->csize - fp->csect;
if (disk_write(fp->fs->drive, wbuff, sect, (BYTE)cc) != RES_OK)
ABORT(fp->fs, FR_DISK_ERR);
#if _FS_TINY
if (fp->fs->winsect - sect < cc) { /* Refill sector cache if it gets dirty by the direct write */
mem_cpy(fp->fs->win, wbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), SS(fp->fs));
fp->fs->wflag = 0;
}
#else
if (fp->dsect - sect < cc) { /* Refill sector cache if it gets dirty by the direct write */
mem_cpy(fp->buf, wbuff + ((fp->dsect - sect) * SS(fp->fs)), SS(fp->fs));
fp->flag &= ~FA__DIRTY;
}
#endif
fp->csect += (BYTE)cc; /* Next sector address in the cluster */
wcnt = SS(fp->fs) * cc; /* Number of bytes transferred */
continue;
}
#if _FS_TINY
if (fp->fptr >= fp->fsize) { /* Avoid silly buffer filling at growing edge */
if (move_window(fp->fs, 0)) ABORT(fp->fs, FR_DISK_ERR);
fp->fs->winsect = sect;
}
#else
if (fp->dsect != sect) { /* Fill sector buffer with file data */
if (fp->fptr < fp->fsize &&
disk_read(fp->fs->drive, fp->buf, sect, 1) != RES_OK)
ABORT(fp->fs, FR_DISK_ERR);
}
#endif
fp->dsect = sect;
fp->csect++; /* Next sector address in the cluster */
}
wcnt = SS(fp->fs) - (fp->fptr % SS(fp->fs)); /* Put partial sector into file I/O buffer */
if (wcnt > btw) wcnt = btw;
#if _FS_TINY
if (move_window(fp->fs, fp->dsect)) /* Move sector window */
ABORT(fp->fs, FR_DISK_ERR);
mem_cpy(&fp->fs->win[fp->fptr % SS(fp->fs)], wbuff, wcnt); /* Fit partial sector */
fp->fs->wflag = 1;
#else
mem_cpy(&fp->buf[fp->fptr % SS(fp->fs)], wbuff, wcnt); /* Fit partial sector */
fp->flag |= FA__DIRTY;
#endif
}
if (fp->fptr > fp->fsize) fp->fsize = fp->fptr; /* Update file size if needed */
fp->flag |= FA__WRITTEN; /* Set file changed flag */
LEAVE_FF(fp->fs, FR_OK);
}
不懂?到底是哪里出现问题,检测SD卡中的文件都是0KB,显然是没有写进去。求解!!
|
最佳答案
查看完整内容[请看2#楼]
回复【8楼】名侦探:
---------------------------------
w=1的时候代表2个字节。
剩下的你自己想。
|