forked from includeos/IncludeOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtc.cpp
More file actions
37 lines (29 loc) · 765 Bytes
/
rtc.cpp
File metadata and controls
37 lines (29 loc) · 765 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <kernel/rtc.hpp>
#include <kernel/timers.hpp>
#include <hw/cpu.hpp>
#include <hw/cmos.hpp>
#include <hertz>
static int64_t current_time = 0;
static uint64_t current_ticks = 0;
extern double _CPUFreq_;
using namespace std::chrono;
void RTC::init()
{
// Initialize CMOS
cmos::init();
// set current timestamp and ticks
current_time = cmos::now().to_epoch();
current_ticks = hw::CPU::rdtsc();
// every minute recalibrate
Timers::periodic(seconds(60), seconds(60),
[] (uint32_t) {
current_time = cmos::now().to_epoch();
current_ticks = hw::CPU::rdtsc();
});
}
RTC::timestamp_t RTC::now()
{
auto ticks = hw::CPU::rdtsc() - current_ticks;
auto diff = ticks / Hz(MHz(_CPUFreq_)).count();
return current_time + diff;
}