mbed_semihost_api.c 4.3 KB

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