Example of using the SDFileSystem library on a K64F to write data to files and also read data into dynamically created arrays.
diskio.cpp
00001 /*-----------------------------------------------------------------------*/ 00002 /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2014 */ 00003 /*-----------------------------------------------------------------------*/ 00004 /* If a working storage control module is available, it should be */ 00005 /* attached to the FatFs via a glue function rather than modifying it. */ 00006 /* This is an example of glue functions to attach various exsisting */ 00007 /* storage control modules to the FatFs module with a defined API. */ 00008 /*-----------------------------------------------------------------------*/ 00009 00010 #include "diskio.h" 00011 #include "mbed_debug.h" 00012 #include "FATFileSystem.h" 00013 00014 using namespace mbed; 00015 00016 /*-----------------------------------------------------------------------*/ 00017 /* Get Drive Status */ 00018 /*-----------------------------------------------------------------------*/ 00019 00020 DSTATUS disk_status ( 00021 BYTE pdrv /* Physical drive nmuber to identify the drive */ 00022 ) 00023 { 00024 debug_if(FFS_DBG, "disk_status on pdrv [%d]\n", pdrv); 00025 return (DSTATUS)FATFileSystem::_ffs[pdrv]->disk_status(); 00026 } 00027 00028 /*-----------------------------------------------------------------------*/ 00029 /* Inidialize a Drive */ 00030 /*-----------------------------------------------------------------------*/ 00031 00032 DSTATUS disk_initialize ( 00033 BYTE pdrv /* Physical drive nmuber to identify the drive */ 00034 ) 00035 { 00036 debug_if(FFS_DBG, "disk_initialize on pdrv [%d]\n", pdrv); 00037 return (DSTATUS)FATFileSystem::_ffs[pdrv]->disk_initialize(); 00038 } 00039 00040 /*-----------------------------------------------------------------------*/ 00041 /* Read Sector(s) */ 00042 /*-----------------------------------------------------------------------*/ 00043 00044 DRESULT disk_read ( 00045 BYTE pdrv, /* Physical drive nmuber to identify the drive */ 00046 BYTE* buff, /* Data buffer to store read data */ 00047 DWORD sector, /* Sector address in LBA */ 00048 UINT count /* Number of sectors to read */ 00049 ) 00050 { 00051 debug_if(FFS_DBG, "disk_read(sector %d, count %d) on pdrv [%d]\n", sector, count, pdrv); 00052 if (FATFileSystem::_ffs[pdrv]->disk_read((uint8_t*)buff, sector, count)) 00053 return RES_PARERR; 00054 else 00055 return RES_OK; 00056 } 00057 00058 /*-----------------------------------------------------------------------*/ 00059 /* Write Sector(s) */ 00060 /*-----------------------------------------------------------------------*/ 00061 00062 #if _USE_WRITE 00063 DRESULT disk_write ( 00064 BYTE pdrv, /* Physical drive nmuber to identify the drive */ 00065 const BYTE* buff, /* Data to be written */ 00066 DWORD sector, /* Sector address in LBA */ 00067 UINT count /* Number of sectors to write */ 00068 ) 00069 { 00070 debug_if(FFS_DBG, "disk_write(sector %d, count %d) on pdrv [%d]\n", sector, count, pdrv); 00071 if (FATFileSystem::_ffs[pdrv]->disk_write((uint8_t*)buff, sector, count)) 00072 return RES_PARERR; 00073 else 00074 return RES_OK; 00075 } 00076 #endif 00077 00078 /*-----------------------------------------------------------------------*/ 00079 /* Miscellaneous Functions */ 00080 /*-----------------------------------------------------------------------*/ 00081 00082 #if _USE_IOCTL 00083 DRESULT disk_ioctl ( 00084 BYTE pdrv, /* Physical drive nmuber (0..) */ 00085 BYTE cmd, /* Control code */ 00086 void* buff /* Buffer to send/receive control data */ 00087 ) 00088 { 00089 debug_if(FFS_DBG, "disk_ioctl(%d)\n", cmd); 00090 switch(cmd) { 00091 case CTRL_SYNC: 00092 if(FATFileSystem::_ffs[pdrv] == NULL) { 00093 return RES_NOTRDY; 00094 } else if(FATFileSystem::_ffs[pdrv]->disk_sync()) { 00095 return RES_ERROR; 00096 } 00097 return RES_OK; 00098 case GET_SECTOR_COUNT: 00099 if(FATFileSystem::_ffs[pdrv] == NULL) { 00100 return RES_NOTRDY; 00101 } else { 00102 DWORD res = FATFileSystem::_ffs[pdrv]->disk_sectors(); 00103 if(res > 0) { 00104 *((DWORD*)buff) = res; // minimum allowed 00105 return RES_OK; 00106 } else { 00107 return RES_ERROR; 00108 } 00109 } 00110 case GET_BLOCK_SIZE: 00111 *((DWORD*)buff) = 1; // default when not known 00112 return RES_OK; 00113 00114 } 00115 return RES_PARERR; 00116 } 00117 #endif
Generated on Wed Jul 13 2022 01:25:51 by 1.7.2