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