1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "semihost_api.h"
24 // ARM Semihosting Commands
25 #define SYS_OPEN (0x1)
26 #define SYS_CLOSE (0x2)
27 #define SYS_WRITE (0x5)
28 #define SYS_READ (0x6)
29 #define SYS_ISTTY (0x9)
30 #define SYS_SEEK (0xa)
31 #define SYS_ENSURE (0xb)
32 #define SYS_FLEN (0xc)
33 #define SYS_REMOVE (0xe)
34 #define SYS_RENAME (0xf)
35 #define SYS_EXIT (0x18)
37 // mbed Semihosting Commands
38 #define RESERVED_FOR_USER_APPLICATIONS (0x100) // 0x100 - 0x1ff
39 #define USR_XFFIND (RESERVED_FOR_USER_APPLICATIONS + 0)
40 #define USR_UID (RESERVED_FOR_USER_APPLICATIONS + 1)
41 #define USR_RESET (RESERVED_FOR_USER_APPLICATIONS + 2)
42 #define USR_VBUS (RESERVED_FOR_USER_APPLICATIONS + 3)
43 #define USR_POWERDOWN (RESERVED_FOR_USER_APPLICATIONS + 4)
44 #define USR_DISABLEDEBUG (RESERVED_FOR_USER_APPLICATIONS + 5)
46 #if DEVICE_LOCALFILESYSTEM
47 FILEHANDLE
semihost_open(const char* name
, int openmode
) {
49 args
[0] = (uint32_t)name
;
50 args
[1] = (uint32_t)openmode
;
51 args
[2] = (uint32_t)strlen(name
);
52 return __semihost(SYS_OPEN
, args
);
55 int semihost_close(FILEHANDLE fh
) {
56 return __semihost(SYS_CLOSE
, &fh
);
59 int semihost_write(FILEHANDLE fh
, const unsigned char* buffer
, unsigned int length
, int mode
) {
60 if (length
== 0) return 0;
63 args
[0] = (uint32_t)fh
;
64 args
[1] = (uint32_t)buffer
;
65 args
[2] = (uint32_t)length
;
66 return __semihost(SYS_WRITE
, args
);
69 int semihost_read(FILEHANDLE fh
, unsigned char* buffer
, unsigned int length
, int mode
) {
71 args
[0] = (uint32_t)fh
;
72 args
[1] = (uint32_t)buffer
;
73 args
[2] = (uint32_t)length
;
74 return __semihost(SYS_READ
, args
);
77 int semihost_istty(FILEHANDLE fh
) {
78 return __semihost(SYS_ISTTY
, &fh
);
81 int semihost_seek(FILEHANDLE fh
, long position
) {
83 args
[0] = (uint32_t)fh
;
84 args
[1] = (uint32_t)position
;
85 return __semihost(SYS_SEEK
, args
);
88 int semihost_ensure(FILEHANDLE fh
) {
89 return __semihost(SYS_ENSURE
, &fh
);
92 long semihost_flen(FILEHANDLE fh
) {
93 return __semihost(SYS_FLEN
, &fh
);
96 int semihost_remove(const char *name
) {
98 args
[0] = (uint32_t)name
;
99 args
[1] = (uint32_t)strlen(name
);
100 return __semihost(SYS_REMOVE
, args
);
103 int semihost_rename(const char *old_name
, const char *new_name
) {
105 args
[0] = (uint32_t)old_name
;
106 args
[1] = (uint32_t)strlen(old_name
);
107 args
[0] = (uint32_t)new_name
;
108 args
[1] = (uint32_t)strlen(new_name
);
109 return __semihost(SYS_RENAME
, args
);
113 int semihost_exit(void) {
115 return __semihost(SYS_EXIT
, args
);
118 int semihost_uid(char *uid
) {
120 args
[0] = (uint32_t)uid
;
121 args
[1] = DEVICE_ID_LENGTH
+ 1;
122 return __semihost(USR_UID
, &args
);
125 int semihost_reset(void) {
126 // Does not normally return, however if used with older firmware versions
127 // that do not support this call it will return -1.
128 return __semihost(USR_RESET
, NULL
);
131 int semihost_vbus(void) {
132 return __semihost(USR_VBUS
, NULL
);
135 int semihost_powerdown(void) {
136 return __semihost(USR_POWERDOWN
, NULL
);
139 #if DEVICE_DEBUG_AWARENESS
141 int semihost_connected(void) {
142 return (CoreDebug
->DHCSR
& CoreDebug_DHCSR_C_DEBUGEN_Msk
) ? 1 : 0;
146 // These processors cannot know if the interface is connect, assume so:
147 static int is_debugger_attached
= 1;
149 int semihost_connected(void) {
150 return is_debugger_attached
;
154 int semihost_disabledebug(void) {
155 #if !(DEVICE_DEBUG_AWARENESS)
156 is_debugger_attached
= 0;
158 return __semihost(USR_DISABLEDEBUG
, NULL
);