LPC1788+SDHC+Fatfs R0.09a 格式化失败
最近在学习Fatfs 文件系统,平台为LPC1788, SDHC,已经调试通过的有利用Fatfs文件系统创建文件夹、文件、写文件、追加文件、删除文件、读SD卡容量、读SD卡剩余容量,现在想要在整个程序开始时对SD卡进行初始化,但是一只有一些问题,我用的是4G的SD卡,初始化过程大概有9s,但是初始化结果返回0x01,FR_DISK_ERR,参考网上众多程序,调用f_mkfs()函数需要如下设置:1.To enable f_mkfs function, set _USE_MKFS to 1 and set _FS_READONLY to 0
2.disk_ioctl 中编写GET_SECTOR_COUNT,GET_BLOCK_SIZE
反复检查这两点没有问题啊,但是程序硬件调试仍然显示初始化结果返回0x01,求指教
1、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, *ptr = buff;
res = RES_ERROR;
switch (ctrl) {
case CTRL_SYNC : /* Make sure that no pending write process */
res=RES_OK;
break;
case GET_SECTOR_COUNT : /* Get number of sectors on the disk (DWORD) */
*(DWORD*)buff = CardConfig.sectorcnt;
res = RES_OK;
break;
case GET_SECTOR_SIZE : /* Get R/W sector size (WORD) */
*(WORD*)buff = CardConfig.sectorsize; //512;
res = RES_OK;
break;
case GET_BLOCK_SIZE : /* Get erase block size in unit of sector (DWORD) */
*(DWORD*)buff = CardConfig.blocksize;
res = RES_OK;
break;
case MMC_GET_TYPE : /* Get card type flags (1 byte) */
*ptr = CardConfig.cardtype;
res = RES_OK;
break;
case MMC_GET_CSD : /* Receive CSD as a data block (16 bytes) */
for (n=0;n<16;n++)
*(ptr+n) = CardConfig.csd;
res = RES_OK;
break;
case MMC_GET_CID : /* Receive CID as a data block (16 bytes) */
for (n=0;n<16;n++)
*(ptr+n) = CardConfig.cid;
res = RES_OK;
break;
default:
res = RES_PARERR;
}
return res;
}
2、SD_BOOL SD_ReadConfiguration ()
{
uint8_t buf;
uint32_t i, c_size, c_size_mult, read_bl_len;
SD_BOOL retv;
retv = SD_FALSE;
/* Read OCR */
if (SD_SendCommand(READ_OCR, 0, CardConfig.ocr, 4) != R1_NO_ERROR) goto end;
/* Read CID */
if ((SD_SendCommand(SEND_CID, 0, NULL, 0) != R1_NO_ERROR) ||
SD_RecvDataBlock (CardConfig.cid, 16)==SD_FALSE) goto end;
/* Read CSD */
if ((SD_SendCommand(SEND_CSD, 0, NULL, 0) != R1_NO_ERROR) ||
SD_RecvDataBlock (CardConfig.csd, 16)==SD_FALSE) goto end;
/* sector size */
CardConfig.sectorsize = 512;
/* sector count */
if (((CardConfig.csd>>6) & 0x3) == 0x1) /* CSD V2.0 (for High/eXtended Capacity) */
{
/* Read C_SIZE */
c_size =(((uint32_t)CardConfig.csd<<16) + ((uint32_t)CardConfig.csd<<8) + CardConfig.csd) & 0x3FFFFF;
/* Calculate sector count */
CardConfig.sectorcnt = (c_size + 1) * 1024;
} else /* CSD V1.0 (for Standard Capacity) */
{
/* C_SIZE */
c_size = (((uint32_t)(CardConfig.csd&0x3)<<10) + ((uint32_t)CardConfig.csd<<2) + (CardConfig.csd>>6)) & 0xFFF;
/* C_SIZE_MUTE */
c_size_mult = ((CardConfig.csd&0x3)<<1) + ((CardConfig.csd&0x80)>>7);
/* READ_BL_LEN */
read_bl_len = CardConfig.csd & 0xF;
/* sector count = BLOCKNR*BLOCK_LEN/512, we manually set SECTOR_SIZE to 512*/
//CardConfig.sectorcnt = (c_size+1)*(1<<read_bl_len) * (1<<(c_size_mult+2)) / 512;
CardConfig.sectorcnt = (c_size+1) << (read_bl_len + c_size_mult - 7);
}
/* Get erase block size in unit of sector */
switch (CardType)
{
case CARDTYPE_SDV2_SC:
case CARDTYPE_SDV2_HC:
if ((SD_SendACommand (SD_STATUS, 0, buf, 1) !=R1_NO_ERROR) ||
SD_RecvDataBlock(buf, 16) == SD_FALSE) goto end; /* Read partial block */
for (i=64-16;i;i--) SPI_RecvByte();/* Purge trailing data */
CardConfig.blocksize = 16UL << (buf >> 4); /* Calculate block size based on AU size */
break;
case CARDTYPE_MMC:
CardConfig.blocksize = ((uint16_t)((CardConfig.csd & 124) >> 2) + 1) * (((CardConfig.csd & 3) << 3) + ((CardConfig.csd & 224) >> 5) + 1);
break;
case CARDTYPE_SDV1:
CardConfig.blocksize = (((CardConfig.csd & 63) << 1) + ((uint16_t)(CardConfig.csd & 128) >> 7) + 1) << ((CardConfig.csd >> 6) - 1);
break;
default:
goto end;
}
CardConfig.cardtype=CardType;
retv = SD_TRUE;
end:
SD_DeSelect ();
return retv;
}
3、FRESULT F_format(viod)
{
FATFS fs; // Work area (file system object) for logical drive
FRESULT res;
f_mount(0, &fs);
res=f_mkfs(0,0,4096) ; //每个簇包含8个sector
f_mount(0, NULL);
return res;
}
页:
[1]