mbed library sources

Fork of mbed-src by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers semihost_api.c Source File

semihost_api.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 "semihost_api.h"
00018 
00019 #include <stdint.h>
00020 #include <string.h>
00021 
00022 #if DEVICE_SEMIHOST
00023 
00024 // ARM Semihosting Commands
00025 #define SYS_OPEN   (0x1)
00026 #define SYS_CLOSE  (0x2)
00027 #define SYS_WRITE  (0x5)
00028 #define SYS_READ   (0x6)
00029 #define SYS_ISTTY  (0x9)
00030 #define SYS_SEEK   (0xa)
00031 #define SYS_ENSURE (0xb)
00032 #define SYS_FLEN   (0xc)
00033 #define SYS_REMOVE (0xe)
00034 #define SYS_RENAME (0xf)
00035 #define SYS_EXIT   (0x18)
00036 
00037 // mbed Semihosting Commands
00038 #define RESERVED_FOR_USER_APPLICATIONS (0x100) // 0x100 - 0x1ff
00039 #define USR_XFFIND      (RESERVED_FOR_USER_APPLICATIONS + 0)
00040 #define USR_UID      (RESERVED_FOR_USER_APPLICATIONS + 1)
00041 #define USR_RESET     (RESERVED_FOR_USER_APPLICATIONS + 2)
00042 #define USR_VBUS     (RESERVED_FOR_USER_APPLICATIONS + 3)
00043 #define USR_POWERDOWN     (RESERVED_FOR_USER_APPLICATIONS + 4)
00044 #define USR_DISABLEDEBUG (RESERVED_FOR_USER_APPLICATIONS + 5)
00045 
00046 #if DEVICE_LOCALFILESYSTEM
00047 FILEHANDLE semihost_open(const char* name, int openmode) {
00048     uint32_t args[3];
00049     args[0] = (uint32_t)name;
00050     args[1] = (uint32_t)openmode;
00051     args[2] = (uint32_t)strlen(name);
00052     return __semihost(SYS_OPEN, args);
00053 }
00054 
00055 int semihost_close(FILEHANDLE fh) {
00056     return __semihost(SYS_CLOSE, &fh);
00057 }
00058 
00059 int semihost_write(FILEHANDLE fh, const unsigned char* buffer, unsigned int length, int mode) {
00060     if (length == 0) return 0;
00061 
00062     uint32_t args[3];
00063     args[0] = (uint32_t)fh;
00064     args[1] = (uint32_t)buffer;
00065     args[2] = (uint32_t)length;
00066     return __semihost(SYS_WRITE, args);
00067 }
00068 
00069 int semihost_read(FILEHANDLE fh, unsigned char* buffer, unsigned int length, int mode) {
00070     uint32_t args[3];
00071     args[0] = (uint32_t)fh;
00072     args[1] = (uint32_t)buffer;
00073     args[2] = (uint32_t)length;
00074     return __semihost(SYS_READ, args);
00075 }
00076 
00077 int semihost_istty(FILEHANDLE fh) {
00078     return __semihost(SYS_ISTTY, &fh);
00079 }
00080 
00081 int semihost_seek(FILEHANDLE fh, long position) {
00082     uint32_t args[2];
00083     args[0] = (uint32_t)fh;
00084     args[1] = (uint32_t)position;
00085     return __semihost(SYS_SEEK, args);
00086 }
00087 
00088 int semihost_ensure(FILEHANDLE fh) {
00089     return __semihost(SYS_ENSURE, &fh);
00090 }
00091 
00092 long semihost_flen(FILEHANDLE fh) {
00093     return __semihost(SYS_FLEN, &fh);
00094 }
00095 
00096 int semihost_remove(const char *name) {
00097     uint32_t args[2];
00098     args[0] = (uint32_t)name;
00099     args[1] = (uint32_t)strlen(name);
00100     return __semihost(SYS_REMOVE, args);
00101 }
00102 
00103 int semihost_rename(const char *old_name, const char *new_name) {
00104     uint32_t args[4];
00105     args[0] = (uint32_t)old_name;
00106     args[1] = (uint32_t)strlen(old_name);
00107     args[0] = (uint32_t)new_name;
00108     args[1] = (uint32_t)strlen(new_name);
00109     return __semihost(SYS_RENAME, args);
00110 }
00111 #endif
00112 
00113 int semihost_exit(void) {
00114     uint32_t args[4];
00115     return __semihost(SYS_EXIT, args);
00116 }
00117 
00118 int semihost_uid(char *uid) {
00119     uint32_t args[2];
00120     args[0] = (uint32_t)uid;
00121     args[1] = DEVICE_ID_LENGTH + 1;
00122     return __semihost(USR_UID, &args);
00123 }
00124 
00125 int semihost_reset(void) {
00126     // Does not normally return, however if used with older firmware versions
00127     // that do not support this call it will return -1.
00128     return __semihost(USR_RESET, NULL);
00129 }
00130 
00131 int semihost_vbus(void) {
00132     return __semihost(USR_VBUS, NULL);
00133 }
00134 
00135 int semihost_powerdown(void) {
00136     return __semihost(USR_POWERDOWN, NULL);
00137 }
00138 
00139 #if DEVICE_DEBUG_AWARENESS
00140 
00141 int semihost_connected(void) {
00142     return (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk) ? 1 : 0;
00143 }
00144 
00145 #else
00146 // These processors cannot know if the interface is connect, assume so:
00147 static int is_debugger_attached = 1;
00148 
00149 int semihost_connected(void) {
00150     return is_debugger_attached;
00151 }
00152 #endif
00153 
00154 int semihost_disabledebug(void) {
00155 #if !(DEVICE_DEBUG_AWARENESS)
00156     is_debugger_attached = 0;
00157 #endif
00158     return __semihost(USR_DISABLEDEBUG, NULL);
00159 }
00160 
00161 #endif
00162