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 "hal/gpio_api.h"
marcozecchini 0:9fca2b23d0ba 18 #include "platform/mbed_wait_api.h"
marcozecchini 0:9fca2b23d0ba 19 #include "platform/mbed_toolchain.h"
marcozecchini 0:9fca2b23d0ba 20 #include "platform/mbed_interface.h"
marcozecchini 0:9fca2b23d0ba 21 #include "platform/mbed_critical.h"
marcozecchini 0:9fca2b23d0ba 22 #include "hal/serial_api.h"
marcozecchini 0:9fca2b23d0ba 23
marcozecchini 0:9fca2b23d0ba 24 #if DEVICE_SERIAL
marcozecchini 0:9fca2b23d0ba 25 extern int stdio_uart_inited;
marcozecchini 0:9fca2b23d0ba 26 extern serial_t stdio_uart;
marcozecchini 0:9fca2b23d0ba 27 #endif
marcozecchini 0:9fca2b23d0ba 28
marcozecchini 0:9fca2b23d0ba 29 WEAK void mbed_die(void) {
marcozecchini 0:9fca2b23d0ba 30 #if !defined (NRF51_H) && !defined(TARGET_EFM32)
marcozecchini 0:9fca2b23d0ba 31 core_util_critical_section_enter();
marcozecchini 0:9fca2b23d0ba 32 #endif
marcozecchini 0:9fca2b23d0ba 33 gpio_t led_err; gpio_init_out(&led_err, LED1);
marcozecchini 0:9fca2b23d0ba 34
marcozecchini 0:9fca2b23d0ba 35 while (1) {
marcozecchini 0:9fca2b23d0ba 36 for (int i = 0; i < 4; ++i) {
marcozecchini 0:9fca2b23d0ba 37 gpio_write(&led_err, 1);
marcozecchini 0:9fca2b23d0ba 38 wait_ms(150);
marcozecchini 0:9fca2b23d0ba 39 gpio_write(&led_err, 0);
marcozecchini 0:9fca2b23d0ba 40 wait_ms(150);
marcozecchini 0:9fca2b23d0ba 41 }
marcozecchini 0:9fca2b23d0ba 42
marcozecchini 0:9fca2b23d0ba 43 for (int i = 0; i < 4; ++i) {
marcozecchini 0:9fca2b23d0ba 44 gpio_write(&led_err, 1);
marcozecchini 0:9fca2b23d0ba 45 wait_ms(400);
marcozecchini 0:9fca2b23d0ba 46 gpio_write(&led_err, 0);
marcozecchini 0:9fca2b23d0ba 47 wait_ms(400);
marcozecchini 0:9fca2b23d0ba 48 }
marcozecchini 0:9fca2b23d0ba 49 }
marcozecchini 0:9fca2b23d0ba 50 }
marcozecchini 0:9fca2b23d0ba 51
marcozecchini 0:9fca2b23d0ba 52 void mbed_error_printf(const char* format, ...) {
marcozecchini 0:9fca2b23d0ba 53 va_list arg;
marcozecchini 0:9fca2b23d0ba 54 va_start(arg, format);
marcozecchini 0:9fca2b23d0ba 55 mbed_error_vfprintf(format, arg);
marcozecchini 0:9fca2b23d0ba 56 va_end(arg);
marcozecchini 0:9fca2b23d0ba 57 }
marcozecchini 0:9fca2b23d0ba 58
marcozecchini 0:9fca2b23d0ba 59 void mbed_error_vfprintf(const char * format, va_list arg) {
marcozecchini 0:9fca2b23d0ba 60 #if DEVICE_SERIAL
marcozecchini 0:9fca2b23d0ba 61 #define ERROR_BUF_SIZE (128)
marcozecchini 0:9fca2b23d0ba 62 core_util_critical_section_enter();
marcozecchini 0:9fca2b23d0ba 63 char buffer[ERROR_BUF_SIZE];
marcozecchini 0:9fca2b23d0ba 64 int size = vsnprintf(buffer, ERROR_BUF_SIZE, format, arg);
marcozecchini 0:9fca2b23d0ba 65 if (size > 0) {
marcozecchini 0:9fca2b23d0ba 66 if (!stdio_uart_inited) {
marcozecchini 0:9fca2b23d0ba 67 serial_init(&stdio_uart, STDIO_UART_TX, STDIO_UART_RX);
marcozecchini 0:9fca2b23d0ba 68 }
marcozecchini 0:9fca2b23d0ba 69 #if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
marcozecchini 0:9fca2b23d0ba 70 char stdio_out_prev = '\0';
marcozecchini 0:9fca2b23d0ba 71 for (int i = 0; i < size; i++) {
marcozecchini 0:9fca2b23d0ba 72 if (buffer[i] == '\n' && stdio_out_prev != '\r') {
marcozecchini 0:9fca2b23d0ba 73 serial_putc(&stdio_uart, '\r');
marcozecchini 0:9fca2b23d0ba 74 }
marcozecchini 0:9fca2b23d0ba 75 serial_putc(&stdio_uart, buffer[i]);
marcozecchini 0:9fca2b23d0ba 76 stdio_out_prev = buffer[i];
marcozecchini 0:9fca2b23d0ba 77 }
marcozecchini 0:9fca2b23d0ba 78 #else
marcozecchini 0:9fca2b23d0ba 79 for (int i = 0; i < size; i++) {
marcozecchini 0:9fca2b23d0ba 80 serial_putc(&stdio_uart, buffer[i]);
marcozecchini 0:9fca2b23d0ba 81 }
marcozecchini 0:9fca2b23d0ba 82 #endif
marcozecchini 0:9fca2b23d0ba 83 }
marcozecchini 0:9fca2b23d0ba 84 core_util_critical_section_exit();
marcozecchini 0:9fca2b23d0ba 85 #endif
marcozecchini 0:9fca2b23d0ba 86 }