From f36fae273ec84ee2c53a33caa2dddea2d79db0da Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Tue, 15 Nov 2011 12:45:43 -0500 Subject: 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 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 . - 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 . 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 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 --- wirish/HardwareTimer.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'wirish/HardwareTimer.cpp') diff --git a/wirish/HardwareTimer.cpp b/wirish/HardwareTimer.cpp index bd61a89..52257bf 100644 --- a/wirish/HardwareTimer.cpp +++ b/wirish/HardwareTimer.cpp @@ -24,9 +24,9 @@ * SOFTWARE. *****************************************************************************/ -#include "HardwareTimer.h" -#include "boards.h" // for CYCLES_PER_MICROSECOND -#include "wirish_math.h" +#include +#include // for CYCLES_PER_MICROSECOND +#include // TODO [0.1.0] Remove deprecated pieces -- cgit v1.2.3 From 73f2cefbdf6d8f255bc5284c64d315f38ee45616 Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Fri, 1 Jun 2012 03:26:19 -0400 Subject: Bring back HardwareTimer. Untested, but the timers work on F2 (see exampes/test-timers.cpp), so I'm hoping this is mostly OK. Note that there's an issue with TIMER2 and TIMER5 on F2: these timers have 32-bit counters, and the HardwareTimer methods are all based on uint16 (like on F1). I'm sorely tempted to keep this as-is; exposing the extra bits is just extra documentation, and the HardwareTimer interface is already way too complicated. The interface should still _work_; it just hides the fact that you're missing out on the extra bits for some of the timers. Signed-off-by: Marti Bolivar --- wirish/HardwareTimer.cpp | 123 ++++++++++++++++++++++++++++------ wirish/include/wirish/HardwareTimer.h | 2 +- wirish/rules.mk | 2 +- 3 files changed, 104 insertions(+), 23 deletions(-) (limited to 'wirish/HardwareTimer.cpp') diff --git a/wirish/HardwareTimer.cpp b/wirish/HardwareTimer.cpp index 52257bf..ca8ea63 100644 --- a/wirish/HardwareTimer.cpp +++ b/wirish/HardwareTimer.cpp @@ -30,33 +30,114 @@ // TODO [0.1.0] Remove deprecated pieces -#ifdef STM32_MEDIUM_DENSITY -#define NR_TIMERS 4 -#elif defined(STM32_HIGH_DENSITY) -#define NR_TIMERS 8 +#define MAX_RELOAD ((1 << 16) - 1) + +/* + * Big ugly table of timers available on the MCU. Needed by the + * HardwareTimer constructor. Sigh; maybe predefined instances weren't + * such a bad idea. Oh well, at least the HardwareTimer interface is + * officially unstable since v0.0.12; we can always get rid of this + * later. + */ + +#define MAX_NR_TIMERS 17 // No STM32 I've ever heard of has higher than TIMER17 + +static timer_dev *devs[MAX_NR_TIMERS] = { +#if STM32_HAVE_TIMER(1) + TIMER1, +#else + NULL, +#endif +#if STM32_HAVE_TIMER(2) + TIMER2, +#else + NULL, +#endif +#if STM32_HAVE_TIMER(3) + TIMER3, +#else + NULL, +#endif +#if STM32_HAVE_TIMER(4) + TIMER4, +#else + NULL, +#endif +#if STM32_HAVE_TIMER(5) + TIMER5, +#else + NULL, +#endif +#if STM32_HAVE_TIMER(6) + TIMER6, +#else + NULL, +#endif +#if STM32_HAVE_TIMER(7) + TIMER7, +#else + NULL, +#endif +#if STM32_HAVE_TIMER(8) + TIMER8, +#else + NULL, +#endif +#if STM32_HAVE_TIMER(9) + TIMER9, #else -#error "Unsupported density" + NULL, #endif +#if STM32_HAVE_TIMER(10) + TIMER10, +#else + NULL, +#endif +#if STM32_HAVE_TIMER(11) + TIMER11, +#else + NULL, +#endif +#if STM32_HAVE_TIMER(12) + TIMER12, +#else + NULL, +#endif +#if STM32_HAVE_TIMER(13) + TIMER13, +#else + NULL, +#endif +#if STM32_HAVE_TIMER(14) + TIMER14, +#else + NULL, +#endif +#if STM32_HAVE_TIMER(15) + TIMER15, +#else + NULL, +#endif +#if STM32_HAVE_TIMER(16) + TIMER16, +#else + NULL, +#endif +#if STM32_HAVE_TIMER(17) + TIMER17, +#else + NULL, +#endif +}; -#define MAX_RELOAD ((1 << 16) - 1) +/* + * HardwareTimer routines + */ HardwareTimer::HardwareTimer(uint8 timerNum) { - if (timerNum > NR_TIMERS) { - ASSERT(0); - } - timer_dev *devs[] = { - TIMER1, - TIMER2, - TIMER3, - TIMER4, -#ifdef STM32_HIGH_DENSITY - TIMER5, - TIMER6, - TIMER7, - TIMER8, -#endif - }; + ASSERT(timerNum <= MAX_NR_TIMERS); this->dev = devs[timerNum - 1]; + ASSERT(this->dev != NULL); } void HardwareTimer::pause(void) { diff --git a/wirish/include/wirish/HardwareTimer.h b/wirish/include/wirish/HardwareTimer.h index bdcca5d..d322033 100644 --- a/wirish/include/wirish/HardwareTimer.h +++ b/wirish/include/wirish/HardwareTimer.h @@ -313,7 +313,7 @@ extern HardwareTimer Timer3; * Pre-instantiated timer. */ extern HardwareTimer Timer4; -#ifdef STM32_HIGH_DENSITY +#if (STM32_MCU_SERIES == STM32_SERIES_F1) && defined(STM32_HIGH_DENSITY) /** * @brief Deprecated. * diff --git a/wirish/rules.mk b/wirish/rules.mk index deb3906..54c9180 100644 --- a/wirish/rules.mk +++ b/wirish/rules.mk @@ -23,6 +23,7 @@ cSRCS_$(d) := start_c.c cppSRCS_$(d) := boards.cpp cppSRCS_$(d) += cxxabi-compat.cpp cppSRCS_$(d) += HardwareSerial.cpp +cppSRCS_$(d) += HardwareTimer.cpp cppSRCS_$(d) += Print.cpp cppSRCS_$(d) += wirish_analog.cpp cppSRCS_$(d) += wirish_digital.cpp @@ -34,7 +35,6 @@ cppSRCS_$(d) += $(MCU_SERIES)/wirish_digital.cpp cppSRCS_$(d) += $(WIRISH_BOARD_PATH)/board.cpp # TODO: revise these appropriately F2 and put them back in: # HardwareSPI.cpp -# HardwareTimer.cpp # usb_serial.cpp # pwm.cpp # ext_interrupts.cpp -- cgit v1.2.3 From 98cc4bfcace01da7b467280f586bb6844916dea7 Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Sat, 2 Jun 2012 21:55:10 -0400 Subject: HardwareTimer.cpp: save some space with an rcc_clk_id hack. Since rcc_clk_ids for a peripheral now form a contiguous range by peripheral number, we can infer the rcc_clk_id for a timer given its number (e.g., can calculate RCC_TIMER2 given timerNum == 2). This lets us use timer_foreach() to avoid keeping a table of available timers in HardwareTimer.cpp. The implementation is hackish, but can be fixed up later if need be. Signed-off-by: Marti Bolivar --- wirish/HardwareTimer.cpp | 123 ++++++++++------------------------------------- 1 file changed, 26 insertions(+), 97 deletions(-) (limited to 'wirish/HardwareTimer.cpp') diff --git a/wirish/HardwareTimer.cpp b/wirish/HardwareTimer.cpp index ca8ea63..c35804c 100644 --- a/wirish/HardwareTimer.cpp +++ b/wirish/HardwareTimer.cpp @@ -25,7 +25,10 @@ *****************************************************************************/ #include -#include // for CYCLES_PER_MICROSECOND + +#include // for rcc_clk_id +#include // for CYCLES_PER_MICROSECOND +#include // for noInterrupts(), interrupts() #include // TODO [0.1.0] Remove deprecated pieces @@ -33,110 +36,36 @@ #define MAX_RELOAD ((1 << 16) - 1) /* - * Big ugly table of timers available on the MCU. Needed by the - * HardwareTimer constructor. Sigh; maybe predefined instances weren't - * such a bad idea. Oh well, at least the HardwareTimer interface is - * officially unstable since v0.0.12; we can always get rid of this - * later. + * Evil hack to infer this->dev from timerNum in the HardwareTimer + * constructor. See: + * + * http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.2 + * http://yosefk.com/c++fqa/function.html#fqa-33.2 */ -#define MAX_NR_TIMERS 17 // No STM32 I've ever heard of has higher than TIMER17 - -static timer_dev *devs[MAX_NR_TIMERS] = { -#if STM32_HAVE_TIMER(1) - TIMER1, -#else - NULL, -#endif -#if STM32_HAVE_TIMER(2) - TIMER2, -#else - NULL, -#endif -#if STM32_HAVE_TIMER(3) - TIMER3, -#else - NULL, -#endif -#if STM32_HAVE_TIMER(4) - TIMER4, -#else - NULL, -#endif -#if STM32_HAVE_TIMER(5) - TIMER5, -#else - NULL, -#endif -#if STM32_HAVE_TIMER(6) - TIMER6, -#else - NULL, -#endif -#if STM32_HAVE_TIMER(7) - TIMER7, -#else - NULL, -#endif -#if STM32_HAVE_TIMER(8) - TIMER8, -#else - NULL, -#endif -#if STM32_HAVE_TIMER(9) - TIMER9, -#else - NULL, -#endif -#if STM32_HAVE_TIMER(10) - TIMER10, -#else - NULL, -#endif -#if STM32_HAVE_TIMER(11) - TIMER11, -#else - NULL, -#endif -#if STM32_HAVE_TIMER(12) - TIMER12, -#else - NULL, -#endif -#if STM32_HAVE_TIMER(13) - TIMER13, -#else - NULL, -#endif -#if STM32_HAVE_TIMER(14) - TIMER14, -#else - NULL, -#endif -#if STM32_HAVE_TIMER(15) - TIMER15, -#else - NULL, -#endif -#if STM32_HAVE_TIMER(16) - TIMER16, -#else - NULL, -#endif -#if STM32_HAVE_TIMER(17) - TIMER17, -#else - NULL, -#endif -}; +extern "C" { + static timer_dev **this_devp; + static rcc_clk_id this_id; + static void set_this_dev(timer_dev *dev) { + if (dev->clk_id == this_id) { + *this_devp = dev; + } + } +} /* * HardwareTimer routines */ HardwareTimer::HardwareTimer(uint8 timerNum) { - ASSERT(timerNum <= MAX_NR_TIMERS); - this->dev = devs[timerNum - 1]; + rcc_clk_id timerID = (rcc_clk_id)(RCC_TIMER1 + (timerNum - 1)); + this->dev = NULL; + noInterrupts(); // Hack to ensure we're the only ones using + // set_this_dev() and friends. TODO: use a lock. + this_id = timerID; + this_devp = &this->dev; + timer_foreach(set_this_dev); + interrupts(); ASSERT(this->dev != NULL); } -- cgit v1.2.3 From c274dfafc202134e791de1468ba2cf0c9137621e Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Sat, 2 Jun 2012 22:01:29 -0400 Subject: HardwareTimer::setPeriod(): Don't use floating point. I can't believe we've been shipping this for so long. Signed-off-by: Marti Bolivar --- wirish/HardwareTimer.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'wirish/HardwareTimer.cpp') diff --git a/wirish/HardwareTimer.cpp b/wirish/HardwareTimer.cpp index c35804c..ef720ce 100644 --- a/wirish/HardwareTimer.cpp +++ b/wirish/HardwareTimer.cpp @@ -26,10 +26,10 @@ #include -#include // for rcc_clk_id -#include // for CYCLES_PER_MICROSECOND +#include #include // for noInterrupts(), interrupts() #include +#include // for CYCLES_PER_MICROSECOND // TODO [0.1.0] Remove deprecated pieces @@ -112,7 +112,7 @@ uint16 HardwareTimer::setPeriod(uint32 microseconds) { uint32 period_cyc = microseconds * CYCLES_PER_MICROSECOND; uint16 prescaler = (uint16)(period_cyc / MAX_RELOAD + 1); - uint16 overflow = (uint16)round(period_cyc / prescaler); + uint16 overflow = (uint16)((period_cyc + (prescaler / 2)) / prescaler); this->setPrescaleFactor(prescaler); this->setOverflow(overflow); return overflow; -- cgit v1.2.3 From 058c2d284a7f0da96a30b17764676a940e102d93 Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Sat, 2 Jun 2012 22:06:33 -0400 Subject: HardwareTimer.cpp: cosmetics. Signed-off-by: Marti Bolivar --- wirish/HardwareTimer.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'wirish/HardwareTimer.cpp') diff --git a/wirish/HardwareTimer.cpp b/wirish/HardwareTimer.cpp index ef720ce..4f68ad7 100644 --- a/wirish/HardwareTimer.cpp +++ b/wirish/HardwareTimer.cpp @@ -33,8 +33,6 @@ // TODO [0.1.0] Remove deprecated pieces -#define MAX_RELOAD ((1 << 16) - 1) - /* * Evil hack to infer this->dev from timerNum in the HardwareTimer * constructor. See: @@ -102,6 +100,7 @@ void HardwareTimer::setCount(uint16 val) { timer_set_count(this->dev, min(val, ovf)); } +#define MAX_RELOAD ((1 << 16) - 1) uint16 HardwareTimer::setPeriod(uint32 microseconds) { // Not the best way to handle this edge case? if (!microseconds) { -- cgit v1.2.3