Debian Codegraph @

demo/gpct_buffered_counting.c

raw symbols SCIP
1 /*
2 * NI general-purpose counter example. Makes the counter
3 * do buffered event counting using comedi_command().
4 * Different clock sources can be chosen
5 * with the choose_clock demo. The gate source is
6 * set to PFI1 (which is assumed to already be configured as
7 * an input). The gate source latches the count
8 * into the data stream, and possibly resets it (for
9 * non-cumulative buffered counting).
10 * Part of Comedilib
11 *
12 * Copyright (c) 2007 Frank Mori Hess <[email protected]>
13 *
14 * This file may be freely modified, distributed, and combined with
15 * other software, as long as proper attribution is given in the
16 * source code.
17 */
18 /*
19 * Requirements: A board with a National Instruments general-purpose
20 * counter, and comedi driver version 0.7.74 or newer.
21 */
22
23 #define _GNU_SOURCEcxx . . $ `demo/gpct_buffered_counting.c:23:9`!
24
25 #include <assert.h>
26 #include <stdio.h>
27 #include <comedilib.h>cxx . . $ `<file>/include/comedilib.h`/
28 #include <math.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <getopt.h>
35 #include <ctype.h>
36 #include "examples.h"cxx . . $ `<file>/demo/examples.h`/
37
38 int check_subdevicecxx . . $ check_subdevice(bde13464cf6d94ba).(comedi_tcxx . . $ comedi_t# *device, int *subdevice, const char *device_filepath)
39 {
40 int subdevice_type;
41 int read_subdevice;
42
43 read_subdevice = comedi_get_read_subdevicecxx . . $ comedi_get_read_subdevice(33bb234f098f8f92).(device);
44 if (*subdevice >= 0 && read_subdevice != *subdevice)
45 {
46 if (comedi_set_read_subdevicecxx . . $ comedi_set_read_subdevice(7ee088ff0d1b2810).(device, *subdevice) == 0)
47 {
48 /* 'read' subdevice changed successfully */
49 read_subdevice = *subdevice;
50 }
51 }
52 if(read_subdevice < 0)
53 {
54 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Device file \"%s\" cannot do streaming input.\n", device_filepath);
55 return -1;
56 }
57 if(*subdevice < 0) *subdevice = read_subdevice;
58 if(read_subdevice != *subdevice)
59 {
60 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "You specified subdevice %i, but the read subdevice of device file \"%s\" is %i.\n",
61 *subdevice, device_filepath, read_subdevice);
62 return -1;
63 }
64 subdevice_type = comedi_get_subdevice_typecxx . . $ comedi_get_subdevice_type(7ee088ff0d1b2810).(device, *subdevice);
65 if(subdevice_type < 0)
66 {
67 comedi_perrorcxx . . $ comedi_perror(85c52e162fed56f9).("comedi_get_subdevice_type()");
68 return -1;
69 }
70 if(subdevice_type != COMEDI_SUBD_COUNTERcxx . . $ COMEDI_SUBD_COUNTER.)
71 {
72 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Subdevice is not a counter (type %i), but of type %i.\n",
73 COMEDI_SUBD_COUNTERcxx . . $ COMEDI_SUBD_COUNTER., subdevice_type);
74 return -1;
75 }
76 return 0;
77 }
78
79 int ni_gpct_configure_buffered_event_countingcxx . . $ ni_gpct_configure_buffered_event_counting(7ee088ff0d1b2810).(comedi_tcxx . . $ comedi_t# *device, unsigned subdevice)
80 {
81 int retval;
82 lsampl_tcxx . . $ lsampl_t# counter_mode;
83 static const unsigned gate_pfi_channel = 1;
84 static const unsigned initial_count = 0;
85
86 retval = comedi_resetcxx . . $ comedi_reset(7ee088ff0d1b2810).(device, subdevice);
87 if(retval < 0) return retval;
88
89 #if 1
90 // PFI gate select works for e and m series
91 retval = comedi_set_gate_sourcecxx . . $ comedi_set_gate_source(443237b9c4bc6ce0).(device, subdevice, 0, 0, NI_GPCT_PFI_GATE_SELECTcxx . . $ `include/comedi.h:1110:9`!(gate_pfi_channel) /*| CR_EDGE*/);
92 #else
93 // gate pin gate select works for 660x
94 retval = comedi_set_gate_source(device, subdevice, 0, 0, NI_GPCT_GATE_PIN_i_GATE_SELECT /*| CR_EDGE*/);
95 #endif
96 if(retval < 0) return retval;
97 retval = comedi_set_gate_sourcecxx . . $ comedi_set_gate_source(443237b9c4bc6ce0).(device, subdevice, 0, 1, NI_GPCT_DISABLED_GATE_SELECTcxx . . $ NI_GPCT_DISABLED_GATE_SELECT. | CR_EDGEcxx . . $ `include/comedi.h:93:9`!);
98 if(retval < 0)
99 {
100 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to set second gate source. This is expected for older boards (e-series, etc.)\n"
101 "that don't have a second gate.\n");
102 }
103
104 counter_mode = NI_GPCT_COUNTING_MODE_NORMAL_BITScxx . . $ NI_GPCT_COUNTING_MODE_NORMAL_BITS.;
105 // output pulse on terminal count (doesn't really matter for this application)
106 counter_mode |= NI_GPCT_OUTPUT_TC_PULSE_BITScxx . . $ NI_GPCT_OUTPUT_TC_PULSE_BITS.;
107 /* Don't alternate the reload source between the load a and load b registers.*/
108 counter_mode |= NI_GPCT_RELOAD_SOURCE_FIXED_BITScxx . . $ NI_GPCT_RELOAD_SOURCE_FIXED_BITS.;
109 /* Reload counter on gate. Do not set if you want to do cumulative buffered counting. */
110 counter_mode |= NI_GPCT_LOADING_ON_GATE_BITcxx . . $ NI_GPCT_LOADING_ON_GATE_BIT.;
111 /* Make gate start/stop counting */
112 counter_mode |= NI_GPCT_EDGE_GATE_STARTS_STOPS_BITScxx . . $ NI_GPCT_EDGE_GATE_STARTS_STOPS_BITS.;
113 // count up
114 counter_mode |= NI_GPCT_COUNTING_DIRECTION_UP_BITScxx . . $ NI_GPCT_COUNTING_DIRECTION_UP_BITS.;
115 // don't stop on terminal count
116 counter_mode |= NI_GPCT_STOP_ON_GATE_BITScxx . . $ NI_GPCT_STOP_ON_GATE_BITS.;
117 // don't disarm on terminal count or gate signal
118 counter_mode |= NI_GPCT_NO_HARDWARE_DISARM_BITScxx . . $ NI_GPCT_NO_HARDWARE_DISARM_BITS.;
119 retval = comedi_set_counter_modecxx . . $ comedi_set_counter_mode(e80ed4ed1c56b623).(device, subdevice, 0, counter_mode);
120 if(retval < 0) return retval;
121
122 /* Set initial counter value by writing to channel 0.*/
123 retval = comedi_data_writecxx . . $ comedi_data_write(a3c2e9a72a6aa865).(device, subdevice, 0, 0, 0, initial_count);
124 /* Set value of "load a" register by writing to channel 1.*/
125 retval = comedi_data_writecxx . . $ comedi_data_write(a3c2e9a72a6aa865).(device, subdevice, 1, 0, 0, initial_count);
126
127 return 0;
128 }
129
130 int ni_gpct_send_commandcxx . . $ ni_gpct_send_command(c0e34f94d748b9d4).(comedi_tcxx . . $ comedi_t# *device, unsigned subdevice, unsigned n_counts)
131 {
132 comedi_cmdcxx . . $ comedi_cmd# cmd;
133 static const unsigned num_chan = 1;
134 lsampl_tcxx . . $ lsampl_t# chanlist[num_chan];
135 int retval;
136
137 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&cmd, 0, sizeof(cmd));
138 cmd.subdevcxx . . $ comedi_cmd_struct#subdev. = subdevice;
139 cmd.flagscxx . . $ comedi_cmd_struct#flags. = 0;
140 /* Wake up at the end of every scan, reduces latency for slow data streams.
141 Turn off for more efficient throughput. */
142 cmd.flagscxx . . $ comedi_cmd_struct#flags. |= TRIG_WAKE_EOScxx . . $ `include/comedi.h:168:9`!;
143 cmd.start_srccxx . . $ comedi_cmd_struct#start_src. = TRIG_NOWcxx . . $ `include/comedi.h:182:9`!;
144 cmd.start_argcxx . . $ comedi_cmd_struct#start_arg. = 0;
145 /* TRIG_OTHER will use the already configured gate source to latch counts. Can also use
146 TRIG_EXT and specify the gate source as the scan_begin_arg */
147 cmd.scan_begin_srccxx . . $ comedi_cmd_struct#scan_begin_src. = TRIG_OTHERcxx . . $ `include/comedi.h:189:9`!;
148 cmd.scan_begin_argcxx . . $ comedi_cmd_struct#scan_begin_arg. = 0;
149 cmd.convert_srccxx . . $ comedi_cmd_struct#convert_src. = TRIG_NOWcxx . . $ `include/comedi.h:182:9`!;
150 cmd.convert_argcxx . . $ comedi_cmd_struct#convert_arg. = 0;
151 cmd.scan_end_srccxx . . $ comedi_cmd_struct#scan_end_src. = TRIG_COUNTcxx . . $ `include/comedi.h:186:9`!;
152 cmd.scan_end_argcxx . . $ comedi_cmd_struct#scan_end_arg. = num_chan;
153 cmd.stop_srccxx . . $ comedi_cmd_struct#stop_src. = TRIG_NONEcxx . . $ `include/comedi.h:181:9`!;
154 cmd.stop_argcxx . . $ comedi_cmd_struct#stop_arg. = 0;
155 cmd.chanlistcxx . . $ comedi_cmd_struct#chanlist. = chanlist;
156 cmd.chanlist_lencxx . . $ comedi_cmd_struct#chanlist_len. = num_chan;
157 chanlist[0] = 0;
158
159 retval = comedi_command_testcxx . . $ comedi_command_test(43d1de8b01a26507).(device, &cmd);
160 if(retval)
161 {
162 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "comedi_command_test() failed.\n");
163 return -1;
164 }
165 retval = comedi_commandcxx . . $ comedi_command(43d1de8b01a26507).(device, &cmd);
166 if(retval)
167 {
168 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "comedi_command() failed.\n");
169 return -1;
170 }
171 return 0;
172 }
173
174 int ni_gpct_read_and_dump_countscxx . . $ ni_gpct_read_and_dump_counts(7ee088ff0d1b2810).(comedi_tcxx . . $ comedi_t# *device, unsigned subdevice)
175 {
176 int retval;
177 int fd;
178 static const unsigned buffer_size = 1000;
179 lsampl_tcxx . . $ lsampl_t# buffer[buffer_size];
180
181 fd = comedi_filenocxx . . $ comedi_fileno(33bb234f098f8f92).(device);
182 retval = readcxx . . $ read(7e34d0a36f355389).(fd, buffer, buffer_size * sizeof(lsampl_tcxx . . $ lsampl_t#));
183 while(retval > 0)
184 {
185 unsigned num_counts = retval / sizeof(lsampl_tcxx . . $ lsampl_t#);
186 unsigned i;
187 for(i = 0; i < num_counts; ++i)
188 {
189 printfcxx . . $ printf(581fddd72a8db8fc).("%i\n", buffer[i]);
190 }
191 retval = readcxx . . $ read(7e34d0a36f355389).(fd, buffer, buffer_size * sizeof(lsampl_tcxx . . $ lsampl_t#));
192 }
193 if(retval < 0)
194 {
195 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "error reading from subdevice file.\n");
196 perrorcxx . . $ perror(85c52e162fed56f9).("read");
197 return -errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
198 }
199 return 0;
200 }
201
202 int maincxx . . $ main(afcd3168fc1188a4).(int argc, char *argv[])
203 {
204 comedi_tcxx . . $ comedi_t# *device;
205 int retval;
206 struct parsed_optionscxx . . $ parsed_options# options;
207
208 init_parsed_optionscxx . . $ init_parsed_options(8caace97c9c42d8f).(&options);
209 options.subdevicecxx . . $ parsed_options#subdevice. = -1;
210 parse_optionscxx . . $ parse_options(b6aabb1543a6f628).(&options, argc, argv);
211 device = comedi_opencxx . . $ comedi_open(d67864c6c007fe39).(options.filenamecxx . . $ parsed_options#filename.);
212 if(!device)
213 {
214 comedi_perrorcxx . . $ comedi_perror(85c52e162fed56f9).(options.filenamecxx . . $ parsed_options#filename.);
215 exitcxx . . $ exit(3ca292c338df9bb3).(-1);
216 }
217 retval = check_subdevicecxx . . $ check_subdevice(bde13464cf6d94ba).(device, &options.subdevicecxx . . $ parsed_options#subdevice., options.filenamecxx . . $ parsed_options#filename.);
218 if(retval < 0) return retval;
219 printfcxx . . $ printf(581fddd72a8db8fc).("Running buffered event counting on subdevice %d.\n", options.subdevicecxx . . $ parsed_options#subdevice.);
220
221 retval = ni_gpct_configure_buffered_event_countingcxx . . $ ni_gpct_configure_buffered_event_counting(7ee088ff0d1b2810).(device, options.subdevicecxx . . $ parsed_options#subdevice.);
222 if(retval < 0) return retval;
223 retval = ni_gpct_send_commandcxx . . $ ni_gpct_send_command(c0e34f94d748b9d4).(device, options.subdevicecxx . . $ parsed_options#subdevice., options.n_scancxx . . $ parsed_options#n_scan.);
224 if(retval < 0) return retval;
225 retval = ni_gpct_read_and_dump_countscxx . . $ ni_gpct_read_and_dump_counts(7ee088ff0d1b2810).(device, options.subdevicecxx . . $ parsed_options#subdevice.);
226 if(retval < 0) return retval;
227 return 0;
228 }
229