iforce2d Chris / Mbed 2 deprecated ubxDistanceMeter

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Clock.cpp Source File

Clock.cpp

00001 #include <mbed.h>
00002  
00003 // The us ticker is a wrapping uint32_t. We insert interrupts at
00004 // 0, 0x40000000, 0x8000000, and 0xC0000000, rather than just 0 or just 0xFFFFFFFF because there is
00005 // code that calls interrupts that are "very soon" immediately and we don't
00006 // want that. Also because if we only use 0 and 0x80000000 then there is a chance it would
00007 // be considered to be in the past and executed immediately.
00008  
00009 class ExtendedClock : public TimerEvent
00010 {
00011 public:
00012     ExtendedClock()
00013     {
00014         // This also starts the us ticker.
00015         mTriggers = 0;
00016         insert(0x40000000);
00017     }
00018  
00019     float read()
00020     {
00021         return read_us() / 1000000.0f;
00022     }
00023  
00024     uint64_t read_ms()
00025     {
00026         return read_us() / 1000;
00027     }
00028  
00029     uint64_t read_us()
00030     {
00031         return mTriggers * 0x40000000ull + (ticker_read(_ticker_data) & 0x3FFFFFFF);
00032     }
00033  
00034 private:
00035     void handler() 
00036     {
00037         ++mTriggers;
00038         // If this is the first time we've been called (at 0x4...)
00039         // then mTriggers now equals 1 and we want to insert at 0x80000000.
00040         insert((mTriggers+1) * 0x40000000);
00041     }
00042  
00043     // The number of times the us_ticker has rolled over.
00044     uint32_t mTriggers;
00045 };
00046  
00047 static ExtendedClock _GlobalClock;
00048  
00049 // Return the number of seconds since boot.
00050 float clock_s()
00051 {
00052     return _GlobalClock.read();
00053 }
00054  
00055 // Return the number of milliseconds since boot.
00056 uint64_t clock_ms()
00057 {
00058     return _GlobalClock.read_ms();
00059 }
00060  
00061 // Return the number of microseconds since boot.
00062 uint64_t clock_us()
00063 {
00064     return _GlobalClock.read_us();
00065 }