From 605fd5565556b75cae0769ae11d64b888549f2cc Mon Sep 17 00:00:00 2001 From: Ian Abbott Date: Wed, 7 Nov 2007 13:01:50 +0000 Subject: [PATCH] Fix use of INSN_GTOD so it works on 64-bit platforms. A struct timeval is not the same size as a pair of lsampl_t's for 64-bit, so can't point 'data' in a comedi_insn to a struct timeval and expect it to work! --- demo/insn.c | 19 ++++++++++++------- doc/reference.sgml | 3 ++- doc/tutorial.sgml | 18 +++++++++++------- testing/insn_read_time.c | 8 ++++---- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/demo/insn.c b/demo/insn.c index b842d04..a67d4d0 100644 --- a/demo/insn.c +++ b/demo/insn.c @@ -34,6 +34,11 @@ * This example does 3 instructions in one system call. It does * a gettimeofday() call, then reads N_SAMPLES samples from an * analog input, and the another gettimeofday() call. + * + * (Note: The gettimeofday() value is obtained using an INSN_GTOD + * instruction, which places the seconds value in data[0] and the + * microseconds in data[1], so the seconds value is limited to + * 32-bits even on 64-bit systems.) */ #define MAX_SAMPLES 128 @@ -45,7 +50,7 @@ int main(int argc, char *argv[]) int ret,i; comedi_insn insn[3]; comedi_insnlist il; - struct timeval t1,t2; + lsampl_t t1[2], t2[2]; lsampl_t data[MAX_SAMPLES]; struct parsed_options options; @@ -77,7 +82,7 @@ int main(int argc, char *argv[]) /* Instruction 0: perform a gettimeofday() */ insn[0].insn=INSN_GTOD; insn[0].n=2; - insn[0].data=(void *)&t1; + insn[0].data=t1; /* Instruction 1: do 10 analog input reads */ insn[1].insn=INSN_READ; @@ -89,7 +94,7 @@ int main(int argc, char *argv[]) /* Instruction 2: perform a gettimeofday() */ insn[2].insn=INSN_GTOD; insn[2].n=2; - insn[2].data=(void *)&t2; + insn[2].data=t2; ret=comedi_do_insnlist(device,&il); if(ret<0){ @@ -97,14 +102,14 @@ int main(int argc, char *argv[]) exit(-1); } - printf("initial time: %ld.%06ld\n", t1.tv_sec, t1.tv_usec); + printf("initial time: %d.%06d\n", t1[0], t1[1]); for(i = 0; i < options.n_scan; i++){ printf("%d\n", data[i]); } - printf("final time: %ld.%06ld\n", t2.tv_sec, t2.tv_usec); + printf("final time: %d.%06d\n", t2[0], t2[1]); - printf("difference (us): %ld\n",(t2.tv_sec-t1.tv_sec) * 1000000 + - (t2.tv_usec - t1.tv_usec)); + printf("difference (us): %ld\n",(long)(t2[0]-t1[0]) * 1000000 + + (t2[1] - t1[1])); return 0; } diff --git a/doc/reference.sgml b/doc/reference.sgml index 8584cc5..45e46c6 100644 --- a/doc/reference.sgml +++ b/doc/reference.sgml @@ -518,7 +518,8 @@ INSN_GTOD -read a timestamp, identical to gettimeofday() +read a timestamp, identical to gettimeofday() except the seconds +and microseconds values are of type lsampl_t. diff --git a/doc/tutorial.sgml b/doc/tutorial.sgml index 9d37f28..a6934f6 100644 --- a/doc/tutorial.sgml +++ b/doc/tutorial.sgml @@ -306,6 +306,11 @@ function, the so-called instruction. * This example does 3 instructions in one system call. It does * a gettimeofday() call, then reads N_SAMPLES samples from an * analog input, and the another gettimeofday() call. + * + * (Note: The gettimeofday() value is obtained using an INSN_GTOD + * instruction, which places the seconds value in data[0] and the + * microseconds in data[1], so the seconds value is limited to + * 32-bits even on 64-bit systems.) */ #define MAX_SAMPLES 128 @@ -317,7 +322,7 @@ int main(int argc, char *argv[]) int ret,i; comedi_insn insn[3]; comedi_insnlist il; - struct timeval t1,t2; + lsampl_t t1[2], t2[2]; lsampl_t data[MAX_SAMPLES]; parse_options(argc,argv); @@ -343,7 +348,7 @@ int main(int argc, char *argv[]) /* Instruction 0: perform a gettimeofday() */ insn[0].insn=INSN_GTOD; insn[0].n=2; - insn[0].data=(void *)&t1; + insn[0].data=t1; /* Instruction 1: do 10 analog input reads */ insn[1].insn=INSN_READ; @@ -355,7 +360,7 @@ int main(int argc, char *argv[]) /* Instruction 2: perform a gettimeofday() */ insn[2].insn=INSN_GTOD; insn[2].n=2; - insn[2].data=(void *)&t2; + insn[2].data=t2; ret=comedi_do_insnlist(device,&il); if(ret0){ @@ -363,14 +368,13 @@ int main(int argc, char *argv[]) exit(0); } - printf("initial time: %ld.%06ld\n",t1.tv_sec,t1.tv_usec); + printf("initial time: %d.%06d\n",t1[0],t1[1]); for(i=0;in_scan;i++){ printf("%d\n",data[i]); } - printf("final time: %ld.%06ld\n",t2.tv_sec,t2.tv_usec); + printf("final time: %d.%06d\n",t2[0],t2[1]); - printf("difference (us): %ld\n",(t2.tv_sec-t1.tv_sec)*1000000+ - (t2.tv_usec-t1.tv_usec)); + printf("difference (us): %ld\n",(long)(t2[0]-t1[0])*1000000+(t2[1]-t1[1])); return 0; } diff --git a/testing/insn_read_time.c b/testing/insn_read_time.c index 5f202b5..cd6b0c5 100644 --- a/testing/insn_read_time.c +++ b/testing/insn_read_time.c @@ -17,7 +17,7 @@ int test_insn_read_time(void) { comedi_insn insn[3]; comedi_insnlist il; - struct timeval t1,t2; + lsampl_t t1[2],t2[2]; lsampl_t data; int save_errno; int ret; @@ -37,7 +37,7 @@ int test_insn_read_time(void) insn[0].insn = INSN_GTOD; insn[0].n=2; - insn[0].data = (void *)&t1; + insn[0].data = t1; insn[1].subdev = subdevice; insn[1].insn = INSN_READ; @@ -47,7 +47,7 @@ int test_insn_read_time(void) insn[2].insn = INSN_GTOD; insn[2].n=2; - insn[2].data = (void *)&t2; + insn[2].data = t2; ret = comedi_do_insnlist(device,&il); save_errno = errno; @@ -61,7 +61,7 @@ int test_insn_read_time(void) } printf("read time: %ld us\n", - (t2.tv_sec-t1.tv_sec)*1000000+(t2.tv_usec-t1.tv_usec)); + (long)(t2[0]-t1[0])*1000000+(t2[1]-t1[1])); return 0; -- 2.26.2