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