Skip to content

Commit 0af0153

Browse files
committed
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.
1 parent c462509 commit 0af0153

File tree

4 files changed

+51
-11
lines changed

4 files changed

+51
-11
lines changed

hal/api/mbed_interface.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#ifndef MBED_INTERFACE_H
1717
#define MBED_INTERFACE_H
1818

19+
#include <stdarg.h>
20+
1921
#include "device.h"
2022

2123
/* Mbed interface mac address
@@ -107,6 +109,20 @@ void mbed_mac_address(char *mac);
107109
*/
108110
void mbed_die(void);
109111

112+
/** Print out an error message. This is typically called when
113+
* hanlding a crash.
114+
*
115+
* @Note Synchronization level: Interrupt safe
116+
*/
117+
void mbed_error_printf(const char* format, ...);
118+
119+
/** Print out an error message. Similar to mbed_error_printf
120+
* but uses a va_list.
121+
*
122+
* @Note Synchronization level: Interrupt safe
123+
*/
124+
void mbed_error_vfprintf(const char * format, va_list arg);
125+
110126
#ifdef __cplusplus
111127
}
112128
#endif

hal/common/assert.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,12 @@
1616
#include "mbed_assert.h"
1717
#include "device.h"
1818

19-
#if DEVICE_STDIO_MESSAGES
20-
#include <stdio.h>
21-
#endif
22-
23-
#include <stdlib.h>
2419
#include "mbed_interface.h"
20+
#include "critical.h"
2521

2622
void mbed_assert_internal(const char *expr, const char *file, int line)
2723
{
28-
#if DEVICE_STDIO_MESSAGES
29-
fprintf(stderr, "mbed assertation failed: %s, file: %s, line %d \n", expr, file, line);
30-
#endif
24+
core_util_critical_section_enter();
25+
mbed_error_printf("mbed assertation failed: %s, file: %s, line %d \n", expr, file, line);
3126
mbed_die();
3227
}

hal/common/board.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
#include <stdio.h>
1617
#include "gpio_api.h"
1718
#include "wait_api.h"
1819
#include "toolchain.h"
1920
#include "mbed_interface.h"
2021
#include "critical.h"
22+
#include "serial_api.h"
23+
24+
#if DEVICE_SERIAL
25+
extern int stdio_uart_inited;
26+
extern serial_t stdio_uart;
27+
#endif
2128

2229
WEAK void mbed_die(void) {
2330
#if !defined (NRF51_H) && !defined(TARGET_EFM32)
@@ -58,3 +65,27 @@ WEAK void mbed_die(void) {
5865
wait_ms(150);
5966
}
6067
}
68+
69+
void mbed_error_printf(const char* format, ...) {
70+
va_list arg;
71+
va_start(arg, format);
72+
mbed_error_vfprintf(format, arg);
73+
va_end(arg);
74+
}
75+
76+
void mbed_error_vfprintf(const char * format, va_list arg) {
77+
#if DEVICE_SERIAL
78+
core_util_critical_section_enter();
79+
char buffer[128];
80+
int size = vsprintf(buffer, format, arg);
81+
if (size > 0) {
82+
if (!stdio_uart_inited) {
83+
serial_init(&stdio_uart, STDIO_UART_TX, STDIO_UART_RX);
84+
}
85+
for (int i = 0; i < size; i++) {
86+
serial_putc(&stdio_uart, buffer[i]);
87+
}
88+
}
89+
core_util_critical_section_exit();
90+
#endif
91+
}

hal/common/error.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@
2323
#endif
2424

2525
WEAK void error(const char* format, ...) {
26-
#if DEVICE_STDIO_MESSAGES
2726
va_list arg;
2827
va_start(arg, format);
29-
vfprintf(stderr, format, arg);
28+
mbed_error_vfprintf(format, arg);
3029
va_end(arg);
31-
#endif
3230
exit(1);
3331
}

0 commit comments

Comments
 (0)