Make mbed_error not serial-specific

Use write() on current output device instead - this works on the
assumption that write() is safe to call from critical section.

UARTSerial has previously been upgraded to support this, and this also
improves the behaviour when buffered serial is in use - the current
buffered output will be fully flushed before outputting the error
message.
pull/8076/head
Kevin Bracey 2018-09-10 11:15:37 +03:00
parent 1094c08129
commit 78f4b4bc82
2 changed files with 6 additions and 18 deletions

View File

@ -18,13 +18,8 @@
#include "platform/mbed_wait_api.h"
#include "platform/mbed_toolchain.h"
#include "platform/mbed_interface.h"
#include "platform/mbed_retarget.h"
#include "platform/mbed_critical.h"
#include "hal/serial_api.h"
#if DEVICE_SERIAL
extern int stdio_uart_inited;
extern serial_t stdio_uart;
#endif
WEAK void mbed_die(void)
{
@ -61,30 +56,24 @@ void mbed_error_printf(const char *format, ...)
void mbed_error_vfprintf(const char *format, va_list arg)
{
#if DEVICE_SERIAL
#define ERROR_BUF_SIZE (128)
core_util_critical_section_enter();
char buffer[ERROR_BUF_SIZE];
int size = vsnprintf(buffer, ERROR_BUF_SIZE, format, arg);
if (size > 0) {
if (!stdio_uart_inited) {
serial_init(&stdio_uart, STDIO_UART_TX, STDIO_UART_RX);
}
#if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
#if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES || MBED_CONF_PLATFORM_STDIO_CONVERT_TTY_NEWLINES
char stdio_out_prev = '\0';
for (int i = 0; i < size; i++) {
if (buffer[i] == '\n' && stdio_out_prev != '\r') {
serial_putc(&stdio_uart, '\r');
const char cr = '\r';
write(STDERR_FILENO, &cr, 1);
}
serial_putc(&stdio_uart, buffer[i]);
write(STDERR_FILENO, &buffer[i], 1);
stdio_out_prev = buffer[i];
}
#else
for (int i = 0; i < size; i++) {
serial_putc(&stdio_uart, buffer[i]);
}
write(STDERR_FILENO, buffer, size);
#endif
}
core_util_critical_section_exit();
#endif
}

View File

@ -18,7 +18,6 @@
#include "device.h"
#include "platform/mbed_error.h"
#include "platform/mbed_interface.h"
#include "hal/serial_api.h"
#ifndef MBED_FAULT_HANDLER_DISABLED
#include "mbed_rtx_fault_handler.h"