PokittoLib with changes to lcd refresh etc.
Fork of Pokitto by
This is a fork by user @Spinal, and is used in Pokittris for testing. Do not import this to your own program.
libpff/pff.cpp@0:e8b8f36b4505, 2017-09-18 (annotated)
- Committer:
- Pokitto
- Date:
- Mon Sep 18 11:47:51 2017 +0000
- Revision:
- 0:e8b8f36b4505
- Child:
- 2:968589ca3484
Initial;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Pokitto | 0:e8b8f36b4505 | 1 | /*----------------------------------------------------------------------------/ |
Pokitto | 0:e8b8f36b4505 | 2 | / Petit FatFs - FAT file system module R0.02 (C)ChaN, 2009 |
Pokitto | 0:e8b8f36b4505 | 3 | /-----------------------------------------------------------------------------/ |
Pokitto | 0:e8b8f36b4505 | 4 | / Petit FatFs module is an open source software to implement FAT file system to |
Pokitto | 0:e8b8f36b4505 | 5 | / small embedded systems. This is a free software and is opened for education, |
Pokitto | 0:e8b8f36b4505 | 6 | / research and commercial developments under license policy of following trems. |
Pokitto | 0:e8b8f36b4505 | 7 | / |
Pokitto | 0:e8b8f36b4505 | 8 | / Copyright (C) 2009, ChaN, all right reserved. |
Pokitto | 0:e8b8f36b4505 | 9 | / |
Pokitto | 0:e8b8f36b4505 | 10 | / * The Petit FatFs module is a free software and there is NO WARRANTY. |
Pokitto | 0:e8b8f36b4505 | 11 | / * No restriction on use. You can use, modify and redistribute it for |
Pokitto | 0:e8b8f36b4505 | 12 | / personal, non-profit or commercial use UNDER YOUR RESPONSIBILITY. |
Pokitto | 0:e8b8f36b4505 | 13 | / * Redistributions of source code must retain the above copyright notice. |
Pokitto | 0:e8b8f36b4505 | 14 | / |
Pokitto | 0:e8b8f36b4505 | 15 | /-----------------------------------------------------------------------------/ |
Pokitto | 0:e8b8f36b4505 | 16 | / Jun 15,'09 R0.01a First release. (Branched from FatFs R0.07b.) |
Pokitto | 0:e8b8f36b4505 | 17 | / |
Pokitto | 0:e8b8f36b4505 | 18 | / Dec 14,'09 R0.02 Added multiple code page support. |
Pokitto | 0:e8b8f36b4505 | 19 | / Added write funciton. |
Pokitto | 0:e8b8f36b4505 | 20 | / Changed stream read mode interface. |
Pokitto | 0:e8b8f36b4505 | 21 | /----------------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 22 | |
Pokitto | 0:e8b8f36b4505 | 23 | #include "pff.h" /* Petit FatFs configurations and declarations */ |
Pokitto | 0:e8b8f36b4505 | 24 | #include "diskio.h" /* Declarations of low level disk I/O functions */ |
Pokitto | 0:e8b8f36b4505 | 25 | |
Pokitto | 0:e8b8f36b4505 | 26 | |
Pokitto | 0:e8b8f36b4505 | 27 | /*-------------------------------------------------------------------------- |
Pokitto | 0:e8b8f36b4505 | 28 | |
Pokitto | 0:e8b8f36b4505 | 29 | Private Functions |
Pokitto | 0:e8b8f36b4505 | 30 | |
Pokitto | 0:e8b8f36b4505 | 31 | ---------------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 32 | |
Pokitto | 0:e8b8f36b4505 | 33 | static |
Pokitto | 0:e8b8f36b4505 | 34 | FATFS *FatFs; /* Pointer to the file system object (logical drive) */ |
Pokitto | 0:e8b8f36b4505 | 35 | |
Pokitto | 0:e8b8f36b4505 | 36 | |
Pokitto | 0:e8b8f36b4505 | 37 | |
Pokitto | 0:e8b8f36b4505 | 38 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 39 | /* String functions */ |
Pokitto | 0:e8b8f36b4505 | 40 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 41 | |
Pokitto | 0:e8b8f36b4505 | 42 | /* Fill memory */ |
Pokitto | 0:e8b8f36b4505 | 43 | static |
Pokitto | 0:e8b8f36b4505 | 44 | void mem_set (void* dst, int val, int cnt) { |
Pokitto | 0:e8b8f36b4505 | 45 | char *d = (char*)dst; |
Pokitto | 0:e8b8f36b4505 | 46 | while (cnt--) *d++ = (char)val; |
Pokitto | 0:e8b8f36b4505 | 47 | } |
Pokitto | 0:e8b8f36b4505 | 48 | |
Pokitto | 0:e8b8f36b4505 | 49 | /* Compare memory to memory */ |
Pokitto | 0:e8b8f36b4505 | 50 | static |
Pokitto | 0:e8b8f36b4505 | 51 | int mem_cmp (const void* dst, const void* src, int cnt) { |
Pokitto | 0:e8b8f36b4505 | 52 | const char *d = (const char *)dst, *s = (const char *)src; |
Pokitto | 0:e8b8f36b4505 | 53 | int r = 0; |
Pokitto | 0:e8b8f36b4505 | 54 | while (cnt-- && (r = *d++ - *s++) == 0) ; |
Pokitto | 0:e8b8f36b4505 | 55 | return r; |
Pokitto | 0:e8b8f36b4505 | 56 | } |
Pokitto | 0:e8b8f36b4505 | 57 | |
Pokitto | 0:e8b8f36b4505 | 58 | |
Pokitto | 0:e8b8f36b4505 | 59 | |
Pokitto | 0:e8b8f36b4505 | 60 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 61 | /* FAT access - Read value of a FAT entry */ |
Pokitto | 0:e8b8f36b4505 | 62 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 63 | |
Pokitto | 0:e8b8f36b4505 | 64 | static |
Pokitto | 0:e8b8f36b4505 | 65 | CLUST get_fat ( /* 1:IO error, Else:Cluster status */ |
Pokitto | 0:e8b8f36b4505 | 66 | CLUST clst /* Cluster# to get the link information */ |
Pokitto | 0:e8b8f36b4505 | 67 | ) { |
Pokitto | 0:e8b8f36b4505 | 68 | WORD wc, bc, ofs; |
Pokitto | 0:e8b8f36b4505 | 69 | BYTE buf[4]; |
Pokitto | 0:e8b8f36b4505 | 70 | FATFS *fs = FatFs; |
Pokitto | 0:e8b8f36b4505 | 71 | |
Pokitto | 0:e8b8f36b4505 | 72 | |
Pokitto | 0:e8b8f36b4505 | 73 | if (clst < 2 || clst >= fs->max_clust) /* Range check */ |
Pokitto | 0:e8b8f36b4505 | 74 | return 1; |
Pokitto | 0:e8b8f36b4505 | 75 | |
Pokitto | 0:e8b8f36b4505 | 76 | switch (fs->fs_type) { |
Pokitto | 0:e8b8f36b4505 | 77 | case FS_FAT12 : |
Pokitto | 0:e8b8f36b4505 | 78 | bc = (WORD)clst; |
Pokitto | 0:e8b8f36b4505 | 79 | bc += bc / 2; |
Pokitto | 0:e8b8f36b4505 | 80 | ofs = bc % 512; |
Pokitto | 0:e8b8f36b4505 | 81 | bc /= 512; |
Pokitto | 0:e8b8f36b4505 | 82 | if (ofs != 511) { |
Pokitto | 0:e8b8f36b4505 | 83 | if (disk_readp(buf, fs->fatbase + bc, ofs, 2)) break; |
Pokitto | 0:e8b8f36b4505 | 84 | } else { |
Pokitto | 0:e8b8f36b4505 | 85 | if (disk_readp(buf, fs->fatbase + bc, 511, 1)) break; |
Pokitto | 0:e8b8f36b4505 | 86 | if (disk_readp(buf+1, fs->fatbase + bc + 1, 0, 1)) break; |
Pokitto | 0:e8b8f36b4505 | 87 | } |
Pokitto | 0:e8b8f36b4505 | 88 | wc = LD_WORD(buf); |
Pokitto | 0:e8b8f36b4505 | 89 | return (clst & 1) ? (wc >> 4) : (wc & 0xFFF); |
Pokitto | 0:e8b8f36b4505 | 90 | |
Pokitto | 0:e8b8f36b4505 | 91 | case FS_FAT16 : |
Pokitto | 0:e8b8f36b4505 | 92 | if (disk_readp(buf, fs->fatbase + clst / 256, (WORD)(((WORD)clst % 256) * 2), 2)) break; |
Pokitto | 0:e8b8f36b4505 | 93 | return LD_WORD(buf); |
Pokitto | 0:e8b8f36b4505 | 94 | #if _FS_FAT32 |
Pokitto | 0:e8b8f36b4505 | 95 | case FS_FAT32 : |
Pokitto | 0:e8b8f36b4505 | 96 | if (disk_readp(buf, fs->fatbase + clst / 128, (WORD)(((WORD)clst % 128) * 4), 4)) break; |
Pokitto | 0:e8b8f36b4505 | 97 | return LD_DWORD(buf) & 0x0FFFFFFF; |
Pokitto | 0:e8b8f36b4505 | 98 | #endif |
Pokitto | 0:e8b8f36b4505 | 99 | } |
Pokitto | 0:e8b8f36b4505 | 100 | |
Pokitto | 0:e8b8f36b4505 | 101 | return 1; /* An error occured at the disk I/O layer */ |
Pokitto | 0:e8b8f36b4505 | 102 | } |
Pokitto | 0:e8b8f36b4505 | 103 | |
Pokitto | 0:e8b8f36b4505 | 104 | |
Pokitto | 0:e8b8f36b4505 | 105 | |
Pokitto | 0:e8b8f36b4505 | 106 | |
Pokitto | 0:e8b8f36b4505 | 107 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 108 | /* Get sector# from cluster# */ |
Pokitto | 0:e8b8f36b4505 | 109 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 110 | |
Pokitto | 0:e8b8f36b4505 | 111 | static |
Pokitto | 0:e8b8f36b4505 | 112 | DWORD clust2sect ( /* !=0: Sector number, 0: Failed - invalid cluster# */ |
Pokitto | 0:e8b8f36b4505 | 113 | CLUST clst /* Cluster# to be converted */ |
Pokitto | 0:e8b8f36b4505 | 114 | ) { |
Pokitto | 0:e8b8f36b4505 | 115 | FATFS *fs = FatFs; |
Pokitto | 0:e8b8f36b4505 | 116 | |
Pokitto | 0:e8b8f36b4505 | 117 | |
Pokitto | 0:e8b8f36b4505 | 118 | clst -= 2; |
Pokitto | 0:e8b8f36b4505 | 119 | if (clst >= (fs->max_clust - 2)) return 0; /* Invalid cluster# */ |
Pokitto | 0:e8b8f36b4505 | 120 | return (DWORD)clst * fs->csize + fs->database; |
Pokitto | 0:e8b8f36b4505 | 121 | } |
Pokitto | 0:e8b8f36b4505 | 122 | |
Pokitto | 0:e8b8f36b4505 | 123 | |
Pokitto | 0:e8b8f36b4505 | 124 | |
Pokitto | 0:e8b8f36b4505 | 125 | |
Pokitto | 0:e8b8f36b4505 | 126 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 127 | /* Directory handling - Rewind directory index */ |
Pokitto | 0:e8b8f36b4505 | 128 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 129 | |
Pokitto | 0:e8b8f36b4505 | 130 | static |
Pokitto | 0:e8b8f36b4505 | 131 | FRESULT dir_rewind ( |
Pokitto | 0:e8b8f36b4505 | 132 | FATDIR *dj /* Pointer to directory object */ |
Pokitto | 0:e8b8f36b4505 | 133 | ) { |
Pokitto | 0:e8b8f36b4505 | 134 | CLUST clst; |
Pokitto | 0:e8b8f36b4505 | 135 | FATFS *fs = FatFs; |
Pokitto | 0:e8b8f36b4505 | 136 | |
Pokitto | 0:e8b8f36b4505 | 137 | |
Pokitto | 0:e8b8f36b4505 | 138 | dj->index = 0; |
Pokitto | 0:e8b8f36b4505 | 139 | clst = dj->sclust; |
Pokitto | 0:e8b8f36b4505 | 140 | if (clst == 1 || clst >= fs->max_clust) /* Check start cluster range */ |
Pokitto | 0:e8b8f36b4505 | 141 | return FR_DISK_ERR; |
Pokitto | 0:e8b8f36b4505 | 142 | #if _FS_FAT32 |
Pokitto | 0:e8b8f36b4505 | 143 | if (!clst && fs->fs_type == FS_FAT32) /* Replace cluster# 0 with root cluster# if in FAT32 */ |
Pokitto | 0:e8b8f36b4505 | 144 | clst = fs->dirbase; |
Pokitto | 0:e8b8f36b4505 | 145 | #endif |
Pokitto | 0:e8b8f36b4505 | 146 | dj->clust = clst; /* Current cluster */ |
Pokitto | 0:e8b8f36b4505 | 147 | dj->sect = clst ? clust2sect(clst) : fs->dirbase; /* Current sector */ |
Pokitto | 0:e8b8f36b4505 | 148 | |
Pokitto | 0:e8b8f36b4505 | 149 | return FR_OK; /* Seek succeeded */ |
Pokitto | 0:e8b8f36b4505 | 150 | } |
Pokitto | 0:e8b8f36b4505 | 151 | |
Pokitto | 0:e8b8f36b4505 | 152 | |
Pokitto | 0:e8b8f36b4505 | 153 | |
Pokitto | 0:e8b8f36b4505 | 154 | |
Pokitto | 0:e8b8f36b4505 | 155 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 156 | /* Directory handling - Move directory index next */ |
Pokitto | 0:e8b8f36b4505 | 157 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 158 | |
Pokitto | 0:e8b8f36b4505 | 159 | static |
Pokitto | 0:e8b8f36b4505 | 160 | FRESULT dir_next ( /* FR_OK:Succeeded, FR_NO_FILE:End of table */ |
Pokitto | 0:e8b8f36b4505 | 161 | FATDIR *dj /* Pointer to directory object */ |
Pokitto | 0:e8b8f36b4505 | 162 | ) { |
Pokitto | 0:e8b8f36b4505 | 163 | CLUST clst; |
Pokitto | 0:e8b8f36b4505 | 164 | WORD i; |
Pokitto | 0:e8b8f36b4505 | 165 | FATFS *fs = FatFs; |
Pokitto | 0:e8b8f36b4505 | 166 | |
Pokitto | 0:e8b8f36b4505 | 167 | |
Pokitto | 0:e8b8f36b4505 | 168 | i = dj->index + 1; |
Pokitto | 0:e8b8f36b4505 | 169 | if (!i || !dj->sect) /* Report EOT when index has reached 65535 */ |
Pokitto | 0:e8b8f36b4505 | 170 | return FR_NO_FILE; |
Pokitto | 0:e8b8f36b4505 | 171 | |
Pokitto | 0:e8b8f36b4505 | 172 | if (!(i & (16-1))) { /* Sector changed? */ |
Pokitto | 0:e8b8f36b4505 | 173 | dj->sect++; /* Next sector */ |
Pokitto | 0:e8b8f36b4505 | 174 | |
Pokitto | 0:e8b8f36b4505 | 175 | if (dj->clust == 0) { /* Static table */ |
Pokitto | 0:e8b8f36b4505 | 176 | if (i >= fs->n_rootdir) /* Report EOT when end of table */ |
Pokitto | 0:e8b8f36b4505 | 177 | return FR_NO_FILE; |
Pokitto | 0:e8b8f36b4505 | 178 | } else { /* Dynamic table */ |
Pokitto | 0:e8b8f36b4505 | 179 | if (((i / 16) & (fs->csize-1)) == 0) { /* Cluster changed? */ |
Pokitto | 0:e8b8f36b4505 | 180 | clst = get_fat(dj->clust); /* Get next cluster */ |
Pokitto | 0:e8b8f36b4505 | 181 | if (clst <= 1) return FR_DISK_ERR; |
Pokitto | 0:e8b8f36b4505 | 182 | if (clst >= fs->max_clust) /* When it reached end of dynamic table */ |
Pokitto | 0:e8b8f36b4505 | 183 | return FR_NO_FILE; /* Report EOT */ |
Pokitto | 0:e8b8f36b4505 | 184 | dj->clust = clst; /* Initialize data for new cluster */ |
Pokitto | 0:e8b8f36b4505 | 185 | dj->sect = clust2sect(clst); |
Pokitto | 0:e8b8f36b4505 | 186 | } |
Pokitto | 0:e8b8f36b4505 | 187 | } |
Pokitto | 0:e8b8f36b4505 | 188 | } |
Pokitto | 0:e8b8f36b4505 | 189 | |
Pokitto | 0:e8b8f36b4505 | 190 | dj->index = i; |
Pokitto | 0:e8b8f36b4505 | 191 | |
Pokitto | 0:e8b8f36b4505 | 192 | return FR_OK; |
Pokitto | 0:e8b8f36b4505 | 193 | } |
Pokitto | 0:e8b8f36b4505 | 194 | |
Pokitto | 0:e8b8f36b4505 | 195 | |
Pokitto | 0:e8b8f36b4505 | 196 | |
Pokitto | 0:e8b8f36b4505 | 197 | |
Pokitto | 0:e8b8f36b4505 | 198 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 199 | /* Directory handling - Find an object in the directory */ |
Pokitto | 0:e8b8f36b4505 | 200 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 201 | |
Pokitto | 0:e8b8f36b4505 | 202 | static |
Pokitto | 0:e8b8f36b4505 | 203 | FRESULT dir_find ( |
Pokitto | 0:e8b8f36b4505 | 204 | FATDIR *dj /* Pointer to the directory object linked to the file name */ |
Pokitto | 0:e8b8f36b4505 | 205 | ) { |
Pokitto | 0:e8b8f36b4505 | 206 | FRESULT res; |
Pokitto | 0:e8b8f36b4505 | 207 | BYTE c, *dir; |
Pokitto | 0:e8b8f36b4505 | 208 | |
Pokitto | 0:e8b8f36b4505 | 209 | |
Pokitto | 0:e8b8f36b4505 | 210 | res = dir_rewind(dj); /* Rewind directory object */ |
Pokitto | 0:e8b8f36b4505 | 211 | if (res != FR_OK) return res; |
Pokitto | 0:e8b8f36b4505 | 212 | |
Pokitto | 0:e8b8f36b4505 | 213 | dir = FatFs->buf; |
Pokitto | 0:e8b8f36b4505 | 214 | do { |
Pokitto | 0:e8b8f36b4505 | 215 | res = disk_readp(dir, dj->sect, (WORD)((dj->index % 16) * 32), 32) /* Read an entry */ |
Pokitto | 0:e8b8f36b4505 | 216 | ? FR_DISK_ERR : FR_OK; |
Pokitto | 0:e8b8f36b4505 | 217 | if (res != FR_OK) break; |
Pokitto | 0:e8b8f36b4505 | 218 | c = dir[DIR_Name]; /* First character */ |
Pokitto | 0:e8b8f36b4505 | 219 | if (c == 0) { |
Pokitto | 0:e8b8f36b4505 | 220 | res = FR_NO_FILE; /* Reached to end of table */ |
Pokitto | 0:e8b8f36b4505 | 221 | break; |
Pokitto | 0:e8b8f36b4505 | 222 | } |
Pokitto | 0:e8b8f36b4505 | 223 | if (!(dir[DIR_Attr] & AM_VOL) && !mem_cmp(dir, dj->fn, 11)) /* Is it a valid entry? */ |
Pokitto | 0:e8b8f36b4505 | 224 | break; |
Pokitto | 0:e8b8f36b4505 | 225 | res = dir_next(dj); /* Next entry */ |
Pokitto | 0:e8b8f36b4505 | 226 | } while (res == FR_OK); |
Pokitto | 0:e8b8f36b4505 | 227 | |
Pokitto | 0:e8b8f36b4505 | 228 | return res; |
Pokitto | 0:e8b8f36b4505 | 229 | } |
Pokitto | 0:e8b8f36b4505 | 230 | |
Pokitto | 0:e8b8f36b4505 | 231 | |
Pokitto | 0:e8b8f36b4505 | 232 | |
Pokitto | 0:e8b8f36b4505 | 233 | |
Pokitto | 0:e8b8f36b4505 | 234 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 235 | /* Read an object from the directory */ |
Pokitto | 0:e8b8f36b4505 | 236 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 237 | #if _USE_DIR |
Pokitto | 0:e8b8f36b4505 | 238 | static |
Pokitto | 0:e8b8f36b4505 | 239 | FRESULT dir_read ( |
Pokitto | 0:e8b8f36b4505 | 240 | FATDIR *dj /* Pointer to the directory object to store read object name */ |
Pokitto | 0:e8b8f36b4505 | 241 | ) { |
Pokitto | 0:e8b8f36b4505 | 242 | FRESULT res; |
Pokitto | 0:e8b8f36b4505 | 243 | BYTE a, c, *dir; |
Pokitto | 0:e8b8f36b4505 | 244 | |
Pokitto | 0:e8b8f36b4505 | 245 | |
Pokitto | 0:e8b8f36b4505 | 246 | res = FR_NO_FILE; |
Pokitto | 0:e8b8f36b4505 | 247 | dir = FatFs->buf; |
Pokitto | 0:e8b8f36b4505 | 248 | while (dj->sect) { |
Pokitto | 0:e8b8f36b4505 | 249 | res = disk_readp(dir, dj->sect, (WORD)((dj->index % 16) * 32), 32) /* Read an entry */ |
Pokitto | 0:e8b8f36b4505 | 250 | ? FR_DISK_ERR : FR_OK; |
Pokitto | 0:e8b8f36b4505 | 251 | if (res != FR_OK) break; |
Pokitto | 0:e8b8f36b4505 | 252 | c = dir[DIR_Name]; |
Pokitto | 0:e8b8f36b4505 | 253 | if (c == 0) { |
Pokitto | 0:e8b8f36b4505 | 254 | res = FR_NO_FILE; /* Reached to end of table */ |
Pokitto | 0:e8b8f36b4505 | 255 | break; |
Pokitto | 0:e8b8f36b4505 | 256 | } |
Pokitto | 0:e8b8f36b4505 | 257 | a = dir[DIR_Attr] & AM_MASK; |
Pokitto | 0:e8b8f36b4505 | 258 | if (c != 0xE5 && c != '.' && !(a & AM_VOL)) /* Is it a valid entry? */ |
Pokitto | 0:e8b8f36b4505 | 259 | break; |
Pokitto | 0:e8b8f36b4505 | 260 | res = dir_next(dj); /* Next entry */ |
Pokitto | 0:e8b8f36b4505 | 261 | if (res != FR_OK) break; |
Pokitto | 0:e8b8f36b4505 | 262 | } |
Pokitto | 0:e8b8f36b4505 | 263 | |
Pokitto | 0:e8b8f36b4505 | 264 | if (res != FR_OK) dj->sect = 0; |
Pokitto | 0:e8b8f36b4505 | 265 | |
Pokitto | 0:e8b8f36b4505 | 266 | return res; |
Pokitto | 0:e8b8f36b4505 | 267 | } |
Pokitto | 0:e8b8f36b4505 | 268 | #endif |
Pokitto | 0:e8b8f36b4505 | 269 | |
Pokitto | 0:e8b8f36b4505 | 270 | |
Pokitto | 0:e8b8f36b4505 | 271 | |
Pokitto | 0:e8b8f36b4505 | 272 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 273 | /* Pick a segment and create the object name in directory form */ |
Pokitto | 0:e8b8f36b4505 | 274 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 275 | |
Pokitto | 0:e8b8f36b4505 | 276 | #ifdef _EXCVT |
Pokitto | 0:e8b8f36b4505 | 277 | static const BYTE cvt[] = _EXCVT; |
Pokitto | 0:e8b8f36b4505 | 278 | #endif |
Pokitto | 0:e8b8f36b4505 | 279 | |
Pokitto | 0:e8b8f36b4505 | 280 | static |
Pokitto | 0:e8b8f36b4505 | 281 | FRESULT create_name ( |
Pokitto | 0:e8b8f36b4505 | 282 | FATDIR *dj, /* Pointer to the directory object */ |
Pokitto | 0:e8b8f36b4505 | 283 | const char **path /* Pointer to pointer to the segment in the path string */ |
Pokitto | 0:e8b8f36b4505 | 284 | ) { |
Pokitto | 0:e8b8f36b4505 | 285 | BYTE c, d, ni, si, i, *sfn; |
Pokitto | 0:e8b8f36b4505 | 286 | const char *p; |
Pokitto | 0:e8b8f36b4505 | 287 | |
Pokitto | 0:e8b8f36b4505 | 288 | /* Create file name in directory form */ |
Pokitto | 0:e8b8f36b4505 | 289 | sfn = dj->fn; |
Pokitto | 0:e8b8f36b4505 | 290 | mem_set(sfn, ' ', 11); |
Pokitto | 0:e8b8f36b4505 | 291 | si = i = 0; |
Pokitto | 0:e8b8f36b4505 | 292 | ni = 8; |
Pokitto | 0:e8b8f36b4505 | 293 | p = *path; |
Pokitto | 0:e8b8f36b4505 | 294 | for (;;) { |
Pokitto | 0:e8b8f36b4505 | 295 | c = p[si++]; |
Pokitto | 0:e8b8f36b4505 | 296 | if (c <= ' ' || c == '/') break; /* Break on end of segment */ |
Pokitto | 0:e8b8f36b4505 | 297 | if (c == '.' || i >= ni) { |
Pokitto | 0:e8b8f36b4505 | 298 | if (ni != 8 || c != '.') break; |
Pokitto | 0:e8b8f36b4505 | 299 | i = 8; |
Pokitto | 0:e8b8f36b4505 | 300 | ni = 11; |
Pokitto | 0:e8b8f36b4505 | 301 | continue; |
Pokitto | 0:e8b8f36b4505 | 302 | } |
Pokitto | 0:e8b8f36b4505 | 303 | #ifdef _EXCVT |
Pokitto | 0:e8b8f36b4505 | 304 | if (c >= 0x80) /* To upper extended char (SBCS) */ |
Pokitto | 0:e8b8f36b4505 | 305 | c = cvt[c - 0x80]; |
Pokitto | 0:e8b8f36b4505 | 306 | #endif |
Pokitto | 0:e8b8f36b4505 | 307 | if (IsDBCS1(c) && i >= ni - 1) { /* DBC 1st byte? */ |
Pokitto | 0:e8b8f36b4505 | 308 | d = p[si++]; /* Get 2nd byte */ |
Pokitto | 0:e8b8f36b4505 | 309 | sfn[i++] = c; |
Pokitto | 0:e8b8f36b4505 | 310 | sfn[i++] = d; |
Pokitto | 0:e8b8f36b4505 | 311 | } else { /* Single byte code */ |
Pokitto | 0:e8b8f36b4505 | 312 | if (IsLower(c)) c -= 0x20; /* toupper */ |
Pokitto | 0:e8b8f36b4505 | 313 | sfn[i++] = c; |
Pokitto | 0:e8b8f36b4505 | 314 | } |
Pokitto | 0:e8b8f36b4505 | 315 | } |
Pokitto | 0:e8b8f36b4505 | 316 | *path = &p[si]; /* Rerurn pointer to the next segment */ |
Pokitto | 0:e8b8f36b4505 | 317 | |
Pokitto | 0:e8b8f36b4505 | 318 | sfn[11] = (c <= ' ') ? 1 : 0; /* Set last segment flag if end of path */ |
Pokitto | 0:e8b8f36b4505 | 319 | |
Pokitto | 0:e8b8f36b4505 | 320 | return FR_OK; |
Pokitto | 0:e8b8f36b4505 | 321 | } |
Pokitto | 0:e8b8f36b4505 | 322 | |
Pokitto | 0:e8b8f36b4505 | 323 | |
Pokitto | 0:e8b8f36b4505 | 324 | |
Pokitto | 0:e8b8f36b4505 | 325 | |
Pokitto | 0:e8b8f36b4505 | 326 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 327 | /* Get file information from directory entry */ |
Pokitto | 0:e8b8f36b4505 | 328 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 329 | #if _USE_DIR |
Pokitto | 0:e8b8f36b4505 | 330 | static |
Pokitto | 0:e8b8f36b4505 | 331 | void get_fileinfo ( /* No return code */ |
Pokitto | 0:e8b8f36b4505 | 332 | FATDIR *dj, /* Pointer to the directory object */ |
Pokitto | 0:e8b8f36b4505 | 333 | FILINFO *fno /* Pointer to store the file information */ |
Pokitto | 0:e8b8f36b4505 | 334 | ) { |
Pokitto | 0:e8b8f36b4505 | 335 | BYTE i, c, *dir; |
Pokitto | 0:e8b8f36b4505 | 336 | char *p; |
Pokitto | 0:e8b8f36b4505 | 337 | |
Pokitto | 0:e8b8f36b4505 | 338 | |
Pokitto | 0:e8b8f36b4505 | 339 | p = fno->fname; |
Pokitto | 0:e8b8f36b4505 | 340 | if (dj->sect) { |
Pokitto | 0:e8b8f36b4505 | 341 | dir = FatFs->buf; |
Pokitto | 0:e8b8f36b4505 | 342 | for (i = 0; i < 8; i++) { /* Copy file name body */ |
Pokitto | 0:e8b8f36b4505 | 343 | c = dir[i]; |
Pokitto | 0:e8b8f36b4505 | 344 | if (c == ' ') break; |
Pokitto | 0:e8b8f36b4505 | 345 | if (c == 0x05) c = 0xE5; |
Pokitto | 0:e8b8f36b4505 | 346 | *p++ = c; |
Pokitto | 0:e8b8f36b4505 | 347 | } |
Pokitto | 0:e8b8f36b4505 | 348 | if (dir[8] != ' ') { /* Copy file name extension */ |
Pokitto | 0:e8b8f36b4505 | 349 | *p++ = '.'; |
Pokitto | 0:e8b8f36b4505 | 350 | for (i = 8; i < 11; i++) { |
Pokitto | 0:e8b8f36b4505 | 351 | c = dir[i]; |
Pokitto | 0:e8b8f36b4505 | 352 | if (c == ' ') break; |
Pokitto | 0:e8b8f36b4505 | 353 | *p++ = c; |
Pokitto | 0:e8b8f36b4505 | 354 | } |
Pokitto | 0:e8b8f36b4505 | 355 | } |
Pokitto | 0:e8b8f36b4505 | 356 | fno->fattrib = dir[DIR_Attr]; /* Attribute */ |
Pokitto | 0:e8b8f36b4505 | 357 | fno->fsize = LD_DWORD(dir+DIR_FileSize); /* Size */ |
Pokitto | 0:e8b8f36b4505 | 358 | fno->fdate = LD_WORD(dir+DIR_WrtDate); /* Date */ |
Pokitto | 0:e8b8f36b4505 | 359 | fno->ftime = LD_WORD(dir+DIR_WrtTime); /* Time */ |
Pokitto | 0:e8b8f36b4505 | 360 | } |
Pokitto | 0:e8b8f36b4505 | 361 | *p = 0; |
Pokitto | 0:e8b8f36b4505 | 362 | } |
Pokitto | 0:e8b8f36b4505 | 363 | #endif /* _USE_DIR */ |
Pokitto | 0:e8b8f36b4505 | 364 | |
Pokitto | 0:e8b8f36b4505 | 365 | |
Pokitto | 0:e8b8f36b4505 | 366 | |
Pokitto | 0:e8b8f36b4505 | 367 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 368 | /* Follow a file path */ |
Pokitto | 0:e8b8f36b4505 | 369 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 370 | |
Pokitto | 0:e8b8f36b4505 | 371 | static |
Pokitto | 0:e8b8f36b4505 | 372 | FRESULT follow_path ( /* FR_OK(0): successful, !=0: error code */ |
Pokitto | 0:e8b8f36b4505 | 373 | FATDIR *dj, /* Directory object to return last directory and found object */ |
Pokitto | 0:e8b8f36b4505 | 374 | const char *path /* Full-path string to find a file or directory */ |
Pokitto | 0:e8b8f36b4505 | 375 | ) { |
Pokitto | 0:e8b8f36b4505 | 376 | FRESULT res; |
Pokitto | 0:e8b8f36b4505 | 377 | BYTE *dir; |
Pokitto | 0:e8b8f36b4505 | 378 | |
Pokitto | 0:e8b8f36b4505 | 379 | |
Pokitto | 0:e8b8f36b4505 | 380 | while (*path == ' ') path++; /* Skip leading spaces */ |
Pokitto | 0:e8b8f36b4505 | 381 | if (*path == '/') path++; /* Strip heading separator */ |
Pokitto | 0:e8b8f36b4505 | 382 | dj->sclust = 0; /* Set start directory (always root dir) */ |
Pokitto | 0:e8b8f36b4505 | 383 | |
Pokitto | 0:e8b8f36b4505 | 384 | if ((BYTE)*path <= ' ') { /* Null path means the root directory */ |
Pokitto | 0:e8b8f36b4505 | 385 | res = dir_rewind(dj); |
Pokitto | 0:e8b8f36b4505 | 386 | FatFs->buf[0] = 0; |
Pokitto | 0:e8b8f36b4505 | 387 | |
Pokitto | 0:e8b8f36b4505 | 388 | } else { /* Follow path */ |
Pokitto | 0:e8b8f36b4505 | 389 | for (;;) { |
Pokitto | 0:e8b8f36b4505 | 390 | res = create_name(dj, &path); /* Get a segment */ |
Pokitto | 0:e8b8f36b4505 | 391 | if (res != FR_OK) break; |
Pokitto | 0:e8b8f36b4505 | 392 | res = dir_find(dj); /* Find it */ |
Pokitto | 0:e8b8f36b4505 | 393 | if (res != FR_OK) { /* Could not find the object */ |
Pokitto | 0:e8b8f36b4505 | 394 | if (res == FR_NO_FILE && !*(dj->fn+11)) |
Pokitto | 0:e8b8f36b4505 | 395 | res = FR_NO_PATH; |
Pokitto | 0:e8b8f36b4505 | 396 | break; |
Pokitto | 0:e8b8f36b4505 | 397 | } |
Pokitto | 0:e8b8f36b4505 | 398 | if (*(dj->fn+11)) break; /* Last segment match. Function completed. */ |
Pokitto | 0:e8b8f36b4505 | 399 | dir = FatFs->buf; /* There is next segment. Follow the sub directory */ |
Pokitto | 0:e8b8f36b4505 | 400 | if (!(dir[DIR_Attr] & AM_DIR)) { /* Cannot follow because it is a file */ |
Pokitto | 0:e8b8f36b4505 | 401 | res = FR_NO_PATH; |
Pokitto | 0:e8b8f36b4505 | 402 | break; |
Pokitto | 0:e8b8f36b4505 | 403 | } |
Pokitto | 0:e8b8f36b4505 | 404 | dj->sclust = |
Pokitto | 0:e8b8f36b4505 | 405 | #if _FS_FAT32 |
Pokitto | 0:e8b8f36b4505 | 406 | ((DWORD)LD_WORD(dir+DIR_FstClusHI) << 16) | |
Pokitto | 0:e8b8f36b4505 | 407 | #endif |
Pokitto | 0:e8b8f36b4505 | 408 | LD_WORD(dir+DIR_FstClusLO); |
Pokitto | 0:e8b8f36b4505 | 409 | } |
Pokitto | 0:e8b8f36b4505 | 410 | } |
Pokitto | 0:e8b8f36b4505 | 411 | |
Pokitto | 0:e8b8f36b4505 | 412 | return res; |
Pokitto | 0:e8b8f36b4505 | 413 | } |
Pokitto | 0:e8b8f36b4505 | 414 | |
Pokitto | 0:e8b8f36b4505 | 415 | |
Pokitto | 0:e8b8f36b4505 | 416 | |
Pokitto | 0:e8b8f36b4505 | 417 | |
Pokitto | 0:e8b8f36b4505 | 418 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 419 | /* Check a sector if it is an FAT boot record */ |
Pokitto | 0:e8b8f36b4505 | 420 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 421 | |
Pokitto | 0:e8b8f36b4505 | 422 | static |
Pokitto | 0:e8b8f36b4505 | 423 | __attribute__((section(".SD_Code"))) BYTE check_fs ( /* 0:The FAT boot record, 1:Valid boot record but not an FAT, 2:Not a boot record, 3:Error */ |
Pokitto | 0:e8b8f36b4505 | 424 | BYTE *buf, /* Working buffer */ |
Pokitto | 0:e8b8f36b4505 | 425 | DWORD sect /* Sector# (lba) to check if it is an FAT boot record or not */ |
Pokitto | 0:e8b8f36b4505 | 426 | ) { |
Pokitto | 0:e8b8f36b4505 | 427 | if (disk_readp(buf, sect, 510, 2)) /* Read the boot sector */ |
Pokitto | 0:e8b8f36b4505 | 428 | return 3; |
Pokitto | 0:e8b8f36b4505 | 429 | if (LD_WORD(buf) != 0xAA55) /* Check record signature */ |
Pokitto | 0:e8b8f36b4505 | 430 | return 2; |
Pokitto | 0:e8b8f36b4505 | 431 | |
Pokitto | 0:e8b8f36b4505 | 432 | if (!disk_readp(buf, sect, BS_FilSysType, 2) && LD_WORD(buf) == 0x4146) /* Check FAT12/16 */ |
Pokitto | 0:e8b8f36b4505 | 433 | return 0; |
Pokitto | 0:e8b8f36b4505 | 434 | #if _FS_FAT32 |
Pokitto | 0:e8b8f36b4505 | 435 | if (!disk_readp(buf, sect, BS_FilSysType32, 2) && LD_WORD(buf) == 0x4146) /* Check FAT32 */ |
Pokitto | 0:e8b8f36b4505 | 436 | return 0; |
Pokitto | 0:e8b8f36b4505 | 437 | #endif |
Pokitto | 0:e8b8f36b4505 | 438 | return 1; |
Pokitto | 0:e8b8f36b4505 | 439 | } |
Pokitto | 0:e8b8f36b4505 | 440 | |
Pokitto | 0:e8b8f36b4505 | 441 | |
Pokitto | 0:e8b8f36b4505 | 442 | |
Pokitto | 0:e8b8f36b4505 | 443 | |
Pokitto | 0:e8b8f36b4505 | 444 | /*-------------------------------------------------------------------------- |
Pokitto | 0:e8b8f36b4505 | 445 | |
Pokitto | 0:e8b8f36b4505 | 446 | Public Functions |
Pokitto | 0:e8b8f36b4505 | 447 | |
Pokitto | 0:e8b8f36b4505 | 448 | --------------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 449 | |
Pokitto | 0:e8b8f36b4505 | 450 | |
Pokitto | 0:e8b8f36b4505 | 451 | |
Pokitto | 0:e8b8f36b4505 | 452 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 453 | /* Mount/Unmount a Locical Drive */ |
Pokitto | 0:e8b8f36b4505 | 454 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 455 | |
Pokitto | 0:e8b8f36b4505 | 456 | __attribute__((section(".SD_Code"))) FRESULT pf_mount ( |
Pokitto | 0:e8b8f36b4505 | 457 | FATFS *fs /* Pointer to new file system object (NULL: Unmount) */ |
Pokitto | 0:e8b8f36b4505 | 458 | ) { |
Pokitto | 0:e8b8f36b4505 | 459 | BYTE fmt, buf[36]; |
Pokitto | 0:e8b8f36b4505 | 460 | DWORD bsect, fsize, tsect, mclst; |
Pokitto | 0:e8b8f36b4505 | 461 | |
Pokitto | 0:e8b8f36b4505 | 462 | |
Pokitto | 0:e8b8f36b4505 | 463 | FatFs = 0; |
Pokitto | 0:e8b8f36b4505 | 464 | if (!fs) return FR_OK; /* Unregister fs object */ |
Pokitto | 0:e8b8f36b4505 | 465 | |
Pokitto | 0:e8b8f36b4505 | 466 | if (disk_initialize() & STA_NOINIT) /* Check if the drive is ready or not */ |
Pokitto | 0:e8b8f36b4505 | 467 | return FR_NOT_READY; |
Pokitto | 0:e8b8f36b4505 | 468 | |
Pokitto | 0:e8b8f36b4505 | 469 | /* Search FAT partition on the drive */ |
Pokitto | 0:e8b8f36b4505 | 470 | bsect = 0; |
Pokitto | 0:e8b8f36b4505 | 471 | fmt = check_fs(buf, bsect); /* Check sector 0 as an SFD format */ |
Pokitto | 0:e8b8f36b4505 | 472 | if (fmt == 1) { /* Not an FAT boot record, it may be FDISK format */ |
Pokitto | 0:e8b8f36b4505 | 473 | /* Check a partition listed in top of the partition table */ |
Pokitto | 0:e8b8f36b4505 | 474 | if (disk_readp(buf, bsect, MBR_Table, 16)) { /* 1st partition entry */ |
Pokitto | 0:e8b8f36b4505 | 475 | fmt = 3; |
Pokitto | 0:e8b8f36b4505 | 476 | } else { |
Pokitto | 0:e8b8f36b4505 | 477 | if (buf[4]) { /* Is the partition existing? */ |
Pokitto | 0:e8b8f36b4505 | 478 | bsect = LD_DWORD(&buf[8]); /* Partition offset in LBA */ |
Pokitto | 0:e8b8f36b4505 | 479 | fmt = check_fs(buf, bsect); /* Check the partition */ |
Pokitto | 0:e8b8f36b4505 | 480 | } |
Pokitto | 0:e8b8f36b4505 | 481 | } |
Pokitto | 0:e8b8f36b4505 | 482 | } |
Pokitto | 0:e8b8f36b4505 | 483 | if (fmt == 3) return FR_DISK_ERR; |
Pokitto | 0:e8b8f36b4505 | 484 | if (fmt) return FR_NO_FILESYSTEM; /* No valid FAT patition is found */ |
Pokitto | 0:e8b8f36b4505 | 485 | |
Pokitto | 0:e8b8f36b4505 | 486 | /* Initialize the file system object */ |
Pokitto | 0:e8b8f36b4505 | 487 | if (disk_readp(buf, bsect, 13, sizeof(buf))) return FR_DISK_ERR; |
Pokitto | 0:e8b8f36b4505 | 488 | |
Pokitto | 0:e8b8f36b4505 | 489 | fsize = LD_WORD(buf+BPB_FATSz16-13); /* Number of sectors per FAT */ |
Pokitto | 0:e8b8f36b4505 | 490 | if (!fsize) fsize = LD_DWORD(buf+BPB_FATSz32-13); |
Pokitto | 0:e8b8f36b4505 | 491 | |
Pokitto | 0:e8b8f36b4505 | 492 | fsize *= buf[BPB_NumFATs-13]; /* Number of sectors in FAT area */ |
Pokitto | 0:e8b8f36b4505 | 493 | fs->fatbase = bsect + LD_WORD(buf+BPB_RsvdSecCnt-13); /* FAT start sector (lba) */ |
Pokitto | 0:e8b8f36b4505 | 494 | fs->csize = buf[BPB_SecPerClus-13]; /* Number of sectors per cluster */ |
Pokitto | 0:e8b8f36b4505 | 495 | fs->n_rootdir = LD_WORD(buf+BPB_RootEntCnt-13); /* Nmuber of root directory entries */ |
Pokitto | 0:e8b8f36b4505 | 496 | tsect = LD_WORD(buf+BPB_TotSec16-13); /* Number of sectors on the file system */ |
Pokitto | 0:e8b8f36b4505 | 497 | if (!tsect) tsect = LD_DWORD(buf+BPB_TotSec32-13); |
Pokitto | 0:e8b8f36b4505 | 498 | mclst = (tsect /* Last cluster# + 1 */ |
Pokitto | 0:e8b8f36b4505 | 499 | - LD_WORD(buf+BPB_RsvdSecCnt-13) - fsize - fs->n_rootdir / 16 |
Pokitto | 0:e8b8f36b4505 | 500 | ) / fs->csize + 2; |
Pokitto | 0:e8b8f36b4505 | 501 | fs->max_clust = (CLUST)mclst; |
Pokitto | 0:e8b8f36b4505 | 502 | |
Pokitto | 0:e8b8f36b4505 | 503 | fmt = FS_FAT12; /* Determine the FAT sub type */ |
Pokitto | 0:e8b8f36b4505 | 504 | if (mclst >= 0xFF7) fmt = FS_FAT16; /* Number of clusters >= 0xFF5 */ |
Pokitto | 0:e8b8f36b4505 | 505 | if (mclst >= 0xFFF7) /* Number of clusters >= 0xFFF5 */ |
Pokitto | 0:e8b8f36b4505 | 506 | #if _FS_FAT32 |
Pokitto | 0:e8b8f36b4505 | 507 | fmt = FS_FAT32; |
Pokitto | 0:e8b8f36b4505 | 508 | #else |
Pokitto | 0:e8b8f36b4505 | 509 | return FR_NO_FILESYSTEM; |
Pokitto | 0:e8b8f36b4505 | 510 | #endif |
Pokitto | 0:e8b8f36b4505 | 511 | |
Pokitto | 0:e8b8f36b4505 | 512 | fs->fs_type = fmt; /* FAT sub-type */ |
Pokitto | 0:e8b8f36b4505 | 513 | #if _FS_FAT32 |
Pokitto | 0:e8b8f36b4505 | 514 | if (fmt == FS_FAT32) |
Pokitto | 0:e8b8f36b4505 | 515 | fs->dirbase = LD_DWORD(buf+(BPB_RootClus-13)); /* Root directory start cluster */ |
Pokitto | 0:e8b8f36b4505 | 516 | else |
Pokitto | 0:e8b8f36b4505 | 517 | #endif |
Pokitto | 0:e8b8f36b4505 | 518 | fs->dirbase = fs->fatbase + fsize; /* Root directory start sector (lba) */ |
Pokitto | 0:e8b8f36b4505 | 519 | fs->database = fs->fatbase + fsize + fs->n_rootdir / 16; /* Data start sector (lba) */ |
Pokitto | 0:e8b8f36b4505 | 520 | |
Pokitto | 0:e8b8f36b4505 | 521 | fs->flag = 0; |
Pokitto | 0:e8b8f36b4505 | 522 | FatFs = fs; |
Pokitto | 0:e8b8f36b4505 | 523 | |
Pokitto | 0:e8b8f36b4505 | 524 | return FR_OK; |
Pokitto | 0:e8b8f36b4505 | 525 | } |
Pokitto | 0:e8b8f36b4505 | 526 | |
Pokitto | 0:e8b8f36b4505 | 527 | |
Pokitto | 0:e8b8f36b4505 | 528 | |
Pokitto | 0:e8b8f36b4505 | 529 | |
Pokitto | 0:e8b8f36b4505 | 530 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 531 | /* Open or Create a File */ |
Pokitto | 0:e8b8f36b4505 | 532 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 533 | |
Pokitto | 0:e8b8f36b4505 | 534 | FRESULT pf_open ( |
Pokitto | 0:e8b8f36b4505 | 535 | const char *path /* Pointer to the file name */ |
Pokitto | 0:e8b8f36b4505 | 536 | ) { |
Pokitto | 0:e8b8f36b4505 | 537 | FRESULT res; |
Pokitto | 0:e8b8f36b4505 | 538 | FATDIR dj; |
Pokitto | 0:e8b8f36b4505 | 539 | BYTE sp[12], dir[32]; |
Pokitto | 0:e8b8f36b4505 | 540 | FATFS *fs = FatFs; |
Pokitto | 0:e8b8f36b4505 | 541 | |
Pokitto | 0:e8b8f36b4505 | 542 | |
Pokitto | 0:e8b8f36b4505 | 543 | if (!fs) /* Check file system */ |
Pokitto | 0:e8b8f36b4505 | 544 | return FR_NOT_ENABLED; |
Pokitto | 0:e8b8f36b4505 | 545 | |
Pokitto | 0:e8b8f36b4505 | 546 | fs->flag = 0; |
Pokitto | 0:e8b8f36b4505 | 547 | fs->buf = dir; |
Pokitto | 0:e8b8f36b4505 | 548 | dj.fn = sp; |
Pokitto | 0:e8b8f36b4505 | 549 | res = follow_path(&dj, path); /* Follow the file path */ |
Pokitto | 0:e8b8f36b4505 | 550 | if (res != FR_OK) return res; /* Follow failed */ |
Pokitto | 0:e8b8f36b4505 | 551 | if (!dir[0] || (dir[DIR_Attr] & AM_DIR)) /* It is a directory */ |
Pokitto | 0:e8b8f36b4505 | 552 | return FR_NO_FILE; |
Pokitto | 0:e8b8f36b4505 | 553 | |
Pokitto | 0:e8b8f36b4505 | 554 | fs->org_clust = /* File start cluster */ |
Pokitto | 0:e8b8f36b4505 | 555 | #if _FS_FAT32 |
Pokitto | 0:e8b8f36b4505 | 556 | ((DWORD)LD_WORD(dir+DIR_FstClusHI) << 16) | |
Pokitto | 0:e8b8f36b4505 | 557 | #endif |
Pokitto | 0:e8b8f36b4505 | 558 | LD_WORD(dir+DIR_FstClusLO); |
Pokitto | 0:e8b8f36b4505 | 559 | fs->fsize = LD_DWORD(dir+DIR_FileSize); /* File size */ |
Pokitto | 0:e8b8f36b4505 | 560 | fs->fptr = 0; /* File pointer */ |
Pokitto | 0:e8b8f36b4505 | 561 | fs->flag = FA_OPENED; |
Pokitto | 0:e8b8f36b4505 | 562 | |
Pokitto | 0:e8b8f36b4505 | 563 | return FR_OK; |
Pokitto | 0:e8b8f36b4505 | 564 | } |
Pokitto | 0:e8b8f36b4505 | 565 | |
Pokitto | 0:e8b8f36b4505 | 566 | |
Pokitto | 0:e8b8f36b4505 | 567 | |
Pokitto | 0:e8b8f36b4505 | 568 | |
Pokitto | 0:e8b8f36b4505 | 569 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 570 | /* Read File */ |
Pokitto | 0:e8b8f36b4505 | 571 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 572 | #if _USE_READ |
Pokitto | 0:e8b8f36b4505 | 573 | |
Pokitto | 0:e8b8f36b4505 | 574 | FRESULT pf_read ( |
Pokitto | 0:e8b8f36b4505 | 575 | void* buff, /* Pointer to the read buffer (NULL:Forward data to the stream)*/ |
Pokitto | 0:e8b8f36b4505 | 576 | WORD btr, /* Number of bytes to read */ |
Pokitto | 0:e8b8f36b4505 | 577 | WORD* br /* Pointer to number of bytes read */ |
Pokitto | 0:e8b8f36b4505 | 578 | ) { |
Pokitto | 0:e8b8f36b4505 | 579 | DRESULT dr; |
Pokitto | 0:e8b8f36b4505 | 580 | CLUST clst; |
Pokitto | 0:e8b8f36b4505 | 581 | DWORD sect, remain; |
Pokitto | 0:e8b8f36b4505 | 582 | BYTE *rbuff = (BYTE *)buff; |
Pokitto | 0:e8b8f36b4505 | 583 | WORD rcnt; |
Pokitto | 0:e8b8f36b4505 | 584 | FATFS *fs = FatFs; |
Pokitto | 0:e8b8f36b4505 | 585 | |
Pokitto | 0:e8b8f36b4505 | 586 | |
Pokitto | 0:e8b8f36b4505 | 587 | *br = 0; |
Pokitto | 0:e8b8f36b4505 | 588 | if (!fs) return FR_NOT_ENABLED; /* Check file system */ |
Pokitto | 0:e8b8f36b4505 | 589 | if (!(fs->flag & FA_OPENED)) /* Check if opened */ |
Pokitto | 0:e8b8f36b4505 | 590 | return FR_NOT_OPENED; |
Pokitto | 0:e8b8f36b4505 | 591 | |
Pokitto | 0:e8b8f36b4505 | 592 | remain = fs->fsize - fs->fptr; |
Pokitto | 0:e8b8f36b4505 | 593 | if (btr > remain) btr = (WORD)remain; /* Truncate btr by remaining bytes */ |
Pokitto | 0:e8b8f36b4505 | 594 | |
Pokitto | 0:e8b8f36b4505 | 595 | while (btr) { /* Repeat until all data transferred */ |
Pokitto | 0:e8b8f36b4505 | 596 | if ((fs->fptr % 512) == 0) { /* On the sector boundary? */ |
Pokitto | 0:e8b8f36b4505 | 597 | if ((fs->fptr / 512 % fs->csize) == 0) { /* On the cluster boundary? */ |
Pokitto | 0:e8b8f36b4505 | 598 | clst = (fs->fptr == 0) ? /* On the top of the file? */ |
Pokitto | 0:e8b8f36b4505 | 599 | fs->org_clust : get_fat(fs->curr_clust); |
Pokitto | 0:e8b8f36b4505 | 600 | if (clst <= 1) goto fr_abort; |
Pokitto | 0:e8b8f36b4505 | 601 | fs->curr_clust = clst; /* Update current cluster */ |
Pokitto | 0:e8b8f36b4505 | 602 | fs->csect = 0; /* Reset sector offset in the cluster */ |
Pokitto | 0:e8b8f36b4505 | 603 | } |
Pokitto | 0:e8b8f36b4505 | 604 | sect = clust2sect(fs->curr_clust); /* Get current sector */ |
Pokitto | 0:e8b8f36b4505 | 605 | if (!sect) goto fr_abort; |
Pokitto | 0:e8b8f36b4505 | 606 | fs->dsect = sect + fs->csect++; |
Pokitto | 0:e8b8f36b4505 | 607 | } |
Pokitto | 0:e8b8f36b4505 | 608 | rcnt = 512 - ((WORD)fs->fptr % 512); /* Get partial sector data from sector buffer */ |
Pokitto | 0:e8b8f36b4505 | 609 | if (rcnt > btr) rcnt = btr; |
Pokitto | 0:e8b8f36b4505 | 610 | dr = disk_readp(!buff ? 0 : rbuff, fs->dsect, (WORD)(fs->fptr % 512), rcnt); |
Pokitto | 0:e8b8f36b4505 | 611 | if (dr) goto fr_abort; |
Pokitto | 0:e8b8f36b4505 | 612 | fs->fptr += rcnt; |
Pokitto | 0:e8b8f36b4505 | 613 | rbuff += rcnt; /* Update pointers and counters */ |
Pokitto | 0:e8b8f36b4505 | 614 | btr -= rcnt; |
Pokitto | 0:e8b8f36b4505 | 615 | *br += rcnt; |
Pokitto | 0:e8b8f36b4505 | 616 | } |
Pokitto | 0:e8b8f36b4505 | 617 | |
Pokitto | 0:e8b8f36b4505 | 618 | return FR_OK; |
Pokitto | 0:e8b8f36b4505 | 619 | |
Pokitto | 0:e8b8f36b4505 | 620 | fr_abort: |
Pokitto | 0:e8b8f36b4505 | 621 | fs->flag = 0; |
Pokitto | 0:e8b8f36b4505 | 622 | return FR_DISK_ERR; |
Pokitto | 0:e8b8f36b4505 | 623 | } |
Pokitto | 0:e8b8f36b4505 | 624 | #endif |
Pokitto | 0:e8b8f36b4505 | 625 | |
Pokitto | 0:e8b8f36b4505 | 626 | |
Pokitto | 0:e8b8f36b4505 | 627 | |
Pokitto | 0:e8b8f36b4505 | 628 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 629 | /* Write File */ |
Pokitto | 0:e8b8f36b4505 | 630 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 631 | #if _USE_WRITE |
Pokitto | 0:e8b8f36b4505 | 632 | |
Pokitto | 0:e8b8f36b4505 | 633 | FRESULT pf_write ( |
Pokitto | 0:e8b8f36b4505 | 634 | const void* buff, /* Pointer to the data to be written */ |
Pokitto | 0:e8b8f36b4505 | 635 | WORD btw, /* Number of bytes to write (0:Finalize the current write operation) */ |
Pokitto | 0:e8b8f36b4505 | 636 | WORD* bw /* Pointer to number of bytes written */ |
Pokitto | 0:e8b8f36b4505 | 637 | ) { |
Pokitto | 0:e8b8f36b4505 | 638 | CLUST clst; |
Pokitto | 0:e8b8f36b4505 | 639 | DWORD sect, remain; |
Pokitto | 0:e8b8f36b4505 | 640 | const BYTE *p = (const BYTE *)buff; |
Pokitto | 0:e8b8f36b4505 | 641 | WORD wcnt; |
Pokitto | 0:e8b8f36b4505 | 642 | FATFS *fs = FatFs; |
Pokitto | 0:e8b8f36b4505 | 643 | |
Pokitto | 0:e8b8f36b4505 | 644 | |
Pokitto | 0:e8b8f36b4505 | 645 | *bw = 0; |
Pokitto | 0:e8b8f36b4505 | 646 | if (!fs) return FR_NOT_ENABLED; /* Check file system */ |
Pokitto | 0:e8b8f36b4505 | 647 | if (!(fs->flag & FA_OPENED)) /* Check if opened */ |
Pokitto | 0:e8b8f36b4505 | 648 | return FR_NOT_OPENED; |
Pokitto | 0:e8b8f36b4505 | 649 | |
Pokitto | 0:e8b8f36b4505 | 650 | if (!btw) { /* Finalize request */ |
Pokitto | 0:e8b8f36b4505 | 651 | if ((fs->flag & FA__WIP) && disk_writep(0, 0)) goto fw_abort; |
Pokitto | 0:e8b8f36b4505 | 652 | fs->flag &= ~FA__WIP; |
Pokitto | 0:e8b8f36b4505 | 653 | return FR_OK; |
Pokitto | 0:e8b8f36b4505 | 654 | } else { /* Write data request */ |
Pokitto | 0:e8b8f36b4505 | 655 | if (!(fs->flag & FA__WIP)) /* Round down fptr to the sector boundary */ |
Pokitto | 0:e8b8f36b4505 | 656 | fs->fptr &= 0xFFFFFE00; |
Pokitto | 0:e8b8f36b4505 | 657 | } |
Pokitto | 0:e8b8f36b4505 | 658 | remain = fs->fsize - fs->fptr; |
Pokitto | 0:e8b8f36b4505 | 659 | if (btw > remain) btw = (WORD)remain; /* Truncate btw by remaining bytes */ |
Pokitto | 0:e8b8f36b4505 | 660 | |
Pokitto | 0:e8b8f36b4505 | 661 | while (btw) { /* Repeat until all data transferred */ |
Pokitto | 0:e8b8f36b4505 | 662 | if (((WORD)fs->fptr % 512) == 0) { /* On the sector boundary? */ |
Pokitto | 0:e8b8f36b4505 | 663 | if ((fs->fptr / 512 % fs->csize) == 0) { /* On the cluster boundary? */ |
Pokitto | 0:e8b8f36b4505 | 664 | clst = (fs->fptr == 0) ? /* On the top of the file? */ |
Pokitto | 0:e8b8f36b4505 | 665 | fs->org_clust : get_fat(fs->curr_clust); |
Pokitto | 0:e8b8f36b4505 | 666 | if (clst <= 1) goto fw_abort; |
Pokitto | 0:e8b8f36b4505 | 667 | fs->curr_clust = clst; /* Update current cluster */ |
Pokitto | 0:e8b8f36b4505 | 668 | fs->csect = 0; /* Reset sector offset in the cluster */ |
Pokitto | 0:e8b8f36b4505 | 669 | } |
Pokitto | 0:e8b8f36b4505 | 670 | sect = clust2sect(fs->curr_clust); /* Get current sector */ |
Pokitto | 0:e8b8f36b4505 | 671 | if (!sect) goto fw_abort; |
Pokitto | 0:e8b8f36b4505 | 672 | fs->dsect = sect + fs->csect++; |
Pokitto | 0:e8b8f36b4505 | 673 | if (disk_writep(0, fs->dsect)) goto fw_abort; /* Initiate a sector write operation */ |
Pokitto | 0:e8b8f36b4505 | 674 | fs->flag |= FA__WIP; |
Pokitto | 0:e8b8f36b4505 | 675 | } |
Pokitto | 0:e8b8f36b4505 | 676 | wcnt = 512 - ((WORD)fs->fptr % 512); /* Number of bytes to write to the sector */ |
Pokitto | 0:e8b8f36b4505 | 677 | if (wcnt > btw) wcnt = btw; |
Pokitto | 0:e8b8f36b4505 | 678 | if (disk_writep(p, wcnt)) goto fw_abort; /* Send data to the sector */ |
Pokitto | 0:e8b8f36b4505 | 679 | fs->fptr += wcnt; |
Pokitto | 0:e8b8f36b4505 | 680 | p += wcnt; /* Update pointers and counters */ |
Pokitto | 0:e8b8f36b4505 | 681 | btw -= wcnt; |
Pokitto | 0:e8b8f36b4505 | 682 | *bw += wcnt; |
Pokitto | 0:e8b8f36b4505 | 683 | if (((WORD)fs->fptr % 512) == 0) { |
Pokitto | 0:e8b8f36b4505 | 684 | if (disk_writep(0, 0)) goto fw_abort; /* Finalize the currtent secter write operation */ |
Pokitto | 0:e8b8f36b4505 | 685 | fs->flag &= ~FA__WIP; |
Pokitto | 0:e8b8f36b4505 | 686 | } |
Pokitto | 0:e8b8f36b4505 | 687 | } |
Pokitto | 0:e8b8f36b4505 | 688 | |
Pokitto | 0:e8b8f36b4505 | 689 | return FR_OK; |
Pokitto | 0:e8b8f36b4505 | 690 | |
Pokitto | 0:e8b8f36b4505 | 691 | fw_abort: |
Pokitto | 0:e8b8f36b4505 | 692 | fs->flag = 0; |
Pokitto | 0:e8b8f36b4505 | 693 | return FR_DISK_ERR; |
Pokitto | 0:e8b8f36b4505 | 694 | } |
Pokitto | 0:e8b8f36b4505 | 695 | #endif |
Pokitto | 0:e8b8f36b4505 | 696 | |
Pokitto | 0:e8b8f36b4505 | 697 | |
Pokitto | 0:e8b8f36b4505 | 698 | |
Pokitto | 0:e8b8f36b4505 | 699 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 700 | /* Seek File R/W Pointer */ |
Pokitto | 0:e8b8f36b4505 | 701 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 702 | #if _USE_LSEEK |
Pokitto | 0:e8b8f36b4505 | 703 | |
Pokitto | 0:e8b8f36b4505 | 704 | FRESULT pf_lseek ( |
Pokitto | 0:e8b8f36b4505 | 705 | DWORD ofs /* File pointer from top of file */ |
Pokitto | 0:e8b8f36b4505 | 706 | ) { |
Pokitto | 0:e8b8f36b4505 | 707 | CLUST clst; |
Pokitto | 0:e8b8f36b4505 | 708 | DWORD bcs, sect, ifptr; |
Pokitto | 0:e8b8f36b4505 | 709 | FATFS *fs = FatFs; |
Pokitto | 0:e8b8f36b4505 | 710 | |
Pokitto | 0:e8b8f36b4505 | 711 | |
Pokitto | 0:e8b8f36b4505 | 712 | if (!fs) return FR_NOT_ENABLED; /* Check file system */ |
Pokitto | 0:e8b8f36b4505 | 713 | if (!(fs->flag & FA_OPENED)) /* Check if opened */ |
Pokitto | 0:e8b8f36b4505 | 714 | return FR_NOT_OPENED; |
Pokitto | 0:e8b8f36b4505 | 715 | |
Pokitto | 0:e8b8f36b4505 | 716 | if (ofs > fs->fsize) ofs = fs->fsize; /* Clip offset with the file size */ |
Pokitto | 0:e8b8f36b4505 | 717 | ifptr = fs->fptr; |
Pokitto | 0:e8b8f36b4505 | 718 | fs->fptr = 0; |
Pokitto | 0:e8b8f36b4505 | 719 | if (ofs > 0) { |
Pokitto | 0:e8b8f36b4505 | 720 | bcs = (DWORD)fs->csize * 512; /* Cluster size (byte) */ |
Pokitto | 0:e8b8f36b4505 | 721 | if (ifptr > 0 && |
Pokitto | 0:e8b8f36b4505 | 722 | (ofs - 1) / bcs >= (ifptr - 1) / bcs) { /* When seek to same or following cluster, */ |
Pokitto | 0:e8b8f36b4505 | 723 | fs->fptr = (ifptr - 1) & ~(bcs - 1); /* start from the current cluster */ |
Pokitto | 0:e8b8f36b4505 | 724 | ofs -= fs->fptr; |
Pokitto | 0:e8b8f36b4505 | 725 | clst = fs->curr_clust; |
Pokitto | 0:e8b8f36b4505 | 726 | } else { /* When seek to back cluster, */ |
Pokitto | 0:e8b8f36b4505 | 727 | clst = fs->org_clust; /* start from the first cluster */ |
Pokitto | 0:e8b8f36b4505 | 728 | fs->curr_clust = clst; |
Pokitto | 0:e8b8f36b4505 | 729 | } |
Pokitto | 0:e8b8f36b4505 | 730 | while (ofs > bcs) { /* Cluster following loop */ |
Pokitto | 0:e8b8f36b4505 | 731 | clst = get_fat(clst); /* Follow cluster chain */ |
Pokitto | 0:e8b8f36b4505 | 732 | if (clst <= 1 || clst >= fs->max_clust) goto fe_abort; |
Pokitto | 0:e8b8f36b4505 | 733 | fs->curr_clust = clst; |
Pokitto | 0:e8b8f36b4505 | 734 | fs->fptr += bcs; |
Pokitto | 0:e8b8f36b4505 | 735 | ofs -= bcs; |
Pokitto | 0:e8b8f36b4505 | 736 | } |
Pokitto | 0:e8b8f36b4505 | 737 | fs->fptr += ofs; |
Pokitto | 0:e8b8f36b4505 | 738 | sect = clust2sect(clst); /* Current sector */ |
Pokitto | 0:e8b8f36b4505 | 739 | if (!sect) goto fe_abort; |
Pokitto | 0:e8b8f36b4505 | 740 | fs->csect = (BYTE)(ofs / 512); /* Sector offset in the cluster */ |
Pokitto | 0:e8b8f36b4505 | 741 | if (ofs % 512) |
Pokitto | 0:e8b8f36b4505 | 742 | fs->dsect = sect + fs->csect++; |
Pokitto | 0:e8b8f36b4505 | 743 | } |
Pokitto | 0:e8b8f36b4505 | 744 | |
Pokitto | 0:e8b8f36b4505 | 745 | return FR_OK; |
Pokitto | 0:e8b8f36b4505 | 746 | |
Pokitto | 0:e8b8f36b4505 | 747 | fe_abort: |
Pokitto | 0:e8b8f36b4505 | 748 | fs->flag = 0; |
Pokitto | 0:e8b8f36b4505 | 749 | return FR_DISK_ERR; |
Pokitto | 0:e8b8f36b4505 | 750 | } |
Pokitto | 0:e8b8f36b4505 | 751 | #endif |
Pokitto | 0:e8b8f36b4505 | 752 | |
Pokitto | 0:e8b8f36b4505 | 753 | |
Pokitto | 0:e8b8f36b4505 | 754 | |
Pokitto | 0:e8b8f36b4505 | 755 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 756 | /* Create a Directroy Object */ |
Pokitto | 0:e8b8f36b4505 | 757 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 758 | #if _USE_DIR |
Pokitto | 0:e8b8f36b4505 | 759 | |
Pokitto | 0:e8b8f36b4505 | 760 | FRESULT pf_opendir ( |
Pokitto | 0:e8b8f36b4505 | 761 | FATDIR *dj, /* Pointer to directory object to create */ |
Pokitto | 0:e8b8f36b4505 | 762 | const char *path /* Pointer to the directory path */ |
Pokitto | 0:e8b8f36b4505 | 763 | ) { |
Pokitto | 0:e8b8f36b4505 | 764 | FRESULT res; |
Pokitto | 0:e8b8f36b4505 | 765 | BYTE sp[12], dir[32]; |
Pokitto | 0:e8b8f36b4505 | 766 | FATFS *fs = FatFs; |
Pokitto | 0:e8b8f36b4505 | 767 | |
Pokitto | 0:e8b8f36b4505 | 768 | |
Pokitto | 0:e8b8f36b4505 | 769 | if (!fs) { /* Check file system */ |
Pokitto | 0:e8b8f36b4505 | 770 | res = FR_NOT_ENABLED; |
Pokitto | 0:e8b8f36b4505 | 771 | } else { |
Pokitto | 0:e8b8f36b4505 | 772 | fs->buf = dir; |
Pokitto | 0:e8b8f36b4505 | 773 | dj->fn = sp; |
Pokitto | 0:e8b8f36b4505 | 774 | res = follow_path(dj, path); /* Follow the path to the directory */ |
Pokitto | 0:e8b8f36b4505 | 775 | if (res == FR_OK) { /* Follow completed */ |
Pokitto | 0:e8b8f36b4505 | 776 | if (dir[0]) { /* It is not the root dir */ |
Pokitto | 0:e8b8f36b4505 | 777 | if (dir[DIR_Attr] & AM_DIR) { /* The object is a directory */ |
Pokitto | 0:e8b8f36b4505 | 778 | dj->sclust = |
Pokitto | 0:e8b8f36b4505 | 779 | #if _FS_FAT32 |
Pokitto | 0:e8b8f36b4505 | 780 | ((DWORD)LD_WORD(dir+DIR_FstClusHI) << 16) | |
Pokitto | 0:e8b8f36b4505 | 781 | #endif |
Pokitto | 0:e8b8f36b4505 | 782 | LD_WORD(dir+DIR_FstClusLO); |
Pokitto | 0:e8b8f36b4505 | 783 | } else { /* The object is not a directory */ |
Pokitto | 0:e8b8f36b4505 | 784 | res = FR_NO_PATH; |
Pokitto | 0:e8b8f36b4505 | 785 | } |
Pokitto | 0:e8b8f36b4505 | 786 | } |
Pokitto | 0:e8b8f36b4505 | 787 | if (res == FR_OK) |
Pokitto | 0:e8b8f36b4505 | 788 | res = dir_rewind(dj); /* Rewind dir */ |
Pokitto | 0:e8b8f36b4505 | 789 | } |
Pokitto | 0:e8b8f36b4505 | 790 | if (res == FR_NO_FILE) res = FR_NO_PATH; |
Pokitto | 0:e8b8f36b4505 | 791 | } |
Pokitto | 0:e8b8f36b4505 | 792 | |
Pokitto | 0:e8b8f36b4505 | 793 | return res; |
Pokitto | 0:e8b8f36b4505 | 794 | } |
Pokitto | 0:e8b8f36b4505 | 795 | |
Pokitto | 0:e8b8f36b4505 | 796 | |
Pokitto | 0:e8b8f36b4505 | 797 | |
Pokitto | 0:e8b8f36b4505 | 798 | |
Pokitto | 0:e8b8f36b4505 | 799 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 800 | /* Read Directory Entry in Sequense */ |
Pokitto | 0:e8b8f36b4505 | 801 | /*-----------------------------------------------------------------------*/ |
Pokitto | 0:e8b8f36b4505 | 802 | |
Pokitto | 0:e8b8f36b4505 | 803 | FRESULT pf_readdir ( |
Pokitto | 0:e8b8f36b4505 | 804 | FATDIR *dj, /* Pointer to the open directory object */ |
Pokitto | 0:e8b8f36b4505 | 805 | FILINFO *fno /* Pointer to file information to return */ |
Pokitto | 0:e8b8f36b4505 | 806 | ) { |
Pokitto | 0:e8b8f36b4505 | 807 | FRESULT res; |
Pokitto | 0:e8b8f36b4505 | 808 | BYTE sp[12], dir[32]; |
Pokitto | 0:e8b8f36b4505 | 809 | FATFS *fs = FatFs; |
Pokitto | 0:e8b8f36b4505 | 810 | |
Pokitto | 0:e8b8f36b4505 | 811 | |
Pokitto | 0:e8b8f36b4505 | 812 | if (!fs) { /* Check file system */ |
Pokitto | 0:e8b8f36b4505 | 813 | res = FR_NOT_ENABLED; |
Pokitto | 0:e8b8f36b4505 | 814 | } else { |
Pokitto | 0:e8b8f36b4505 | 815 | fs->buf = dir; |
Pokitto | 0:e8b8f36b4505 | 816 | dj->fn = sp; |
Pokitto | 0:e8b8f36b4505 | 817 | if (!fno) { |
Pokitto | 0:e8b8f36b4505 | 818 | res = dir_rewind(dj); |
Pokitto | 0:e8b8f36b4505 | 819 | } else { |
Pokitto | 0:e8b8f36b4505 | 820 | res = dir_read(dj); |
Pokitto | 0:e8b8f36b4505 | 821 | if (res == FR_NO_FILE) { |
Pokitto | 0:e8b8f36b4505 | 822 | dj->sect = 0; |
Pokitto | 0:e8b8f36b4505 | 823 | res = FR_OK; |
Pokitto | 0:e8b8f36b4505 | 824 | } |
Pokitto | 0:e8b8f36b4505 | 825 | if (res == FR_OK) { /* A valid entry is found */ |
Pokitto | 0:e8b8f36b4505 | 826 | get_fileinfo(dj, fno); /* Get the object information */ |
Pokitto | 0:e8b8f36b4505 | 827 | res = dir_next(dj); /* Increment index for next */ |
Pokitto | 0:e8b8f36b4505 | 828 | if (res == FR_NO_FILE) { |
Pokitto | 0:e8b8f36b4505 | 829 | dj->sect = 0; |
Pokitto | 0:e8b8f36b4505 | 830 | res = FR_OK; |
Pokitto | 0:e8b8f36b4505 | 831 | } |
Pokitto | 0:e8b8f36b4505 | 832 | } |
Pokitto | 0:e8b8f36b4505 | 833 | } |
Pokitto | 0:e8b8f36b4505 | 834 | } |
Pokitto | 0:e8b8f36b4505 | 835 | |
Pokitto | 0:e8b8f36b4505 | 836 | return res; |
Pokitto | 0:e8b8f36b4505 | 837 | } |
Pokitto | 0:e8b8f36b4505 | 838 | |
Pokitto | 0:e8b8f36b4505 | 839 | #endif /* _USE_DIR */ |
Pokitto | 0:e8b8f36b4505 | 840 | |
Pokitto | 0:e8b8f36b4505 | 841 |