Skip to content

Commit 2029529

Browse files
authored
Merge pull request #722 from fjtrujy/fix_timer_acurracy
Fix timer accuracy
2 parents 76fc7b9 + 4e71d87 commit 2029529

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

ee/kernel/src/timer.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,10 @@ __attribute__((weak)) s32 StopTimerSystemTime(void)
263263
#endif
264264

265265
#ifdef F_SetNextComp
266+
#define CLOCKS_GAP 0x200 // For assuring the timer interrupt is triggered
266267
void SetNextComp(u64 time_now)
267268
{
268-
u64 a0, a1;
269+
u64 current_schedule, next_schedule;
269270
counter_struct_t *timer_current;
270271

271272
if (g_Timer.current_handling_timer_id >= 0)
@@ -279,30 +280,32 @@ void SetNextComp(u64 time_now)
279280
SetT2_MODE((*T2_MODE) & (~(1 << 11)));
280281
return;
281282
}
282-
a0 = timer_current->timer_schedule + timer_current->timer_base_time - timer_current->timer_base_count;
283+
current_schedule = timer_current->timer_schedule + timer_current->timer_base_time - timer_current->timer_base_count;
283284
timer_current = timer_current->timer_next;
285+
286+
// Grouping the timers that are so close to each other, to reduce the number of interrupts
284287
while (timer_current != NULL)
285288
{
286-
a1 = timer_current->timer_schedule + timer_current->timer_base_time - timer_current->timer_base_count;
287-
if (a1 < (a0 + 0x733))
289+
next_schedule = timer_current->timer_schedule + timer_current->timer_base_time - timer_current->timer_base_count;
290+
if (next_schedule < (current_schedule + CLOCKS_GAP))
288291
{
289-
a0 = a1;
292+
current_schedule = next_schedule;
290293
}
291294
else
292295
{
293296
break;
294297
}
295298
timer_current = timer_current->timer_next;
296299
}
297-
if (a0 < (time_now + 0x733))
300+
if (current_schedule < (time_now + CLOCKS_GAP))
298301
{
299-
SetT2_COMP((*T2_COUNT) + (0x733 >> (((*T2_MODE) & 3) << 2)));
302+
SetT2_COMP((*T2_COUNT) + (CLOCKS_GAP >> (((*T2_MODE) & 3) << 2)));
300303
SetT2_MODE((*T2_MODE) & (~(1 << 11)));
301304
}
302305
else
303306
{
304307
SetT2_MODE((*T2_MODE) & (~(1 << 11)));
305-
SetT2_COMP(a0 >> (((*T2_MODE) & 3) << 2));
308+
SetT2_COMP(current_schedule >> (((*T2_MODE) & 3) << 2));
306309
}
307310
}
308311
#endif

ee/libcglue/samples/nanosleep/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ int main(int argc, char *argv[])
3636
struct timespec tv = {0};
3737
tv.tv_sec = 1;
3838
tv.tv_nsec = 0;
39-
error_tolerance = 200; // 200 miliseconds of error tolerance
39+
error_tolerance = 5; // 5 miliseconds of error tolerance
4040

4141
#if defined(SCREEN_DEBUG)
4242
init_scr();

ee/libcglue/src/glue.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,12 @@ int _gettimeofday(struct timeval *tv, struct timezone *tz)
805805
#ifdef F__times
806806
// Called from newlib timesr.c
807807
clock_t _times(struct tms *buffer) {
808-
clock_t clk = GetTimerSystemTime() / (kBUSCLK / (1000 * 1000));
808+
clock_t clk;
809+
u32 busclock_sec;
810+
u32 busclock_usec;
811+
812+
TimerBusClock2USec(GetTimerSystemTime(), &busclock_sec, &busclock_usec);
813+
clk = busclock_sec * CLOCKS_PER_SEC + busclock_usec;
809814

810815
if (buffer != NULL) {
811816
buffer->tms_utime = clk;

ee/libcglue/src/sleep.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,24 @@ int nanosleep(const struct timespec *req, struct timespec *rem)
4343
__asm__ __volatile__ ("mfc0\t%0, $12" : "=r" (eie));
4444
if ((eie & 0x10000) == 0)
4545
{
46-
return 0;
46+
errno = ENOSYS; // Functionality not available
47+
return -1;
4748
}
4849
sema.max_count = 1;
4950
sema.option = (u32)"nanosleep";
5051
sema.init_count = 0;
5152
sema_id = CreateSema(&sema);
5253
if (sema_id < 0)
5354
{
54-
return 0;
55+
errno = EAGAIN; // Resource temporarily unavailable
56+
return -1;
5557
}
5658
timer_alarm_id = SetTimerAlarm(Sec2TimerBusClock(req->tv_sec) + NSec2TimerBusClock(req->tv_nsec), nanosleep_wakeup_callback, (void *)sema_id);
5759
if (timer_alarm_id < 0)
5860
{
5961
DeleteSema(sema_id);
60-
return 0;
62+
errno = EAGAIN; // Resource temporarily unavailable
63+
return -1;
6164
}
6265
WaitSema(sema_id);
6366
DeleteSema(sema_id);

0 commit comments

Comments
 (0)