Example SDFileSystem in which FATDirHandle exposes the file info struct of the current file/directory.

Committer:
uci1
Date:
Fri Nov 28 05:12:38 2014 +0000
Revision:
1:a7d8fc28a863
Parent:
0:687056ba3278
change SD init so it won't stall on failed initialization

Who changed what in which revision?

UserRevisionLine numberNew contents of line
uci1 0:687056ba3278 1 /*-----------------------------------------------------------------------*/
uci1 0:687056ba3278 2 /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2007 */
uci1 0:687056ba3278 3 /*-----------------------------------------------------------------------*/
uci1 0:687056ba3278 4 /* This is a stub disk I/O module that acts as front end of the existing */
uci1 0:687056ba3278 5 /* disk I/O modules and attach it to FatFs module with common interface. */
uci1 0:687056ba3278 6 /*-----------------------------------------------------------------------*/
uci1 0:687056ba3278 7 #include "ffconf.h"
uci1 0:687056ba3278 8 #include "diskio.h"
uci1 0:687056ba3278 9
uci1 0:687056ba3278 10 #include "mbed_debug.h"
uci1 0:687056ba3278 11 #include "FATFileSystem.h"
uci1 0:687056ba3278 12
uci1 0:687056ba3278 13 using namespace mbed;
uci1 0:687056ba3278 14
uci1 0:687056ba3278 15 DSTATUS disk_initialize (
uci1 0:687056ba3278 16 BYTE drv /* Physical drive nmuber (0..) */
uci1 0:687056ba3278 17 )
uci1 0:687056ba3278 18 {
uci1 0:687056ba3278 19 debug_if(FFS_DBG, "disk_initialize on drv [%d]\n", drv);
uci1 0:687056ba3278 20 return (DSTATUS)FATFileSystem::_ffs[drv]->disk_initialize();
uci1 0:687056ba3278 21 }
uci1 0:687056ba3278 22
uci1 0:687056ba3278 23 DSTATUS disk_status (
uci1 0:687056ba3278 24 BYTE drv /* Physical drive nmuber (0..) */
uci1 0:687056ba3278 25 )
uci1 0:687056ba3278 26 {
uci1 0:687056ba3278 27 debug_if(FFS_DBG, "disk_status on drv [%d]\n", drv);
uci1 0:687056ba3278 28 return (DSTATUS)FATFileSystem::_ffs[drv]->disk_status();
uci1 0:687056ba3278 29 }
uci1 0:687056ba3278 30
uci1 0:687056ba3278 31 DRESULT disk_read (
uci1 0:687056ba3278 32 BYTE drv, /* Physical drive nmuber (0..) */
uci1 0:687056ba3278 33 BYTE *buff, /* Data buffer to store read data */
uci1 0:687056ba3278 34 DWORD sector, /* Sector address (LBA) */
uci1 0:687056ba3278 35 BYTE count /* Number of sectors to read (1..255) */
uci1 0:687056ba3278 36 )
uci1 0:687056ba3278 37 {
uci1 0:687056ba3278 38 debug_if(FFS_DBG, "disk_read(sector %d, count %d) on drv [%d]\n", sector, count, drv);
uci1 0:687056ba3278 39 for(DWORD s=sector; s<sector+count; s++) {
uci1 0:687056ba3278 40 debug_if(FFS_DBG, " disk_read(sector %d)\n", s);
uci1 0:687056ba3278 41 int res = FATFileSystem::_ffs[drv]->disk_read((uint8_t*)buff, s);
uci1 0:687056ba3278 42 if(res) {
uci1 0:687056ba3278 43 return RES_PARERR;
uci1 0:687056ba3278 44 }
uci1 0:687056ba3278 45 buff += 512;
uci1 0:687056ba3278 46 }
uci1 0:687056ba3278 47 return RES_OK;
uci1 0:687056ba3278 48 }
uci1 0:687056ba3278 49
uci1 0:687056ba3278 50 #if _READONLY == 0
uci1 0:687056ba3278 51 DRESULT disk_write (
uci1 0:687056ba3278 52 BYTE drv, /* Physical drive nmuber (0..) */
uci1 0:687056ba3278 53 const BYTE *buff, /* Data to be written */
uci1 0:687056ba3278 54 DWORD sector, /* Sector address (LBA) */
uci1 0:687056ba3278 55 BYTE count /* Number of sectors to write (1..255) */
uci1 0:687056ba3278 56 )
uci1 0:687056ba3278 57 {
uci1 0:687056ba3278 58 debug_if(FFS_DBG, "disk_write(sector %d, count %d) on drv [%d]\n", sector, count, drv);
uci1 0:687056ba3278 59 for(DWORD s = sector; s < sector + count; s++) {
uci1 0:687056ba3278 60 debug_if(FFS_DBG, " disk_write(sector %d)\n", s);
uci1 0:687056ba3278 61 int res = FATFileSystem::_ffs[drv]->disk_write((uint8_t*)buff, s);
uci1 0:687056ba3278 62 if(res) {
uci1 0:687056ba3278 63 return RES_PARERR;
uci1 0:687056ba3278 64 }
uci1 0:687056ba3278 65 buff += 512;
uci1 0:687056ba3278 66 }
uci1 0:687056ba3278 67 return RES_OK;
uci1 0:687056ba3278 68 }
uci1 0:687056ba3278 69 #endif /* _READONLY */
uci1 0:687056ba3278 70
uci1 0:687056ba3278 71 DRESULT disk_ioctl (
uci1 0:687056ba3278 72 BYTE drv, /* Physical drive nmuber (0..) */
uci1 0:687056ba3278 73 BYTE ctrl, /* Control code */
uci1 0:687056ba3278 74 void *buff /* Buffer to send/receive control data */
uci1 0:687056ba3278 75 )
uci1 0:687056ba3278 76 {
uci1 0:687056ba3278 77 debug_if(FFS_DBG, "disk_ioctl(%d)\n", ctrl);
uci1 0:687056ba3278 78 switch(ctrl) {
uci1 0:687056ba3278 79 case CTRL_SYNC:
uci1 0:687056ba3278 80 if(FATFileSystem::_ffs[drv] == NULL) {
uci1 0:687056ba3278 81 return RES_NOTRDY;
uci1 0:687056ba3278 82 } else if(FATFileSystem::_ffs[drv]->disk_sync()) {
uci1 0:687056ba3278 83 return RES_ERROR;
uci1 0:687056ba3278 84 }
uci1 0:687056ba3278 85 return RES_OK;
uci1 0:687056ba3278 86 case GET_SECTOR_COUNT:
uci1 0:687056ba3278 87 if(FATFileSystem::_ffs[drv] == NULL) {
uci1 0:687056ba3278 88 return RES_NOTRDY;
uci1 0:687056ba3278 89 } else {
uci1 0:687056ba3278 90 DWORD res = FATFileSystem::_ffs[drv]->disk_sectors();
uci1 0:687056ba3278 91 if(res > 0) {
uci1 0:687056ba3278 92 *((DWORD*)buff) = res; // minimum allowed
uci1 0:687056ba3278 93 return RES_OK;
uci1 0:687056ba3278 94 } else {
uci1 0:687056ba3278 95 return RES_ERROR;
uci1 0:687056ba3278 96 }
uci1 0:687056ba3278 97 }
uci1 0:687056ba3278 98 case GET_BLOCK_SIZE:
uci1 0:687056ba3278 99 *((DWORD*)buff) = 1; // default when not known
uci1 0:687056ba3278 100 return RES_OK;
uci1 0:687056ba3278 101
uci1 0:687056ba3278 102 }
uci1 0:687056ba3278 103 return RES_PARERR;
uci1 0:687056ba3278 104 }