Debian Codegraph @

demo/mmap.c

raw symbols SCIP
1 /*
2 * Example of using commands - asynchronous input
3 * Part of Comedilib
4 *
5 * Copyright (c) 1999,2000 David A. Schleef <[email protected]>
6 *
7 * This file may be freely modified, distributed, and combined with
8 * other software, as long as proper attribution is given in the
9 * source code.
10 */
11
12 /*
13 * An example for directly using Comedi commands. Comedi commands
14 * are used for asynchronous acquisition, with the timing controlled
15 * by on-board timers or external events.
16 */
17
18 #include <stdio.h>
19 #include <comedilib.h>cxx . . $ `<file>/include/comedilib.h`/
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <sys/mman.h>
26 #include <string.h>
27 #include "examples.h"cxx . . $ `<file>/demo/examples.h`/
28
29 #define N_CHANScxx . . $ `demo/mmap.c:29:9`! 256
30 unsigned int chanlistcxx . . $ chanlist.[N_CHANScxx . . $ `demo/mmap.c:29:9`!];
31
32 void *mapcxx . . $ map.;
33
34 int prepare_cmd_libcxx . . $ prepare_cmd_lib(dccf6e8bc23bbc03).(comedi_tcxx . . $ comedi_t# *dev, int subdevice, int n_scan, int n_chan, unsigned period_nanosec, comedi_cmdcxx . . $ comedi_cmd# *cmd);
35 int prepare_cmdcxx . . $ prepare_cmd(dccf6e8bc23bbc03).(comedi_tcxx . . $ comedi_t# *dev, int subdevice, int n_scan, int n_chan, unsigned period_nanosec, comedi_cmdcxx . . $ comedi_cmd# *cmd);
36
37
38 int maincxx . . $ main(afcd3168fc1188a4).(int argc, char *argv[])
39 {
40 comedi_tcxx . . $ comedi_t# *dev;
41 comedi_cmdcxx . . $ comedi_cmd# c,*cmd=&c;
42 unsigned int bufsize;
43 unsigned int front, back;
44 int ret;
45 unsigned int i;
46 unsigned int bufpos;
47 unsigned int subdev_flags;
48 unsigned int sample_size;
49 unsigned int nsamples;
50 unsigned int col;
51 struct parsed_optionscxx . . $ parsed_options# options;
52
53 init_parsed_optionscxx . . $ init_parsed_options(8caace97c9c42d8f).(&options);
54 options.subdevicecxx . . $ parsed_options#subdevice. = -1;
55 parse_optionscxx . . $ parse_options(b6aabb1543a6f628).(&options, argc, argv);
56 if(options.n_chancxx . . $ parsed_options#n_chan. > N_CHANScxx . . $ `demo/mmap.c:29:9`!){
57 options.n_chancxx . . $ parsed_options#n_chan. = N_CHANScxx . . $ `demo/mmap.c:29:9`!;
58 }
59
60 dev = comedi_opencxx . . $ comedi_open(d67864c6c007fe39).(options.filenamecxx . . $ parsed_options#filename.);
61 if(!dev){
62 comedi_perrorcxx . . $ comedi_perror(85c52e162fed56f9).(options.filenamecxx . . $ parsed_options#filename.);
63 exitcxx . . $ exit(3ca292c338df9bb3).(1);
64 }
65
66 if(options.subdevicecxx . . $ parsed_options#subdevice. < 0) {
67 /* Subdevice not set on command line. */
68 /* Default to the 'read' subdevice (if any). */
69 options.subdevicecxx . . $ parsed_options#subdevice. = comedi_get_read_subdevicecxx . . $ comedi_get_read_subdevice(33bb234f098f8f92).(dev);
70 if(options.subdevicecxx . . $ parsed_options#subdevice. < 0) {
71 /* No 'read' subdevice, so default to 0 instead. */
72 options.subdevicecxx . . $ parsed_options#subdevice. = 0;
73 }
74 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "defaulted to subdevice %d\n", options.subdevicecxx . . $ parsed_options#subdevice.);
75 }
76
77 /* Check subdevice exists. */
78 ret = comedi_get_n_subdevicescxx . . $ comedi_get_n_subdevices(33bb234f098f8f92).(dev);
79 if(ret <= options.subdevicecxx . . $ parsed_options#subdevice.){
80 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "subdevice %d does not exist\n", options.subdevicecxx . . $ parsed_options#subdevice.);
81 exitcxx . . $ exit(3ca292c338df9bb3).(1);
82 }
83
84 /* Check subdevice supports 'read' commands. */
85 ret = comedi_get_subdevice_flagscxx . . $ comedi_get_subdevice_flags(7ee088ff0d1b2810).(dev, options.subdevicecxx . . $ parsed_options#subdevice.);
86 if(ret < 0 || !(ret & SDF_CMD_READcxx . . $ `include/comedi.h:205:9`!)) {
87 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "subdevice %d does not support 'read' commands\n", options.subdevicecxx . . $ parsed_options#subdevice.);
88 exitcxx . . $ exit(3ca292c338df9bb3).(1);
89 }
90
91 ret = comedi_get_buffer_sizecxx . . $ comedi_get_buffer_size(7ee088ff0d1b2810).(dev, options.subdevicecxx . . $ parsed_options#subdevice.);
92 if(ret < 0){
93 comedi_perrorcxx . . $ comedi_perror(85c52e162fed56f9).("comedi_get_buffer_size");
94 exitcxx . . $ exit(3ca292c338df9bb3).(1);
95 }
96 bufsize = ret;
97 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!,"buffer size is %u\n", bufsize);
98 ret = comedi_get_subdevice_flagscxx . . $ comedi_get_subdevice_flags(7ee088ff0d1b2810).(dev, options.subdevicecxx . . $ parsed_options#subdevice.);
99 if(ret < 0){
100 comedi_perrorcxx . . $ comedi_perror(85c52e162fed56f9).("comedi_get_subdevice_flags");
101 }
102 subdev_flags = ret;
103 sample_size = (subdev_flags & SDF_LSAMPLcxx . . $ `include/comedi.h:222:9`!)
104 ? sizeof(lsampl_tcxx . . $ lsampl_t#) : sizeof(sampl_tcxx . . $ sampl_t#);
105 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!,"sample size is %u\n", sample_size);
106
107 /*
108 * Attempt to make options.subdevice the current 'read' subdevice,
109 * as that is the one whose buffer will be mapped.
110 */
111 comedi_set_read_subdevicecxx . . $ comedi_set_read_subdevice(7ee088ff0d1b2810).(dev, options.subdevicecxx . . $ parsed_options#subdevice.);
112 ret = comedi_get_read_subdevicecxx . . $ comedi_get_read_subdevice(33bb234f098f8f92).(dev);
113 if (ret < 0 || ret != options.subdevicecxx . . $ parsed_options#subdevice.) {
114 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!,
115 "failed to change 'read' subdevice from %d to %d\n",
116 ret, cmd->subdevcxx . . $ comedi_cmd_struct#subdev.);
117 exitcxx . . $ exit(3ca292c338df9bb3).(1);
118 }
119
120 mapcxx . . $ map. = mmapcxx . . $ mmap(ff06c1c904cdf0e2).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!,bufsize,PROT_READcxx . . $ `<external>/17226767460805440860/mman-linux.h:32:9`!,MAP_SHAREDcxx . . $ `<external>/17226767460805440860/mman-linux.h:42:9`!, comedi_filenocxx . . $ comedi_fileno(33bb234f098f8f92).(dev), 0);
121 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "map=%p\n", mapcxx . . $ map.);
122 if( mapcxx . . $ map. == MAP_FAILEDcxx . . $ `<external>/6724834311684550606/mman.h:44:9`! ){
123 perrorcxx . . $ perror(85c52e162fed56f9).( "mmap" );
124 exitcxx . . $ exit(3ca292c338df9bb3).(1);
125 }
126
127 for(i = 0; i < options.n_chancxx . . $ parsed_options#n_chan.; i++){
128 chanlistcxx . . $ chanlist.[i] = CR_PACKcxx . . $ `include/comedi.h:79:9`!(options.channel + i, options.range, options.aref);
129 }
130
131 /* prepare_cmd_lib() uses a Comedilib routine to find a
132 * good command for the device. prepare_cmd() explicitly
133 * creates a command, which may not work for your device. */
134 ret = prepare_cmd_libcxx . . $ prepare_cmd_lib(dccf6e8bc23bbc03).(dev, options.subdevicecxx . . $ parsed_options#subdevice., options.n_scancxx . . $ parsed_options#n_scan., options.n_chancxx . . $ parsed_options#n_chan., 1e9 / options.freqcxx . . $ parsed_options#freq., cmd);
135 //prepare_cmd(dev, options.subdevice, options.n_scan, options.n_chan, 1e9 / options.freq, cmd);
136 if(ret < 0){
137 exitcxx . . $ exit(3ca292c338df9bb3).(1);
138 }
139
140 ret = comedi_command_testcxx . . $ comedi_command_test(43d1de8b01a26507).(dev, cmd);
141
142 ret = comedi_command_testcxx . . $ comedi_command_test(43d1de8b01a26507).(dev, cmd);
143
144 if(ret != 0){
145 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!,"command_test failed\n");
146 exitcxx . . $ exit(3ca292c338df9bb3).(1);
147 }
148
149 dump_cmdcxx . . $ dump_cmd(2d5db0a34d5045d1).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, cmd);
150
151 ret = comedi_commandcxx . . $ comedi_command(43d1de8b01a26507).(dev, cmd);
152 if(ret < 0){
153 comedi_perrorcxx . . $ comedi_perror(85c52e162fed56f9).("comedi_command");
154 exitcxx . . $ exit(3ca292c338df9bb3).(1);
155 }
156
157 front = 0;
158 back = 0;
159 bufpos = 0;
160 col = 0;
161 while(1){
162 ret = comedi_get_buffer_contentscxx . . $ comedi_get_buffer_contents(7ee088ff0d1b2810).(dev, options.subdevicecxx . . $ parsed_options#subdevice.);
163 if(ret < 0){
164 comedi_perrorcxx . . $ comedi_perror(85c52e162fed56f9).("comedi_get_buffer_contents");
165 break;
166 }
167 front += ret;
168 nsamples = (front - back) / sample_size;
169 front = back + nsamples * sample_size;
170 if(options.verbosecxx . . $ parsed_options#verbose.) fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "front = %u, back = %u, samples = %u\n", front, back, nsamples);
171 if(front == back){
172 //comedi_poll(dev, options.subdevice);
173 usleepcxx . . $ usleep(4c679297bf94b9eb).(10000);
174 continue;
175 }
176 for(i = 0; i < nsamples; i++){
177 unsigned int sample;
178
179 if(sample_size == sizeof(sampl_tcxx . . $ sampl_t#))
180 sample = *(sampl_tcxx . . $ sampl_t# *)((char *)mapcxx . . $ map. + bufpos);
181 else
182 sample = *(lsampl_tcxx . . $ lsampl_t# *)((char *)mapcxx . . $ map. + bufpos);
183 printfcxx . . $ printf(581fddd72a8db8fc).("%u ", sample);
184 col++;
185 if(col == options.n_chancxx . . $ parsed_options#n_chan.){
186 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
187 col = 0;
188 }
189 bufpos += sample_size;
190 if(bufpos >= bufsize){
191 bufpos = 0;
192 }
193 }
194
195 ret = comedi_mark_buffer_readcxx . . $ comedi_mark_buffer_read(c0e34f94d748b9d4).(dev, options.subdevicecxx . . $ parsed_options#subdevice., front - back);
196 if(ret < 0){
197 comedi_perrorcxx . . $ comedi_perror(85c52e162fed56f9).("comedi_mark_buffer_read");
198 break;
199 }
200 back = front;
201 }
202
203 return 0;
204 }
205
206 int prepare_cmd_libcxx . . $ prepare_cmd_lib(dccf6e8bc23bbc03).(comedi_tcxx . . $ comedi_t# *dev, int subdevice, int n_scan, int n_chan, unsigned scan_period_nanosec, comedi_cmdcxx . . $ comedi_cmd# *cmd)
207 {
208 int ret;
209
210 ret = comedi_get_cmd_generic_timedcxx . . $ comedi_get_cmd_generic_timed(6f6d668026c3281f).(dev, subdevice, cmd, n_chan, scan_period_nanosec);
211 if(ret<0){
212 comedi_perrorcxx . . $ comedi_perror(85c52e162fed56f9).("comedi_get_cmd_generic_timed\n");
213 return ret;
214 }
215
216 cmd->chanlistcxx . . $ comedi_cmd_struct#chanlist. = chanlistcxx . . $ chanlist.;
217 cmd->chanlist_lencxx . . $ comedi_cmd_struct#chanlist_len. = n_chan;
218 if(cmd->stop_srccxx . . $ comedi_cmd_struct#stop_src. == TRIG_COUNTcxx . . $ `include/comedi.h:186:9`!) cmd->stop_argcxx . . $ comedi_cmd_struct#stop_arg. = n_scan;
219
220 return 0;
221 }
222
223 int prepare_cmdcxx . . $ prepare_cmd(dccf6e8bc23bbc03).(comedi_tcxx . . $ comedi_t# *dev, int subdevice, int n_scan, int n_chan, unsigned period_nanosec, comedi_cmdcxx . . $ comedi_cmd# *cmd)
224 {
225 memsetcxx . . $ memset(d1de1e4fb215f1c6).(cmd,0,sizeof(*cmd));
226
227 cmd->subdevcxx . . $ comedi_cmd_struct#subdev. = subdevice;
228
229 cmd->flagscxx . . $ comedi_cmd_struct#flags. = 0;
230
231 cmd->start_srccxx . . $ comedi_cmd_struct#start_src. = TRIG_NOWcxx . . $ `include/comedi.h:182:9`!;
232 cmd->start_argcxx . . $ comedi_cmd_struct#start_arg. = 0;
233
234 cmd->scan_begin_srccxx . . $ comedi_cmd_struct#scan_begin_src. = TRIG_TIMERcxx . . $ `include/comedi.h:185:9`!;
235 cmd->scan_begin_argcxx . . $ comedi_cmd_struct#scan_begin_arg. = period_nanosec;
236
237 cmd->convert_srccxx . . $ comedi_cmd_struct#convert_src. = TRIG_TIMERcxx . . $ `include/comedi.h:185:9`!;
238 cmd->convert_argcxx . . $ comedi_cmd_struct#convert_arg. = 1;
239
240 cmd->scan_end_srccxx . . $ comedi_cmd_struct#scan_end_src. = TRIG_COUNTcxx . . $ `include/comedi.h:186:9`!;
241 cmd->scan_end_argcxx . . $ comedi_cmd_struct#scan_end_arg. = n_chan;
242
243 cmd->stop_srccxx . . $ comedi_cmd_struct#stop_src. = TRIG_NONEcxx . . $ `include/comedi.h:181:9`!;
244 cmd->stop_argcxx . . $ comedi_cmd_struct#stop_arg. = 0;
245
246 cmd->chanlistcxx . . $ comedi_cmd_struct#chanlist. = chanlistcxx . . $ chanlist.;
247 cmd->chanlist_lencxx . . $ comedi_cmd_struct#chanlist_len. = n_chan;
248
249 return 0;
250 }
251
252
253