/*-----------------------------------------------------------------------*/
/* Write Sector(s) */
/*-----------------------------------------------------------------------*/
/* The FatFs module will issue multiple sector transfer request
/ (count > 1) to the disk I/O layer. The disk function should process
/ the multiple sector transfer properly Do. not translate it into
/ multiple single sector transfers to the media, or the data read/write
/ performance may be drasticaly decreased. */
#if _READONLY == 0
DRESULT disk_write (
BYTE drv, /* Physical drive nmuber (0..) */
const BYTE *buff, /* Data to be written */
DWORD sector, /* Sector address (LBA) */
BYTE count /* Number of sectors to write (1..255) */
)
{
if (drv || (!count))
{
// translate the reslut code here
return RES_PARERR;
}
//if (Stat & STA_NOINIT) return RES_NOTRDY;
if (count == 1) /* Single block write */
{
if (!MMCWriteSingleBlock(sector,buff))
{
count = 0;
}
}
else /* Multiple block write */
{
if(!MMCWriteMultipleBlock(sector,buff,count))
{
count = 0;
}
}
return (count ? RES_ERROR : RES_OK);
}
#endif /* _READONLY */
/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions */
/*-----------------------------------------------------------------------*/
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, csd[16];
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: /* Get number of sectors on the disk (WORD) */
if((MMCWriteCmd(CMD9,0x95,0x00) == 0) && MMCCSD_CID(CMD9, csd))
{
if((csd[0] >> 6) == 1) /* SDC ver 2.00 */
{
csize = csd[9] + ((WORD)csd[8] << 8) + 1;
*(DWORD*)buff = (DWORD)csize << 10;
}
else /* MMC or SDC ver 1.XX */
{
n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2;
csize = (csd[8] >> 6) + ((WORD)csd[7] << 2) + ((WORD)(csd[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 : if ((MMCWriteCmd(CMD9,0x95,0x00) == 0) && MMCCSD_CID(CMD9, csd)) /* Read CSD */
{
*(DWORD*)buff = (((csd[10] & 63) << 1) + ((WORD)(csd[11] & 128) >> 7) + 1) << ((csd[13] >> 6) - 1);
res = RES_OK;
}
break;
default : res = RES_PARERR; break;
}
return res;
}
编写好以上代码后就可以用API函数进行文件操作了。
|