Delay by 1 microsecond in C MIPS Environment -
i'm trying port u8glib (graphics library) mips processor, openwrt router. here's example in arm environment.
as such, 1 of routines must implement is:
delay_micro_seconds(uint32_t us)
since high resolution unit of time, how can reliably in environment? i've tried following, i'm not sure how validate it:
nanosleep((struct timespec[]){{0, 1000}}, null);
how can validate approach? if bad approach, how reliably delay 1 microsecond in c?
edit: i've tried this, i'm getting strange output, expect difference between 2 print s 1000*10 iterations = 10,000 , closer 670,000 nanoseconds:
int main(int argc, char **argv) { long res, resb; struct timespec ts, tsb; int i; res = clock_gettime(clock_realtime, &ts); for(i=0;i<10;i++){ nanosleep((struct timespec[]){{0,1000}}, null); } resb = clock_gettime(clock_realtime, &tsb); if (0 == res) printf("%ld %ld\n", ts.tv_sec, ts.tv_nsec); else perror("clock_gettime"); if (0 == resb) printf("%ld %ld\n", tsb.tv_sec, tsb.tv_nsec); else perror("clock_gettime"); //getting 670k delta instead of 10k tv_nsec return 0; }
i assume codes run under linux.
first, using clock_getres(2)
find clock's resolution. , then, using clock_nanosleep(2)
might make more accurate sleep.
to validate sleep, suggest check elapsed time clock_gettime(2)
res = clock_gettime(clock_realtime, &ts); if (0 == res) printf("%ld %ld\n", ts.tv_sec, ts.tv_nsec); else perror("clock_gettime"); clock_nanosleep(clock_realtime, 0, &delay, null); res = clock_gettime(clock_realtime, &ts); if (0 == res) printf("%ld %ld\n", ts.tv_sec, ts.tv_nsec); else perror("clock_gettime");
also, if necessary, can recompile kernel higher hz configuration. helpful read time(7) man page. especially, the software clock, hz, , jiffies , high-resolution timers sections. though man pages says high-resolution timer not supported under mips architecture googled , mips-linux support hrt apparently.
Comments
Post a Comment