litex/software/libbase/timer.c

51 lines
1.0 KiB
C
Raw Normal View History

2012-05-21 16:56:21 -04:00
#include <hw/timer.h>
2012-05-22 07:23:44 -04:00
#include <hw/id.h>
2012-05-21 16:56:21 -04:00
#include "timer.h"
unsigned int get_system_frequency(void)
{
2012-05-22 07:23:44 -04:00
return (CSR_ID_FREQ3 << 24)
|(CSR_ID_FREQ2 << 16)
|(CSR_ID_FREQ1 << 8)
|CSR_ID_FREQ0;
2012-05-21 16:56:21 -04:00
}
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());
}