mbed-os for GR-LYCHEE
Dependents: mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more
features/filesystem/fat/FATFileSystem.h@0:f782d9c66c49, 2018-02-02 (annotated)
- Committer:
- dkato
- Date:
- Fri Feb 02 05:42:23 2018 +0000
- Revision:
- 0:f782d9c66c49
mbed-os for GR-LYCHEE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dkato | 0:f782d9c66c49 | 1 | /* mbed Microcontroller Library |
dkato | 0:f782d9c66c49 | 2 | * Copyright (c) 2006-2012 ARM Limited |
dkato | 0:f782d9c66c49 | 3 | * |
dkato | 0:f782d9c66c49 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
dkato | 0:f782d9c66c49 | 5 | * of this software and associated documentation files (the "Software"), to deal |
dkato | 0:f782d9c66c49 | 6 | * in the Software without restriction, including without limitation the rights |
dkato | 0:f782d9c66c49 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
dkato | 0:f782d9c66c49 | 8 | * copies of the Software, and to permit persons to whom the Software is |
dkato | 0:f782d9c66c49 | 9 | * furnished to do so, subject to the following conditions: |
dkato | 0:f782d9c66c49 | 10 | * |
dkato | 0:f782d9c66c49 | 11 | * The above copyright notice and this permission notice shall be included in |
dkato | 0:f782d9c66c49 | 12 | * all copies or substantial portions of the Software. |
dkato | 0:f782d9c66c49 | 13 | * |
dkato | 0:f782d9c66c49 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
dkato | 0:f782d9c66c49 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
dkato | 0:f782d9c66c49 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
dkato | 0:f782d9c66c49 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
dkato | 0:f782d9c66c49 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
dkato | 0:f782d9c66c49 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
dkato | 0:f782d9c66c49 | 20 | * SOFTWARE. |
dkato | 0:f782d9c66c49 | 21 | */ |
dkato | 0:f782d9c66c49 | 22 | #ifndef MBED_FATFILESYSTEM_H |
dkato | 0:f782d9c66c49 | 23 | #define MBED_FATFILESYSTEM_H |
dkato | 0:f782d9c66c49 | 24 | |
dkato | 0:f782d9c66c49 | 25 | #include "FileSystem.h" |
dkato | 0:f782d9c66c49 | 26 | #include "BlockDevice.h" |
dkato | 0:f782d9c66c49 | 27 | #include "FileHandle.h" |
dkato | 0:f782d9c66c49 | 28 | #include "ff.h" |
dkato | 0:f782d9c66c49 | 29 | #include <stdint.h> |
dkato | 0:f782d9c66c49 | 30 | #include "PlatformMutex.h" |
dkato | 0:f782d9c66c49 | 31 | |
dkato | 0:f782d9c66c49 | 32 | using namespace mbed; |
dkato | 0:f782d9c66c49 | 33 | |
dkato | 0:f782d9c66c49 | 34 | /** |
dkato | 0:f782d9c66c49 | 35 | * FATFileSystem based on ChaN's Fat Filesystem library v0.8 |
dkato | 0:f782d9c66c49 | 36 | */ |
dkato | 0:f782d9c66c49 | 37 | class FATFileSystem : public FileSystem { |
dkato | 0:f782d9c66c49 | 38 | public: |
dkato | 0:f782d9c66c49 | 39 | /** Lifetime of the FATFileSystem |
dkato | 0:f782d9c66c49 | 40 | * |
dkato | 0:f782d9c66c49 | 41 | * @param name Name to add filesystem to tree as |
dkato | 0:f782d9c66c49 | 42 | * @param bd BlockDevice to mount, may be passed instead to mount call |
dkato | 0:f782d9c66c49 | 43 | */ |
dkato | 0:f782d9c66c49 | 44 | FATFileSystem(const char *name = NULL, BlockDevice *bd = NULL); |
dkato | 0:f782d9c66c49 | 45 | virtual ~FATFileSystem(); |
dkato | 0:f782d9c66c49 | 46 | |
dkato | 0:f782d9c66c49 | 47 | /** Formats a logical drive, FDISK partitioning rule. |
dkato | 0:f782d9c66c49 | 48 | * |
dkato | 0:f782d9c66c49 | 49 | * The block device to format should be mounted when this function is called. |
dkato | 0:f782d9c66c49 | 50 | * |
dkato | 0:f782d9c66c49 | 51 | * @param bd |
dkato | 0:f782d9c66c49 | 52 | * This is the block device that will be formated. |
dkato | 0:f782d9c66c49 | 53 | * |
dkato | 0:f782d9c66c49 | 54 | * @param allocation_unit |
dkato | 0:f782d9c66c49 | 55 | * This is the number of bytes per cluster size. The valid value is N |
dkato | 0:f782d9c66c49 | 56 | * times the sector size. N is a power of 2 from 1 to 128 for FAT |
dkato | 0:f782d9c66c49 | 57 | * volume and upto 16MiB for exFAT volume. If zero is given, |
dkato | 0:f782d9c66c49 | 58 | * the default allocation unit size is selected by the underlying |
dkato | 0:f782d9c66c49 | 59 | * filesystem, which depends on the volume size. |
dkato | 0:f782d9c66c49 | 60 | */ |
dkato | 0:f782d9c66c49 | 61 | static int format(BlockDevice *bd, int allocation_unit = 0); |
dkato | 0:f782d9c66c49 | 62 | |
dkato | 0:f782d9c66c49 | 63 | /** Mounts a filesystem to a block device |
dkato | 0:f782d9c66c49 | 64 | * |
dkato | 0:f782d9c66c49 | 65 | * @param bd BlockDevice to mount to |
dkato | 0:f782d9c66c49 | 66 | * @return 0 on success, negative error code on failure |
dkato | 0:f782d9c66c49 | 67 | */ |
dkato | 0:f782d9c66c49 | 68 | virtual int mount(BlockDevice *bd); |
dkato | 0:f782d9c66c49 | 69 | |
dkato | 0:f782d9c66c49 | 70 | /** Mounts a filesystem to a block device |
dkato | 0:f782d9c66c49 | 71 | * |
dkato | 0:f782d9c66c49 | 72 | * @param bd BlockDevice to mount to |
dkato | 0:f782d9c66c49 | 73 | * @param force Flag to force the underlying filesystem to force mounting the filesystem |
dkato | 0:f782d9c66c49 | 74 | * @return 0 on success, negative error code on failure |
dkato | 0:f782d9c66c49 | 75 | */ |
dkato | 0:f782d9c66c49 | 76 | virtual int mount(BlockDevice *bd, bool force); |
dkato | 0:f782d9c66c49 | 77 | |
dkato | 0:f782d9c66c49 | 78 | /** Unmounts a filesystem from the underlying block device |
dkato | 0:f782d9c66c49 | 79 | * |
dkato | 0:f782d9c66c49 | 80 | * @return 0 on success, negative error code on failure |
dkato | 0:f782d9c66c49 | 81 | */ |
dkato | 0:f782d9c66c49 | 82 | virtual int unmount(); |
dkato | 0:f782d9c66c49 | 83 | |
dkato | 0:f782d9c66c49 | 84 | /** Remove a file from the filesystem. |
dkato | 0:f782d9c66c49 | 85 | * |
dkato | 0:f782d9c66c49 | 86 | * @param path The name of the file to remove. |
dkato | 0:f782d9c66c49 | 87 | * @return 0 on success, negative error code on failure |
dkato | 0:f782d9c66c49 | 88 | */ |
dkato | 0:f782d9c66c49 | 89 | virtual int remove(const char *path); |
dkato | 0:f782d9c66c49 | 90 | |
dkato | 0:f782d9c66c49 | 91 | /** Rename a file in the filesystem. |
dkato | 0:f782d9c66c49 | 92 | * |
dkato | 0:f782d9c66c49 | 93 | * @param path The name of the file to rename. |
dkato | 0:f782d9c66c49 | 94 | * @param newpath The name to rename it to |
dkato | 0:f782d9c66c49 | 95 | * @return 0 on success, negative error code on failure |
dkato | 0:f782d9c66c49 | 96 | */ |
dkato | 0:f782d9c66c49 | 97 | virtual int rename(const char *path, const char *newpath); |
dkato | 0:f782d9c66c49 | 98 | |
dkato | 0:f782d9c66c49 | 99 | /** Store information about the file in a stat structure |
dkato | 0:f782d9c66c49 | 100 | * |
dkato | 0:f782d9c66c49 | 101 | * @param path The name of the file to find information about |
dkato | 0:f782d9c66c49 | 102 | * @param st The stat buffer to write to |
dkato | 0:f782d9c66c49 | 103 | * @return 0 on success, negative error code on failure |
dkato | 0:f782d9c66c49 | 104 | */ |
dkato | 0:f782d9c66c49 | 105 | virtual int stat(const char *path, struct stat *st); |
dkato | 0:f782d9c66c49 | 106 | |
dkato | 0:f782d9c66c49 | 107 | /** Create a directory in the filesystem. |
dkato | 0:f782d9c66c49 | 108 | * |
dkato | 0:f782d9c66c49 | 109 | * @param path The name of the directory to create. |
dkato | 0:f782d9c66c49 | 110 | * @param mode The permissions with which to create the directory |
dkato | 0:f782d9c66c49 | 111 | * @return 0 on success, negative error code on failure |
dkato | 0:f782d9c66c49 | 112 | */ |
dkato | 0:f782d9c66c49 | 113 | virtual int mkdir(const char *path, mode_t mode); |
dkato | 0:f782d9c66c49 | 114 | |
dkato | 0:f782d9c66c49 | 115 | protected: |
dkato | 0:f782d9c66c49 | 116 | /** Open a file on the filesystem |
dkato | 0:f782d9c66c49 | 117 | * |
dkato | 0:f782d9c66c49 | 118 | * @param file Destination for the handle to a newly created file |
dkato | 0:f782d9c66c49 | 119 | * @param path The name of the file to open |
dkato | 0:f782d9c66c49 | 120 | * @param flags The flags to open the file in, one of O_RDONLY, O_WRONLY, O_RDWR, |
dkato | 0:f782d9c66c49 | 121 | * bitwise or'd with one of O_CREAT, O_TRUNC, O_APPEND |
dkato | 0:f782d9c66c49 | 122 | * @return 0 on success, negative error code on failure |
dkato | 0:f782d9c66c49 | 123 | */ |
dkato | 0:f782d9c66c49 | 124 | virtual int file_open(fs_file_t *file, const char *path, int flags); |
dkato | 0:f782d9c66c49 | 125 | |
dkato | 0:f782d9c66c49 | 126 | /** Close a file |
dkato | 0:f782d9c66c49 | 127 | * |
dkato | 0:f782d9c66c49 | 128 | * @param file File handle |
dkato | 0:f782d9c66c49 | 129 | * return 0 on success, negative error code on failure |
dkato | 0:f782d9c66c49 | 130 | */ |
dkato | 0:f782d9c66c49 | 131 | virtual int file_close(fs_file_t file); |
dkato | 0:f782d9c66c49 | 132 | |
dkato | 0:f782d9c66c49 | 133 | /** Read the contents of a file into a buffer |
dkato | 0:f782d9c66c49 | 134 | * |
dkato | 0:f782d9c66c49 | 135 | * @param file File handle |
dkato | 0:f782d9c66c49 | 136 | * @param buffer The buffer to read in to |
dkato | 0:f782d9c66c49 | 137 | * @param size The number of bytes to read |
dkato | 0:f782d9c66c49 | 138 | * @return The number of bytes read, 0 at end of file, negative error on failure |
dkato | 0:f782d9c66c49 | 139 | */ |
dkato | 0:f782d9c66c49 | 140 | virtual ssize_t file_read(fs_file_t file, void *buffer, size_t len); |
dkato | 0:f782d9c66c49 | 141 | |
dkato | 0:f782d9c66c49 | 142 | /** Write the contents of a buffer to a file |
dkato | 0:f782d9c66c49 | 143 | * |
dkato | 0:f782d9c66c49 | 144 | * @param file File handle |
dkato | 0:f782d9c66c49 | 145 | * @param buffer The buffer to write from |
dkato | 0:f782d9c66c49 | 146 | * @param size The number of bytes to write |
dkato | 0:f782d9c66c49 | 147 | * @return The number of bytes written, negative error on failure |
dkato | 0:f782d9c66c49 | 148 | */ |
dkato | 0:f782d9c66c49 | 149 | virtual ssize_t file_write(fs_file_t file, const void *buffer, size_t len); |
dkato | 0:f782d9c66c49 | 150 | |
dkato | 0:f782d9c66c49 | 151 | /** Flush any buffers associated with the file |
dkato | 0:f782d9c66c49 | 152 | * |
dkato | 0:f782d9c66c49 | 153 | * @param file File handle |
dkato | 0:f782d9c66c49 | 154 | * @return 0 on success, negative error code on failure |
dkato | 0:f782d9c66c49 | 155 | */ |
dkato | 0:f782d9c66c49 | 156 | virtual int file_sync(fs_file_t file); |
dkato | 0:f782d9c66c49 | 157 | |
dkato | 0:f782d9c66c49 | 158 | /** Move the file position to a given offset from from a given location |
dkato | 0:f782d9c66c49 | 159 | * |
dkato | 0:f782d9c66c49 | 160 | * @param file File handle |
dkato | 0:f782d9c66c49 | 161 | * @param offset The offset from whence to move to |
dkato | 0:f782d9c66c49 | 162 | * @param whence The start of where to seek |
dkato | 0:f782d9c66c49 | 163 | * SEEK_SET to start from beginning of file, |
dkato | 0:f782d9c66c49 | 164 | * SEEK_CUR to start from current position in file, |
dkato | 0:f782d9c66c49 | 165 | * SEEK_END to start from end of file |
dkato | 0:f782d9c66c49 | 166 | * @return The new offset of the file |
dkato | 0:f782d9c66c49 | 167 | */ |
dkato | 0:f782d9c66c49 | 168 | virtual off_t file_seek(fs_file_t file, off_t offset, int whence); |
dkato | 0:f782d9c66c49 | 169 | |
dkato | 0:f782d9c66c49 | 170 | /** Get the file position of the file |
dkato | 0:f782d9c66c49 | 171 | * |
dkato | 0:f782d9c66c49 | 172 | * @param file File handle |
dkato | 0:f782d9c66c49 | 173 | * @return The current offset in the file |
dkato | 0:f782d9c66c49 | 174 | */ |
dkato | 0:f782d9c66c49 | 175 | virtual off_t file_tell(fs_file_t file); |
dkato | 0:f782d9c66c49 | 176 | |
dkato | 0:f782d9c66c49 | 177 | /** Get the size of the file |
dkato | 0:f782d9c66c49 | 178 | * |
dkato | 0:f782d9c66c49 | 179 | * @param file File handle |
dkato | 0:f782d9c66c49 | 180 | * @return Size of the file in bytes |
dkato | 0:f782d9c66c49 | 181 | */ |
dkato | 0:f782d9c66c49 | 182 | virtual size_t file_size(fs_file_t file); |
dkato | 0:f782d9c66c49 | 183 | |
dkato | 0:f782d9c66c49 | 184 | /** Open a directory on the filesystem |
dkato | 0:f782d9c66c49 | 185 | * |
dkato | 0:f782d9c66c49 | 186 | * @param dir Destination for the handle to the directory |
dkato | 0:f782d9c66c49 | 187 | * @param path Name of the directory to open |
dkato | 0:f782d9c66c49 | 188 | * @return 0 on success, negative error code on failure |
dkato | 0:f782d9c66c49 | 189 | */ |
dkato | 0:f782d9c66c49 | 190 | virtual int dir_open(fs_dir_t *dir, const char *path); |
dkato | 0:f782d9c66c49 | 191 | |
dkato | 0:f782d9c66c49 | 192 | /** Close a directory |
dkato | 0:f782d9c66c49 | 193 | * |
dkato | 0:f782d9c66c49 | 194 | * @param dir Dir handle |
dkato | 0:f782d9c66c49 | 195 | * return 0 on success, negative error code on failure |
dkato | 0:f782d9c66c49 | 196 | */ |
dkato | 0:f782d9c66c49 | 197 | virtual int dir_close(fs_dir_t dir); |
dkato | 0:f782d9c66c49 | 198 | |
dkato | 0:f782d9c66c49 | 199 | /** Read the next directory entry |
dkato | 0:f782d9c66c49 | 200 | * |
dkato | 0:f782d9c66c49 | 201 | * @param dir Dir handle |
dkato | 0:f782d9c66c49 | 202 | * @param ent The directory entry to fill out |
dkato | 0:f782d9c66c49 | 203 | * @return 1 on reading a filename, 0 at end of directory, negative error on failure |
dkato | 0:f782d9c66c49 | 204 | */ |
dkato | 0:f782d9c66c49 | 205 | virtual ssize_t dir_read(fs_dir_t dir, struct dirent *ent); |
dkato | 0:f782d9c66c49 | 206 | |
dkato | 0:f782d9c66c49 | 207 | /** Set the current position of the directory |
dkato | 0:f782d9c66c49 | 208 | * |
dkato | 0:f782d9c66c49 | 209 | * @param dir Dir handle |
dkato | 0:f782d9c66c49 | 210 | * @param offset Offset of the location to seek to, |
dkato | 0:f782d9c66c49 | 211 | * must be a value returned from dir_tell |
dkato | 0:f782d9c66c49 | 212 | */ |
dkato | 0:f782d9c66c49 | 213 | virtual void dir_seek(fs_dir_t dir, off_t offset); |
dkato | 0:f782d9c66c49 | 214 | |
dkato | 0:f782d9c66c49 | 215 | /** Get the current position of the directory |
dkato | 0:f782d9c66c49 | 216 | * |
dkato | 0:f782d9c66c49 | 217 | * @param dir Dir handle |
dkato | 0:f782d9c66c49 | 218 | * @return Position of the directory that can be passed to dir_rewind |
dkato | 0:f782d9c66c49 | 219 | */ |
dkato | 0:f782d9c66c49 | 220 | virtual off_t dir_tell(fs_dir_t dir); |
dkato | 0:f782d9c66c49 | 221 | |
dkato | 0:f782d9c66c49 | 222 | /** Rewind the current position to the beginning of the directory |
dkato | 0:f782d9c66c49 | 223 | * |
dkato | 0:f782d9c66c49 | 224 | * @param dir Dir handle |
dkato | 0:f782d9c66c49 | 225 | */ |
dkato | 0:f782d9c66c49 | 226 | virtual void dir_rewind(fs_dir_t dir); |
dkato | 0:f782d9c66c49 | 227 | |
dkato | 0:f782d9c66c49 | 228 | private: |
dkato | 0:f782d9c66c49 | 229 | FATFS _fs; // Work area (file system object) for logical drive |
dkato | 0:f782d9c66c49 | 230 | char _fsid[2]; |
dkato | 0:f782d9c66c49 | 231 | int _id; |
dkato | 0:f782d9c66c49 | 232 | |
dkato | 0:f782d9c66c49 | 233 | protected: |
dkato | 0:f782d9c66c49 | 234 | virtual void lock(); |
dkato | 0:f782d9c66c49 | 235 | virtual void unlock(); |
dkato | 0:f782d9c66c49 | 236 | }; |
dkato | 0:f782d9c66c49 | 237 | |
dkato | 0:f782d9c66c49 | 238 | #endif |