Debian Codegraph @

demo/gpct_pulse_generator.c

raw symbols SCIP
1 /*
2 * NI general-purpose counter example. Configures the counter to
3 * produce a continuous pulse train. The argument specifies the
4 * number of nanoseconds the output pulse should remain high.
5 * The example assumes the board has already been configured to
6 * route the output signal of the counter to an appropriate
7 * location (you may need to route it to a PFI output line
8 * for example). The choose_routing demo may be helpful to
9 * set the routing of the counter's output signal.
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_pulse_generator.c:23:9`!
24
25 #include <stdio.h>
26 #include <comedilib.h>cxx . . $ `<file>/include/comedilib.h`/
27 #include <math.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <getopt.h>
34 #include <ctype.h>
35 #include "examples.h"cxx . . $ `<file>/demo/examples.h`/
36
37
38 int ni_gpct_start_pulse_generatorcxx . . $ ni_gpct_start_pulse_generator(e80ed4ed1c56b623).(comedi_tcxx . . $ comedi_t# *device, unsigned subdevice, unsigned period_ns, unsigned up_time_ns)
39 {
40 int retval;
41 lsampl_tcxx . . $ lsampl_t# counter_mode;
42 const unsigned clock_period_ns = 50; /* 20MHz clock */
43 unsigned up_ticks, down_ticks;
44
45 retval = comedi_resetcxx . . $ comedi_reset(7ee088ff0d1b2810).(device, subdevice);
46 if(retval < 0) return retval;
47
48 retval = comedi_set_gate_sourcecxx . . $ comedi_set_gate_source(443237b9c4bc6ce0).(device, subdevice, 0, 0, NI_GPCT_DISABLED_GATE_SELECTcxx . . $ NI_GPCT_DISABLED_GATE_SELECT. | CR_EDGEcxx . . $ `include/comedi.h:93:9`!);
49 if(retval < 0) return retval;
50 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`!);
51 if(retval < 0)
52 {
53 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"
54 "that don't have a second gate.\n");
55 }
56
57 counter_mode = NI_GPCT_COUNTING_MODE_NORMAL_BITScxx . . $ NI_GPCT_COUNTING_MODE_NORMAL_BITS.;
58 // toggle output on terminal count
59 counter_mode |= NI_GPCT_OUTPUT_TC_TOGGLE_BITScxx . . $ NI_GPCT_OUTPUT_TC_TOGGLE_BITS.;
60 // load on terminal count
61 counter_mode |= NI_GPCT_LOADING_ON_TC_BITcxx . . $ NI_GPCT_LOADING_ON_TC_BIT.;
62 // alternate the reload source between the load a and load b registers
63 counter_mode |= NI_GPCT_RELOAD_SOURCE_SWITCHING_BITScxx . . $ NI_GPCT_RELOAD_SOURCE_SWITCHING_BITS.;
64 // count down
65 counter_mode |= NI_GPCT_COUNTING_DIRECTION_DOWN_BITScxx . . $ NI_GPCT_COUNTING_DIRECTION_DOWN_BITS.;
66 // initialize load source as load b register
67 counter_mode |= NI_GPCT_LOAD_B_SELECT_BITcxx . . $ NI_GPCT_LOAD_B_SELECT_BIT.;
68 // don't stop on terminal count
69 counter_mode |= NI_GPCT_STOP_ON_GATE_BITScxx . . $ NI_GPCT_STOP_ON_GATE_BITS.;
70 // don't disarm on terminal count or gate signal
71 counter_mode |= NI_GPCT_NO_HARDWARE_DISARM_BITScxx . . $ NI_GPCT_NO_HARDWARE_DISARM_BITS.;
72 retval = comedi_set_counter_modecxx . . $ comedi_set_counter_mode(e80ed4ed1c56b623).(device, subdevice, 0, counter_mode);
73 if(retval < 0) return retval;
74
75 /* 20MHz clock */
76 retval = comedi_set_clock_sourcecxx . . $ comedi_set_clock_source(443237b9c4bc6ce0).(device, subdevice, 0, NI_GPCT_TIMEBASE_1_CLOCK_SRC_BITScxx . . $ NI_GPCT_TIMEBASE_1_CLOCK_SRC_BITS., clock_period_ns);
77 if(retval < 0) return retval;
78
79 up_ticks = (up_time_ns + clock_period_ns / 2) / clock_period_ns;
80 down_ticks = (period_ns + clock_period_ns / 2) / clock_period_ns - up_ticks;
81 /* set initial counter value by writing to channel 0 */
82 retval = comedi_data_writecxx . . $ comedi_data_write(a3c2e9a72a6aa865).(device, subdevice, 0, 0, 0, down_ticks);
83 if(retval < 0) return retval;
84 /* set "load a" register to the number of clock ticks the counter output should remain low
85 by writing to channel 1. */
86 retval = comedi_data_writecxx . . $ comedi_data_write(a3c2e9a72a6aa865).(device, subdevice, 1, 0, 0, down_ticks);
87 if(retval < 0) return retval;
88 /* set "load b" register to the number of clock ticks the counter output should remain high
89 by writing to channel 2 */
90 retval = comedi_data_writecxx . . $ comedi_data_write(a3c2e9a72a6aa865).(device, subdevice, 2, 0, 0, up_ticks);
91 if(retval < 0) return retval;
92
93 retval = comedi_armcxx . . $ comedi_arm(c0e34f94d748b9d4).(device, subdevice, NI_GPCT_ARM_IMMEDIATEcxx . . $ NI_GPCT_ARM_IMMEDIATE.);
94 if(retval < 0) return retval;
95
96 return 0;
97 }
98
99 int maincxx . . $ main(afcd3168fc1188a4).(int argc, char *argv[])
100 {
101 comedi_tcxx . . $ comedi_t# *device;
102 unsigned up_time;
103 unsigned period_ns;
104 int retval;
105 int subdevice_type;
106 struct parsed_optionscxx . . $ parsed_options# options;
107
108 init_parsed_optionscxx . . $ init_parsed_options(8caace97c9c42d8f).(&options);
109 options.valuecxx . . $ parsed_options#value. = -0.5;
110 parse_optionscxx . . $ parse_options(b6aabb1543a6f628).(&options, argc, argv);
111 period_ns = lrint(1e9 / options.freqcxx . . $ parsed_options#freq.);
112 if(options.valuecxx . . $ parsed_options#value. < 0.)
113 {
114 double duty;
115 if(-options.valuecxx . . $ parsed_options#value. < 1.0)
116 duty = -options.valuecxx . . $ parsed_options#value.;
117 else
118 duty = 1.0 / -options.valuecxx . . $ parsed_options#value.;
119 up_time = lrint(duty * period_ns);
120 }
121 else
122 up_time = lrint(options.valuecxx . . $ parsed_options#value.);
123 device = comedi_opencxx . . $ comedi_open(d67864c6c007fe39).(options.filenamecxx . . $ parsed_options#filename.);
124 if(!device)
125 {
126 comedi_perrorcxx . . $ comedi_perror(85c52e162fed56f9).(options.filenamecxx . . $ parsed_options#filename.);
127 exitcxx . . $ exit(3ca292c338df9bb3).(-1);
128 }
129 subdevice_type = comedi_get_subdevice_typecxx . . $ comedi_get_subdevice_type(7ee088ff0d1b2810).(device, options.subdevicecxx . . $ parsed_options#subdevice.);
130 if(subdevice_type < 0)
131 {
132 comedi_perrorcxx . . $ comedi_perror(85c52e162fed56f9).("comedi_get_subdevice_type()");
133 return -1;
134 }
135 if(subdevice_type != COMEDI_SUBD_COUNTERcxx . . $ COMEDI_SUBD_COUNTER.)
136 {
137 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Subdevice is not a counter (type %i), but of type %i.\n",
138 COMEDI_SUBD_COUNTERcxx . . $ COMEDI_SUBD_COUNTER., subdevice_type);
139 return -1;
140 }
141 printfcxx . . $ printf(581fddd72a8db8fc).("Generating pulse train on subdevice %d.\n", options.subdevicecxx . . $ parsed_options#subdevice.);
142 printfcxx . . $ printf(581fddd72a8db8fc).("Period = %d ns.\n", period_ns);
143 printfcxx . . $ printf(581fddd72a8db8fc).("Up Time = %d ns.\n", up_time);
144 printfcxx . . $ printf(581fddd72a8db8fc).("Down Time = %d ns.\n", period_ns - up_time);
145
146 retval = ni_gpct_start_pulse_generatorcxx . . $ ni_gpct_start_pulse_generator(e80ed4ed1c56b623).(device, options.subdevicecxx . . $ parsed_options#subdevice., period_ns, up_time);
147 if(retval < 0) return retval;
148 return 0;
149 }
150