mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_interface.c Source File

mbed_interface.c

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include <stdio.h>
00018 #include "platform/mbed_interface.h"
00019 
00020 #include "hal/gpio_api.h"
00021 #include "platform/mbed_wait_api.h"
00022 #include "platform/mbed_semihost_api.h"
00023 #include "platform/mbed_error.h"
00024 #include "platform/mbed_toolchain.h"
00025 
00026 #if DEVICE_SEMIHOST
00027 
00028 // return true if a debugger is attached, indicating mbed interface is connected
00029 int mbed_interface_connected(void)
00030 {
00031     return semihost_connected();
00032 }
00033 
00034 int mbed_interface_reset(void)
00035 {
00036     if (mbed_interface_connected()) {
00037         semihost_reset();
00038         return 0;
00039     } else {
00040         return -1;
00041     }
00042 }
00043 
00044 WEAK int mbed_interface_uid(char *uid)
00045 {
00046     if (mbed_interface_connected()) {
00047         return semihost_uid(uid); // Returns 0 if successful, -1 on failure
00048     } else {
00049         uid[0] = 0;
00050         return -1;
00051     }
00052 }
00053 
00054 int mbed_interface_disconnect(void)
00055 {
00056     int res;
00057     if (mbed_interface_connected()) {
00058         if ((res = semihost_disabledebug()) != 0) {
00059             return res;
00060         }
00061         while (mbed_interface_connected());
00062         return 0;
00063     } else {
00064         return -1;
00065     }
00066 }
00067 
00068 int mbed_interface_powerdown(void)
00069 {
00070     int res;
00071     if (mbed_interface_connected()) {
00072         if ((res = semihost_powerdown()) != 0) {
00073             return res;
00074         }
00075         while (mbed_interface_connected());
00076         return 0;
00077     } else {
00078         return -1;
00079     }
00080 }
00081 
00082 MBED_DEPRECATED_SINCE("mbed-os-5.9", "This function shouldn't be used in new code."
00083                       "For system reset funcionality use system_reset()")
00084 void mbed_reset(void)
00085 {
00086     mbed_interface_reset();
00087 }
00088 
00089 WEAK int mbed_uid(char *uid)
00090 {
00091     return mbed_interface_uid(uid);
00092 }
00093 #endif
00094 
00095 WEAK void mbed_mac_address(char *mac)
00096 {
00097 #if DEVICE_SEMIHOST
00098     char uid[DEVICE_ID_LENGTH + 1];
00099     int i;
00100 
00101     // if we have a UID, extract the MAC
00102     if (mbed_interface_uid(uid) == 0) {
00103         char *p = uid;
00104 #if defined(DEVICE_MAC_OFFSET)
00105         p += DEVICE_MAC_OFFSET;
00106 #endif
00107         for (i = 0; i < 6; i++) {
00108             int byte;
00109             sscanf(p, "%2x", &byte);
00110             mac[i] = byte;
00111             p += 2;
00112         }
00113         mac[0] &= ~0x01;    // reset the IG bit in the address; see IEE 802.3-2002, Section 3.2.3(b)
00114     } else {  // else return a default MAC
00115 #endif
00116         mac[0] = 0x00;
00117         mac[1] = 0x02;
00118         mac[2] = 0xF7;
00119         mac[3] = 0xF0;
00120         mac[4] = 0x00;
00121         mac[5] = 0x00;
00122 #if DEVICE_SEMIHOST
00123     }
00124 #endif
00125 }