libmaple/libmaple/util.c

151 lines
4.6 KiB
C
Raw Permalink Normal View History

2010-09-27 06:40:44 +02:00
/******************************************************************************
* The MIT License
*
* Copyright (c) 2010 Perry Hung.
* Copyright (c) 2011, 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.
2010-09-27 06:40:44 +02:00
*****************************************************************************/
/**
* @file libmaple/util.c
* @brief Utility procedures for debugging
*/
Move public headers to include directories; related cleanups. Move libmaple/*.h to (new) libmaple/include/libmaple/. The new accepted way to include a libmaple header foo.h is with: #include <libmaple/foo.h> This is more polite in terms of the include namespace. It also allows us to e.g. implement the Arduino SPI library at all (which has header SPI.h; providing it was previously impossible on case-insensitive filesystems due to libmaple's spi.h). Similarly for Wirish. The old include style (#include "header.h") is now deprecated. libmaple/*.h: - Change include guard #defines from _FOO_H_ to _LIBMAPLE_FOO_H_. - Add license headers where they're missing - Add conditional extern "C" { ... } blocks where they're missing (they aren't always necessary, but we might was well do it against the future, while we're at it.). - Change includes from #include "foo.h" to #include <libmaple/foo.h>. - Move includes after extern "C". - Remove extra trailing newlines Note that this doesn't include the headers under libmaple/usb/ or libmaple/usb/usb_lib. These will get fixed later. libmaple/*.c: - Change includes from #include "foo.h" to #include <libmaple/foo.h>. Makefile: - Add I$(LIBMAPLE_PATH)/include/libmaple to GLOBAL_FLAGS. This allows for users (including Wirish) to migrate their code, but should go away ASAP, since it slows down compilation. Wirish: - Move wirish/**/*.h to (new) wirish/include/wirish/. This ignores the USB headers, which, as usual, are getting handled after everything else. - Similarly generify wirish/boards/ structure. For each supported board "foo", move wirish/boards/foo.h and wirish/boards/foo.cpp to wirish/boards/foo/include/board/board.h and wirish/boards/foo/board.cpp, respectively. Also remove the #ifdef hacks around the .cpp files. - wirish/rules.mk: put wirish/boards/foo/include in the include path (and add wirish/boards/foo/board.cpp to the list of sources to be compiled). This allows saying: #include <board/board.h> instead of the hack currently in place. We can allow the user to override this setting later to make adding custom board definitions easier. - Disable -Werror in libmaple/rules.mk, as the current USB warnings don't let the olimex_stm32_h103 board compile. We can re-enable -Werror once we've moved the board-specific bits out of libmaple proper. libraries, examples: - Update includes accordingly. - Miscellaneous cosmetic fixups. Signed-off-by: Marti Bolivar <mbolivar@leaflabs.com>
2011-11-15 18:45:43 +01:00
#include <libmaple/libmaple.h>
#include <libmaple/usart.h>
#include <libmaple/gpio.h>
#include <libmaple/nvic.h>
/* (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
2011-04-06 19:57:30 +02:00
* ASSERT() will also throb() an LED connected to that port and pin.
*/
#if defined(ERROR_LED_PORT) && defined(ERROR_LED_PIN)
#define HAVE_ERROR_LED
#endif
2011-04-06 19:57:30 +02:00
/* (Called from exc.S with global interrupts disabled.) */
__attribute__((noreturn)) void __error(void) {
if (__lm_error) {
__lm_error();
}
2011-02-27 23:26:19 +01:00
/* Reenable global interrupts */
nvic_globalirq_enable();
throb();
}
/*
* Print an error message on a UART upon a failed assertion (if one is
* available), and punt to __error().
*
2011-02-27 23:26:19 +01:00
* @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) {
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 */
2011-02-27 23:26:19 +01:00
__error();
}
/*
* 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);
}
/*
* Provide an abort() implementation that aborts execution and punts
* to __error().
*/
void abort() {
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();
}
/* This was public as of v0.0.12, so we've got to keep it public. */
2011-02-27 23:26:19 +01:00
/**
* @brief Fades the error LED on and off
* @sideeffect Sets output push-pull on ERROR_LED_PIN.
*/
__attribute__((noreturn)) void throb(void) {
#ifdef HAVE_ERROR_LED
2010-09-27 06:40:44 +02:00
int32 slope = 1;
uint32 CC = 0x0000;
uint32 TOP_CNT = 0x0200;
uint32 i = 0;
gpio_set_mode(ERROR_LED_PORT, ERROR_LED_PIN, GPIO_MODE_OUTPUT);
2011-04-06 19:57:30 +02:00
/* Error fade. */
2010-09-27 06:40:44 +02:00
while (1) {
if (CC == TOP_CNT) {
slope = -1;
} else if (CC == 0) {
slope = 1;
}
if (i == TOP_CNT) {
CC += slope;
i = 0;
}
if (i < CC) {
gpio_write_bit(ERROR_LED_PORT, ERROR_LED_PIN, 1);
} else {
gpio_write_bit(ERROR_LED_PORT, ERROR_LED_PIN, 0);
}
i++;
}
#else
2011-04-06 19:57:30 +02:00
/* No error LED is defined; do nothing. */
while (1)
;
#endif
}