ea25eb5e8f4bad1316fa03bc552c8ee394ba4acb
[litex.git] / software / libbase / timer.c
1 #include <hw/timer.h>
2 #include <hw/identifier.h>
3
4 #include "timer.h"
5
6 unsigned int get_system_frequency(void)
7 {
8 return (CSR_IDENTIFIER_FREQ3 << 24)
9 |(CSR_IDENTIFIER_FREQ2 << 16)
10 |(CSR_IDENTIFIER_FREQ1 << 8)
11 |CSR_IDENTIFIER_FREQ0;
12 }
13
14 void timer_enable(int en)
15 {
16 CSR_TIMER0_EN = en;
17 }
18
19 unsigned int timer_get(void)
20 {
21 return (CSR_TIMER0_COUNT3 << 24)
22 |(CSR_TIMER0_COUNT2 << 16)
23 |(CSR_TIMER0_COUNT1 << 8)
24 |CSR_TIMER0_COUNT0;
25 }
26
27 void timer_set_counter(unsigned int value)
28 {
29 CSR_TIMER0_COUNT3 = (value & 0xff000000) >> 24;
30 CSR_TIMER0_COUNT2 = (value & 0x00ff0000) >> 16;
31 CSR_TIMER0_COUNT1 = (value & 0x0000ff00) >> 8;
32 CSR_TIMER0_COUNT0 = value & 0x000000ff;
33 }
34
35 void timer_set_reload(unsigned int value)
36 {
37 CSR_TIMER0_RELOAD3 = (value & 0xff000000) >> 24;
38 CSR_TIMER0_RELOAD2 = (value & 0x00ff0000) >> 16;
39 CSR_TIMER0_RELOAD1 = (value & 0x0000ff00) >> 8;
40 CSR_TIMER0_RELOAD0 = value & 0x000000ff;
41 }
42
43 void busy_wait(unsigned int ds)
44 {
45 timer_enable(0);
46 timer_set_reload(0);
47 timer_set_counter(get_system_frequency()/10*ds);
48 timer_enable(1);
49 while(timer_get());
50 }