mbed library sources

Dependents:   FRDM-KL46Z_LCD_Test FRDM-KL46Z_LCD_Test FRDM-KL46Z_Plantilla FRDM-KL46Z_Plantilla ... more

Committer:
ebrus
Date:
Thu Jul 28 15:56:34 2016 +0000
Revision:
0:6bc4ac881c8e
1;

Who changed what in which revision?

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