mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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