Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mbed-dev/platform/mbed_interface.c@2:5acdd8565d02, 2017-02-25 (annotated)
- Committer:
- elmot
- Date:
- Sat Feb 25 00:23:53 2017 +0000
- Revision:
- 2:5acdd8565d02
- Parent:
- 1:d0dfbce63a89
Ready to show
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elmot | 1:d0dfbce63a89 | 1 | /* mbed Microcontroller Library |
elmot | 1:d0dfbce63a89 | 2 | * Copyright (c) 2006-2013 ARM Limited |
elmot | 1:d0dfbce63a89 | 3 | * |
elmot | 1:d0dfbce63a89 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
elmot | 1:d0dfbce63a89 | 5 | * you may not use this file except in compliance with the License. |
elmot | 1:d0dfbce63a89 | 6 | * You may obtain a copy of the License at |
elmot | 1:d0dfbce63a89 | 7 | * |
elmot | 1:d0dfbce63a89 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
elmot | 1:d0dfbce63a89 | 9 | * |
elmot | 1:d0dfbce63a89 | 10 | * Unless required by applicable law or agreed to in writing, software |
elmot | 1:d0dfbce63a89 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
elmot | 1:d0dfbce63a89 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
elmot | 1:d0dfbce63a89 | 13 | * See the License for the specific language governing permissions and |
elmot | 1:d0dfbce63a89 | 14 | * limitations under the License. |
elmot | 1:d0dfbce63a89 | 15 | */ |
elmot | 1:d0dfbce63a89 | 16 | #include <stdio.h> |
elmot | 1:d0dfbce63a89 | 17 | #include "platform/mbed_interface.h" |
elmot | 1:d0dfbce63a89 | 18 | |
elmot | 1:d0dfbce63a89 | 19 | #include "hal/gpio_api.h" |
elmot | 1:d0dfbce63a89 | 20 | #include "platform/wait_api.h" |
elmot | 1:d0dfbce63a89 | 21 | #include "platform/semihost_api.h" |
elmot | 1:d0dfbce63a89 | 22 | #include "platform/mbed_error.h" |
elmot | 1:d0dfbce63a89 | 23 | #include "platform/toolchain.h" |
elmot | 1:d0dfbce63a89 | 24 | |
elmot | 1:d0dfbce63a89 | 25 | #if DEVICE_SEMIHOST |
elmot | 1:d0dfbce63a89 | 26 | |
elmot | 1:d0dfbce63a89 | 27 | // return true if a debugger is attached, indicating mbed interface is connected |
elmot | 1:d0dfbce63a89 | 28 | int mbed_interface_connected(void) { |
elmot | 1:d0dfbce63a89 | 29 | return semihost_connected(); |
elmot | 1:d0dfbce63a89 | 30 | } |
elmot | 1:d0dfbce63a89 | 31 | |
elmot | 1:d0dfbce63a89 | 32 | int mbed_interface_reset(void) { |
elmot | 1:d0dfbce63a89 | 33 | if (mbed_interface_connected()) { |
elmot | 1:d0dfbce63a89 | 34 | semihost_reset(); |
elmot | 1:d0dfbce63a89 | 35 | return 0; |
elmot | 1:d0dfbce63a89 | 36 | } else { |
elmot | 1:d0dfbce63a89 | 37 | return -1; |
elmot | 1:d0dfbce63a89 | 38 | } |
elmot | 1:d0dfbce63a89 | 39 | } |
elmot | 1:d0dfbce63a89 | 40 | |
elmot | 1:d0dfbce63a89 | 41 | WEAK int mbed_interface_uid(char *uid) { |
elmot | 1:d0dfbce63a89 | 42 | if (mbed_interface_connected()) { |
elmot | 1:d0dfbce63a89 | 43 | return semihost_uid(uid); // Returns 0 if successful, -1 on failure |
elmot | 1:d0dfbce63a89 | 44 | } else { |
elmot | 1:d0dfbce63a89 | 45 | uid[0] = 0; |
elmot | 1:d0dfbce63a89 | 46 | return -1; |
elmot | 1:d0dfbce63a89 | 47 | } |
elmot | 1:d0dfbce63a89 | 48 | } |
elmot | 1:d0dfbce63a89 | 49 | |
elmot | 1:d0dfbce63a89 | 50 | int mbed_interface_disconnect(void) { |
elmot | 1:d0dfbce63a89 | 51 | int res; |
elmot | 1:d0dfbce63a89 | 52 | if (mbed_interface_connected()) { |
elmot | 1:d0dfbce63a89 | 53 | if ((res = semihost_disabledebug()) != 0) |
elmot | 1:d0dfbce63a89 | 54 | return res; |
elmot | 1:d0dfbce63a89 | 55 | while (mbed_interface_connected()); |
elmot | 1:d0dfbce63a89 | 56 | return 0; |
elmot | 1:d0dfbce63a89 | 57 | } else { |
elmot | 1:d0dfbce63a89 | 58 | return -1; |
elmot | 1:d0dfbce63a89 | 59 | } |
elmot | 1:d0dfbce63a89 | 60 | } |
elmot | 1:d0dfbce63a89 | 61 | |
elmot | 1:d0dfbce63a89 | 62 | int mbed_interface_powerdown(void) { |
elmot | 1:d0dfbce63a89 | 63 | int res; |
elmot | 1:d0dfbce63a89 | 64 | if (mbed_interface_connected()) { |
elmot | 1:d0dfbce63a89 | 65 | if ((res = semihost_powerdown()) != 0) |
elmot | 1:d0dfbce63a89 | 66 | return res; |
elmot | 1:d0dfbce63a89 | 67 | while (mbed_interface_connected()); |
elmot | 1:d0dfbce63a89 | 68 | return 0; |
elmot | 1:d0dfbce63a89 | 69 | } else { |
elmot | 1:d0dfbce63a89 | 70 | return -1; |
elmot | 1:d0dfbce63a89 | 71 | } |
elmot | 1:d0dfbce63a89 | 72 | } |
elmot | 1:d0dfbce63a89 | 73 | |
elmot | 1:d0dfbce63a89 | 74 | // for backward compatibility |
elmot | 1:d0dfbce63a89 | 75 | void mbed_reset(void) { |
elmot | 1:d0dfbce63a89 | 76 | mbed_interface_reset(); |
elmot | 1:d0dfbce63a89 | 77 | } |
elmot | 1:d0dfbce63a89 | 78 | |
elmot | 1:d0dfbce63a89 | 79 | WEAK int mbed_uid(char *uid) { |
elmot | 1:d0dfbce63a89 | 80 | return mbed_interface_uid(uid); |
elmot | 1:d0dfbce63a89 | 81 | } |
elmot | 1:d0dfbce63a89 | 82 | #endif |
elmot | 1:d0dfbce63a89 | 83 | |
elmot | 1:d0dfbce63a89 | 84 | WEAK void mbed_mac_address(char *mac) { |
elmot | 1:d0dfbce63a89 | 85 | #if DEVICE_SEMIHOST |
elmot | 1:d0dfbce63a89 | 86 | char uid[DEVICE_ID_LENGTH + 1]; |
elmot | 1:d0dfbce63a89 | 87 | int i; |
elmot | 1:d0dfbce63a89 | 88 | |
elmot | 1:d0dfbce63a89 | 89 | // if we have a UID, extract the MAC |
elmot | 1:d0dfbce63a89 | 90 | if (mbed_interface_uid(uid) == 0) { |
elmot | 1:d0dfbce63a89 | 91 | char *p = uid; |
elmot | 1:d0dfbce63a89 | 92 | #if defined(DEVICE_MAC_OFFSET) |
elmot | 1:d0dfbce63a89 | 93 | p += DEVICE_MAC_OFFSET; |
elmot | 1:d0dfbce63a89 | 94 | #endif |
elmot | 1:d0dfbce63a89 | 95 | for (i=0; i<6; i++) { |
elmot | 1:d0dfbce63a89 | 96 | int byte; |
elmot | 1:d0dfbce63a89 | 97 | sscanf(p, "%2x", &byte); |
elmot | 1:d0dfbce63a89 | 98 | mac[i] = byte; |
elmot | 1:d0dfbce63a89 | 99 | p += 2; |
elmot | 1:d0dfbce63a89 | 100 | } |
elmot | 1:d0dfbce63a89 | 101 | mac[0] &= ~0x01; // reset the IG bit in the address; see IEE 802.3-2002, Section 3.2.3(b) |
elmot | 1:d0dfbce63a89 | 102 | } else { // else return a default MAC |
elmot | 1:d0dfbce63a89 | 103 | #endif |
elmot | 1:d0dfbce63a89 | 104 | mac[0] = 0x00; |
elmot | 1:d0dfbce63a89 | 105 | mac[1] = 0x02; |
elmot | 1:d0dfbce63a89 | 106 | mac[2] = 0xF7; |
elmot | 1:d0dfbce63a89 | 107 | mac[3] = 0xF0; |
elmot | 1:d0dfbce63a89 | 108 | mac[4] = 0x00; |
elmot | 1:d0dfbce63a89 | 109 | mac[5] = 0x00; |
elmot | 1:d0dfbce63a89 | 110 | #if DEVICE_SEMIHOST |
elmot | 1:d0dfbce63a89 | 111 | } |
elmot | 1:d0dfbce63a89 | 112 | #endif |
elmot | 1:d0dfbce63a89 | 113 | } |