mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Committer:
<>
Date:
Fri Sep 02 15:07:44 2016 +0100
Revision:
144:ef7eb2e8f9f7
This updates the lib to the mbed lib v125

Who changed what in which revision?

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