forked from organix/pijFORTHos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.c
61 lines (56 loc) · 1.04 KB
/
timer.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
54
55
56
57
58
59
60
61
/*
* timer.c -- Raspberry Pi timer routines written in C
*
* Some of this code was inspired by bare-metal examples
* from David Welch at https://github.com/dwelch67/raspberrypi
*/
#include "timer.h"
struct timer {
u32 _00;
u32 _04;
u32 CTL; //_08;
u32 _0c;
u32 _10;
u32 _14;
u32 _18;
u32 _1c;
u32 CNT; //_20;
u32 _24;
u32 _28;
u32 _2c;
};
#define TIMER ((volatile struct timer *)0x2000b400)
/*
* Initialize 1Mhz timer
*/
void
timer_init()
{
TIMER->CTL = 0x00F90000; // 0xF9+1 = 250
TIMER->CTL = 0x00F90200; // 250MHz/250 = 1MHz
}
/*
* Get 1Mhz timer tick count (microseconds)
*/
int
timer_usecs()
{
return TIMER->CNT;
}
/*
* Delay loop (microseconds)
*/
int
timer_wait(int dt)
{
int t0;
int t1;
t0 = timer_usecs();
t1 = t0 + dt;
for (;;) {
t0 = timer_usecs();
if ((t0 - t1) >= 0) { // timeout
return t0;
}
}
}