2013-11-24 13:50:17 -05:00
|
|
|
#include <generated/csr.h>
|
2013-07-11 13:00:48 -04:00
|
|
|
#include <time.h>
|
2013-05-13 09:45:28 -04:00
|
|
|
|
|
|
|
void time_init(void)
|
|
|
|
{
|
2013-05-13 11:18:30 -04:00
|
|
|
int t;
|
|
|
|
|
|
|
|
timer0_en_write(0);
|
|
|
|
t = 2*identifier_frequency_read();
|
|
|
|
timer0_reload_write(t);
|
|
|
|
timer0_load_write(t);
|
2013-05-13 09:45:28 -04:00
|
|
|
timer0_en_write(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int elapsed(int *last_event, int period)
|
|
|
|
{
|
|
|
|
int t, dt;
|
|
|
|
|
2013-05-13 11:18:30 -04:00
|
|
|
timer0_update_value_write(1);
|
|
|
|
t = timer0_reload_read() - timer0_value_read();
|
2013-11-13 10:50:09 -05:00
|
|
|
if(period < 0) {
|
|
|
|
*last_event = t;
|
|
|
|
return 1;
|
|
|
|
}
|
2013-05-13 09:45:28 -04:00
|
|
|
dt = t - *last_event;
|
|
|
|
if(dt < 0)
|
|
|
|
dt += timer0_reload_read();
|
|
|
|
if((dt > period) || (dt < 0)) {
|
|
|
|
*last_event = t;
|
|
|
|
return 1;
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
}
|