Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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