Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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