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