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