mbed library for NZ32-SC151

Committer:
modtronix-com
Date:
Fri Aug 19 15:46:42 2016 +1000
Revision:
17:639ed60ce759
Parent:
1:71204b8406f2
Added tag v1.1 for changeset 076cbe3e55be

Who changed what in which revision?

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