Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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