Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
SDFileSystem/FATFileSystem/ChaN/diskio.cpp@140:d8634e76ecce, 2019-05-09 (annotated)
- Committer:
- jamesheavey
- Date:
- Thu May 09 14:36:51 2019 +0000
- Revision:
- 140:d8634e76ecce
- Parent:
- 129:b47c28c7eaaf
Final Submission. I have read and agreed with Statement of Academic Integrity.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| jamesheavey | 129:b47c28c7eaaf | 1 | /*-----------------------------------------------------------------------*/ |
| jamesheavey | 129:b47c28c7eaaf | 2 | /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2014 */ |
| jamesheavey | 129:b47c28c7eaaf | 3 | /*-----------------------------------------------------------------------*/ |
| jamesheavey | 129:b47c28c7eaaf | 4 | /* If a working storage control module is available, it should be */ |
| jamesheavey | 129:b47c28c7eaaf | 5 | /* attached to the FatFs via a glue function rather than modifying it. */ |
| jamesheavey | 129:b47c28c7eaaf | 6 | /* This is an example of glue functions to attach various exsisting */ |
| jamesheavey | 129:b47c28c7eaaf | 7 | /* storage control modules to the FatFs module with a defined API. */ |
| jamesheavey | 129:b47c28c7eaaf | 8 | /*-----------------------------------------------------------------------*/ |
| jamesheavey | 129:b47c28c7eaaf | 9 | |
| jamesheavey | 129:b47c28c7eaaf | 10 | #include "diskio.h" |
| jamesheavey | 129:b47c28c7eaaf | 11 | #include "mbed_debug.h" |
| jamesheavey | 129:b47c28c7eaaf | 12 | #include "FATFileSystem.h" |
| jamesheavey | 129:b47c28c7eaaf | 13 | |
| jamesheavey | 129:b47c28c7eaaf | 14 | using namespace mbed; |
| jamesheavey | 129:b47c28c7eaaf | 15 | |
| jamesheavey | 129:b47c28c7eaaf | 16 | /*-----------------------------------------------------------------------*/ |
| jamesheavey | 129:b47c28c7eaaf | 17 | /* Get Drive Status */ |
| jamesheavey | 129:b47c28c7eaaf | 18 | /*-----------------------------------------------------------------------*/ |
| jamesheavey | 129:b47c28c7eaaf | 19 | |
| jamesheavey | 129:b47c28c7eaaf | 20 | DSTATUS disk_status ( |
| jamesheavey | 129:b47c28c7eaaf | 21 | BYTE pdrv /* Physical drive nmuber to identify the drive */ |
| jamesheavey | 129:b47c28c7eaaf | 22 | ) |
| jamesheavey | 129:b47c28c7eaaf | 23 | { |
| jamesheavey | 129:b47c28c7eaaf | 24 | debug_if(FFS_DBG, "disk_status on pdrv [%d]\n", pdrv); |
| jamesheavey | 129:b47c28c7eaaf | 25 | return (DSTATUS)FATFileSystem::_ffs[pdrv]->disk_status(); |
| jamesheavey | 129:b47c28c7eaaf | 26 | } |
| jamesheavey | 129:b47c28c7eaaf | 27 | |
| jamesheavey | 129:b47c28c7eaaf | 28 | /*-----------------------------------------------------------------------*/ |
| jamesheavey | 129:b47c28c7eaaf | 29 | /* Inidialize a Drive */ |
| jamesheavey | 129:b47c28c7eaaf | 30 | /*-----------------------------------------------------------------------*/ |
| jamesheavey | 129:b47c28c7eaaf | 31 | |
| jamesheavey | 129:b47c28c7eaaf | 32 | DSTATUS disk_initialize ( |
| jamesheavey | 129:b47c28c7eaaf | 33 | BYTE pdrv /* Physical drive nmuber to identify the drive */ |
| jamesheavey | 129:b47c28c7eaaf | 34 | ) |
| jamesheavey | 129:b47c28c7eaaf | 35 | { |
| jamesheavey | 129:b47c28c7eaaf | 36 | debug_if(FFS_DBG, "disk_initialize on pdrv [%d]\n", pdrv); |
| jamesheavey | 129:b47c28c7eaaf | 37 | return (DSTATUS)FATFileSystem::_ffs[pdrv]->disk_initialize(); |
| jamesheavey | 129:b47c28c7eaaf | 38 | } |
| jamesheavey | 129:b47c28c7eaaf | 39 | |
| jamesheavey | 129:b47c28c7eaaf | 40 | /*-----------------------------------------------------------------------*/ |
| jamesheavey | 129:b47c28c7eaaf | 41 | /* Read Sector(s) */ |
| jamesheavey | 129:b47c28c7eaaf | 42 | /*-----------------------------------------------------------------------*/ |
| jamesheavey | 129:b47c28c7eaaf | 43 | |
| jamesheavey | 129:b47c28c7eaaf | 44 | DRESULT disk_read ( |
| jamesheavey | 129:b47c28c7eaaf | 45 | BYTE pdrv, /* Physical drive nmuber to identify the drive */ |
| jamesheavey | 129:b47c28c7eaaf | 46 | BYTE* buff, /* Data buffer to store read data */ |
| jamesheavey | 129:b47c28c7eaaf | 47 | DWORD sector, /* Sector address in LBA */ |
| jamesheavey | 129:b47c28c7eaaf | 48 | UINT count /* Number of sectors to read */ |
| jamesheavey | 129:b47c28c7eaaf | 49 | ) |
| jamesheavey | 129:b47c28c7eaaf | 50 | { |
| jamesheavey | 129:b47c28c7eaaf | 51 | debug_if(FFS_DBG, "disk_read(sector %d, count %d) on pdrv [%d]\n", sector, count, pdrv); |
| jamesheavey | 129:b47c28c7eaaf | 52 | if (FATFileSystem::_ffs[pdrv]->disk_read((uint8_t*)buff, sector, count)) |
| jamesheavey | 129:b47c28c7eaaf | 53 | return RES_PARERR; |
| jamesheavey | 129:b47c28c7eaaf | 54 | else |
| jamesheavey | 129:b47c28c7eaaf | 55 | return RES_OK; |
| jamesheavey | 129:b47c28c7eaaf | 56 | } |
| jamesheavey | 129:b47c28c7eaaf | 57 | |
| jamesheavey | 129:b47c28c7eaaf | 58 | /*-----------------------------------------------------------------------*/ |
| jamesheavey | 129:b47c28c7eaaf | 59 | /* Write Sector(s) */ |
| jamesheavey | 129:b47c28c7eaaf | 60 | /*-----------------------------------------------------------------------*/ |
| jamesheavey | 129:b47c28c7eaaf | 61 | |
| jamesheavey | 129:b47c28c7eaaf | 62 | #if _USE_WRITE |
| jamesheavey | 129:b47c28c7eaaf | 63 | DRESULT disk_write ( |
| jamesheavey | 129:b47c28c7eaaf | 64 | BYTE pdrv, /* Physical drive nmuber to identify the drive */ |
| jamesheavey | 129:b47c28c7eaaf | 65 | const BYTE* buff, /* Data to be written */ |
| jamesheavey | 129:b47c28c7eaaf | 66 | DWORD sector, /* Sector address in LBA */ |
| jamesheavey | 129:b47c28c7eaaf | 67 | UINT count /* Number of sectors to write */ |
| jamesheavey | 129:b47c28c7eaaf | 68 | ) |
| jamesheavey | 129:b47c28c7eaaf | 69 | { |
| jamesheavey | 129:b47c28c7eaaf | 70 | debug_if(FFS_DBG, "disk_write(sector %d, count %d) on pdrv [%d]\n", sector, count, pdrv); |
| jamesheavey | 129:b47c28c7eaaf | 71 | if (FATFileSystem::_ffs[pdrv]->disk_write((uint8_t*)buff, sector, count)) |
| jamesheavey | 129:b47c28c7eaaf | 72 | return RES_PARERR; |
| jamesheavey | 129:b47c28c7eaaf | 73 | else |
| jamesheavey | 129:b47c28c7eaaf | 74 | return RES_OK; |
| jamesheavey | 129:b47c28c7eaaf | 75 | } |
| jamesheavey | 129:b47c28c7eaaf | 76 | #endif |
| jamesheavey | 129:b47c28c7eaaf | 77 | |
| jamesheavey | 129:b47c28c7eaaf | 78 | /*-----------------------------------------------------------------------*/ |
| jamesheavey | 129:b47c28c7eaaf | 79 | /* Miscellaneous Functions */ |
| jamesheavey | 129:b47c28c7eaaf | 80 | /*-----------------------------------------------------------------------*/ |
| jamesheavey | 129:b47c28c7eaaf | 81 | |
| jamesheavey | 129:b47c28c7eaaf | 82 | #if _USE_IOCTL |
| jamesheavey | 129:b47c28c7eaaf | 83 | DRESULT disk_ioctl ( |
| jamesheavey | 129:b47c28c7eaaf | 84 | BYTE pdrv, /* Physical drive nmuber (0..) */ |
| jamesheavey | 129:b47c28c7eaaf | 85 | BYTE cmd, /* Control code */ |
| jamesheavey | 129:b47c28c7eaaf | 86 | void* buff /* Buffer to send/receive control data */ |
| jamesheavey | 129:b47c28c7eaaf | 87 | ) |
| jamesheavey | 129:b47c28c7eaaf | 88 | { |
| jamesheavey | 129:b47c28c7eaaf | 89 | debug_if(FFS_DBG, "disk_ioctl(%d)\n", cmd); |
| jamesheavey | 129:b47c28c7eaaf | 90 | switch(cmd) { |
| jamesheavey | 129:b47c28c7eaaf | 91 | case CTRL_SYNC: |
| jamesheavey | 129:b47c28c7eaaf | 92 | if(FATFileSystem::_ffs[pdrv] == NULL) { |
| jamesheavey | 129:b47c28c7eaaf | 93 | return RES_NOTRDY; |
| jamesheavey | 129:b47c28c7eaaf | 94 | } else if(FATFileSystem::_ffs[pdrv]->disk_sync()) { |
| jamesheavey | 129:b47c28c7eaaf | 95 | return RES_ERROR; |
| jamesheavey | 129:b47c28c7eaaf | 96 | } |
| jamesheavey | 129:b47c28c7eaaf | 97 | return RES_OK; |
| jamesheavey | 129:b47c28c7eaaf | 98 | case GET_SECTOR_COUNT: |
| jamesheavey | 129:b47c28c7eaaf | 99 | if(FATFileSystem::_ffs[pdrv] == NULL) { |
| jamesheavey | 129:b47c28c7eaaf | 100 | return RES_NOTRDY; |
| jamesheavey | 129:b47c28c7eaaf | 101 | } else { |
| jamesheavey | 129:b47c28c7eaaf | 102 | DWORD res = FATFileSystem::_ffs[pdrv]->disk_sectors(); |
| jamesheavey | 129:b47c28c7eaaf | 103 | if(res > 0) { |
| jamesheavey | 129:b47c28c7eaaf | 104 | *((DWORD*)buff) = res; // minimum allowed |
| jamesheavey | 129:b47c28c7eaaf | 105 | return RES_OK; |
| jamesheavey | 129:b47c28c7eaaf | 106 | } else { |
| jamesheavey | 129:b47c28c7eaaf | 107 | return RES_ERROR; |
| jamesheavey | 129:b47c28c7eaaf | 108 | } |
| jamesheavey | 129:b47c28c7eaaf | 109 | } |
| jamesheavey | 129:b47c28c7eaaf | 110 | case GET_BLOCK_SIZE: |
| jamesheavey | 129:b47c28c7eaaf | 111 | *((DWORD*)buff) = 1; // default when not known |
| jamesheavey | 129:b47c28c7eaaf | 112 | return RES_OK; |
| jamesheavey | 129:b47c28c7eaaf | 113 | |
| jamesheavey | 129:b47c28c7eaaf | 114 | } |
| jamesheavey | 129:b47c28c7eaaf | 115 | return RES_PARERR; |
| jamesheavey | 129:b47c28c7eaaf | 116 | } |
| jamesheavey | 129:b47c28c7eaaf | 117 | #endif |