Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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