mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_semihost_api.c Source File

mbed_semihost_api.c

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "cmsis.h"
00018 #include "platform/mbed_semihost_api.h"
00019 
00020 #include <stdint.h>
00021 #include <string.h>
00022 
00023 #if DEVICE_SEMIHOST
00024 
00025 // ARM Semihosting Commands
00026 #define SYS_OPEN   (0x1)
00027 #define SYS_CLOSE  (0x2)
00028 #define SYS_WRITE  (0x5)
00029 #define SYS_READ   (0x6)
00030 #define SYS_ISTTY  (0x9)
00031 #define SYS_SEEK   (0xa)
00032 #define SYS_ENSURE (0xb)
00033 #define SYS_FLEN   (0xc)
00034 #define SYS_REMOVE (0xe)
00035 #define SYS_RENAME (0xf)
00036 #define SYS_EXIT   (0x18)
00037 
00038 // mbed Semihosting Commands
00039 #define RESERVED_FOR_USER_APPLICATIONS (0x100) // 0x100 - 0x1ff
00040 #define USR_XFFIND      (RESERVED_FOR_USER_APPLICATIONS + 0)
00041 #define USR_UID      (RESERVED_FOR_USER_APPLICATIONS + 1)
00042 #define USR_RESET     (RESERVED_FOR_USER_APPLICATIONS + 2)
00043 #define USR_VBUS     (RESERVED_FOR_USER_APPLICATIONS + 3)
00044 #define USR_POWERDOWN     (RESERVED_FOR_USER_APPLICATIONS + 4)
00045 #define USR_DISABLEDEBUG (RESERVED_FOR_USER_APPLICATIONS + 5)
00046 
00047 #if DEVICE_LOCALFILESYSTEM
00048 FILEHANDLE semihost_open(const char *name, int openmode)
00049 {
00050     uint32_t args[3];
00051     args[0] = (uint32_t)name;
00052     args[1] = (uint32_t)openmode;
00053     args[2] = (uint32_t)strlen(name);
00054     return __semihost(SYS_OPEN, args);
00055 }
00056 
00057 int semihost_close(FILEHANDLE fh)
00058 {
00059     return __semihost(SYS_CLOSE, &fh);
00060 }
00061 
00062 int semihost_write(FILEHANDLE fh, const unsigned char *buffer, unsigned int length, int mode)
00063 {
00064     if (length == 0) {
00065         return 0;
00066     }
00067 
00068     uint32_t args[3];
00069     args[0] = (uint32_t)fh;
00070     args[1] = (uint32_t)buffer;
00071     args[2] = (uint32_t)length;
00072     return __semihost(SYS_WRITE, args);
00073 }
00074 
00075 int semihost_read(FILEHANDLE fh, unsigned char *buffer, unsigned int length, int mode)
00076 {
00077     uint32_t args[3];
00078     args[0] = (uint32_t)fh;
00079     args[1] = (uint32_t)buffer;
00080     args[2] = (uint32_t)length;
00081     return __semihost(SYS_READ, args);
00082 }
00083 
00084 int semihost_istty(FILEHANDLE fh)
00085 {
00086     return __semihost(SYS_ISTTY, &fh);
00087 }
00088 
00089 int semihost_seek(FILEHANDLE fh, long position)
00090 {
00091     uint32_t args[2];
00092     args[0] = (uint32_t)fh;
00093     args[1] = (uint32_t)position;
00094     return __semihost(SYS_SEEK, args);
00095 }
00096 
00097 int semihost_ensure(FILEHANDLE fh)
00098 {
00099     return __semihost(SYS_ENSURE, &fh);
00100 }
00101 
00102 long semihost_flen(FILEHANDLE fh)
00103 {
00104     return __semihost(SYS_FLEN, &fh);
00105 }
00106 
00107 int semihost_remove(const char *name)
00108 {
00109     uint32_t args[2];
00110     args[0] = (uint32_t)name;
00111     args[1] = (uint32_t)strlen(name);
00112     return __semihost(SYS_REMOVE, args);
00113 }
00114 
00115 int semihost_rename(const char *old_name, const char *new_name)
00116 {
00117     uint32_t args[4];
00118     args[0] = (uint32_t)old_name;
00119     args[1] = (uint32_t)strlen(old_name);
00120     args[0] = (uint32_t)new_name;
00121     args[1] = (uint32_t)strlen(new_name);
00122     return __semihost(SYS_RENAME, args);
00123 }
00124 #endif
00125 
00126 int semihost_exit(void)
00127 {
00128     uint32_t args[4];
00129     return __semihost(SYS_EXIT, args);
00130 }
00131 
00132 int semihost_uid(char *uid)
00133 {
00134     uint32_t args[2];
00135     args[0] = (uint32_t)uid;
00136     args[1] = DEVICE_ID_LENGTH + 1;
00137     return __semihost(USR_UID, &args);
00138 }
00139 
00140 int semihost_reset(void)
00141 {
00142     // Does not normally return, however if used with older firmware versions
00143     // that do not support this call it will return -1.
00144     return __semihost(USR_RESET, NULL);
00145 }
00146 
00147 int semihost_vbus(void)
00148 {
00149     return __semihost(USR_VBUS, NULL);
00150 }
00151 
00152 int semihost_powerdown(void)
00153 {
00154     return __semihost(USR_POWERDOWN, NULL);
00155 }
00156 
00157 #if DEVICE_DEBUG_AWARENESS
00158 
00159 int semihost_connected(void)
00160 {
00161     return (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk) ? 1 : 0;
00162 }
00163 
00164 #else
00165 // These processors cannot know if the interface is connect, assume so:
00166 static int is_debugger_attached = 1;
00167 
00168 int semihost_connected(void)
00169 {
00170     return is_debugger_attached;
00171 }
00172 #endif
00173 
00174 int semihost_disabledebug(void)
00175 {
00176     uint32_t args[1];
00177 #if !(DEVICE_DEBUG_AWARENESS)
00178     is_debugger_attached = 0;
00179 #endif
00180     return __semihost(USR_DISABLEDEBUG, &args);
00181 }
00182 
00183 #endif
00184