大家好,我现在用LPC2368读写SD卡,文件系统为fatfs,用f_open创建文件时老发生错误,有时返回无文件系统,有时返回磁盘错误。有关底层移植函数如下:
DSTATUS disk_initialize (
BYTE drv /* Physical drive nmuber (0..) */
)
{
DSTATUS stat;
if (drv)
{
return STA_NOINIT; /* Supports only single drive */
}
if (!SD_Initialize(&sds)) /* Initialization succeded */
{
stat = 0; /* Clear STA_NOINIT */
}
return stat;
}初始化函数
DSTATUS disk_status (
BYTE drv /* Physical drive nmuber (0..) */
)
{
DSTATUS stat;
if (drv)
{
return STA_NOINIT; /* Supports only single drive */
}
else
{
stat = 0;
}
return stat;
}
读状态函数
DRESULT disk_read (
BYTE drv, /* Physical drive nmuber (0..) */
BYTE *buff, /* Data buffer to store read data */
DWORD sector, /* Sector address (LBA) */
BYTE count /* Number of sectors to read (1..255) */
)
{
INT8U res = 0;
if (drv || (!count))
{
return RES_PARERR;
}
//if (Stat & STA_NOINIT) return RES_NOTRDY;
if (count == 1) /* Single block read */
{
res = SD_ReadBlock(&sds, sector, buff);
}
else /* Multiple block read */
{
res = SD_ReadMultiBlock(&sds, sector, count, buff);
}
if(res == 0x00)
{
return RES_OK;
}
else
{
return RES_ERROR;
}
}
读扇区函数,其中SD卡的底层读块程序正确
DRESULT disk_ioctl (
BYTE drv, /* Physical drive nmuber (0..) */
BYTE ctrl, /* Control code */
void *buff /* Buffer to send/receive control data */
)
{
DRESULT res;
BYTE n, csdbuf[16],ret;
DWORD csize;
if (drv)
{
return RES_PARERR;
}
//if (stat & STA_NOINIT) return RES_NOTRDY;
//res = RES_ERROR;
//if (Stat & STA_NOINIT) return RES_NOTRDY;
switch (ctrl)
{
case CTRL_SYNC : res = RES_OK; break;
case GET_SECTOR_COUNT: ret = SdCmd_Send_CSD(&sds, 16, csdbuf);
if (ret != SD_NO_ERR)
return ret;
{
if((csdbuf[0] >> 6) == 1) /* SDC ver 2.00 */
{
csize = csdbuf[9] + ((WORD)csdbuf[8] << 8) + 1;
*(DWORD*)buff = (DWORD)csize << 10;
}
else /* MMC or SDC ver 1.XX */
{
n = (csdbuf[5] & 15) + ((csdbuf[10] & 128) >> 7) + ((csdbuf[9] & 3) << 1) + 2;
csize = (csdbuf[8]>> 6) + ((WORD)csdbuf[7] << 2) + ((WORD)(csdbuf[6] & 3) << 10) + 1;
*(DWORD*)buff = (DWORD)csize << (n - 9);
}
res = RES_OK;
}
break;
case GET_SECTOR_SIZE : /* Get sectors on the disk (WORD) */
*(WORD*)buff = 512;
res = RES_OK;
break;
case GET_BLOCK_SIZE : ret = SdCmd_Send_CSD(&sds, 16, csdbuf);
if (ret != SD_NO_ERR)
return ret;
{
*(DWORD*)buff = (((csdbuf[SECTOR_SIZE_POS1] & SECTOR_SIZE_MSK1) << 1) +
((csdbuf[SECTOR_SIZE_POS2] & SECTOR_SIZE_MSK2) >> 7) + 1) << ((csdbuf[13] >> 6) - 1);
res = RES_OK;
}
break;
default : res = RES_PARERR; break;
}
return res;
}
调了两天,老创建不了文件,头大,有做过相关的朋友进来指点看看,谢谢! |