mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

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