Skip to content

Commit

Permalink
Merge pull request #722 from fjtrujy/fix_timer_acurracy
Browse files Browse the repository at this point in the history
Fix timer accuracy
  • Loading branch information
rickgaiser authored Jan 22, 2025
2 parents 76fc7b9 + 4e71d87 commit 2029529
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
19 changes: 11 additions & 8 deletions ee/kernel/src/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,10 @@ __attribute__((weak)) s32 StopTimerSystemTime(void)
#endif

#ifdef F_SetNextComp
#define CLOCKS_GAP 0x200 // For assuring the timer interrupt is triggered
void SetNextComp(u64 time_now)
{
u64 a0, a1;
u64 current_schedule, next_schedule;
counter_struct_t *timer_current;

if (g_Timer.current_handling_timer_id >= 0)
Expand All @@ -279,30 +280,32 @@ void SetNextComp(u64 time_now)
SetT2_MODE((*T2_MODE) & (~(1 << 11)));
return;
}
a0 = timer_current->timer_schedule + timer_current->timer_base_time - timer_current->timer_base_count;
current_schedule = timer_current->timer_schedule + timer_current->timer_base_time - timer_current->timer_base_count;
timer_current = timer_current->timer_next;

// Grouping the timers that are so close to each other, to reduce the number of interrupts
while (timer_current != NULL)
{
a1 = timer_current->timer_schedule + timer_current->timer_base_time - timer_current->timer_base_count;
if (a1 < (a0 + 0x733))
next_schedule = timer_current->timer_schedule + timer_current->timer_base_time - timer_current->timer_base_count;
if (next_schedule < (current_schedule + CLOCKS_GAP))
{
a0 = a1;
current_schedule = next_schedule;
}
else
{
break;
}
timer_current = timer_current->timer_next;
}
if (a0 < (time_now + 0x733))
if (current_schedule < (time_now + CLOCKS_GAP))
{
SetT2_COMP((*T2_COUNT) + (0x733 >> (((*T2_MODE) & 3) << 2)));
SetT2_COMP((*T2_COUNT) + (CLOCKS_GAP >> (((*T2_MODE) & 3) << 2)));
SetT2_MODE((*T2_MODE) & (~(1 << 11)));
}
else
{
SetT2_MODE((*T2_MODE) & (~(1 << 11)));
SetT2_COMP(a0 >> (((*T2_MODE) & 3) << 2));
SetT2_COMP(current_schedule >> (((*T2_MODE) & 3) << 2));
}
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion ee/libcglue/samples/nanosleep/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ int main(int argc, char *argv[])
struct timespec tv = {0};
tv.tv_sec = 1;
tv.tv_nsec = 0;
error_tolerance = 200; // 200 miliseconds of error tolerance
error_tolerance = 5; // 5 miliseconds of error tolerance

#if defined(SCREEN_DEBUG)
init_scr();
Expand Down
7 changes: 6 additions & 1 deletion ee/libcglue/src/glue.c
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,12 @@ int _gettimeofday(struct timeval *tv, struct timezone *tz)
#ifdef F__times
// Called from newlib timesr.c
clock_t _times(struct tms *buffer) {
clock_t clk = GetTimerSystemTime() / (kBUSCLK / (1000 * 1000));
clock_t clk;
u32 busclock_sec;
u32 busclock_usec;

TimerBusClock2USec(GetTimerSystemTime(), &busclock_sec, &busclock_usec);
clk = busclock_sec * CLOCKS_PER_SEC + busclock_usec;

if (buffer != NULL) {
buffer->tms_utime = clk;
Expand Down
9 changes: 6 additions & 3 deletions ee/libcglue/src/sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,24 @@ int nanosleep(const struct timespec *req, struct timespec *rem)
__asm__ __volatile__ ("mfc0\t%0, $12" : "=r" (eie));
if ((eie & 0x10000) == 0)
{
return 0;
errno = ENOSYS; // Functionality not available
return -1;
}
sema.max_count = 1;
sema.option = (u32)"nanosleep";
sema.init_count = 0;
sema_id = CreateSema(&sema);
if (sema_id < 0)
{
return 0;
errno = EAGAIN; // Resource temporarily unavailable
return -1;
}
timer_alarm_id = SetTimerAlarm(Sec2TimerBusClock(req->tv_sec) + NSec2TimerBusClock(req->tv_nsec), nanosleep_wakeup_callback, (void *)sema_id);
if (timer_alarm_id < 0)
{
DeleteSema(sema_id);
return 0;
errno = EAGAIN; // Resource temporarily unavailable
return -1;
}
WaitSema(sema_id);
DeleteSema(sema_id);
Expand Down

0 comments on commit 2029529

Please sign in to comment.