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