Maxim mbed development library

Dependents:   sensomed

Committer:
switches
Date:
Tue Nov 08 18:27:11 2016 +0000
Revision:
0:0e018d759a2a
Initial commit

Who changed what in which revision?

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