Sebastien Luzy / Mbed OS Code_capteur-APDS9960_ecran-STM32F746G-DISCO

Dependencies:   TS_DISCO_F746NG LCD_DISCO_F746NG BSP_DISCO_F746NG BUTTON_GROUP

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         insert(0x40000000);
00016     }
00017  
00018     float read()
00019     {
00020         return read_us() / 1000000.0f;
00021     }
00022  
00023     uint64_t read_ms()
00024     {
00025         return read_us() / 1000;
00026     }
00027  
00028     uint64_t read_us()
00029     {
00030         return mTriggers * 0x40000000ull + (ticker_read(_ticker_data) & 0x3FFFFFFF);
00031     }
00032  
00033 private:
00034     void handler() override
00035     {
00036         ++mTriggers;
00037         // If this is the first time we've been called (at 0x4...)
00038         // then mTriggers now equals 1 and we want to insert at 0x80000000.
00039         insert((mTriggers+1) * 0x40000000);
00040     }
00041  
00042     // The number of times the us_ticker has rolled over.
00043     uint32_t mTriggers = 0;
00044 };
00045  
00046 static ExtendedClock _GlobalClock;
00047  
00048 // Return the number of seconds since boot.
00049 float clock_s()
00050 {
00051     return _GlobalClock.read();
00052 }
00053  
00054 // Return the number of milliseconds since boot.
00055 uint64_t clock_ms()
00056 {
00057     return _GlobalClock.read_ms();
00058 }
00059  
00060 // Return the number of microseconds since boot.
00061 uint64_t clock_us()
00062 {
00063     return _GlobalClock.read_us();
00064 }