aboutsummaryrefslogtreecommitdiffstats
path: root/libmaple/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'libmaple/util.c')
-rw-r--r--libmaple/util.c69
1 files changed, 53 insertions, 16 deletions
diff --git a/libmaple/util.c b/libmaple/util.c
index 135f005..3408a2e 100644
--- a/libmaple/util.c
+++ b/libmaple/util.c
@@ -34,25 +34,58 @@
#include "adc.h"
#include "timers.h"
-/* Error assert + fade */
-void _fail(const char* file, int line, const char* exp) {
- int32 slope = 1;
- uint32 CC = 0x0000;
- uint32 TOP_CNT = 0x02FF;
- uint32 i = 0;
+/* Failed asserts send out a message on this USART. */
+#ifndef ERROR_USART_NUM
+#define ERROR_USART_NUM USART2
+#define ERROR_USART_BAUD 9600
+#define ERROR_TX_PORT GPIOA_BASE
+#define ERROR_TX_PIN 2
+#endif
+
+/* If you define ERROR_LED_PORT and ERROR_LED_PIN, then a failed
+ assert will also throb an LED connected to that port an pin.
+ FIXME this should work together with wirish somehow. */
+#if defined(ERROR_LED_PORT) && defined(ERROR_LED_PIN)
+#define HAVE_ERROR_LED
+#endif
- /* Turn off interrupts */
+/**
+ * @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();
+ 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_INT_USBHP);
+ nvic_irq_enable(NVIC_INT_USBLP);
+
+ /* Reenable global interrupts */
+ nvic_globalirq_enable();
+ throb();
+}
+
+/**
+ * @brief Prints an error message on a uart upon a failed assertion
+ * and error throbs.
+ * @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 */
gpio_set_mode(ERROR_TX_PORT, ERROR_TX_PIN, GPIO_MODE_AF_OUTPUT_PP);
usart_init(ERROR_USART_NUM, ERROR_USART_BAUD);
@@ -67,18 +100,17 @@ void _fail(const char* file, int line, const char* exp) {
usart_putc(ERROR_USART_NUM, '\n');
usart_putc(ERROR_USART_NUM, '\r');
- /* Turn on the error LED */
- gpio_set_mode(ERROR_LED_PORT, ERROR_LED_PIN, GPIO_MODE_OUTPUT_PP);
-
- /* Turn the USB interrupt back on so the bootloader keeps on functioning */
- nvic_irq_enable(NVIC_INT_USBHP);
- nvic_irq_enable(NVIC_INT_USBLP);
-
/* Error fade */
- throb();
+ __error();
}
+
+/**
+ * @brief Fades the error LED on and off
+ * @sideeffect Sets output push-pull on ERROR_LED_PIN.
+ */
void throb(void) {
+#ifdef HAVE_ERROR_LED
int32 slope = 1;
uint32 CC = 0x0000;
uint32 TOP_CNT = 0x0200;
@@ -105,5 +137,10 @@ void throb(void) {
}
i++;
}
+#else
+ /* No error LED is connected; do nothing. */
+ while (1)
+ ;
+#endif
}