-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock.cpp
More file actions
29 lines (25 loc) · 833 Bytes
/
Copy pathclock.cpp
File metadata and controls
29 lines (25 loc) · 833 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
#include "clock.h"
static uint64_t lastTickRecorded = 0;
static uint64_t reload;
static uint64_t clockFreq;
void clock_init() {
clockFreq = HAL_RCC_GetHCLKFreq();
reload = clockFreq / 1000;
HAL_SYSTICK_Config(reload);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(SysTick_IRQn);
}
float clock_getDeltaTime() {
uint64_t tick = (reload * HAL_GetTick()) + (reload - SysTick->VAL);
float deltaTime = ((float)(tick - lastTickRecorded)) / ((float)clockFreq);
if(tick < lastTickRecorded) {
return 0;
}
lastTickRecorded = tick;
return deltaTime;
}
float clock_getTime() {
uint64_t tick = (static_cast<uint64_t>(HAL_GetTick()) * reload) + (reload - SysTick->VAL);
return ((float)tick) / ((float)clockFreq);
}