Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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