Skip to content

Commit

Permalink
Add a libc abort() implementation.
Browse files Browse the repository at this point in the history
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 <rryan@mit.edu>
  • Loading branch information
rryan committed Sep 17, 2011
1 parent c731833 commit c4e0847
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions libmaple/util.c
Expand Up @@ -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.
Expand All @@ -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(");
Expand All @@ -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.
Expand Down

0 comments on commit c4e0847

Please sign in to comment.