| 1 | /* |
| 2 | * NI general-purpose counter example. Configures the counter to |
| 3 | * start simple event counting. The counter's value can be read |
| 4 | * with the inp demo. Different clock sources can be chosen |
| 5 | * with the choose_clock demo. |
| 6 | * Part of Comedilib |
| 7 | * |
| 8 | * Copyright (c) 2007 Frank Mori Hess <[email protected]> |
| 9 | * |
| 10 | * This file may be freely modified, distributed, and combined with |
| 11 | * other software, as long as proper attribution is given in the |
| 12 | * source code. |
| 13 | */ |
| 14 | /* |
| 15 | * Requirements: A board with a National Instruments general-purpose |
| 16 | * counter, and comedi driver version 0.7.74 or newer. |
| 17 | */ |
| 18 | |
| 19 | #define _GNU_SOURCEcxx . . $ `demo/gpct_simple_counting.c:19:9`! |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <comedilib.h>cxx . . $ `<file>/include/comedilib.h`/ |
| 23 | #include <math.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <unistd.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <errno.h> |
| 29 | #include <getopt.h> |
| 30 | #include <ctype.h> |
| 31 | #include "examples.h"cxx . . $ `<file>/demo/examples.h`/ |
| 32 | |
| 33 | int ni_gpct_start_simple_event_countingcxx . . $ ni_gpct_start_simple_event_counting(7ee088ff0d1b2810).(comedi_tcxx . . $ comedi_t# *device, unsigned subdevice) |
| 34 | { |
| 35 | int retval; |
| 36 | lsampl_tcxx . . $ lsampl_t# counter_mode; |
| 37 | static const unsigned initial_count = 0; |
| 38 | |
| 39 | retval = comedi_resetcxx . . $ comedi_reset(7ee088ff0d1b2810).(device, subdevice); |
| 40 | if(retval < 0) return retval; |
| 41 | |
| 42 | retval = comedi_set_gate_sourcecxx . . $ comedi_set_gate_source(443237b9c4bc6ce0).(device, subdevice, 0, 0, NI_GPCT_GATE_PIN_GATE_SELECTcxx . . $ `include/comedi.h:1108:9`!(0) /* NI_GPCT_GATE_PIN_i_GATE_SELECT *//*| CR_EDGE*/); |
| 43 | if(retval < 0) return retval; |
| 44 | 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`!); |
| 45 | if(retval < 0) |
| 46 | { |
| 47 | 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" |
| 48 | "that don't have a second gate.\n"); |
| 49 | } |
| 50 | |
| 51 | counter_mode = NI_GPCT_COUNTING_MODE_NORMAL_BITScxx . . $ NI_GPCT_COUNTING_MODE_NORMAL_BITS.; |
| 52 | // output pulse on terminal count (doesn't really matter for this application) |
| 53 | counter_mode |= NI_GPCT_OUTPUT_TC_PULSE_BITScxx . . $ NI_GPCT_OUTPUT_TC_PULSE_BITS.; |
| 54 | /* Don't alternate the reload source between the load a and load b registers. |
| 55 | Doesn't really matter here, since we aren't going to be reloading the counter. |
| 56 | */ |
| 57 | counter_mode |= NI_GPCT_RELOAD_SOURCE_FIXED_BITScxx . . $ NI_GPCT_RELOAD_SOURCE_FIXED_BITS.; |
| 58 | // count up |
| 59 | counter_mode |= NI_GPCT_COUNTING_DIRECTION_UP_BITScxx . . $ NI_GPCT_COUNTING_DIRECTION_UP_BITS.; |
| 60 | // don't stop on terminal count |
| 61 | counter_mode |= NI_GPCT_STOP_ON_GATE_BITScxx . . $ NI_GPCT_STOP_ON_GATE_BITS.; |
| 62 | // don't disarm on terminal count or gate signal |
| 63 | counter_mode |= NI_GPCT_NO_HARDWARE_DISARM_BITScxx . . $ NI_GPCT_NO_HARDWARE_DISARM_BITS.; |
| 64 | retval = comedi_set_counter_modecxx . . $ comedi_set_counter_mode(e80ed4ed1c56b623).(device, subdevice, 0, counter_mode); |
| 65 | if(retval < 0) return retval; |
| 66 | |
| 67 | /* set initial counter value by writing to channel 0. The "load a" and "load b" registers can be |
| 68 | set by writing to channels 1 and 2 respectively. */ |
| 69 | retval = comedi_data_writecxx . . $ comedi_data_write(a3c2e9a72a6aa865).(device, subdevice, 0, 0, 0, initial_count); |
| 70 | |
| 71 | retval = comedi_armcxx . . $ comedi_arm(c0e34f94d748b9d4).(device, subdevice, NI_GPCT_ARM_IMMEDIATEcxx . . $ NI_GPCT_ARM_IMMEDIATE.); |
| 72 | if(retval < 0) return retval; |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | int maincxx . . $ main(afcd3168fc1188a4).(int argc, char *argv[]) |
| 78 | { |
| 79 | comedi_tcxx . . $ comedi_t# *device; |
| 80 | int retval; |
| 81 | struct parsed_optionscxx . . $ parsed_options# options; |
| 82 | |
| 83 | init_parsed_optionscxx . . $ init_parsed_options(8caace97c9c42d8f).(&options); |
| 84 | parse_optionscxx . . $ parse_options(b6aabb1543a6f628).(&options, argc, argv); |
| 85 | device = comedi_opencxx . . $ comedi_open(d67864c6c007fe39).(options.filenamecxx . . $ parsed_options#filename.); |
| 86 | if(!device) |
| 87 | { |
| 88 | comedi_perrorcxx . . $ comedi_perror(85c52e162fed56f9).(options.filenamecxx . . $ parsed_options#filename.); |
| 89 | exitcxx . . $ exit(3ca292c338df9bb3).(-1); |
| 90 | } |
| 91 | /*FIXME: check that device is counter */ |
| 92 | printfcxx . . $ printf(581fddd72a8db8fc).("Initiating simple event counting on subdevice %d.\n", options.subdevicecxx . . $ parsed_options#subdevice.); |
| 93 | |
| 94 | retval = ni_gpct_start_simple_event_countingcxx . . $ ni_gpct_start_simple_event_counting(7ee088ff0d1b2810).(device, options.subdevicecxx . . $ parsed_options#subdevice.); |
| 95 | if(retval < 0) return retval; |
| 96 | return 0; |
| 97 | } |
| 98 |