Devan Lai / SemihostedFileSystem

Dependents:   SemihostingTest

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_semihost_api_shim.c Source File

mbed_semihost_api_shim.c

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "cmsis.h"
00017 #include "mbed_semihost_api_shim.h"
00018 
00019 #include <stdint.h>
00020 #include <string.h>
00021 
00022 // ARM Semihosting Commands
00023 #define SYS_OPEN   (0x1)
00024 #define SYS_CLOSE  (0x2)
00025 #define SYS_WRITE  (0x5)
00026 #define SYS_READ   (0x6)
00027 #define SYS_ISTTY  (0x9)
00028 #define SYS_SEEK   (0xa)
00029 #define SYS_ENSURE (0xb)
00030 #define SYS_FLEN   (0xc)
00031 #define SYS_REMOVE (0xe)
00032 #define SYS_RENAME (0xf)
00033 #define SYS_EXIT   (0x18)
00034 
00035 #if !DEVICE_LOCALFILESYSTEM
00036 FILEHANDLE semihost_open(const char* name, int openmode) {
00037     uint32_t args[3];
00038     args[0] = (uint32_t)name;
00039     args[1] = (uint32_t)openmode;
00040     args[2] = (uint32_t)strlen(name);
00041     return __semihost(SYS_OPEN, args);
00042 }
00043 
00044 int semihost_close(FILEHANDLE fh) {
00045     return __semihost(SYS_CLOSE, &fh);
00046 }
00047 
00048 int semihost_write(FILEHANDLE fh, const unsigned char* buffer, unsigned int length, int mode) {
00049     if (length == 0) return 0;
00050 
00051     uint32_t args[3];
00052     args[0] = (uint32_t)fh;
00053     args[1] = (uint32_t)buffer;
00054     args[2] = (uint32_t)length;
00055     return __semihost(SYS_WRITE, args);
00056 }
00057 
00058 int semihost_read(FILEHANDLE fh, unsigned char* buffer, unsigned int length, int mode) {
00059     uint32_t args[3];
00060     args[0] = (uint32_t)fh;
00061     args[1] = (uint32_t)buffer;
00062     args[2] = (uint32_t)length;
00063     return __semihost(SYS_READ, args);
00064 }
00065 
00066 int semihost_istty(FILEHANDLE fh) {
00067     return __semihost(SYS_ISTTY, &fh);
00068 }
00069 
00070 int semihost_seek(FILEHANDLE fh, long position) {
00071     uint32_t args[2];
00072     args[0] = (uint32_t)fh;
00073     args[1] = (uint32_t)position;
00074     return __semihost(SYS_SEEK, args);
00075 }
00076 
00077 int semihost_ensure(FILEHANDLE fh) {
00078     return __semihost(SYS_ENSURE, &fh);
00079 }
00080 
00081 long semihost_flen(FILEHANDLE fh) {
00082     return __semihost(SYS_FLEN, &fh);
00083 }
00084 
00085 int semihost_remove(const char *name) {
00086     uint32_t args[2];
00087     args[0] = (uint32_t)name;
00088     args[1] = (uint32_t)strlen(name);
00089     return __semihost(SYS_REMOVE, args);
00090 }
00091 
00092 int semihost_rename(const char *old_name, const char *new_name) {
00093     uint32_t args[4];
00094     args[0] = (uint32_t)old_name;
00095     args[1] = (uint32_t)strlen(old_name);
00096     args[0] = (uint32_t)new_name;
00097     args[1] = (uint32_t)strlen(new_name);
00098     return __semihost(SYS_RENAME, args);
00099 }
00100 
00101 #endif