Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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