add warning to 6711 calibration if driver version might be too old
[comedilib.git] / testing / cmd_1.c
1
2 #include <stdio.h>
3 #include <comedilib.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6 #include <sys/ioctl.h>
7 #include <errno.h>
8 #include <getopt.h>
9 #include <ctype.h>
10 #include <math.h>
11 #include <sys/time.h>
12 #include <string.h>
13 #include "comedi_test.h"
14
15 void probe_max_1chan(comedi_t *it,int s);
16 char *tobinary(char *s,int bits,int n);
17 char *cmd_src(int src,char *buf);
18 int count_bits(unsigned int bits);
19
20 int test_cmd_no_cmd(void)
21 {
22         int ret;
23         comedi_cmd cmd;
24
25         if(comedi_get_subdevice_flags(device,subdevice)&SDF_CMD){
26                 printf("not applicable\n");
27                 return 0;
28         }
29
30         ret = comedi_get_cmd_src_mask(device,subdevice,&cmd);
31         if(ret<0){
32                 if(errno == EIO){
33                         printf("got EIO, good\n");
34                 }else{
35                         printf("E: comedi_get_cmd_src_mask: %s\n",
36                                 strerror(errno));
37                 }
38         }else{
39                 printf("E: comedi_get_cmd_src_mask returned %d\n",ret);
40         }
41
42         return 0;
43 }
44
45 int test_cmd_probe_src_mask(void)
46 {
47         comedi_cmd cmd;
48         char buf[100];
49         int ret;
50
51         if(!(comedi_get_subdevice_flags(device,subdevice)&SDF_CMD)){
52                 printf("not applicable\n");
53                 return 0;
54         }
55
56         printf("rev 1\n");
57
58         ret = comedi_get_cmd_src_mask(device,subdevice,&cmd);
59         if(ret<0){
60                 printf("E: comedi_get_cmd_src_mask failed %s\n",
61                         strerror(errno));
62                 return 0;
63         }
64         printf("command source mask:\n");
65         printf("  start: %s\n",cmd_src(cmd.start_src,buf));
66         printf("  scan_begin: %s\n",cmd_src(cmd.scan_begin_src,buf));
67         printf("  convert: %s\n",cmd_src(cmd.convert_src,buf));
68         printf("  scan_end: %s\n",cmd_src(cmd.scan_end_src,buf));
69         printf("  stop: %s\n",cmd_src(cmd.stop_src,buf));
70
71         return 0;
72 }
73
74 int test_cmd_probe_fast_1chan(void)
75 {
76         comedi_cmd cmd;
77         char buf[100];
78
79         if(!(comedi_get_subdevice_flags(device,subdevice)&SDF_CMD)){
80                 printf("not applicable\n");
81                 return 0;
82         }
83
84         printf("command fast 1chan:\n");
85         if(comedi_get_cmd_generic_timed(device,subdevice,&cmd,1)<0){
86                 printf("  not supported\n");
87                 return 0;
88         }
89         printf("  start: %s %d\n",
90                 cmd_src(cmd.start_src,buf),cmd.start_arg);
91         printf("  scan_begin: %s %d\n",
92                 cmd_src(cmd.scan_begin_src,buf),cmd.scan_begin_arg);
93         printf("  convert: %s %d\n",
94                 cmd_src(cmd.convert_src,buf),cmd.convert_arg);
95         printf("  scan_end: %s %d\n",
96                 cmd_src(cmd.scan_end_src,buf),cmd.scan_end_arg);
97         printf("  stop: %s %d\n",
98                 cmd_src(cmd.stop_src,buf),cmd.stop_arg);
99
100         return 0;
101 }
102
103 #define BUFSZ 2000
104
105 int test_cmd_read_fast_1chan(void)
106 {
107         comedi_cmd cmd;
108         char buf[BUFSZ];
109         unsigned int chanlist[1];
110         int go;
111         int total=0;
112         int ret;
113         unsigned int flags = comedi_get_subdevice_flags(device,subdevice);
114
115         if(!(flags&SDF_CMD) || flags&SDF_WRITEABLE){
116                 printf("not applicable\n");
117                 return 0;
118         }
119
120         if(comedi_get_cmd_generic_timed(device,subdevice,&cmd,1)<0){
121                 printf("  not supported\n");
122                 return 0;
123         }
124
125         if(realtime)cmd.flags |= TRIG_RT;
126         cmd.chanlist = chanlist;
127         cmd.scan_end_arg = 1;
128         cmd.stop_arg = 100000;
129         cmd.chanlist_len = 1;
130         chanlist[0] = CR_PACK(0,0,0);
131
132         comedi_command(device,&cmd);
133
134         go=1;
135         while(go){
136                 ret = read(comedi_fileno(device),buf,BUFSZ);
137                 if(ret<0){
138                         if(errno==EAGAIN){
139                                 usleep(10000);
140                         }else{
141                                 go = 0;
142                                 perror("read");
143                         }
144                 }else if(ret==0){
145                         go = 0;
146                 }else{
147                         total += ret;
148                         if(verbose)printf("read %d %d\n",ret,total);
149                 }
150         }
151
152         return 0;
153 }
154
155 int test_cmd_write_fast_1chan(void)
156 {
157         comedi_cmd cmd;
158         char buf[BUFSZ];
159         unsigned int chanlist[1];
160         int go;
161         int total=0;
162         int ret;
163         unsigned int flags = comedi_get_subdevice_flags(device,subdevice);
164
165         if(!(flags&SDF_CMD) || !(flags&SDF_WRITEABLE)){
166                 printf("not applicable\n");
167                 return 0;
168         }
169
170         if(comedi_get_cmd_generic_timed(device,subdevice,&cmd,1)<0){
171                 printf("  not supported\n");
172                 return 0;
173         }
174
175         if(realtime)cmd.flags |= TRIG_RT;
176         cmd.chanlist = chanlist;
177         cmd.scan_end_arg = 1;
178         cmd.stop_arg = 1000000;
179         cmd.chanlist_len = 1;
180         chanlist[0] = CR_PACK(0,0,0);
181
182         memset(buf,0,BUFSZ);
183
184         ret = comedi_command(device,&cmd);
185         if(ret<0){
186                 perror("comedi_command");
187         }
188
189         go = 1;
190         while(go){
191                 ret = write(comedi_fileno(device), buf, BUFSZ);
192                 if(ret<0){
193                         perror("write");
194                         return 0;
195                 }
196                 if(ret<BUFSZ){
197                         go = 0;
198                         break;
199                 }
200
201                 total += ret;
202                 if(verbose)printf("write %d %d\n",ret,total);
203         }
204         
205         {
206                 comedi_insn insn;
207                 memset(&insn, 0, sizeof(comedi_insn));
208                 insn.insn = INSN_INTTRIG;
209                 insn.subdev = subdevice;
210                 ret = comedi_do_insn(device, &insn);
211                 if(ret<0){
212                         perror("comedi_inttrig");
213                         return 0;
214                 }
215                 if(verbose)printf("inttrig\n");
216         }
217
218         go=1;
219         while(go){
220                 ret = write(comedi_fileno(device),buf,BUFSZ);
221                 if(ret<0){
222                         if(errno==EAGAIN){
223                                 usleep(10000);
224                         }else{
225                                 go = 0;
226                                 perror("write");
227                         }
228                 }else if(ret==0){
229                         go = 0;
230                 }else{
231                         total += ret;
232                         if(verbose)printf("write %d %d\n",ret,total);
233                 }
234         }
235
236         return 0;
237 }
238
239 int test_cmd_logic_bug(void)
240 {
241         comedi_cmd cmd;
242         int ret;
243         int ok=0;
244
245         if(!(comedi_get_subdevice_flags(device,subdevice)&SDF_CMD)){
246                 printf("not applicable\n");
247                 return 0;
248         }
249
250         printf("rev 1\n");
251
252         ret = comedi_get_cmd_src_mask(device,subdevice,&cmd);
253         if(ret<0){
254                 printf("E: comedi_get_cmd_src_mask failed\n");
255                 return 0;
256         }
257
258         if(count_bits(cmd.start_src)>1){ cmd.start_src=0; ok=1; }
259         if(count_bits(cmd.scan_begin_src)>1){ cmd.scan_begin_src=0; ok=1; }
260         if(count_bits(cmd.convert_src)>1){ cmd.convert_src=0; ok=1; }
261         if(count_bits(cmd.scan_end_src)>1){ cmd.scan_end_src=0; ok=1; }
262         if(count_bits(cmd.stop_src)>1){ cmd.stop_src=0; ok=1; }
263
264         if(ok==0){
265                 printf("not applicable (no source choices)\n");
266                 return 0;
267         }
268
269         ret = comedi_command_test(device,&cmd);
270         if(ret!=1){
271                 printf("E: command_test returned %d, expected 1, (allowed src==0)\n",ret);
272         }else{
273                 printf("command_test returned %d, good\n",ret);
274         }
275
276         return 0;
277 }
278
279 int count_bits(unsigned int bits)
280 {
281         int ret=0;
282         while(bits){
283                 if(bits&1)ret++;
284                 bits>>=1;
285         }
286         return ret;
287 }
288
289 char *tobinary(char *s,int bits,int n)
290 {
291         int bit=1<<n;
292         char *t=s;
293         
294         for(;bit;bit>>=1)
295                 *t++=(bits&bit)?'1':'0';
296         *t=0;
297         
298         return s;
299 }
300
301 char *cmd_src(int src,char *buf)
302 {
303         buf[0]=0;
304
305         if(src&TRIG_NONE)strcat(buf,"none|");
306         if(src&TRIG_NOW)strcat(buf,"now|");
307         if(src&TRIG_FOLLOW)strcat(buf,"follow|");
308         if(src&TRIG_TIME)strcat(buf,"time|");
309         if(src&TRIG_TIMER)strcat(buf,"timer|");
310         if(src&TRIG_COUNT)strcat(buf,"count|");
311         if(src&TRIG_EXT)strcat(buf,"ext|");
312         if(src&TRIG_INT)strcat(buf,"int|");
313
314         if(strlen(buf)==0){
315                 sprintf(buf,"unknown(0x%02x)",src);
316         }else{
317                 buf[strlen(buf)-1]=0;
318         }
319
320         return buf;
321 }
322
323