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_board.c Source File

mbed_board.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 "hal/gpio_api.h"
00019 #include "platform/mbed_wait_api.h"
00020 #include "platform/mbed_toolchain.h"
00021 #include "platform/mbed_interface.h"
00022 #include "platform/mbed_retarget.h"
00023 #include "platform/mbed_critical.h"
00024 
00025 WEAK MBED_NORETURN void mbed_die(void)
00026 {
00027 #if !defined (NRF51_H) && !defined(TARGET_EFM32)
00028     core_util_critical_section_enter();
00029 #endif
00030     gpio_t led_err;
00031     gpio_init_out(&led_err, LED1);
00032 
00033     while (1) {
00034         for (int i = 0; i < 4; ++i) {
00035             gpio_write(&led_err, 1);
00036             wait_us(150000);
00037             gpio_write(&led_err, 0);
00038             wait_us(150000);
00039         }
00040 
00041         for (int i = 0; i < 4; ++i) {
00042             gpio_write(&led_err, 1);
00043             wait_us(400000);
00044             gpio_write(&led_err, 0);
00045             wait_us(400000);
00046         }
00047     }
00048 }
00049 
00050 void mbed_error_printf(const char *format, ...)
00051 {
00052     va_list arg;
00053     va_start(arg, format);
00054     mbed_error_vprintf(format, arg);
00055     va_end(arg);
00056 }
00057 
00058 void mbed_error_vprintf(const char *format, va_list arg)
00059 {
00060     char buffer[132];
00061     int size = vsnprintf(buffer, sizeof buffer, format, arg);
00062     if ((unsigned int)size >= sizeof buffer) {
00063         /* Output was truncated - indicate by overwriting tail of buffer
00064          * with ellipsis, newline and null terminator.
00065          */
00066         static const char ellipsis[] = "...\n";
00067         memcpy(&buffer[sizeof buffer - sizeof ellipsis], ellipsis, sizeof ellipsis);
00068     }
00069     if (size > 0) {
00070         mbed_error_puts(buffer);
00071     }
00072 }
00073 
00074 void mbed_error_puts(const char *str)
00075 {
00076     core_util_critical_section_enter();
00077 #if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES || MBED_CONF_PLATFORM_STDIO_CONVERT_TTY_NEWLINES
00078     char stdio_out_prev = '\0';
00079     for (; *str != '\0'; str++) {
00080         if (*str == '\n' && stdio_out_prev != '\r') {
00081             const char cr = '\r';
00082             write(STDERR_FILENO, &cr, 1);
00083         }
00084         write(STDERR_FILENO, str, 1);
00085         stdio_out_prev = *str;
00086     }
00087 #else
00088     write(STDERR_FILENO, str, strlen(str));
00089 #endif
00090     core_util_critical_section_exit();
00091 }
00092 
00093 void mbed_error_vfprintf(const char *format, va_list arg)
00094 {
00095     mbed_error_vprintf(format, arg);
00096 }