Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

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 <string.h>
00019 #include "hal/gpio_api.h"
00020 #include "platform/mbed_wait_api.h"
00021 #include "platform/mbed_toolchain.h"
00022 #include "platform/mbed_interface.h"
00023 #include "platform/mbed_retarget.h"
00024 #include "platform/mbed_critical.h"
00025 
00026 WEAK MBED_NORETURN void mbed_die(void)
00027 {
00028 #if !defined (NRF51_H) && !defined(TARGET_EFM32)
00029     core_util_critical_section_enter();
00030 #endif
00031     gpio_t led_err;
00032     gpio_init_out(&led_err, LED1);
00033 
00034     while (1) {
00035         for (int i = 0; i < 4; ++i) {
00036             gpio_write(&led_err, 1);
00037             wait_us(150000);
00038             gpio_write(&led_err, 0);
00039             wait_us(150000);
00040         }
00041 
00042         for (int i = 0; i < 4; ++i) {
00043             gpio_write(&led_err, 1);
00044             wait_us(400000);
00045             gpio_write(&led_err, 0);
00046             wait_us(400000);
00047         }
00048     }
00049 }
00050 
00051 void mbed_error_printf(const char *format, ...)
00052 {
00053     va_list arg;
00054     va_start(arg, format);
00055     mbed_error_vprintf(format, arg);
00056     va_end(arg);
00057 }
00058 
00059 void mbed_error_vprintf(const char *format, va_list arg)
00060 {
00061     char buffer[132];
00062     int size = vsnprintf(buffer, sizeof buffer, format, arg);
00063     if ((unsigned int)size >= sizeof buffer) {
00064         /* Output was truncated - indicate by overwriting tail of buffer
00065          * with ellipsis, newline and null terminator.
00066          */
00067         static const char ellipsis[] = "...\n";
00068         memcpy(&buffer[sizeof buffer - sizeof ellipsis], ellipsis, sizeof ellipsis);
00069     }
00070     if (size > 0) {
00071         mbed_error_puts(buffer);
00072     }
00073 }
00074 
00075 void mbed_error_puts(const char *str)
00076 {
00077     // Writing the string to the console in a critical section is
00078     // potentially beneficial - for example in UARTSerial it
00079     // forces the "unbuffered" mode that makes sure all characters
00080     // go out now. If we made the call not in a critical section,
00081     // it would go to the software buffer and we would be reliant
00082     // on platform.stdio-flush-at-exit forcing a fsync before
00083     // entering mbed_die().
00084     //
00085     // But this may be the very first write to the console, and hence
00086     // require it to be initialized - doing this in a critical
00087     // section could be problematic. So we prime it outside the
00088     // critical section with a zero-length write - this forces
00089     // the initialization.
00090     //
00091     // It's still possible that we were in a critical section
00092     // or interrupt on entry anyway (eg if this is an error coming
00093     // from inside RTX), so in other areas of the system we suppress
00094     // things like mutex creation asserts and RTX traps while
00095     // an error is in progress, so that console initialization
00096     // may work.
00097     write(STDERR_FILENO, str, 0);
00098 
00099     core_util_critical_section_enter();
00100 #if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES || MBED_CONF_PLATFORM_STDIO_CONVERT_TTY_NEWLINES
00101     char stdio_out_prev = '\0';
00102     for (; *str != '\0'; str++) {
00103         if (*str == '\n' && stdio_out_prev != '\r') {
00104             const char cr = '\r';
00105             write(STDERR_FILENO, &cr, 1);
00106         }
00107         write(STDERR_FILENO, str, 1);
00108         stdio_out_prev = *str;
00109     }
00110 #else
00111     write(STDERR_FILENO, str, strlen(str));
00112 #endif
00113     core_util_critical_section_exit();
00114 }
00115 
00116 void mbed_error_vfprintf(const char *format, va_list arg)
00117 {
00118     mbed_error_vprintf(format, arg);
00119 }