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