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