mbed library sources. Supersedes mbed-src.
Dependents: Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more
platform/mbed_semihost_api.c@187:0387e8f68319, 2018-09-06 (annotated)
- Committer:
- AnnaBridge
- Date:
- Thu Sep 06 13:40:20 2018 +0100
- Revision:
- 187:0387e8f68319
- Parent:
- 162:e13f6fdb2ac4
- Child:
- 189:f392fc9709a3
mbed-dev library. Release version 163
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 149:156823d33999 | 1 | /* mbed Microcontroller Library |
<> | 149:156823d33999 | 2 | * Copyright (c) 2006-2013 ARM Limited |
<> | 149:156823d33999 | 3 | * |
<> | 149:156823d33999 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
<> | 149:156823d33999 | 5 | * you may not use this file except in compliance with the License. |
<> | 149:156823d33999 | 6 | * You may obtain a copy of the License at |
<> | 149:156823d33999 | 7 | * |
<> | 149:156823d33999 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 149:156823d33999 | 9 | * |
<> | 149:156823d33999 | 10 | * Unless required by applicable law or agreed to in writing, software |
<> | 149:156823d33999 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
<> | 149:156823d33999 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 149:156823d33999 | 13 | * See the License for the specific language governing permissions and |
<> | 149:156823d33999 | 14 | * limitations under the License. |
<> | 149:156823d33999 | 15 | */ |
<> | 149:156823d33999 | 16 | #include "cmsis.h" |
<> | 160:d5399cc887bb | 17 | #include "platform/mbed_semihost_api.h" |
<> | 149:156823d33999 | 18 | |
<> | 149:156823d33999 | 19 | #include <stdint.h> |
<> | 149:156823d33999 | 20 | #include <string.h> |
<> | 149:156823d33999 | 21 | |
<> | 149:156823d33999 | 22 | #if DEVICE_SEMIHOST |
<> | 149:156823d33999 | 23 | |
<> | 149:156823d33999 | 24 | // ARM Semihosting Commands |
<> | 149:156823d33999 | 25 | #define SYS_OPEN (0x1) |
<> | 149:156823d33999 | 26 | #define SYS_CLOSE (0x2) |
<> | 149:156823d33999 | 27 | #define SYS_WRITE (0x5) |
<> | 149:156823d33999 | 28 | #define SYS_READ (0x6) |
<> | 149:156823d33999 | 29 | #define SYS_ISTTY (0x9) |
<> | 149:156823d33999 | 30 | #define SYS_SEEK (0xa) |
<> | 149:156823d33999 | 31 | #define SYS_ENSURE (0xb) |
<> | 149:156823d33999 | 32 | #define SYS_FLEN (0xc) |
<> | 149:156823d33999 | 33 | #define SYS_REMOVE (0xe) |
<> | 149:156823d33999 | 34 | #define SYS_RENAME (0xf) |
<> | 149:156823d33999 | 35 | #define SYS_EXIT (0x18) |
<> | 149:156823d33999 | 36 | |
<> | 149:156823d33999 | 37 | // mbed Semihosting Commands |
<> | 149:156823d33999 | 38 | #define RESERVED_FOR_USER_APPLICATIONS (0x100) // 0x100 - 0x1ff |
<> | 149:156823d33999 | 39 | #define USR_XFFIND (RESERVED_FOR_USER_APPLICATIONS + 0) |
<> | 149:156823d33999 | 40 | #define USR_UID (RESERVED_FOR_USER_APPLICATIONS + 1) |
<> | 149:156823d33999 | 41 | #define USR_RESET (RESERVED_FOR_USER_APPLICATIONS + 2) |
<> | 149:156823d33999 | 42 | #define USR_VBUS (RESERVED_FOR_USER_APPLICATIONS + 3) |
<> | 149:156823d33999 | 43 | #define USR_POWERDOWN (RESERVED_FOR_USER_APPLICATIONS + 4) |
<> | 149:156823d33999 | 44 | #define USR_DISABLEDEBUG (RESERVED_FOR_USER_APPLICATIONS + 5) |
<> | 149:156823d33999 | 45 | |
<> | 149:156823d33999 | 46 | #if DEVICE_LOCALFILESYSTEM |
AnnaBridge | 187:0387e8f68319 | 47 | FILEHANDLE semihost_open(const char *name, int openmode) |
AnnaBridge | 187:0387e8f68319 | 48 | { |
<> | 149:156823d33999 | 49 | uint32_t args[3]; |
<> | 149:156823d33999 | 50 | args[0] = (uint32_t)name; |
<> | 149:156823d33999 | 51 | args[1] = (uint32_t)openmode; |
<> | 149:156823d33999 | 52 | args[2] = (uint32_t)strlen(name); |
<> | 149:156823d33999 | 53 | return __semihost(SYS_OPEN, args); |
<> | 149:156823d33999 | 54 | } |
<> | 149:156823d33999 | 55 | |
AnnaBridge | 187:0387e8f68319 | 56 | int semihost_close(FILEHANDLE fh) |
AnnaBridge | 187:0387e8f68319 | 57 | { |
<> | 149:156823d33999 | 58 | return __semihost(SYS_CLOSE, &fh); |
<> | 149:156823d33999 | 59 | } |
<> | 149:156823d33999 | 60 | |
AnnaBridge | 187:0387e8f68319 | 61 | int semihost_write(FILEHANDLE fh, const unsigned char *buffer, unsigned int length, int mode) |
AnnaBridge | 187:0387e8f68319 | 62 | { |
AnnaBridge | 187:0387e8f68319 | 63 | if (length == 0) { |
AnnaBridge | 187:0387e8f68319 | 64 | return 0; |
AnnaBridge | 187:0387e8f68319 | 65 | } |
<> | 149:156823d33999 | 66 | |
<> | 149:156823d33999 | 67 | uint32_t args[3]; |
<> | 149:156823d33999 | 68 | args[0] = (uint32_t)fh; |
<> | 149:156823d33999 | 69 | args[1] = (uint32_t)buffer; |
<> | 149:156823d33999 | 70 | args[2] = (uint32_t)length; |
<> | 149:156823d33999 | 71 | return __semihost(SYS_WRITE, args); |
<> | 149:156823d33999 | 72 | } |
<> | 149:156823d33999 | 73 | |
AnnaBridge | 187:0387e8f68319 | 74 | int semihost_read(FILEHANDLE fh, unsigned char *buffer, unsigned int length, int mode) |
AnnaBridge | 187:0387e8f68319 | 75 | { |
<> | 149:156823d33999 | 76 | uint32_t args[3]; |
<> | 149:156823d33999 | 77 | args[0] = (uint32_t)fh; |
<> | 149:156823d33999 | 78 | args[1] = (uint32_t)buffer; |
<> | 149:156823d33999 | 79 | args[2] = (uint32_t)length; |
<> | 149:156823d33999 | 80 | return __semihost(SYS_READ, args); |
<> | 149:156823d33999 | 81 | } |
<> | 149:156823d33999 | 82 | |
AnnaBridge | 187:0387e8f68319 | 83 | int semihost_istty(FILEHANDLE fh) |
AnnaBridge | 187:0387e8f68319 | 84 | { |
<> | 149:156823d33999 | 85 | return __semihost(SYS_ISTTY, &fh); |
<> | 149:156823d33999 | 86 | } |
<> | 149:156823d33999 | 87 | |
AnnaBridge | 187:0387e8f68319 | 88 | int semihost_seek(FILEHANDLE fh, long position) |
AnnaBridge | 187:0387e8f68319 | 89 | { |
<> | 149:156823d33999 | 90 | uint32_t args[2]; |
<> | 149:156823d33999 | 91 | args[0] = (uint32_t)fh; |
<> | 149:156823d33999 | 92 | args[1] = (uint32_t)position; |
<> | 149:156823d33999 | 93 | return __semihost(SYS_SEEK, args); |
<> | 149:156823d33999 | 94 | } |
<> | 149:156823d33999 | 95 | |
AnnaBridge | 187:0387e8f68319 | 96 | int semihost_ensure(FILEHANDLE fh) |
AnnaBridge | 187:0387e8f68319 | 97 | { |
<> | 149:156823d33999 | 98 | return __semihost(SYS_ENSURE, &fh); |
<> | 149:156823d33999 | 99 | } |
<> | 149:156823d33999 | 100 | |
AnnaBridge | 187:0387e8f68319 | 101 | long semihost_flen(FILEHANDLE fh) |
AnnaBridge | 187:0387e8f68319 | 102 | { |
<> | 149:156823d33999 | 103 | return __semihost(SYS_FLEN, &fh); |
<> | 149:156823d33999 | 104 | } |
<> | 149:156823d33999 | 105 | |
AnnaBridge | 187:0387e8f68319 | 106 | int semihost_remove(const char *name) |
AnnaBridge | 187:0387e8f68319 | 107 | { |
<> | 149:156823d33999 | 108 | uint32_t args[2]; |
<> | 149:156823d33999 | 109 | args[0] = (uint32_t)name; |
<> | 149:156823d33999 | 110 | args[1] = (uint32_t)strlen(name); |
<> | 149:156823d33999 | 111 | return __semihost(SYS_REMOVE, args); |
<> | 149:156823d33999 | 112 | } |
<> | 149:156823d33999 | 113 | |
AnnaBridge | 187:0387e8f68319 | 114 | int semihost_rename(const char *old_name, const char *new_name) |
AnnaBridge | 187:0387e8f68319 | 115 | { |
<> | 149:156823d33999 | 116 | uint32_t args[4]; |
<> | 149:156823d33999 | 117 | args[0] = (uint32_t)old_name; |
<> | 149:156823d33999 | 118 | args[1] = (uint32_t)strlen(old_name); |
<> | 149:156823d33999 | 119 | args[0] = (uint32_t)new_name; |
<> | 149:156823d33999 | 120 | args[1] = (uint32_t)strlen(new_name); |
<> | 149:156823d33999 | 121 | return __semihost(SYS_RENAME, args); |
<> | 149:156823d33999 | 122 | } |
<> | 149:156823d33999 | 123 | #endif |
<> | 149:156823d33999 | 124 | |
AnnaBridge | 187:0387e8f68319 | 125 | int semihost_exit(void) |
AnnaBridge | 187:0387e8f68319 | 126 | { |
<> | 149:156823d33999 | 127 | uint32_t args[4]; |
<> | 149:156823d33999 | 128 | return __semihost(SYS_EXIT, args); |
<> | 149:156823d33999 | 129 | } |
<> | 149:156823d33999 | 130 | |
AnnaBridge | 187:0387e8f68319 | 131 | int semihost_uid(char *uid) |
AnnaBridge | 187:0387e8f68319 | 132 | { |
<> | 149:156823d33999 | 133 | uint32_t args[2]; |
<> | 149:156823d33999 | 134 | args[0] = (uint32_t)uid; |
<> | 149:156823d33999 | 135 | args[1] = DEVICE_ID_LENGTH + 1; |
<> | 149:156823d33999 | 136 | return __semihost(USR_UID, &args); |
<> | 149:156823d33999 | 137 | } |
<> | 149:156823d33999 | 138 | |
AnnaBridge | 187:0387e8f68319 | 139 | int semihost_reset(void) |
AnnaBridge | 187:0387e8f68319 | 140 | { |
<> | 149:156823d33999 | 141 | // Does not normally return, however if used with older firmware versions |
<> | 149:156823d33999 | 142 | // that do not support this call it will return -1. |
<> | 149:156823d33999 | 143 | return __semihost(USR_RESET, NULL); |
<> | 149:156823d33999 | 144 | } |
<> | 149:156823d33999 | 145 | |
AnnaBridge | 187:0387e8f68319 | 146 | int semihost_vbus(void) |
AnnaBridge | 187:0387e8f68319 | 147 | { |
<> | 149:156823d33999 | 148 | return __semihost(USR_VBUS, NULL); |
<> | 149:156823d33999 | 149 | } |
<> | 149:156823d33999 | 150 | |
AnnaBridge | 187:0387e8f68319 | 151 | int semihost_powerdown(void) |
AnnaBridge | 187:0387e8f68319 | 152 | { |
<> | 149:156823d33999 | 153 | return __semihost(USR_POWERDOWN, NULL); |
<> | 149:156823d33999 | 154 | } |
<> | 149:156823d33999 | 155 | |
<> | 149:156823d33999 | 156 | #if DEVICE_DEBUG_AWARENESS |
<> | 149:156823d33999 | 157 | |
AnnaBridge | 187:0387e8f68319 | 158 | int semihost_connected(void) |
AnnaBridge | 187:0387e8f68319 | 159 | { |
<> | 149:156823d33999 | 160 | return (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk) ? 1 : 0; |
<> | 149:156823d33999 | 161 | } |
<> | 149:156823d33999 | 162 | |
<> | 149:156823d33999 | 163 | #else |
<> | 149:156823d33999 | 164 | // These processors cannot know if the interface is connect, assume so: |
<> | 149:156823d33999 | 165 | static int is_debugger_attached = 1; |
<> | 149:156823d33999 | 166 | |
AnnaBridge | 187:0387e8f68319 | 167 | int semihost_connected(void) |
AnnaBridge | 187:0387e8f68319 | 168 | { |
<> | 149:156823d33999 | 169 | return is_debugger_attached; |
<> | 149:156823d33999 | 170 | } |
<> | 149:156823d33999 | 171 | #endif |
<> | 149:156823d33999 | 172 | |
AnnaBridge | 187:0387e8f68319 | 173 | int semihost_disabledebug(void) |
AnnaBridge | 187:0387e8f68319 | 174 | { |
<> | 162:e13f6fdb2ac4 | 175 | uint32_t args[1]; |
<> | 149:156823d33999 | 176 | #if !(DEVICE_DEBUG_AWARENESS) |
<> | 149:156823d33999 | 177 | is_debugger_attached = 0; |
<> | 149:156823d33999 | 178 | #endif |
<> | 162:e13f6fdb2ac4 | 179 | return __semihost(USR_DISABLEDEBUG, &args); |
<> | 149:156823d33999 | 180 | } |
<> | 149:156823d33999 | 181 | |
<> | 149:156823d33999 | 182 | #endif |
<> | 149:156823d33999 | 183 |