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