From 6bd3f6ae8b957ec3fdc3fbf53b969bfa1dbad69c Mon Sep 17 00:00:00 2001 From: RJ Ryan Date: Fri, 16 Sep 2011 22:14:42 -0400 Subject: Add a libc abort() implementation. The STL in particular relies on abort() for entering an error state. Without an abort() definition, the use of many STL primitives results in a link error because the default implementation of abort() uses _kill, _exit, and _getpid -- none of which are present. My abort() implementation writes an error message to the error USART and enters the throbbing-LED error state. Signed-off-by: RJ Ryan --- libmaple/util.c | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'libmaple') diff --git a/libmaple/util.c b/libmaple/util.c index 1a7c36d..b15d658 100644 --- a/libmaple/util.c +++ b/libmaple/util.c @@ -80,6 +80,16 @@ void __error(void) { throb(); } +/** + * @brief Enable the error USART for writing. + * @sideeffect Configures ERROR_USART appropriately for writing. + */ +void _enable_error_usart() { + gpio_set_mode(ERROR_TX_PORT, ERROR_TX_PIN, GPIO_AF_OUTPUT_PP); + usart_init(ERROR_USART); + usart_set_baud_rate(ERROR_USART, ERROR_USART_CLK_SPEED, ERROR_USART_BAUD); +} + /** * @brief Print an error message on a UART upon a failed assertion * and throb the error LED, if there is one defined. @@ -90,9 +100,7 @@ void __error(void) { */ void _fail(const char* file, int line, const char* exp) { /* Initialize the error USART */ - gpio_set_mode(ERROR_TX_PORT, ERROR_TX_PIN, GPIO_AF_OUTPUT_PP); - usart_init(ERROR_USART); - usart_set_baud_rate(ERROR_USART, ERROR_USART_CLK_SPEED, ERROR_USART_BAUD); + _enable_error_usart(); /* Print failed assert message */ usart_putstr(ERROR_USART, "ERROR: FAILED ASSERT("); @@ -104,19 +112,34 @@ void _fail(const char* file, int line, const char* exp) { usart_putc(ERROR_USART, '\n'); usart_putc(ERROR_USART, '\r'); - /* Error fade */ + /* Shutdown and error fade */ __error(); } /** * @brief Provide an __assert_func handler to libc so that calls to assert() get - * redirected to _fail. + * redirected to _fail. */ void __assert_func(const char* file, int line, const char* method, const char* expression) { _fail(file, line, expression); } +/** + * @brief Provide an abort() implementation that aborts execution and enters an + * error state with the throbbing LED indicator. + */ +void abort() { + /* Initialize the error USART */ + _enable_error_usart(); + + /* Print abort message. */ + usart_putstr(ERROR_USART, "ERROR: PROGRAM ABORTED VIA abort()\n\r"); + + /* Shutdown and error fade */ + __error(); +} + /** * @brief Fades the error LED on and off * @sideeffect Sets output push-pull on ERROR_LED_PIN. -- cgit v1.2.3