|
| 1 | +/*-----------------------------------------------------------------------*/ |
| 2 | +/* Low level disk I/O module skeleton for FatFs (C)ChaN, 2013 */ |
| 3 | +/*-----------------------------------------------------------------------*/ |
| 4 | +/* If a working storage control module is available, it should be */ |
| 5 | +/* attached to the FatFs via a glue function rather than modifying it. */ |
| 6 | +/* This is an example of glue functions to attach various exsisting */ |
| 7 | +/* storage control module to the FatFs module with a defined API. */ |
| 8 | +/*-----------------------------------------------------------------------*/ |
| 9 | + |
| 10 | +#include <stdint.h> |
| 11 | +#include <stdio.h> |
| 12 | +#include "ff.h" /* FatFs lower layer API */ |
| 13 | +#include "diskio.h" /* FatFs lower layer API */ |
| 14 | +#include "misc.h" |
| 15 | +#include "storage.h" |
| 16 | +#include "sdcard.h" |
| 17 | + |
| 18 | +PARTITION VolToPart[] = { |
| 19 | + {0, 1}, // Logical drive 0 ==> Physical drive 0, 1st partition |
| 20 | + {1, 0}, // Logical drive 1 ==> Physical drive 1 (auto detection) |
| 21 | + /* |
| 22 | + {0, 2}, // Logical drive 2 ==> Physical drive 0, 2nd partition |
| 23 | + {0, 3}, // Logical drive 3 ==> Physical drive 0, 3rd partition |
| 24 | + */ |
| 25 | +}; |
| 26 | + |
| 27 | +/* Definitions of physical drive number for each media */ |
| 28 | +#define PD_FLASH (0) |
| 29 | +#define PD_SDCARD (1) |
| 30 | + |
| 31 | +/*-----------------------------------------------------------------------*/ |
| 32 | +/* Initialize a Drive */ |
| 33 | +/*-----------------------------------------------------------------------*/ |
| 34 | + |
| 35 | +DSTATUS disk_initialize ( |
| 36 | + BYTE pdrv /* Physical drive nmuber (0..) */ |
| 37 | +) |
| 38 | +{ |
| 39 | + switch (pdrv) { |
| 40 | + case PD_FLASH: |
| 41 | + storage_init(); |
| 42 | + return 0; |
| 43 | + |
| 44 | + case PD_SDCARD: |
| 45 | + if (!sdcard_power_on()) { |
| 46 | + return STA_NODISK; |
| 47 | + } |
| 48 | + // TODO return STA_PROTECT if SD card is read only |
| 49 | + return 0; |
| 50 | + } |
| 51 | + |
| 52 | + return STA_NOINIT; |
| 53 | +} |
| 54 | + |
| 55 | +/*-----------------------------------------------------------------------*/ |
| 56 | +/* Get Disk Status */ |
| 57 | +/*-----------------------------------------------------------------------*/ |
| 58 | + |
| 59 | +DSTATUS disk_status ( |
| 60 | + BYTE pdrv /* Physical drive nmuber (0..) */ |
| 61 | +) |
| 62 | +{ |
| 63 | + switch (pdrv) { |
| 64 | + case PD_FLASH : |
| 65 | + // flash is ready |
| 66 | + return 0; |
| 67 | + |
| 68 | + case PD_SDCARD: |
| 69 | + // TODO return STA_PROTECT if SD card is read only |
| 70 | + return 0; |
| 71 | + } |
| 72 | + |
| 73 | + return STA_NOINIT; |
| 74 | +} |
| 75 | + |
| 76 | +/*-----------------------------------------------------------------------*/ |
| 77 | +/* Read Sector(s) */ |
| 78 | +/*-----------------------------------------------------------------------*/ |
| 79 | + |
| 80 | +DRESULT disk_read ( |
| 81 | + BYTE pdrv, /* Physical drive nmuber (0..) */ |
| 82 | + BYTE *buff, /* Data buffer to store read data */ |
| 83 | + DWORD sector, /* Sector address (LBA) */ |
| 84 | + UINT count /* Number of sectors to read (1..128) */ |
| 85 | +) |
| 86 | +{ |
| 87 | + switch (pdrv) { |
| 88 | + case PD_FLASH: |
| 89 | + for (int i = 0; i < count; i++) { |
| 90 | + if (!storage_read_block(buff + i * FLASH_BLOCK_SIZE, sector + i)) { |
| 91 | + return RES_ERROR; |
| 92 | + } |
| 93 | + } |
| 94 | + return RES_OK; |
| 95 | + |
| 96 | + case PD_SDCARD: |
| 97 | + if (!sdcard_read_blocks(buff, sector, count)) { |
| 98 | + return RES_ERROR; |
| 99 | + } |
| 100 | + return RES_OK; |
| 101 | + } |
| 102 | + |
| 103 | + return RES_PARERR; |
| 104 | +} |
| 105 | + |
| 106 | +/*-----------------------------------------------------------------------*/ |
| 107 | +/* Write Sector(s) */ |
| 108 | +/*-----------------------------------------------------------------------*/ |
| 109 | + |
| 110 | +#if _USE_WRITE |
| 111 | +DRESULT disk_write ( |
| 112 | + BYTE pdrv, /* Physical drive nmuber (0..) */ |
| 113 | + const BYTE *buff, /* Data to be written */ |
| 114 | + DWORD sector, /* Sector address (LBA) */ |
| 115 | + UINT count /* Number of sectors to write (1..128) */ |
| 116 | +) |
| 117 | +{ |
| 118 | + switch (pdrv) { |
| 119 | + case PD_FLASH: |
| 120 | + for (int i = 0; i < count; i++) { |
| 121 | + if (!storage_write_block(buff + i * FLASH_BLOCK_SIZE, sector + i)) { |
| 122 | + return RES_ERROR; |
| 123 | + } |
| 124 | + } |
| 125 | + return RES_OK; |
| 126 | + |
| 127 | + case PD_SDCARD: |
| 128 | + if (!sdcard_write_blocks(buff, sector, count)) { |
| 129 | + return RES_ERROR; |
| 130 | + } |
| 131 | + return RES_OK; |
| 132 | + } |
| 133 | + |
| 134 | + return RES_PARERR; |
| 135 | +} |
| 136 | +#endif |
| 137 | + |
| 138 | + |
| 139 | +/*-----------------------------------------------------------------------*/ |
| 140 | +/* Miscellaneous Functions */ |
| 141 | +/*-----------------------------------------------------------------------*/ |
| 142 | + |
| 143 | +#if _USE_IOCTL |
| 144 | +DRESULT disk_ioctl ( |
| 145 | + BYTE pdrv, /* Physical drive nmuber (0..) */ |
| 146 | + BYTE cmd, /* Control code */ |
| 147 | + void *buff /* Buffer to send/receive control data */ |
| 148 | +) |
| 149 | +{ |
| 150 | + switch (pdrv) { |
| 151 | + case PD_FLASH: |
| 152 | + switch (cmd) { |
| 153 | + case CTRL_SYNC: |
| 154 | + storage_flush(); |
| 155 | + return RES_OK; |
| 156 | + |
| 157 | + case GET_BLOCK_SIZE: |
| 158 | + *((DWORD*)buff) = 1; // high-level sector erase size in units of the small (512) block size |
| 159 | + return RES_OK; |
| 160 | + } |
| 161 | + break; |
| 162 | + |
| 163 | + case PD_SDCARD: |
| 164 | + switch (cmd) { |
| 165 | + case CTRL_SYNC: |
| 166 | + return RES_OK; |
| 167 | + |
| 168 | + case GET_BLOCK_SIZE: |
| 169 | + *((DWORD*)buff) = 1; // high-level sector erase size in units of the small (512) block size |
| 170 | + return RES_OK; |
| 171 | + } |
| 172 | + break; |
| 173 | + } |
| 174 | + |
| 175 | + return RES_PARERR; |
| 176 | +} |
| 177 | +#endif |
| 178 | + |
| 179 | +DWORD get_fattime ( |
| 180 | + void |
| 181 | +) |
| 182 | +{ |
| 183 | + // TODO replace with call to RTC |
| 184 | + int year = 2013; |
| 185 | + int month = 10; |
| 186 | + int day = 12; |
| 187 | + int hour = 21; |
| 188 | + int minute = 42; |
| 189 | + int second = 13; |
| 190 | + return ((year - 1980) << 25) | ((month) << 21) | ((day) << 16) | ((hour) << 11) | ((minute) << 5) | (second / 2); |
| 191 | +} |
0 commit comments