Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

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