-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
53 lines (40 loc) · 972 Bytes
/
util.c
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "mk20dx128.h"
#include "util.h"
static volatile uint32_t ticks;
static uint32_t state = 0;
__attribute__((interrupt ("IRQ"))) void systick_isr()
{
ticks += 1;
}
uint64_t cycles()
{
uint32_t v, t, t_1;
t = ticks;
v = SYST_CVR;
/* An overflow (tick) might have occured between the previous two
* instructions. Check for it and if it did happen reload the
* current systick value (since the last number of ticks read is
* bound to still be accurate) . */
t_1 = ticks;
if (t_1 != t) {
v = SYST_CVR;
t = t_1;
}
return (((uint64_t)t) << 24) | (uint64_t)(SYST_RVR - v);
}
void delay_ms(uint64_t n)
{
uint64_t t_0;
t_0 = cycles();
while (cycles() - t_0 < cycles_in_ms(n));
}
void delay_us(uint64_t n)
{
uint64_t t_0;
t_0 = cycles();
while (cycles() - t_0 < cycles_in_us(n));
}
uint32_t lcg_rand()
{
return (state = ((state * 1103515245) + 12345) & 0x7fffffff);
}