aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--libmaple/util.c119
-rw-r--r--wirish/rules.mk1
-rw-r--r--wirish/stm32f1/util_hooks.c83
-rw-r--r--wirish/stm32f2/util_hooks.c45
4 files changed, 171 insertions, 77 deletions
diff --git a/libmaple/util.c b/libmaple/util.c
index 88da075..2ee5ede 100644
--- a/libmaple/util.c
+++ b/libmaple/util.c
@@ -26,27 +26,17 @@
/**
* @file libmaple/util.c
- * @brief Utility procedures for debugging, mostly an error LED fade
- * and messages dumped over a UART for failed asserts.
+ * @brief Utility procedures for debugging
*/
#include <libmaple/libmaple.h>
#include <libmaple/usart.h>
#include <libmaple/gpio.h>
#include <libmaple/nvic.h>
-#include <libmaple/adc.h>
-#include <libmaple/timer.h>
-/* FIXME [0.0.13] put this stuff back in some form. Move to Wirish? */
-
-/* Failed ASSERT()s send out a message using this USART config. */
-#ifndef ERROR_USART
-#define ERROR_USART USART2
-#define ERROR_USART_CLK_SPEED STM32_PCLK1
-#define ERROR_USART_BAUD 9600
-#define ERROR_TX_PORT GPIOA
-#define ERROR_TX_PIN 2
-#endif
+/* (Undocumented) hooks used by Wirish to direct our behavior here */
+extern __weak void __lm_error(void);
+extern __weak usart_dev* __lm_enable_error_usart(void);
/* If you define ERROR_LED_PORT and ERROR_LED_PIN, then a failed
* ASSERT() will also throb() an LED connected to that port and pin.
@@ -55,100 +45,75 @@
#define HAVE_ERROR_LED
#endif
-/**
- * @brief Disables all peripheral interrupts except USB and fades the
- * error LED.
- */
/* (Called from exc.S with global interrupts disabled.) */
-void __error(void) {
- /* Turn off peripheral interrupts */
- nvic_irq_disable_all();
-
- /* /\* Turn off timers *\/ */
- /* timer_disable_all(); */
-
- /* /\* Turn off ADC *\/ */
- /* adc_disable_all(); */
-
- /* /\* Turn off all USARTs *\/ */
- /* usart_disable_all(); */
-
- /* /\* Turn the USB interrupt back on so the bootloader keeps on functioning *\/ */
- /* nvic_irq_enable(NVIC_USB_HP_CAN_TX); */
- /* nvic_irq_enable(NVIC_USB_LP_CAN_RX0); */
-
+__attribute__((noreturn)) void __error(void) {
+ if (__lm_error) {
+ __lm_error();
+ }
/* Reenable global interrupts */
nvic_globalirq_enable();
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.
+/*
+ * Print an error message on a UART upon a failed assertion (if one is
+ * available), and punt to __error().
+ *
* @param file Source file of failed assertion
* @param line Source line of failed assertion
* @param exp String representation of failed assertion
* @sideeffect Turns of all peripheral interrupts except USB.
*/
void _fail(const char* file, int line, const char* exp) {
- /* Initialize the error USART */
- /* _enable_error_usart(); */
-
- /* /\* Print failed assert message *\/ */
- /* usart_putstr(ERROR_USART, "ERROR: FAILED ASSERT("); */
- /* usart_putstr(ERROR_USART, exp); */
- /* usart_putstr(ERROR_USART, "): "); */
- /* usart_putstr(ERROR_USART, file); */
- /* usart_putstr(ERROR_USART, ": "); */
- /* usart_putudec(ERROR_USART, line); */
- /* usart_putc(ERROR_USART, '\n'); */
- /* usart_putc(ERROR_USART, '\r'); */
-
+ if (__lm_enable_error_usart) {
+ /* Initialize the error USART */
+ usart_dev *err_usart = __lm_enable_error_usart();
+
+ /* Print failed assert message */
+ usart_putstr(err_usart, "ERROR: FAILED ASSERT(");
+ usart_putstr(err_usart, exp);
+ usart_putstr(err_usart, "): ");
+ usart_putstr(err_usart, file);
+ usart_putstr(err_usart, ": ");
+ usart_putudec(err_usart, line);
+ usart_putc(err_usart, '\n');
+ usart_putc(err_usart, '\r');
+ }
/* Shutdown and error fade */
__error();
}
-/**
- * @brief Provide an __assert_func handler to libc so that calls to assert() get
- * redirected to _fail.
+/*
+ * Provide an __assert_func handler to libc so that calls to assert()
+ * get 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.
+/*
+ * Provide an abort() implementation that aborts execution and punts
+ * to __error().
*/
void abort() {
- /* /\* Initialize the error USART *\/ */
- /* _enable_error_usart(); */
-
- /* /\* Print abort message. *\/ */
- /* usart_putstr(ERROR_USART, "ERROR: PROGRAM ABORTED VIA abort()\n\r"); */
+ if (__lm_enable_error_usart) {
+ /* Initialize the error USART */
+ usart_dev *err_usart = __lm_enable_error_usart();
+ /* Print abort message. */
+ usart_putstr(err_usart, "ERROR: PROGRAM ABORTED VIA abort()\r\n");
+ }
- /* /\* Shutdown and error fade *\/ */
- /* __error(); */
- while (1)
- ;
+ /* Shutdown and error fade */
+ __error();
}
+/* This was public as of v0.0.12, so we've got to keep it public. */
/**
* @brief Fades the error LED on and off
* @sideeffect Sets output push-pull on ERROR_LED_PIN.
*/
-void throb(void) {
+__attribute__((noreturn)) void throb(void) {
#ifdef HAVE_ERROR_LED
int32 slope = 1;
uint32 CC = 0x0000;
diff --git a/wirish/rules.mk b/wirish/rules.mk
index 5138c82..f895213 100644
--- a/wirish/rules.mk
+++ b/wirish/rules.mk
@@ -21,6 +21,7 @@ CFLAGS_$(d) := $(LIBMAPLE_INCLUDES) $(WIRISH_INCLUDES) -I$(d)
sSRCS_$(d) := start.S
cSRCS_$(d) := start_c.c
cSRCS_$(d) += syscalls.c
+cSRCS_$(d) += $(MCU_SERIES)/util_hooks.c
cppSRCS_$(d) := boards.cpp
cppSRCS_$(d) += cxxabi-compat.cpp
cppSRCS_$(d) += ext_interrupts.cpp
diff --git a/wirish/stm32f1/util_hooks.c b/wirish/stm32f1/util_hooks.c
new file mode 100644
index 0000000..8f798a1
--- /dev/null
+++ b/wirish/stm32f1/util_hooks.c
@@ -0,0 +1,83 @@
+/******************************************************************************
+ * The MIT License
+ *
+ * Copyright (c) 2010 Perry Hung (from libmaple/util.c).
+ * Copyright (c) 2012 LeafLabs, LLC.
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *****************************************************************************/
+
+/*
+ * STM32F1 implementations for libmaple/util.c hooks
+ *
+ * These need more love and attention before being made public API
+ * (this includes being easily overridable by user code).
+ */
+
+#include <libmaple/nvic.h>
+#include <libmaple/gpio.h>
+#include <libmaple/stm32.h>
+#include <libmaple/timer.h>
+#include <libmaple/adc.h>
+#include <libmaple/usart.h>
+
+/* Failed ASSERT()s send out a message using this USART config. */
+#ifndef ERROR_USART
+#define ERROR_USART USART2
+#define ERROR_USART_BAUD 9600
+#define ERROR_TX_PORT GPIOA
+#define ERROR_TX_PIN 2
+#endif
+
+/*
+ * Disables all peripheral interrupts except USB (when available),
+ * turns off commonly-used peripherals. Called by __error() with
+ * global interrupts disabled.
+ */
+void __lm_error(void) {
+ /* Turn off peripheral interrupts */
+ nvic_irq_disable_all();
+
+ /* Turn off timers */
+ timer_disable_all();
+
+ /* Turn off ADC */
+ adc_disable_all();
+
+ /* Turn off all USARTs */
+ usart_disable_all();
+
+#if STM32_HAVE_USB
+ /* Turn the USB interrupt back on so the bootloader keeps on functioning */
+ nvic_irq_enable(NVIC_USB_HP_CAN_TX);
+ nvic_irq_enable(NVIC_USB_LP_CAN_RX0);
+#endif
+}
+
+/*
+ * Enable the error USART for writing.
+ */
+usart_dev* __lm_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, USART_USE_PCLK, ERROR_USART_BAUD);
+ return ERROR_USART;
+}
diff --git a/wirish/stm32f2/util_hooks.c b/wirish/stm32f2/util_hooks.c
new file mode 100644
index 0000000..1b519ab
--- /dev/null
+++ b/wirish/stm32f2/util_hooks.c
@@ -0,0 +1,45 @@
+/******************************************************************************
+ * The MIT License
+ *
+ * Copyright (c) 2012 LeafLabs, LLC.
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *****************************************************************************/
+
+/*
+ * STM32F2 implementations for libmaple util.c hooks
+ * FIXME these are just minimal stubs
+ */
+
+#include <libmaple/nvic.h>
+
+/*
+ * Disables all peripheral interrupts. Called by __error() with global
+ * interrupts disabled.
+ */
+void __lm_error(void) {
+ nvic_irq_disable_all();
+}
+
+/*
+ * Enable the error USART for writing.
+ */
+/* usart_dev* __lm_enable_error_usart(void) { (TODO) } */