mbed_interface.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 <stdio.h>
  17. #include "platform/mbed_interface.h"
  18. #include "hal/gpio_api.h"
  19. #include "platform/mbed_wait_api.h"
  20. #include "platform/mbed_semihost_api.h"
  21. #include "platform/mbed_error.h"
  22. #include "platform/mbed_toolchain.h"
  23. #if DEVICE_SEMIHOST
  24. // return true if a debugger is attached, indicating mbed interface is connected
  25. int mbed_interface_connected(void)
  26. {
  27. return semihost_connected();
  28. }
  29. int mbed_interface_reset(void)
  30. {
  31. if (mbed_interface_connected()) {
  32. semihost_reset();
  33. return 0;
  34. } else {
  35. return -1;
  36. }
  37. }
  38. WEAK int mbed_interface_uid(char *uid)
  39. {
  40. if (mbed_interface_connected()) {
  41. return semihost_uid(uid); // Returns 0 if successful, -1 on failure
  42. } else {
  43. uid[0] = 0;
  44. return -1;
  45. }
  46. }
  47. int mbed_interface_disconnect(void)
  48. {
  49. int res;
  50. if (mbed_interface_connected()) {
  51. if ((res = semihost_disabledebug()) != 0) {
  52. return res;
  53. }
  54. while (mbed_interface_connected());
  55. return 0;
  56. } else {
  57. return -1;
  58. }
  59. }
  60. int mbed_interface_powerdown(void)
  61. {
  62. int res;
  63. if (mbed_interface_connected()) {
  64. if ((res = semihost_powerdown()) != 0) {
  65. return res;
  66. }
  67. while (mbed_interface_connected());
  68. return 0;
  69. } else {
  70. return -1;
  71. }
  72. }
  73. MBED_DEPRECATED_SINCE("mbed-os-5.9", "This function shouldn't be used in new code."
  74. "For system reset funcionality use system_reset()")
  75. void mbed_reset(void)
  76. {
  77. mbed_interface_reset();
  78. }
  79. WEAK int mbed_uid(char *uid)
  80. {
  81. return mbed_interface_uid(uid);
  82. }
  83. #endif
  84. WEAK void mbed_mac_address(char *mac)
  85. {
  86. #if DEVICE_SEMIHOST
  87. char uid[DEVICE_ID_LENGTH + 1];
  88. int i;
  89. // if we have a UID, extract the MAC
  90. if (mbed_interface_uid(uid) == 0) {
  91. char *p = uid;
  92. #if defined(DEVICE_MAC_OFFSET)
  93. p += DEVICE_MAC_OFFSET;
  94. #endif
  95. for (i = 0; i < 6; i++) {
  96. int byte;
  97. sscanf(p, "%2x", &byte);
  98. mac[i] = byte;
  99. p += 2;
  100. }
  101. mac[0] &= ~0x01; // reset the IG bit in the address; see IEE 802.3-2002, Section 3.2.3(b)
  102. } else { // else return a default MAC
  103. #endif
  104. mac[0] = 0x00;
  105. mac[1] = 0x02;
  106. mac[2] = 0xF7;
  107. mac[3] = 0xF0;
  108. mac[4] = 0x00;
  109. mac[5] = 0x00;
  110. #if DEVICE_SEMIHOST
  111. }
  112. #endif
  113. }