/
timer.c
63 lines (55 loc) · 1.6 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
62
63
/*
* Milkymist SoC (Software)
* Copyright (C) 2012 Sebastien Bourdeauducq
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <hw/timer.h>
#include "timer.h"
unsigned int get_system_frequency(void)
{
return 83333333; /* TODO */
}
void timer_enable(int en)
{
CSR_TIMER0_EN = en;
}
unsigned int timer_get(void)
{
return (CSR_TIMER0_COUNT3 << 24)
|(CSR_TIMER0_COUNT2 << 16)
|(CSR_TIMER0_COUNT1 << 8)
|CSR_TIMER0_COUNT0;
}
void timer_set_counter(unsigned int value)
{
CSR_TIMER0_COUNT3 = (value & 0xff000000) >> 24;
CSR_TIMER0_COUNT2 = (value & 0x00ff0000) >> 16;
CSR_TIMER0_COUNT1 = (value & 0x0000ff00) >> 8;
CSR_TIMER0_COUNT0 = value & 0x000000ff;
}
void timer_set_reload(unsigned int value)
{
CSR_TIMER0_RELOAD3 = (value & 0xff000000) >> 24;
CSR_TIMER0_RELOAD2 = (value & 0x00ff0000) >> 16;
CSR_TIMER0_RELOAD1 = (value & 0x0000ff00) >> 8;
CSR_TIMER0_RELOAD0 = value & 0x000000ff;
}
void busy_wait(unsigned int ds)
{
timer_enable(0);
timer_set_reload(0);
timer_set_counter(get_system_frequency()/10*ds);
timer_enable(1);
while(timer_get());
}