diff options
author | Vegard Storheil Eriksen <zyp@jvnv.net> | 2011-09-04 02:12:45 +0200 |
---|---|---|
committer | Vegard Storheil Eriksen <zyp@jvnv.net> | 2011-09-04 02:12:45 +0200 |
commit | bb2955ea4bb5c0f9431653351a47b373a2391389 (patch) | |
tree | 29e325c8ab76b089cbbd57505f7adc9b3117a440 | |
parent | e9f8369ce8b464f178c778ff8bcc3f54fe70b30c (diff) |
Added systick based sleep().
-rw-r--r-- | main.cpp | 4 | ||||
-rw-r--r-- | thread.cpp | 5 | ||||
-rw-r--r-- | time.cpp | 9 | ||||
-rw-r--r-- | time.h | 28 |
4 files changed, 41 insertions, 5 deletions
@@ -1,6 +1,7 @@ #include "stm32.h" #include "interrupt.h" #include "thread.h" +#include "time.h" #include "ppmsum.h" #include "i2c.h" @@ -69,6 +70,9 @@ uint32_t thstack[1024]; Thread thread(thstack, sizeof(thstack), threadmain); int main() { + // Initialize system timer. + Time::init(); + RCC.enable(RCC.AFIO); RCC.enable(RCC.IOPA); RCC.enable(RCC.IOPB); @@ -26,11 +26,6 @@ inline void __attribute__((naked)) switch_context() { } template<> -void interrupt<Interrupt::SysTick>() { - switch_context(); -} - -template<> void interrupt<Interrupt::SVCall>() { switch_context(); } diff --git a/time.cpp b/time.cpp new file mode 100644 index 0000000..a5d1f0b --- /dev/null +++ b/time.cpp @@ -0,0 +1,9 @@ +#include "time.h" +#include "interrupt.h" + +volatile uint32_t Time::systime; + +template<> +void interrupt<Interrupt::SysTick>() { + Time::systime++; +} @@ -0,0 +1,28 @@ +#ifndef TIME_H +#define TIME_H + +#include "stm32.h" +#include "interrupt.h" +#include "thread.h" + +class Time { + friend void interrupt<Interrupt::SysTick>(); + + private: + static volatile uint32_t systime; + + public: + static void init() { + STK.LOAD = 72000000 / 8 / 1000; // 1000 Hz. + STK.CTRL = 0x03; + } + + static void sleep(uint32_t ms) { + ms += systime; + while(systime < ms) { + Thread::yield(); + } + } +}; + +#endif |