mbed library for NZ32-SC151

Committer:
modtronix
Date:
Fri Jul 24 21:01:44 2015 +1000
Revision:
1:71204b8406f2
Current mbed v103 (594)

Who changed what in which revision?

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