[352] | 1 | /* mbed Microcontroller Library
|
---|
| 2 | * Copyright (c) 2006-2013 ARM Limited
|
---|
| 3 | *
|
---|
| 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
|
---|
| 7 | *
|
---|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 9 | *
|
---|
| 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.
|
---|
| 15 | */
|
---|
| 16 | #include "cmsis.h"
|
---|
| 17 | #include "platform/mbed_semihost_api.h"
|
---|
| 18 |
|
---|
| 19 | #include <stdint.h>
|
---|
| 20 | #include <string.h>
|
---|
| 21 |
|
---|
| 22 | #if DEVICE_SEMIHOST
|
---|
| 23 |
|
---|
| 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)
|
---|
| 36 |
|
---|
| 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)
|
---|
| 45 |
|
---|
| 46 | #if DEVICE_LOCALFILESYSTEM
|
---|
[374] | 47 | FILEHANDLE semihost_open(const char* name, int openmode)
|
---|
| 48 | {
|
---|
[352] | 49 | uint32_t args[3];
|
---|
| 50 | args[0] = (uint32_t)name;
|
---|
| 51 | args[1] = (uint32_t)openmode;
|
---|
| 52 | args[2] = (uint32_t)strlen(name);
|
---|
| 53 | return __semihost(SYS_OPEN, args);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[374] | 56 | int semihost_close(FILEHANDLE fh)
|
---|
| 57 | {
|
---|
[352] | 58 | return __semihost(SYS_CLOSE, &fh);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[374] | 61 | int semihost_write(FILEHANDLE fh, const unsigned char* buffer, unsigned int length, int mode)
|
---|
| 62 | {
|
---|
| 63 | if (length == 0) {
|
---|
| 64 | return 0;
|
---|
| 65 | }
|
---|
[352] | 66 |
|
---|
| 67 | uint32_t args[3];
|
---|
| 68 | args[0] = (uint32_t)fh;
|
---|
| 69 | args[1] = (uint32_t)buffer;
|
---|
| 70 | args[2] = (uint32_t)length;
|
---|
| 71 | return __semihost(SYS_WRITE, args);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[374] | 74 | int semihost_read(FILEHANDLE fh, unsigned char *buffer, unsigned int length, int mode)
|
---|
| 75 | {
|
---|
[352] | 76 | uint32_t args[3];
|
---|
| 77 | args[0] = (uint32_t)fh;
|
---|
| 78 | args[1] = (uint32_t)buffer;
|
---|
| 79 | args[2] = (uint32_t)length;
|
---|
| 80 | return __semihost(SYS_READ, args);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[374] | 83 | int semihost_istty(FILEHANDLE fh)
|
---|
| 84 | {
|
---|
[352] | 85 | return __semihost(SYS_ISTTY, &fh);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[374] | 88 | int semihost_seek(FILEHANDLE fh, long position)
|
---|
| 89 | {
|
---|
[352] | 90 | uint32_t args[2];
|
---|
| 91 | args[0] = (uint32_t)fh;
|
---|
| 92 | args[1] = (uint32_t)position;
|
---|
| 93 | return __semihost(SYS_SEEK, args);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[374] | 96 | int semihost_ensure(FILEHANDLE fh)
|
---|
| 97 | {
|
---|
[352] | 98 | return __semihost(SYS_ENSURE, &fh);
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[374] | 101 | long semihost_flen(FILEHANDLE fh)
|
---|
| 102 | {
|
---|
[352] | 103 | return __semihost(SYS_FLEN, &fh);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[374] | 106 | int semihost_remove(const char *name)
|
---|
| 107 | {
|
---|
[352] | 108 | uint32_t args[2];
|
---|
| 109 | args[0] = (uint32_t)name;
|
---|
| 110 | args[1] = (uint32_t)strlen(name);
|
---|
| 111 | return __semihost(SYS_REMOVE, args);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[374] | 114 | int semihost_rename(const char *old_name, const char *new_name)
|
---|
| 115 | {
|
---|
[352] | 116 | uint32_t args[4];
|
---|
| 117 | args[0] = (uint32_t)old_name;
|
---|
| 118 | args[1] = (uint32_t)strlen(old_name);
|
---|
| 119 | args[0] = (uint32_t)new_name;
|
---|
| 120 | args[1] = (uint32_t)strlen(new_name);
|
---|
| 121 | return __semihost(SYS_RENAME, args);
|
---|
| 122 | }
|
---|
| 123 | #endif
|
---|
| 124 |
|
---|
[374] | 125 | int semihost_exit(void)
|
---|
| 126 | {
|
---|
[352] | 127 | uint32_t args[4];
|
---|
| 128 | return __semihost(SYS_EXIT, args);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[374] | 131 | int semihost_uid(char *uid)
|
---|
| 132 | {
|
---|
[352] | 133 | uint32_t args[2];
|
---|
| 134 | args[0] = (uint32_t)uid;
|
---|
| 135 | args[1] = DEVICE_ID_LENGTH + 1;
|
---|
| 136 | return __semihost(USR_UID, &args);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[374] | 139 | int semihost_reset(void)
|
---|
| 140 | {
|
---|
[352] | 141 | // Does not normally return, however if used with older firmware versions
|
---|
| 142 | // that do not support this call it will return -1.
|
---|
| 143 | return __semihost(USR_RESET, NULL);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[374] | 146 | int semihost_vbus(void)
|
---|
| 147 | {
|
---|
[352] | 148 | return __semihost(USR_VBUS, NULL);
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[374] | 151 | int semihost_powerdown(void)
|
---|
| 152 | {
|
---|
[352] | 153 | return __semihost(USR_POWERDOWN, NULL);
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | #if DEVICE_DEBUG_AWARENESS
|
---|
| 157 |
|
---|
[374] | 158 | int semihost_connected(void)
|
---|
| 159 | {
|
---|
[352] | 160 | return (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk) ? 1 : 0;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | #else
|
---|
| 164 | // These processors cannot know if the interface is connect, assume so:
|
---|
| 165 | static int is_debugger_attached = 1;
|
---|
| 166 |
|
---|
[374] | 167 | int semihost_connected(void)
|
---|
| 168 | {
|
---|
[352] | 169 | return is_debugger_attached;
|
---|
| 170 | }
|
---|
| 171 | #endif
|
---|
| 172 |
|
---|
[374] | 173 | int semihost_disabledebug(void)
|
---|
| 174 | {
|
---|
[352] | 175 | uint32_t args[1];
|
---|
| 176 | #if !(DEVICE_DEBUG_AWARENESS)
|
---|
| 177 | is_debugger_attached = 0;
|
---|
| 178 | #endif
|
---|
| 179 | return __semihost(USR_DISABLEDEBUG, &args);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | #endif
|
---|
| 183 |
|
---|