mbed library sources

Committer:
ebrus
Date:
Wed Jul 27 18:35:32 2016 +0000
Revision:
0:0a673c671a56
4

Who changed what in which revision?

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