51 lines
1.1 KiB
C
51 lines
1.1 KiB
C
#include <hw/timer.h>
|
|
#include <hw/identifier.h>
|
|
|
|
#include "timer.h"
|
|
|
|
unsigned int get_system_frequency(void)
|
|
{
|
|
return (CSR_IDENTIFIER_FREQ3 << 24)
|
|
|(CSR_IDENTIFIER_FREQ2 << 16)
|
|
|(CSR_IDENTIFIER_FREQ1 << 8)
|
|
|CSR_IDENTIFIER_FREQ0;
|
|
}
|
|
|
|
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());
|
|
}
|