Library for SD card

Dependents:   SDFileSystem_HelloWorld Sharp_ce140f_emul

Committer:
ffxx68
Date:
Tue Jul 19 13:49:28 2022 +0000
Revision:
2:02f003d025a7
Parent:
0:3bdfc1556537
SD Card handling added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AlexVC97 0:3bdfc1556537 1 /*-----------------------------------------------------------------------*/
AlexVC97 0:3bdfc1556537 2 /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2014 */
AlexVC97 0:3bdfc1556537 3 /*-----------------------------------------------------------------------*/
AlexVC97 0:3bdfc1556537 4 /* If a working storage control module is available, it should be */
AlexVC97 0:3bdfc1556537 5 /* attached to the FatFs via a glue function rather than modifying it. */
AlexVC97 0:3bdfc1556537 6 /* This is an example of glue functions to attach various exsisting */
AlexVC97 0:3bdfc1556537 7 /* storage control modules to the FatFs module with a defined API. */
AlexVC97 0:3bdfc1556537 8 /*-----------------------------------------------------------------------*/
AlexVC97 0:3bdfc1556537 9
AlexVC97 0:3bdfc1556537 10 #include "diskio.h"
AlexVC97 0:3bdfc1556537 11 #include "mbed_debug.h"
AlexVC97 0:3bdfc1556537 12 #include "FATFileSystem.h"
AlexVC97 0:3bdfc1556537 13
AlexVC97 0:3bdfc1556537 14 using namespace mbed;
AlexVC97 0:3bdfc1556537 15
AlexVC97 0:3bdfc1556537 16 /*-----------------------------------------------------------------------*/
AlexVC97 0:3bdfc1556537 17 /* Get Drive Status */
AlexVC97 0:3bdfc1556537 18 /*-----------------------------------------------------------------------*/
AlexVC97 0:3bdfc1556537 19
AlexVC97 0:3bdfc1556537 20 DSTATUS disk_status (
AlexVC97 0:3bdfc1556537 21 BYTE pdrv /* Physical drive nmuber to identify the drive */
AlexVC97 0:3bdfc1556537 22 )
AlexVC97 0:3bdfc1556537 23 {
AlexVC97 0:3bdfc1556537 24 debug_if(FFS_DBG, "disk_status on pdrv [%d]\n", pdrv);
AlexVC97 0:3bdfc1556537 25 return (DSTATUS)FATFileSystem::_ffs[pdrv]->disk_status();
AlexVC97 0:3bdfc1556537 26 }
AlexVC97 0:3bdfc1556537 27
AlexVC97 0:3bdfc1556537 28 /*-----------------------------------------------------------------------*/
AlexVC97 0:3bdfc1556537 29 /* Inidialize a Drive */
AlexVC97 0:3bdfc1556537 30 /*-----------------------------------------------------------------------*/
AlexVC97 0:3bdfc1556537 31
AlexVC97 0:3bdfc1556537 32 DSTATUS disk_initialize (
AlexVC97 0:3bdfc1556537 33 BYTE pdrv /* Physical drive nmuber to identify the drive */
AlexVC97 0:3bdfc1556537 34 )
AlexVC97 0:3bdfc1556537 35 {
AlexVC97 0:3bdfc1556537 36 debug_if(FFS_DBG, "disk_initialize on pdrv [%d]\n", pdrv);
AlexVC97 0:3bdfc1556537 37 return (DSTATUS)FATFileSystem::_ffs[pdrv]->disk_initialize();
AlexVC97 0:3bdfc1556537 38 }
AlexVC97 0:3bdfc1556537 39
AlexVC97 0:3bdfc1556537 40 /*-----------------------------------------------------------------------*/
AlexVC97 0:3bdfc1556537 41 /* Read Sector(s) */
AlexVC97 0:3bdfc1556537 42 /*-----------------------------------------------------------------------*/
AlexVC97 0:3bdfc1556537 43
AlexVC97 0:3bdfc1556537 44 DRESULT disk_read (
AlexVC97 0:3bdfc1556537 45 BYTE pdrv, /* Physical drive nmuber to identify the drive */
AlexVC97 0:3bdfc1556537 46 BYTE* buff, /* Data buffer to store read data */
AlexVC97 0:3bdfc1556537 47 DWORD sector, /* Sector address in LBA */
AlexVC97 0:3bdfc1556537 48 UINT count /* Number of sectors to read */
AlexVC97 0:3bdfc1556537 49 )
AlexVC97 0:3bdfc1556537 50 {
AlexVC97 0:3bdfc1556537 51 debug_if(FFS_DBG, "disk_read(sector %d, count %d) on pdrv [%d]\n", sector, count, pdrv);
AlexVC97 0:3bdfc1556537 52 if (FATFileSystem::_ffs[pdrv]->disk_read((uint8_t*)buff, sector, count))
AlexVC97 0:3bdfc1556537 53 return RES_PARERR;
AlexVC97 0:3bdfc1556537 54 else
AlexVC97 0:3bdfc1556537 55 return RES_OK;
AlexVC97 0:3bdfc1556537 56 }
AlexVC97 0:3bdfc1556537 57
AlexVC97 0:3bdfc1556537 58 /*-----------------------------------------------------------------------*/
AlexVC97 0:3bdfc1556537 59 /* Write Sector(s) */
AlexVC97 0:3bdfc1556537 60 /*-----------------------------------------------------------------------*/
AlexVC97 0:3bdfc1556537 61
AlexVC97 0:3bdfc1556537 62 #if _USE_WRITE
AlexVC97 0:3bdfc1556537 63 DRESULT disk_write (
AlexVC97 0:3bdfc1556537 64 BYTE pdrv, /* Physical drive nmuber to identify the drive */
AlexVC97 0:3bdfc1556537 65 const BYTE* buff, /* Data to be written */
AlexVC97 0:3bdfc1556537 66 DWORD sector, /* Sector address in LBA */
AlexVC97 0:3bdfc1556537 67 UINT count /* Number of sectors to write */
AlexVC97 0:3bdfc1556537 68 )
AlexVC97 0:3bdfc1556537 69 {
AlexVC97 0:3bdfc1556537 70 debug_if(FFS_DBG, "disk_write(sector %d, count %d) on pdrv [%d]\n", sector, count, pdrv);
AlexVC97 0:3bdfc1556537 71 if (FATFileSystem::_ffs[pdrv]->disk_write((uint8_t*)buff, sector, count))
AlexVC97 0:3bdfc1556537 72 return RES_PARERR;
AlexVC97 0:3bdfc1556537 73 else
AlexVC97 0:3bdfc1556537 74 return RES_OK;
AlexVC97 0:3bdfc1556537 75 }
AlexVC97 0:3bdfc1556537 76 #endif
AlexVC97 0:3bdfc1556537 77
AlexVC97 0:3bdfc1556537 78 /*-----------------------------------------------------------------------*/
AlexVC97 0:3bdfc1556537 79 /* Miscellaneous Functions */
AlexVC97 0:3bdfc1556537 80 /*-----------------------------------------------------------------------*/
AlexVC97 0:3bdfc1556537 81
AlexVC97 0:3bdfc1556537 82 #if _USE_IOCTL
AlexVC97 0:3bdfc1556537 83 DRESULT disk_ioctl (
AlexVC97 0:3bdfc1556537 84 BYTE pdrv, /* Physical drive nmuber (0..) */
AlexVC97 0:3bdfc1556537 85 BYTE cmd, /* Control code */
AlexVC97 0:3bdfc1556537 86 void* buff /* Buffer to send/receive control data */
AlexVC97 0:3bdfc1556537 87 )
AlexVC97 0:3bdfc1556537 88 {
AlexVC97 0:3bdfc1556537 89 debug_if(FFS_DBG, "disk_ioctl(%d)\n", cmd);
AlexVC97 0:3bdfc1556537 90 switch(cmd) {
AlexVC97 0:3bdfc1556537 91 case CTRL_SYNC:
AlexVC97 0:3bdfc1556537 92 if(FATFileSystem::_ffs[pdrv] == NULL) {
AlexVC97 0:3bdfc1556537 93 return RES_NOTRDY;
AlexVC97 0:3bdfc1556537 94 } else if(FATFileSystem::_ffs[pdrv]->disk_sync()) {
AlexVC97 0:3bdfc1556537 95 return RES_ERROR;
AlexVC97 0:3bdfc1556537 96 }
AlexVC97 0:3bdfc1556537 97 return RES_OK;
AlexVC97 0:3bdfc1556537 98 case GET_SECTOR_COUNT:
AlexVC97 0:3bdfc1556537 99 if(FATFileSystem::_ffs[pdrv] == NULL) {
AlexVC97 0:3bdfc1556537 100 return RES_NOTRDY;
AlexVC97 0:3bdfc1556537 101 } else {
AlexVC97 0:3bdfc1556537 102 DWORD res = FATFileSystem::_ffs[pdrv]->disk_sectors();
AlexVC97 0:3bdfc1556537 103 if(res > 0) {
AlexVC97 0:3bdfc1556537 104 *((DWORD*)buff) = res; // minimum allowed
AlexVC97 0:3bdfc1556537 105 return RES_OK;
AlexVC97 0:3bdfc1556537 106 } else {
AlexVC97 0:3bdfc1556537 107 return RES_ERROR;
AlexVC97 0:3bdfc1556537 108 }
AlexVC97 0:3bdfc1556537 109 }
AlexVC97 0:3bdfc1556537 110 case GET_BLOCK_SIZE:
AlexVC97 0:3bdfc1556537 111 *((DWORD*)buff) = 1; // default when not known
AlexVC97 0:3bdfc1556537 112 return RES_OK;
AlexVC97 0:3bdfc1556537 113
AlexVC97 0:3bdfc1556537 114 }
AlexVC97 0:3bdfc1556537 115 return RES_PARERR;
AlexVC97 0:3bdfc1556537 116 }
AlexVC97 0:3bdfc1556537 117 #endif