iforce2d Chris
/
ubxDistanceMeter
Displays distance to start location on OLED screen.
Clock.cpp@0:972874f31c98, 2018-03-07 (annotated)
- Committer:
- iforce2d
- Date:
- Wed Mar 07 12:49:14 2018 +0000
- Revision:
- 0:972874f31c98
First commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
iforce2d | 0:972874f31c98 | 1 | #include <mbed.h> |
iforce2d | 0:972874f31c98 | 2 | |
iforce2d | 0:972874f31c98 | 3 | // The us ticker is a wrapping uint32_t. We insert interrupts at |
iforce2d | 0:972874f31c98 | 4 | // 0, 0x40000000, 0x8000000, and 0xC0000000, rather than just 0 or just 0xFFFFFFFF because there is |
iforce2d | 0:972874f31c98 | 5 | // code that calls interrupts that are "very soon" immediately and we don't |
iforce2d | 0:972874f31c98 | 6 | // want that. Also because if we only use 0 and 0x80000000 then there is a chance it would |
iforce2d | 0:972874f31c98 | 7 | // be considered to be in the past and executed immediately. |
iforce2d | 0:972874f31c98 | 8 | |
iforce2d | 0:972874f31c98 | 9 | class ExtendedClock : public TimerEvent |
iforce2d | 0:972874f31c98 | 10 | { |
iforce2d | 0:972874f31c98 | 11 | public: |
iforce2d | 0:972874f31c98 | 12 | ExtendedClock() |
iforce2d | 0:972874f31c98 | 13 | { |
iforce2d | 0:972874f31c98 | 14 | // This also starts the us ticker. |
iforce2d | 0:972874f31c98 | 15 | mTriggers = 0; |
iforce2d | 0:972874f31c98 | 16 | insert(0x40000000); |
iforce2d | 0:972874f31c98 | 17 | } |
iforce2d | 0:972874f31c98 | 18 | |
iforce2d | 0:972874f31c98 | 19 | float read() |
iforce2d | 0:972874f31c98 | 20 | { |
iforce2d | 0:972874f31c98 | 21 | return read_us() / 1000000.0f; |
iforce2d | 0:972874f31c98 | 22 | } |
iforce2d | 0:972874f31c98 | 23 | |
iforce2d | 0:972874f31c98 | 24 | uint64_t read_ms() |
iforce2d | 0:972874f31c98 | 25 | { |
iforce2d | 0:972874f31c98 | 26 | return read_us() / 1000; |
iforce2d | 0:972874f31c98 | 27 | } |
iforce2d | 0:972874f31c98 | 28 | |
iforce2d | 0:972874f31c98 | 29 | uint64_t read_us() |
iforce2d | 0:972874f31c98 | 30 | { |
iforce2d | 0:972874f31c98 | 31 | return mTriggers * 0x40000000ull + (ticker_read(_ticker_data) & 0x3FFFFFFF); |
iforce2d | 0:972874f31c98 | 32 | } |
iforce2d | 0:972874f31c98 | 33 | |
iforce2d | 0:972874f31c98 | 34 | private: |
iforce2d | 0:972874f31c98 | 35 | void handler() |
iforce2d | 0:972874f31c98 | 36 | { |
iforce2d | 0:972874f31c98 | 37 | ++mTriggers; |
iforce2d | 0:972874f31c98 | 38 | // If this is the first time we've been called (at 0x4...) |
iforce2d | 0:972874f31c98 | 39 | // then mTriggers now equals 1 and we want to insert at 0x80000000. |
iforce2d | 0:972874f31c98 | 40 | insert((mTriggers+1) * 0x40000000); |
iforce2d | 0:972874f31c98 | 41 | } |
iforce2d | 0:972874f31c98 | 42 | |
iforce2d | 0:972874f31c98 | 43 | // The number of times the us_ticker has rolled over. |
iforce2d | 0:972874f31c98 | 44 | uint32_t mTriggers; |
iforce2d | 0:972874f31c98 | 45 | }; |
iforce2d | 0:972874f31c98 | 46 | |
iforce2d | 0:972874f31c98 | 47 | static ExtendedClock _GlobalClock; |
iforce2d | 0:972874f31c98 | 48 | |
iforce2d | 0:972874f31c98 | 49 | // Return the number of seconds since boot. |
iforce2d | 0:972874f31c98 | 50 | float clock_s() |
iforce2d | 0:972874f31c98 | 51 | { |
iforce2d | 0:972874f31c98 | 52 | return _GlobalClock.read(); |
iforce2d | 0:972874f31c98 | 53 | } |
iforce2d | 0:972874f31c98 | 54 | |
iforce2d | 0:972874f31c98 | 55 | // Return the number of milliseconds since boot. |
iforce2d | 0:972874f31c98 | 56 | uint64_t clock_ms() |
iforce2d | 0:972874f31c98 | 57 | { |
iforce2d | 0:972874f31c98 | 58 | return _GlobalClock.read_ms(); |
iforce2d | 0:972874f31c98 | 59 | } |
iforce2d | 0:972874f31c98 | 60 | |
iforce2d | 0:972874f31c98 | 61 | // Return the number of microseconds since boot. |
iforce2d | 0:972874f31c98 | 62 | uint64_t clock_us() |
iforce2d | 0:972874f31c98 | 63 | { |
iforce2d | 0:972874f31c98 | 64 | return _GlobalClock.read_us(); |
iforce2d | 0:972874f31c98 | 65 | } |