dhgdh

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by joey shelton

Committer:
cyberjoey
Date:
Sat Oct 22 01:31:58 2016 +0000
Revision:
9:6bb35cef007d
Parent:
1:55a6170b404f
WORKING

Who changed what in which revision?

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