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