blob: 91c3c6dad577bb7ed3869c176fffaab61393c255 [file] [log] [blame]
Ollie Lhocbbf1252004-03-17 22:22:08 +00001#include <sys/time.h>
2#include <stdio.h>
Ollie Lho184a4042005-11-26 21:55:36 +00003#include "debug.h"
Ollie Lhocbbf1252004-03-17 22:22:08 +00004
5// count to a billion. Time it. If it's < 1 sec, count to 10B, etc.
6unsigned long micro = 1;
7
8void myusec_delay(int time)
9{
10 volatile unsigned long i;
Uwe Hermann0b7afe62007-04-01 19:44:21 +000011 for (i = 0; i < time * micro; i++) ;
Ollie Lhocbbf1252004-03-17 22:22:08 +000012}
13
14void myusec_calibrate_delay()
15{
16 int count = 1000;
17 unsigned long timeusec;
18 struct timeval start, end;
19 int ok = 0;
Ollie Lhocbbf1252004-03-17 22:22:08 +000020
Ollie Lho184a4042005-11-26 21:55:36 +000021 printf_debug("Setting up microsecond timing loop\n");
Ollie Lhocbbf1252004-03-17 22:22:08 +000022 while (!ok) {
Ollie Lhocbbf1252004-03-17 22:22:08 +000023 gettimeofday(&start, 0);
24 myusec_delay(count);
25 gettimeofday(&end, 0);
Ollie Lho761bf1b2004-03-20 16:46:10 +000026 timeusec = 1000000 * (end.tv_sec - start.tv_sec) +
27 (end.tv_usec - start.tv_usec);
Ollie Lhocbbf1252004-03-17 22:22:08 +000028 count *= 2;
Ollie Lho761bf1b2004-03-20 16:46:10 +000029 if (timeusec < 1000000 / 4)
Ollie Lhocbbf1252004-03-17 22:22:08 +000030 continue;
31 ok = 1;
32 }
33
34 // compute one microsecond. That will be count / time
35 micro = count / timeusec;
36
Uwe Hermann0b7afe62007-04-01 19:44:21 +000037 printf_debug("%ldM loops per second\n", (unsigned long)micro);
Ollie Lhocbbf1252004-03-17 22:22:08 +000038}