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