5.2.1 - Updated I2C files
Dependents: mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510
drivers/LocalFileSystem.cpp@0:098463de4c5d, 2017-01-25 (annotated)
- Committer:
- group-onsemi
- Date:
- Wed Jan 25 20:34:15 2017 +0000
- Revision:
- 0:098463de4c5d
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
group-onsemi | 0:098463de4c5d | 1 | /* mbed Microcontroller Library |
group-onsemi | 0:098463de4c5d | 2 | * Copyright (c) 2006-2013 ARM Limited |
group-onsemi | 0:098463de4c5d | 3 | * |
group-onsemi | 0:098463de4c5d | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
group-onsemi | 0:098463de4c5d | 5 | * you may not use this file except in compliance with the License. |
group-onsemi | 0:098463de4c5d | 6 | * You may obtain a copy of the License at |
group-onsemi | 0:098463de4c5d | 7 | * |
group-onsemi | 0:098463de4c5d | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
group-onsemi | 0:098463de4c5d | 9 | * |
group-onsemi | 0:098463de4c5d | 10 | * Unless required by applicable law or agreed to in writing, software |
group-onsemi | 0:098463de4c5d | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
group-onsemi | 0:098463de4c5d | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
group-onsemi | 0:098463de4c5d | 13 | * See the License for the specific language governing permissions and |
group-onsemi | 0:098463de4c5d | 14 | * limitations under the License. |
group-onsemi | 0:098463de4c5d | 15 | */ |
group-onsemi | 0:098463de4c5d | 16 | #include "drivers/LocalFileSystem.h" |
group-onsemi | 0:098463de4c5d | 17 | |
group-onsemi | 0:098463de4c5d | 18 | #if DEVICE_LOCALFILESYSTEM |
group-onsemi | 0:098463de4c5d | 19 | |
group-onsemi | 0:098463de4c5d | 20 | #include "platform/semihost_api.h" |
group-onsemi | 0:098463de4c5d | 21 | #include <string.h> |
group-onsemi | 0:098463de4c5d | 22 | #include <stdio.h> |
group-onsemi | 0:098463de4c5d | 23 | |
group-onsemi | 0:098463de4c5d | 24 | namespace mbed { |
group-onsemi | 0:098463de4c5d | 25 | |
group-onsemi | 0:098463de4c5d | 26 | /* Extension to FINFO type defined in RTL.h (in Keil RL) - adds 'create time'. */ |
group-onsemi | 0:098463de4c5d | 27 | typedef struct { |
group-onsemi | 0:098463de4c5d | 28 | unsigned char hr; /* Hours [0..23] */ |
group-onsemi | 0:098463de4c5d | 29 | unsigned char min; /* Minutes [0..59] */ |
group-onsemi | 0:098463de4c5d | 30 | unsigned char sec; /* Seconds [0..59] */ |
group-onsemi | 0:098463de4c5d | 31 | unsigned char day; /* Day [1..31] */ |
group-onsemi | 0:098463de4c5d | 32 | unsigned char mon; /* Month [1..12] */ |
group-onsemi | 0:098463de4c5d | 33 | unsigned short year; /* Year [1980..2107] */ |
group-onsemi | 0:098463de4c5d | 34 | } FTIME; |
group-onsemi | 0:098463de4c5d | 35 | |
group-onsemi | 0:098463de4c5d | 36 | typedef struct { /* File Search info record */ |
group-onsemi | 0:098463de4c5d | 37 | char name[32]; /* File name */ |
group-onsemi | 0:098463de4c5d | 38 | long size; /* File size in bytes */ |
group-onsemi | 0:098463de4c5d | 39 | int fileID; /* System File Identification */ |
group-onsemi | 0:098463de4c5d | 40 | FTIME create_time; /* Date & time file was created */ |
group-onsemi | 0:098463de4c5d | 41 | FTIME write_time; /* Date & time of last write */ |
group-onsemi | 0:098463de4c5d | 42 | } XFINFO; |
group-onsemi | 0:098463de4c5d | 43 | |
group-onsemi | 0:098463de4c5d | 44 | #define RESERVED_FOR_USER_APPLICATIONS (0x100) /* 0x100 - 0x1ff */ |
group-onsemi | 0:098463de4c5d | 45 | #define USR_XFFIND (RESERVED_FOR_USER_APPLICATIONS + 0) |
group-onsemi | 0:098463de4c5d | 46 | |
group-onsemi | 0:098463de4c5d | 47 | static int xffind (const char *pattern, XFINFO *info) { |
group-onsemi | 0:098463de4c5d | 48 | unsigned param[4]; |
group-onsemi | 0:098463de4c5d | 49 | |
group-onsemi | 0:098463de4c5d | 50 | param[0] = (unsigned long)pattern; |
group-onsemi | 0:098463de4c5d | 51 | param[1] = (unsigned long)strlen(pattern); |
group-onsemi | 0:098463de4c5d | 52 | param[2] = (unsigned long)info; |
group-onsemi | 0:098463de4c5d | 53 | param[3] = (unsigned long)sizeof(XFINFO); |
group-onsemi | 0:098463de4c5d | 54 | |
group-onsemi | 0:098463de4c5d | 55 | return __semihost(USR_XFFIND, param); |
group-onsemi | 0:098463de4c5d | 56 | } |
group-onsemi | 0:098463de4c5d | 57 | |
group-onsemi | 0:098463de4c5d | 58 | #define OPEN_R 0 |
group-onsemi | 0:098463de4c5d | 59 | #define OPEN_B 1 |
group-onsemi | 0:098463de4c5d | 60 | #define OPEN_PLUS 2 |
group-onsemi | 0:098463de4c5d | 61 | #define OPEN_W 4 |
group-onsemi | 0:098463de4c5d | 62 | #define OPEN_A 8 |
group-onsemi | 0:098463de4c5d | 63 | #define OPEN_INVALID -1 |
group-onsemi | 0:098463de4c5d | 64 | |
group-onsemi | 0:098463de4c5d | 65 | int posix_to_semihost_open_flags(int flags) { |
group-onsemi | 0:098463de4c5d | 66 | /* POSIX flags -> semihosting open mode */ |
group-onsemi | 0:098463de4c5d | 67 | int openmode; |
group-onsemi | 0:098463de4c5d | 68 | if (flags & O_RDWR) { |
group-onsemi | 0:098463de4c5d | 69 | /* a plus mode */ |
group-onsemi | 0:098463de4c5d | 70 | openmode = OPEN_PLUS; |
group-onsemi | 0:098463de4c5d | 71 | if (flags & O_APPEND) { |
group-onsemi | 0:098463de4c5d | 72 | openmode |= OPEN_A; |
group-onsemi | 0:098463de4c5d | 73 | } else if (flags & O_TRUNC) { |
group-onsemi | 0:098463de4c5d | 74 | openmode |= OPEN_W; |
group-onsemi | 0:098463de4c5d | 75 | } else { |
group-onsemi | 0:098463de4c5d | 76 | openmode |= OPEN_R; |
group-onsemi | 0:098463de4c5d | 77 | } |
group-onsemi | 0:098463de4c5d | 78 | } else if (flags & O_WRONLY) { |
group-onsemi | 0:098463de4c5d | 79 | /* write or append */ |
group-onsemi | 0:098463de4c5d | 80 | if (flags & O_APPEND) { |
group-onsemi | 0:098463de4c5d | 81 | openmode = OPEN_A; |
group-onsemi | 0:098463de4c5d | 82 | } else { |
group-onsemi | 0:098463de4c5d | 83 | openmode = OPEN_W; |
group-onsemi | 0:098463de4c5d | 84 | } |
group-onsemi | 0:098463de4c5d | 85 | } else if (flags == O_RDONLY) { |
group-onsemi | 0:098463de4c5d | 86 | /* read mode */ |
group-onsemi | 0:098463de4c5d | 87 | openmode = OPEN_R; |
group-onsemi | 0:098463de4c5d | 88 | } else { |
group-onsemi | 0:098463de4c5d | 89 | /* invalid flags */ |
group-onsemi | 0:098463de4c5d | 90 | openmode = OPEN_INVALID; |
group-onsemi | 0:098463de4c5d | 91 | } |
group-onsemi | 0:098463de4c5d | 92 | |
group-onsemi | 0:098463de4c5d | 93 | return openmode; |
group-onsemi | 0:098463de4c5d | 94 | } |
group-onsemi | 0:098463de4c5d | 95 | |
group-onsemi | 0:098463de4c5d | 96 | FILEHANDLE local_file_open(const char* name, int flags) { |
group-onsemi | 0:098463de4c5d | 97 | int openmode = posix_to_semihost_open_flags(flags); |
group-onsemi | 0:098463de4c5d | 98 | if (openmode == OPEN_INVALID) { |
group-onsemi | 0:098463de4c5d | 99 | return (FILEHANDLE)NULL; |
group-onsemi | 0:098463de4c5d | 100 | } |
group-onsemi | 0:098463de4c5d | 101 | |
group-onsemi | 0:098463de4c5d | 102 | FILEHANDLE fh = semihost_open(name, openmode); |
group-onsemi | 0:098463de4c5d | 103 | if (fh == -1) { |
group-onsemi | 0:098463de4c5d | 104 | return (FILEHANDLE)NULL; |
group-onsemi | 0:098463de4c5d | 105 | } |
group-onsemi | 0:098463de4c5d | 106 | |
group-onsemi | 0:098463de4c5d | 107 | return fh; |
group-onsemi | 0:098463de4c5d | 108 | } |
group-onsemi | 0:098463de4c5d | 109 | |
group-onsemi | 0:098463de4c5d | 110 | LocalFileHandle::LocalFileHandle(FILEHANDLE fh) : _fh(fh), pos(0) { |
group-onsemi | 0:098463de4c5d | 111 | // No lock needed in constructor |
group-onsemi | 0:098463de4c5d | 112 | } |
group-onsemi | 0:098463de4c5d | 113 | |
group-onsemi | 0:098463de4c5d | 114 | int LocalFileHandle::close() { |
group-onsemi | 0:098463de4c5d | 115 | int retval = semihost_close(_fh); |
group-onsemi | 0:098463de4c5d | 116 | delete this; |
group-onsemi | 0:098463de4c5d | 117 | return retval; |
group-onsemi | 0:098463de4c5d | 118 | } |
group-onsemi | 0:098463de4c5d | 119 | |
group-onsemi | 0:098463de4c5d | 120 | ssize_t LocalFileHandle::write(const void *buffer, size_t length) { |
group-onsemi | 0:098463de4c5d | 121 | lock(); |
group-onsemi | 0:098463de4c5d | 122 | ssize_t n = semihost_write(_fh, (const unsigned char*)buffer, length, 0); // number of characters not written |
group-onsemi | 0:098463de4c5d | 123 | n = length - n; // number of characters written |
group-onsemi | 0:098463de4c5d | 124 | pos += n; |
group-onsemi | 0:098463de4c5d | 125 | unlock(); |
group-onsemi | 0:098463de4c5d | 126 | return n; |
group-onsemi | 0:098463de4c5d | 127 | } |
group-onsemi | 0:098463de4c5d | 128 | |
group-onsemi | 0:098463de4c5d | 129 | ssize_t LocalFileHandle::read(void *buffer, size_t length) { |
group-onsemi | 0:098463de4c5d | 130 | lock(); |
group-onsemi | 0:098463de4c5d | 131 | ssize_t n = semihost_read(_fh, (unsigned char*)buffer, length, 0); // number of characters not read |
group-onsemi | 0:098463de4c5d | 132 | n = length - n; // number of characters read |
group-onsemi | 0:098463de4c5d | 133 | pos += n; |
group-onsemi | 0:098463de4c5d | 134 | unlock(); |
group-onsemi | 0:098463de4c5d | 135 | return n; |
group-onsemi | 0:098463de4c5d | 136 | } |
group-onsemi | 0:098463de4c5d | 137 | |
group-onsemi | 0:098463de4c5d | 138 | int LocalFileHandle::isatty() { |
group-onsemi | 0:098463de4c5d | 139 | lock(); |
group-onsemi | 0:098463de4c5d | 140 | int ret = semihost_istty(_fh); |
group-onsemi | 0:098463de4c5d | 141 | unlock(); |
group-onsemi | 0:098463de4c5d | 142 | return ret; |
group-onsemi | 0:098463de4c5d | 143 | } |
group-onsemi | 0:098463de4c5d | 144 | |
group-onsemi | 0:098463de4c5d | 145 | off_t LocalFileHandle::lseek(off_t position, int whence) { |
group-onsemi | 0:098463de4c5d | 146 | lock(); |
group-onsemi | 0:098463de4c5d | 147 | if (whence == SEEK_CUR) { |
group-onsemi | 0:098463de4c5d | 148 | position += pos; |
group-onsemi | 0:098463de4c5d | 149 | } else if (whence == SEEK_END) { |
group-onsemi | 0:098463de4c5d | 150 | position += semihost_flen(_fh); |
group-onsemi | 0:098463de4c5d | 151 | } /* otherwise SEEK_SET, so position is fine */ |
group-onsemi | 0:098463de4c5d | 152 | |
group-onsemi | 0:098463de4c5d | 153 | /* Always seems to return -1, so just ignore for now. */ |
group-onsemi | 0:098463de4c5d | 154 | semihost_seek(_fh, position); |
group-onsemi | 0:098463de4c5d | 155 | pos = position; |
group-onsemi | 0:098463de4c5d | 156 | unlock(); |
group-onsemi | 0:098463de4c5d | 157 | return position; |
group-onsemi | 0:098463de4c5d | 158 | } |
group-onsemi | 0:098463de4c5d | 159 | |
group-onsemi | 0:098463de4c5d | 160 | int LocalFileHandle::fsync() { |
group-onsemi | 0:098463de4c5d | 161 | lock(); |
group-onsemi | 0:098463de4c5d | 162 | int ret = semihost_ensure(_fh); |
group-onsemi | 0:098463de4c5d | 163 | unlock(); |
group-onsemi | 0:098463de4c5d | 164 | return ret; |
group-onsemi | 0:098463de4c5d | 165 | } |
group-onsemi | 0:098463de4c5d | 166 | |
group-onsemi | 0:098463de4c5d | 167 | off_t LocalFileHandle::flen() { |
group-onsemi | 0:098463de4c5d | 168 | lock(); |
group-onsemi | 0:098463de4c5d | 169 | off_t off = semihost_flen(_fh); |
group-onsemi | 0:098463de4c5d | 170 | unlock(); |
group-onsemi | 0:098463de4c5d | 171 | return off; |
group-onsemi | 0:098463de4c5d | 172 | } |
group-onsemi | 0:098463de4c5d | 173 | |
group-onsemi | 0:098463de4c5d | 174 | void LocalFileHandle::lock() { |
group-onsemi | 0:098463de4c5d | 175 | _mutex.lock(); |
group-onsemi | 0:098463de4c5d | 176 | } |
group-onsemi | 0:098463de4c5d | 177 | |
group-onsemi | 0:098463de4c5d | 178 | void LocalFileHandle::unlock() { |
group-onsemi | 0:098463de4c5d | 179 | _mutex.unlock(); |
group-onsemi | 0:098463de4c5d | 180 | } |
group-onsemi | 0:098463de4c5d | 181 | |
group-onsemi | 0:098463de4c5d | 182 | class LocalDirHandle : public DirHandle { |
group-onsemi | 0:098463de4c5d | 183 | |
group-onsemi | 0:098463de4c5d | 184 | public: |
group-onsemi | 0:098463de4c5d | 185 | struct dirent cur_entry; |
group-onsemi | 0:098463de4c5d | 186 | XFINFO info; |
group-onsemi | 0:098463de4c5d | 187 | |
group-onsemi | 0:098463de4c5d | 188 | LocalDirHandle() : cur_entry(), info() { |
group-onsemi | 0:098463de4c5d | 189 | } |
group-onsemi | 0:098463de4c5d | 190 | |
group-onsemi | 0:098463de4c5d | 191 | virtual int closedir() { |
group-onsemi | 0:098463de4c5d | 192 | // No lock can be used in destructor |
group-onsemi | 0:098463de4c5d | 193 | delete this; |
group-onsemi | 0:098463de4c5d | 194 | return 0; |
group-onsemi | 0:098463de4c5d | 195 | } |
group-onsemi | 0:098463de4c5d | 196 | |
group-onsemi | 0:098463de4c5d | 197 | virtual struct dirent *readdir() { |
group-onsemi | 0:098463de4c5d | 198 | lock(); |
group-onsemi | 0:098463de4c5d | 199 | if (xffind("*", &info)!=0) { |
group-onsemi | 0:098463de4c5d | 200 | unlock(); |
group-onsemi | 0:098463de4c5d | 201 | return NULL; |
group-onsemi | 0:098463de4c5d | 202 | } |
group-onsemi | 0:098463de4c5d | 203 | memcpy(cur_entry.d_name, info.name, sizeof(info.name)); |
group-onsemi | 0:098463de4c5d | 204 | unlock(); |
group-onsemi | 0:098463de4c5d | 205 | return &cur_entry; |
group-onsemi | 0:098463de4c5d | 206 | } |
group-onsemi | 0:098463de4c5d | 207 | |
group-onsemi | 0:098463de4c5d | 208 | virtual void rewinddir() { |
group-onsemi | 0:098463de4c5d | 209 | lock(); |
group-onsemi | 0:098463de4c5d | 210 | info.fileID = 0; |
group-onsemi | 0:098463de4c5d | 211 | unlock(); |
group-onsemi | 0:098463de4c5d | 212 | } |
group-onsemi | 0:098463de4c5d | 213 | |
group-onsemi | 0:098463de4c5d | 214 | virtual off_t telldir() { |
group-onsemi | 0:098463de4c5d | 215 | lock(); |
group-onsemi | 0:098463de4c5d | 216 | int fileId = info.fileID; |
group-onsemi | 0:098463de4c5d | 217 | unlock(); |
group-onsemi | 0:098463de4c5d | 218 | return fileId; |
group-onsemi | 0:098463de4c5d | 219 | } |
group-onsemi | 0:098463de4c5d | 220 | |
group-onsemi | 0:098463de4c5d | 221 | virtual void seekdir(off_t offset) { |
group-onsemi | 0:098463de4c5d | 222 | lock(); |
group-onsemi | 0:098463de4c5d | 223 | info.fileID = offset; |
group-onsemi | 0:098463de4c5d | 224 | unlock(); |
group-onsemi | 0:098463de4c5d | 225 | } |
group-onsemi | 0:098463de4c5d | 226 | |
group-onsemi | 0:098463de4c5d | 227 | protected: |
group-onsemi | 0:098463de4c5d | 228 | PlatformMutex _mutex; |
group-onsemi | 0:098463de4c5d | 229 | |
group-onsemi | 0:098463de4c5d | 230 | virtual void lock() { |
group-onsemi | 0:098463de4c5d | 231 | _mutex.lock(); |
group-onsemi | 0:098463de4c5d | 232 | } |
group-onsemi | 0:098463de4c5d | 233 | |
group-onsemi | 0:098463de4c5d | 234 | virtual void unlock() { |
group-onsemi | 0:098463de4c5d | 235 | _mutex.unlock(); |
group-onsemi | 0:098463de4c5d | 236 | } |
group-onsemi | 0:098463de4c5d | 237 | }; |
group-onsemi | 0:098463de4c5d | 238 | |
group-onsemi | 0:098463de4c5d | 239 | FileHandle *LocalFileSystem::open(const char* name, int flags) { |
group-onsemi | 0:098463de4c5d | 240 | // No global state modified so function is thread safe |
group-onsemi | 0:098463de4c5d | 241 | |
group-onsemi | 0:098463de4c5d | 242 | /* reject filenames with / in them */ |
group-onsemi | 0:098463de4c5d | 243 | for (const char *tmp = name; *tmp; tmp++) { |
group-onsemi | 0:098463de4c5d | 244 | if (*tmp == '/') { |
group-onsemi | 0:098463de4c5d | 245 | return NULL; |
group-onsemi | 0:098463de4c5d | 246 | } |
group-onsemi | 0:098463de4c5d | 247 | } |
group-onsemi | 0:098463de4c5d | 248 | |
group-onsemi | 0:098463de4c5d | 249 | int openmode = posix_to_semihost_open_flags(flags); |
group-onsemi | 0:098463de4c5d | 250 | if (openmode == OPEN_INVALID) { |
group-onsemi | 0:098463de4c5d | 251 | return NULL; |
group-onsemi | 0:098463de4c5d | 252 | } |
group-onsemi | 0:098463de4c5d | 253 | |
group-onsemi | 0:098463de4c5d | 254 | FILEHANDLE fh = semihost_open(name, openmode); |
group-onsemi | 0:098463de4c5d | 255 | if (fh == -1) { |
group-onsemi | 0:098463de4c5d | 256 | return NULL; |
group-onsemi | 0:098463de4c5d | 257 | } |
group-onsemi | 0:098463de4c5d | 258 | return new LocalFileHandle(fh); |
group-onsemi | 0:098463de4c5d | 259 | } |
group-onsemi | 0:098463de4c5d | 260 | |
group-onsemi | 0:098463de4c5d | 261 | int LocalFileSystem::remove(const char *filename) { |
group-onsemi | 0:098463de4c5d | 262 | // No global state modified so function is thread safe |
group-onsemi | 0:098463de4c5d | 263 | |
group-onsemi | 0:098463de4c5d | 264 | return semihost_remove(filename); |
group-onsemi | 0:098463de4c5d | 265 | } |
group-onsemi | 0:098463de4c5d | 266 | |
group-onsemi | 0:098463de4c5d | 267 | DirHandle *LocalFileSystem::opendir(const char *name) { |
group-onsemi | 0:098463de4c5d | 268 | // No global state modified so function is thread safe |
group-onsemi | 0:098463de4c5d | 269 | |
group-onsemi | 0:098463de4c5d | 270 | return new LocalDirHandle(); |
group-onsemi | 0:098463de4c5d | 271 | } |
group-onsemi | 0:098463de4c5d | 272 | |
group-onsemi | 0:098463de4c5d | 273 | } // namespace mbed |
group-onsemi | 0:098463de4c5d | 274 | |
group-onsemi | 0:098463de4c5d | 275 | #endif |