For completeness, here is how I've implemented the functions required by FatFs, using SerialFlash:
/*-----------------------------------------------------------------------*/
/* Read Sector(s) */
/*-----------------------------------------------------------------------*/
DRESULT disk_read (
BYTE pdrv, /* Physical drive nmuber to identify the drive */
BYTE *buff, /* Data buffer to store read data */
LBA_t sector, /* Start sector in LBA */
UINT count /* Number of sectors to read */
)
{
BYTE * b = buff;
SerialFlash.read(sector * SECTOR_SIZE, b, SECTOR_SIZE * count);
return RES_OK;
}
/*-----------------------------------------------------------------------*/
/* Write Sector(s) */
/*-----------------------------------------------------------------------*/
#if FF_FS_READONLY == 0
DRESULT disk_write (
BYTE pdrv, /* Physical drive nmuber to identify the drive */
const BYTE *buff, /* Data to be written */
LBA_t sector, /* Start sector in LBA */
UINT count /* Number of sectors to write */
)
{
BYTE * b = (BYTE*) buff;
SerialFlash.eraseSector(sector * SECTOR_SIZE);
while(!SerialFlash.ready()); // don't know if useful, just to be sure that every task has done
SerialFlash.write(sector * SECTOR_SIZE, buff, SECTOR_SIZE * count);
while (!SerialFlash.ready()) ;
return RES_OK;
}
#endif
/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions */
/*-----------------------------------------------------------------------*/
DRESULT disk_ioctl (
BYTE pdrv, /* Physical drive nmuber (0..) */
BYTE cmd, /* Control code */
void *buff /* Buffer to send/receive control data */
)
{
DRESULT res = RES_ERROR;
uint8_t id[5];
switch( cmd )
{
case CTRL_SYNC : // Make sure that data has been written
while (!SerialFlash.ready());
res = RES_OK;
break;
case GET_SECTOR_COUNT :
SerialFlash.readID(id);
*(LBA_t*)buff = SerialFlash.capacity(id) / SECTOR_SIZE;
res = RES_OK;
break;
case GET_SECTOR_SIZE :
*(WORD*)buff = SECTOR_SIZE;
res = RES_OK;
break;
case GET_BLOCK_SIZE :
*(DWORD*)buff = BLOCK_SIZE;
res = RES_OK;
break;
default:
res = RES_PARERR;
}
return res;
}
The function "SerialFlash::eraseSector" has been taken from here: Erase Sector of Flash using SerialFlash - iotespresso.com