mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 31 06:02:27 2019 +0000
Revision:
1:9db0e321a9f4
Parent:
platform/mbed_board.c@0:5b88d5760320
updated based on mbed-os5.15.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 0:5b88d5760320 1 /* mbed Microcontroller Library
kenjiArai 0:5b88d5760320 2 * Copyright (c) 2006-2013 ARM Limited
kenjiArai 0:5b88d5760320 3 * SPDX-License-Identifier: Apache-2.0
kenjiArai 0:5b88d5760320 4 *
kenjiArai 0:5b88d5760320 5 * Licensed under the Apache License, Version 2.0 (the "License");
kenjiArai 0:5b88d5760320 6 * you may not use this file except in compliance with the License.
kenjiArai 0:5b88d5760320 7 * You may obtain a copy of the License at
kenjiArai 0:5b88d5760320 8 *
kenjiArai 0:5b88d5760320 9 * http://www.apache.org/licenses/LICENSE-2.0
kenjiArai 0:5b88d5760320 10 *
kenjiArai 0:5b88d5760320 11 * Unless required by applicable law or agreed to in writing, software
kenjiArai 0:5b88d5760320 12 * distributed under the License is distributed on an "AS IS" BASIS,
kenjiArai 0:5b88d5760320 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kenjiArai 0:5b88d5760320 14 * See the License for the specific language governing permissions and
kenjiArai 0:5b88d5760320 15 * limitations under the License.
kenjiArai 0:5b88d5760320 16 */
kenjiArai 0:5b88d5760320 17 #include <stdio.h>
kenjiArai 0:5b88d5760320 18 #include <string.h>
kenjiArai 0:5b88d5760320 19 #include "hal/gpio_api.h"
kenjiArai 0:5b88d5760320 20 #include "platform/mbed_wait_api.h"
kenjiArai 0:5b88d5760320 21 #include "platform/mbed_toolchain.h"
kenjiArai 0:5b88d5760320 22 #include "platform/mbed_interface.h"
kenjiArai 0:5b88d5760320 23 #include "platform/mbed_retarget.h"
kenjiArai 0:5b88d5760320 24 #include "platform/mbed_critical.h"
kenjiArai 0:5b88d5760320 25
kenjiArai 0:5b88d5760320 26 WEAK MBED_NORETURN void mbed_die(void)
kenjiArai 0:5b88d5760320 27 {
kenjiArai 0:5b88d5760320 28 #if !defined (NRF51_H) && !defined(TARGET_EFM32)
kenjiArai 0:5b88d5760320 29 core_util_critical_section_enter();
kenjiArai 0:5b88d5760320 30 #endif
kenjiArai 0:5b88d5760320 31 gpio_t led_err;
kenjiArai 0:5b88d5760320 32 gpio_init_out(&led_err, LED1);
kenjiArai 0:5b88d5760320 33
kenjiArai 0:5b88d5760320 34 while (1) {
kenjiArai 0:5b88d5760320 35 for (int i = 0; i < 4; ++i) {
kenjiArai 0:5b88d5760320 36 gpio_write(&led_err, 1);
kenjiArai 0:5b88d5760320 37 wait_us(150000);
kenjiArai 0:5b88d5760320 38 gpio_write(&led_err, 0);
kenjiArai 0:5b88d5760320 39 wait_us(150000);
kenjiArai 0:5b88d5760320 40 }
kenjiArai 0:5b88d5760320 41
kenjiArai 0:5b88d5760320 42 for (int i = 0; i < 4; ++i) {
kenjiArai 0:5b88d5760320 43 gpio_write(&led_err, 1);
kenjiArai 0:5b88d5760320 44 wait_us(400000);
kenjiArai 0:5b88d5760320 45 gpio_write(&led_err, 0);
kenjiArai 0:5b88d5760320 46 wait_us(400000);
kenjiArai 0:5b88d5760320 47 }
kenjiArai 0:5b88d5760320 48 }
kenjiArai 0:5b88d5760320 49 }
kenjiArai 0:5b88d5760320 50
kenjiArai 0:5b88d5760320 51 void mbed_error_printf(const char *format, ...)
kenjiArai 0:5b88d5760320 52 {
kenjiArai 0:5b88d5760320 53 va_list arg;
kenjiArai 0:5b88d5760320 54 va_start(arg, format);
kenjiArai 0:5b88d5760320 55 mbed_error_vprintf(format, arg);
kenjiArai 0:5b88d5760320 56 va_end(arg);
kenjiArai 0:5b88d5760320 57 }
kenjiArai 0:5b88d5760320 58
kenjiArai 0:5b88d5760320 59 void mbed_error_vprintf(const char *format, va_list arg)
kenjiArai 0:5b88d5760320 60 {
kenjiArai 0:5b88d5760320 61 char buffer[132];
kenjiArai 0:5b88d5760320 62 int size = vsnprintf(buffer, sizeof buffer, format, arg);
kenjiArai 0:5b88d5760320 63 if ((unsigned int)size >= sizeof buffer) {
kenjiArai 0:5b88d5760320 64 /* Output was truncated - indicate by overwriting tail of buffer
kenjiArai 0:5b88d5760320 65 * with ellipsis, newline and null terminator.
kenjiArai 0:5b88d5760320 66 */
kenjiArai 0:5b88d5760320 67 static const char ellipsis[] = "...\n";
kenjiArai 0:5b88d5760320 68 memcpy(&buffer[sizeof buffer - sizeof ellipsis], ellipsis, sizeof ellipsis);
kenjiArai 0:5b88d5760320 69 }
kenjiArai 0:5b88d5760320 70 if (size > 0) {
kenjiArai 0:5b88d5760320 71 mbed_error_puts(buffer);
kenjiArai 0:5b88d5760320 72 }
kenjiArai 0:5b88d5760320 73 }
kenjiArai 0:5b88d5760320 74
kenjiArai 0:5b88d5760320 75 void mbed_error_puts(const char *str)
kenjiArai 0:5b88d5760320 76 {
kenjiArai 0:5b88d5760320 77 // Writing the string to the console in a critical section is
kenjiArai 0:5b88d5760320 78 // potentially beneficial - for example in UARTSerial it
kenjiArai 0:5b88d5760320 79 // forces the "unbuffered" mode that makes sure all characters
kenjiArai 0:5b88d5760320 80 // go out now. If we made the call not in a critical section,
kenjiArai 0:5b88d5760320 81 // it would go to the software buffer and we would be reliant
kenjiArai 0:5b88d5760320 82 // on platform.stdio-flush-at-exit forcing a fsync before
kenjiArai 0:5b88d5760320 83 // entering mbed_die().
kenjiArai 0:5b88d5760320 84 //
kenjiArai 0:5b88d5760320 85 // But this may be the very first write to the console, and hence
kenjiArai 0:5b88d5760320 86 // require it to be initialized - doing this in a critical
kenjiArai 0:5b88d5760320 87 // section could be problematic. So we prime it outside the
kenjiArai 0:5b88d5760320 88 // critical section with a zero-length write - this forces
kenjiArai 0:5b88d5760320 89 // the initialization.
kenjiArai 0:5b88d5760320 90 //
kenjiArai 0:5b88d5760320 91 // It's still possible that we were in a critical section
kenjiArai 0:5b88d5760320 92 // or interrupt on entry anyway (eg if this is an error coming
kenjiArai 0:5b88d5760320 93 // from inside RTX), so in other areas of the system we suppress
kenjiArai 0:5b88d5760320 94 // things like mutex creation asserts and RTX traps while
kenjiArai 0:5b88d5760320 95 // an error is in progress, so that console initialization
kenjiArai 0:5b88d5760320 96 // may work.
kenjiArai 0:5b88d5760320 97 write(STDERR_FILENO, str, 0);
kenjiArai 0:5b88d5760320 98
kenjiArai 0:5b88d5760320 99 core_util_critical_section_enter();
kenjiArai 0:5b88d5760320 100 #if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES || MBED_CONF_PLATFORM_STDIO_CONVERT_TTY_NEWLINES
kenjiArai 0:5b88d5760320 101 char stdio_out_prev = '\0';
kenjiArai 0:5b88d5760320 102 for (; *str != '\0'; str++) {
kenjiArai 0:5b88d5760320 103 if (*str == '\n' && stdio_out_prev != '\r') {
kenjiArai 0:5b88d5760320 104 const char cr = '\r';
kenjiArai 0:5b88d5760320 105 write(STDERR_FILENO, &cr, 1);
kenjiArai 0:5b88d5760320 106 }
kenjiArai 0:5b88d5760320 107 write(STDERR_FILENO, str, 1);
kenjiArai 0:5b88d5760320 108 stdio_out_prev = *str;
kenjiArai 0:5b88d5760320 109 }
kenjiArai 0:5b88d5760320 110 #else
kenjiArai 0:5b88d5760320 111 write(STDERR_FILENO, str, strlen(str));
kenjiArai 0:5b88d5760320 112 #endif
kenjiArai 0:5b88d5760320 113 core_util_critical_section_exit();
kenjiArai 0:5b88d5760320 114 }
kenjiArai 0:5b88d5760320 115
kenjiArai 0:5b88d5760320 116 void mbed_error_vfprintf(const char *format, va_list arg)
kenjiArai 0:5b88d5760320 117 {
kenjiArai 0:5b88d5760320 118 mbed_error_vprintf(format, arg);
kenjiArai 0:5b88d5760320 119 }