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