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