mbed library sources with internal temperature sensor for nucleo f401

Committer:
elessair
Date:
Sat Jan 17 18:03:58 2015 +0000
Revision:
0:7e2bd16f80af
nucleo f401re internal temperature added

Who changed what in which revision?

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