PokittoLib with changes to lcd refresh etc.

Dependents:   Pokittris

Fork of Pokitto by Pokitto Community Team

This is a fork by user @Spinal, and is used in Pokittris for testing. Do not import this to your own program.

Committer:
Pokitto
Date:
Sat Oct 07 21:31:12 2017 +0000
Revision:
5:7e5c566b1760
mbed-pokitto integrated

Who changed what in which revision?

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