mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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