Yihui Xiong / BLE_NODE_TEST

Dependencies:   BLE_API nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Committer:
yihui
Date:
Thu Nov 27 09:30:36 2014 +0000
Revision:
10:22480ac31879
Parent:
9:05f0b5a3a70a
change to new revision hardware

Who changed what in which revision?

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