aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRJ Ryan <rryan@mit.edu>2011-09-16 22:14:42 -0400
committerRJ Ryan <rryan@mit.edu>2011-09-16 22:14:42 -0400
commit6bd3f6ae8b957ec3fdc3fbf53b969bfa1dbad69c (patch)
tree09d035f9eebf503ec3c31e5a72a0385d1671137c
parent04f4b667595194a4acd29871226dc22463714686 (diff)
downloadlibrambutan-6bd3f6ae8b957ec3fdc3fbf53b969bfa1dbad69c.tar.gz
librambutan-6bd3f6ae8b957ec3fdc3fbf53b969bfa1dbad69c.zip
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 <rryan@mit.edu>
-rw-r--r--libmaple/util.c33
1 files changed, 28 insertions, 5 deletions
diff --git a/libmaple/util.c b/libmaple/util.c
index 1a7c36d..b15d658 100644
--- a/libmaple/util.c
+++ b/libmaple/util.c
@@ -81,6 +81,16 @@ void __error(void) {
}
/**
+ * @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.
* @param file Source file of failed assertion
@@ -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,13 +112,13 @@ 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) {
@@ -118,6 +126,21 @@ void __assert_func(const char* file, int line, const char* method,
}
/**
+ * @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.
*/