Moved some wrappers for INSN_CONFIG instructions into comedilib.
[comedilib.git] / include / comedilib.h
1 /*
2     include/comedilib.h
3     header file for the comedi library routines
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 1998-2002 David A. Schleef <ds@schleef.org>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU Lesser General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 */
23
24
25 #ifndef _COMEDILIB_H
26 #define _COMEDILIB_H
27
28 #include <comedi.h>
29 #include <sys/ioctl.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 typedef struct comedi_t_struct comedi_t;
38
39 typedef struct{
40         double min;
41         double max;
42         unsigned int unit;
43 }comedi_range;
44
45 typedef struct comedi_sv_struct{
46         comedi_t *dev;
47         unsigned int subdevice;
48         unsigned int chan;
49
50         /* range policy */
51         int range;
52         int aref;
53
54         /* number of measurements to average (for ai) */
55         int n;
56
57         lsampl_t maxdata;
58 }comedi_sv_t;
59
60 enum comedi_oor_behavior {
61         COMEDI_OOR_NUMBER = 0,
62         COMEDI_OOR_NAN
63 };
64
65
66
67
68 comedi_t *comedi_open(const char *fn);
69 int comedi_close(comedi_t *it);
70
71 /* logging */
72 int comedi_loglevel(int loglevel);
73 void comedi_perror(const char *s);
74 char *comedi_strerror(int errnum);
75 int comedi_errno(void);
76 int comedi_fileno(comedi_t *it);
77
78 /* global behavior */
79 enum comedi_oor_behavior comedi_set_global_oor_behavior(enum comedi_oor_behavior behavior);
80
81 /* device queries */
82 int comedi_get_n_subdevices(comedi_t *it);
83 #define COMEDI_VERSION_CODE(a,b,c) (((a)<<16) | ((b)<<8) | (c))
84 int comedi_get_version_code(comedi_t *it);
85 const char *comedi_get_driver_name(comedi_t *it);
86 const char *comedi_get_board_name(comedi_t *it);
87 int comedi_get_read_subdevice(comedi_t *dev);
88 int comedi_get_write_subdevice(comedi_t *dev);
89
90 /* subdevice queries */
91 int comedi_get_subdevice_type(comedi_t *it,unsigned int subdevice);
92 int comedi_find_subdevice_by_type(comedi_t *it,int type,unsigned int subd);
93 int comedi_get_subdevice_flags(comedi_t *it,unsigned int subdevice);
94 int comedi_get_n_channels(comedi_t *it,unsigned int subdevice);
95 int comedi_range_is_chan_specific(comedi_t *it,unsigned int subdevice);
96 int comedi_maxdata_is_chan_specific(comedi_t *it,unsigned int subdevice);
97
98 /* channel queries */
99 lsampl_t comedi_get_maxdata(comedi_t *it,unsigned int subdevice,
100         unsigned int chan);
101 int comedi_get_n_ranges(comedi_t *it,unsigned int subdevice,
102         unsigned int chan);
103 comedi_range * comedi_get_range(comedi_t *it,unsigned int subdevice,
104         unsigned int chan,unsigned int range);
105 int comedi_find_range(comedi_t *it,unsigned int subd,unsigned int chan,
106         unsigned int unit,double min,double max);
107
108 /* buffer queries */
109 int comedi_get_buffer_size(comedi_t *it,unsigned int subdevice);
110 int comedi_get_max_buffer_size(comedi_t *it,unsigned int subdevice);
111 int comedi_set_buffer_size(comedi_t *it,unsigned int subdevice,
112         unsigned int len);
113
114 /* low-level stuff */
115 #ifdef _COMEDILIB_DEPRECATED
116 int comedi_trigger(comedi_t *it,comedi_trig *trig); /* deprecated */
117 #endif
118 int comedi_do_insnlist(comedi_t *it,comedi_insnlist *il);
119 int comedi_do_insn(comedi_t *it,comedi_insn *insn);
120 int comedi_lock(comedi_t *it,unsigned int subdevice);
121 int comedi_unlock(comedi_t *it,unsigned int subdevice);
122
123 /* physical units */
124 double comedi_to_phys(lsampl_t data,comedi_range *rng,lsampl_t maxdata);
125 lsampl_t comedi_from_phys(double data,comedi_range *rng,lsampl_t maxdata);
126 int comedi_sampl_to_phys(double *dest, int dst_stride, sampl_t *src,
127         int src_stride, comedi_range *rng, lsampl_t maxdata, int n);
128 int comedi_sampl_from_phys(sampl_t *dest,int dst_stride,double *src,
129         int src_stride, comedi_range *rng, lsampl_t maxdata, int n);
130
131 /* syncronous stuff */
132 int comedi_data_read(comedi_t *it,unsigned int subd,unsigned int chan,
133         unsigned int range,unsigned int aref,lsampl_t *data);
134 int comedi_data_read_n(comedi_t *it,unsigned int subd,unsigned int chan,
135         unsigned int range,unsigned int aref,lsampl_t *data, unsigned int n);
136 int comedi_data_read_hint(comedi_t *it,unsigned int subd,unsigned int chan,
137         unsigned int range,unsigned int aref);
138 int comedi_data_read_delayed(comedi_t *it,unsigned int subd,unsigned int chan,
139         unsigned int range,unsigned int aref,lsampl_t *data, unsigned int nano_sec);
140 int comedi_data_write(comedi_t *it,unsigned int subd,unsigned int chan,
141         unsigned int range,unsigned int aref,lsampl_t data);
142 int comedi_dio_config(comedi_t *it,unsigned int subd,unsigned int chan,
143         unsigned int dir);
144 int comedi_dio_get_config(comedi_t *it,unsigned int subd,unsigned int chan,
145         unsigned int *dir);
146 int comedi_dio_read(comedi_t *it,unsigned int subd,unsigned int chan,
147         unsigned int *bit);
148 int comedi_dio_write(comedi_t *it,unsigned int subd,unsigned int chan,
149         unsigned int bit);
150 int comedi_dio_bitfield2(comedi_t *it,unsigned int subd,
151         unsigned int write_mask, unsigned int *bits, unsigned int base_channel);
152 /* Should be moved to _COMEDILIB_DEPRECATED once bindings for other languages are updated
153  * to use comedi_dio_bitfield2() instead.*/
154 int comedi_dio_bitfield(comedi_t *it,unsigned int subd,
155         unsigned int write_mask, unsigned int *bits);
156
157 /* slowly varying stuff */
158 int comedi_sv_init(comedi_sv_t *it,comedi_t *dev,unsigned int subd,unsigned int chan);
159 int comedi_sv_update(comedi_sv_t *it);
160 int comedi_sv_measure(comedi_sv_t *it,double *data);
161
162 /* streaming I/O (commands) */
163
164 int comedi_get_cmd_src_mask(comedi_t *dev,unsigned int subdevice,
165         comedi_cmd *cmd);
166 int comedi_get_cmd_generic_timed(comedi_t *dev,unsigned int subdevice,
167         comedi_cmd *cmd, unsigned chanlist_len, unsigned scan_period_ns);
168 int comedi_cancel(comedi_t *it,unsigned int subdevice);
169 int comedi_command(comedi_t *it,comedi_cmd *cmd);
170 int comedi_command_test(comedi_t *it,comedi_cmd *cmd);
171 int comedi_poll(comedi_t *dev,unsigned int subdevice);
172
173 /* buffer control */
174
175 int comedi_set_max_buffer_size(comedi_t *it, unsigned int subdev,
176         unsigned int max_size);
177 int comedi_get_buffer_contents(comedi_t *it, unsigned int subdev);
178 int comedi_mark_buffer_read(comedi_t *it, unsigned int subdev,
179         unsigned int bytes);
180 int comedi_mark_buffer_written(comedi_t *it, unsigned int subdev,
181         unsigned int bytes);
182 int comedi_get_buffer_offset(comedi_t *it, unsigned int subdev);
183
184 #ifdef _COMEDILIB_DEPRECATED
185 /*
186  * The following functions are deprecated and should not be used.
187  */
188 int comedi_get_timer(comedi_t *it,unsigned int subdev,double freq,
189         unsigned int *trigvar,double *actual_freq);
190 int comedi_timed_1chan(comedi_t *it,unsigned int subdev,unsigned int chan,
191         unsigned int range, unsigned int aref,double freq,
192         unsigned int n_samples,double *data);
193 int comedi_get_rangetype(comedi_t *it,unsigned int subdevice,
194         unsigned int chan);
195 #endif
196
197
198 #ifndef _COMEDILIB_STRICT_ABI
199 /*
200    The following prototypes are _NOT_ part of the Comedilib ABI, and
201    may change in future versions without regard to source or binary
202    compatibility.  In practice, this is a holding place for the next
203    library ABI version change.
204  */
205 /* structs and functions used for parsing calibration files */
206 typedef struct
207 {
208         unsigned int subdevice;
209         unsigned int channel;
210         unsigned int value;
211 } comedi_caldac_t;
212 #define COMEDI_MAX_NUM_POLYNOMIAL_COEFFICIENTS 4
213 typedef struct
214 {
215         double coefficients[COMEDI_MAX_NUM_POLYNOMIAL_COEFFICIENTS];
216         double expansion_origin;
217         unsigned order;
218 } comedi_polynomial_t;
219 typedef struct
220 {
221         comedi_polynomial_t *to_phys;
222         comedi_polynomial_t *from_phys;
223 } comedi_softcal_t;
224 #define CS_MAX_AREFS_LENGTH 4
225 typedef struct
226 {
227         unsigned int subdevice;
228         unsigned int *channels;
229         unsigned int num_channels;
230         unsigned int *ranges;
231         unsigned int num_ranges;
232         unsigned int arefs[ CS_MAX_AREFS_LENGTH ];
233         unsigned int num_arefs;
234         comedi_caldac_t *caldacs;
235         unsigned int num_caldacs;
236         comedi_softcal_t soft_calibration;
237 } comedi_calibration_setting_t;
238
239 typedef struct
240 {
241         char *driver_name;
242         char *board_name;
243         comedi_calibration_setting_t *settings;
244         unsigned int num_settings;
245 } comedi_calibration_t;
246
247 comedi_calibration_t* comedi_parse_calibration_file( const char *cal_file_path );
248 int comedi_apply_parsed_calibration( comedi_t *dev, unsigned int subdev, unsigned int channel,
249         unsigned int range, unsigned int aref, const comedi_calibration_t *calibration );
250 char* comedi_get_default_calibration_path( comedi_t *dev );
251 void comedi_cleanup_calibration( comedi_calibration_t *calibration );
252 int comedi_apply_calibration( comedi_t *dev, unsigned int subdev, unsigned int channel,
253         unsigned int range, unsigned int aref, const char *cal_file_path);
254
255 /* New stuff to provide conversion between integers and physical values that
256 * can support software calibrations. */
257 enum comedi_conversion_direction
258 {
259         COMEDI_TO_PHYSICAL,
260         COMEDI_FROM_PHYSICAL
261 };
262 int comedi_get_softcal_converter(
263         unsigned subdevice, unsigned channel, unsigned range,
264         enum comedi_conversion_direction direction,
265         const comedi_calibration_t *calibration, comedi_polynomial_t* polynomial);
266 int comedi_get_hardcal_converter(
267         comedi_t *dev, unsigned subdevice, unsigned channel, unsigned range,
268         enum comedi_conversion_direction direction, comedi_polynomial_t* polynomial);
269 double comedi_to_physical(lsampl_t data,
270         const comedi_polynomial_t *conversion_polynomial);
271 lsampl_t comedi_from_physical(double data,
272         const comedi_polynomial_t *conversion_polynomial);
273
274 /* INSN_CONFIG wrappers */
275 int comedi_reset(comedi_t *device, unsigned subdevice);
276 int comedi_arm(comedi_t *device, unsigned subdevice, unsigned target);
277 int comedi_set_counter_mode(comedi_t *device, unsigned subdevice, unsigned mode_bits);
278 int comedi_set_clock_source(comedi_t *device, unsigned subdevice, unsigned clock, unsigned period_ns);
279 int comedi_set_gate_source(comedi_t *device, unsigned subdevice, unsigned gate_index, unsigned gate_source);
280 int comedi_internal_trigger(comedi_t *dev, unsigned subd, unsigned trignum);
281 int comedi_set_other_source(comedi_t *device, unsigned subdevice,
282         unsigned other, unsigned source);
283
284 #endif
285
286 #ifdef __cplusplus
287 }
288 #endif
289
290 #endif
291