Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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