mbed library sources

Dependents:   bare

Fork of mbed-src by mbed official

Committer:
emilmont
Date:
Mon Jun 10 16:03:00 2013 +0100
Revision:
9:0ce32e54c9a7
Refactoring of the mbed SDK:
- Provide a well defined HAL and API
- Keep separated the HAL implementations for the different targets

Who changed what in which revision?

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