Update assert and error to be interrupt safe

Add printf functions intended for errors which are safe to call from
interrupts.  Update assert and error to use this function.
pull/1863/head
Russ Butler 2016-06-08 13:54:41 +01:00
parent a6f611b706
commit e4f6e1b327
4 changed files with 51 additions and 11 deletions

View File

@ -16,6 +16,8 @@
#ifndef MBED_INTERFACE_H
#define MBED_INTERFACE_H
#include <stdarg.h>
#include "device.h"
/* Mbed interface mac address
@ -107,6 +109,20 @@ void mbed_mac_address(char *mac);
*/
void mbed_die(void);
/** Print out an error message. This is typically called when
* hanlding a crash.
*
* @Note Synchronization level: Interrupt safe
*/
void mbed_error_printf(const char* format, ...);
/** Print out an error message. Similar to mbed_error_printf
* but uses a va_list.
*
* @Note Synchronization level: Interrupt safe
*/
void mbed_error_vfprintf(const char * format, va_list arg);
#ifdef __cplusplus
}
#endif

View File

@ -16,17 +16,12 @@
#include "mbed_assert.h"
#include "device.h"
#if DEVICE_STDIO_MESSAGES
#include <stdio.h>
#endif
#include <stdlib.h>
#include "mbed_interface.h"
#include "critical.h"
void mbed_assert_internal(const char *expr, const char *file, int line)
{
#if DEVICE_STDIO_MESSAGES
fprintf(stderr, "mbed assertation failed: %s, file: %s, line %d \n", expr, file, line);
#endif
core_util_critical_section_enter();
mbed_error_printf("mbed assertation failed: %s, file: %s, line %d \n", expr, file, line);
mbed_die();
}

View File

@ -13,11 +13,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include "gpio_api.h"
#include "wait_api.h"
#include "toolchain.h"
#include "mbed_interface.h"
#include "critical.h"
#include "serial_api.h"
#if DEVICE_SERIAL
extern int stdio_uart_inited;
extern serial_t stdio_uart;
#endif
WEAK void mbed_die(void) {
#if !defined (NRF51_H) && !defined(TARGET_EFM32)
@ -58,3 +65,27 @@ WEAK void mbed_die(void) {
wait_ms(150);
}
}
void mbed_error_printf(const char* format, ...) {
va_list arg;
va_start(arg, format);
mbed_error_vfprintf(format, arg);
va_end(arg);
}
void mbed_error_vfprintf(const char * format, va_list arg) {
#if DEVICE_SERIAL
core_util_critical_section_enter();
char buffer[128];
int size = vsprintf(buffer, format, arg);
if (size > 0) {
if (!stdio_uart_inited) {
serial_init(&stdio_uart, STDIO_UART_TX, STDIO_UART_RX);
}
for (int i = 0; i < size; i++) {
serial_putc(&stdio_uart, buffer[i]);
}
}
core_util_critical_section_exit();
#endif
}

View File

@ -23,11 +23,9 @@
#endif
WEAK void error(const char* format, ...) {
#if DEVICE_STDIO_MESSAGES
va_list arg;
va_start(arg, format);
vfprintf(stderr, format, arg);
mbed_error_vfprintf(format, arg);
va_end(arg);
#endif
exit(1);
}