blob: 2f032dfdec2d9fab3c88387ef5fb85b220e4341b [file] [log] [blame]
Ollie Lhocbbf1252004-03-17 22:22:08 +00001#include <sys/time.h>
2#include <stdio.h>
Uwe Hermann0846f892007-08-23 13:34:59 +00003#include "flash.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
Stefan Reinauer70385642007-04-06 11:58:03 +000021 printf("Calibrating delay loop... ");
22
Ollie Lhocbbf1252004-03-17 22:22:08 +000023 while (!ok) {
Ollie Lhocbbf1252004-03-17 22:22:08 +000024 gettimeofday(&start, 0);
25 myusec_delay(count);
26 gettimeofday(&end, 0);
Ollie Lho761bf1b2004-03-20 16:46:10 +000027 timeusec = 1000000 * (end.tv_sec - start.tv_sec) +
28 (end.tv_usec - start.tv_usec);
Ollie Lhocbbf1252004-03-17 22:22:08 +000029 count *= 2;
Ollie Lho761bf1b2004-03-20 16:46:10 +000030 if (timeusec < 1000000 / 4)
Ollie Lhocbbf1252004-03-17 22:22:08 +000031 continue;
32 ok = 1;
33 }
34
35 // compute one microsecond. That will be count / time
36 micro = count / timeusec;
37
Stefan Reinauer70385642007-04-06 11:58:03 +000038 printf_debug("%ldM loops per second. ", (unsigned long)micro);
39 printf("ok\n");
Ollie Lhocbbf1252004-03-17 22:22:08 +000040}