MCU driver/HAL for the Picocell Gateway concentrator board. The firmware implements either a USB CDC protocol or a UART protocol to bridge commands coming from host to the SX1308 SPI interface.

Committer:
dgabino
Date:
Wed Apr 11 14:42:47 2018 +0000
Revision:
0:c76361bd82e8
Initial commit

Who changed what in which revision?

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