hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/**
 * @file
 * Analogy for Linux, instruction related features
 *
 * @note Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
 * @note Copyright (C) 2008 Alexis Berlemont <alexis.berlemont@free.fr>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
 
#include <stdarg.h>
#include <errno.h>
#include <rtdm/analogy.h>
#include "internal.h"
 
/*!
 * @ingroup analogy_lib
 * @defgroup analogy_lib_level1 Level 1 API
 * @{
 */
 
/*!
 * @ingroup analogy_lib_level1
 * @defgroup analogy_lib_sync1 Synchronous acquisition API
 * @{
 */
 
/**
 * @brief Perform a list of synchronous acquisition misc operations
 *
 * The function a4l_snd_insnlist() is able to send many synchronous
 * instructions on a various set of subdevices, channels, etc.
 *
 * @param[in] dsc Device descriptor filled by a4l_open() (and
 * optionally a4l_fill_desc())
 * @param[in] arg Instructions list structure
 *
 * @return 0 on success. Otherwise:
 *
 * - -EINVAL is returned if some argument is missing or wrong (Please,
 *    type "dmesg" for more info)
 * - -EFAULT is returned if a user <-> kernel transfer went wrong
 * - -ENOMEM is returned if the system is out of memory
 *
 */
int a4l_snd_insnlist(a4l_desc_t * dsc, a4l_insnlst_t * arg)
{
   /* Basic checking */
   if (dsc == NULL || dsc->fd < 0)
       return -EINVAL;
 
   return __sys_ioctl(dsc->fd, A4L_INSNLIST, arg);
}
 
/**
 * @brief Perform a synchronous acquisition misc operation
 *
 * The function a4l_snd_insn() triggers a synchronous acquisition.
 *
 * @param[in] dsc Device descriptor filled by a4l_open() (and
 * optionally a4l_fill_desc())
 * @param[in] arg Instruction structure
 *
 * @return 0 on success. Otherwise:
 *
 * - -EINVAL is returned if some argument is missing or wrong (Please,
 *    type "dmesg" for more info)
 * - -EFAULT is returned if a user <-> kernel transfer went wrong
 * - -ENOMEM is returned if the system is out of memory
 *
 */
int a4l_snd_insn(a4l_desc_t * dsc, a4l_insn_t * arg)
{
   /* Basic checking */
   if (dsc == NULL || dsc->fd < 0)
       return -EINVAL;
 
   return __sys_ioctl(dsc->fd, A4L_INSN, arg);
}
 
/** @} Synchronous acquisition API */
 
/** @} Level 1 API */
 
/*!
 * @ingroup analogy_lib
 * @defgroup analogy_lib_level2 Level 2 API
 * @{
 */
 
/*!
 * @ingroup analogy_lib_level2
 * @defgroup analogy_lib_sync2 Synchronous acquisition API
 * @{
 */
 
/**
 * @brief Perform a synchronous acquisition write operation
 *
 * @param[in] dsc Device descriptor filled by a4l_open() (and
 * optionally a4l_fill_desc())
 * @param[in] idx_subd Index of the concerned subdevice
 * @param[in] chan_desc Channel descriptor (channel, range and
 * reference)
 * @param[in] ns_delay Optional delay (in nanoseconds) to wait between
 * the setting of the input channel and sample(s) acquisition(s).
 * @param[in] buf Output buffer
 * @param[in] nbyte Number of bytes to write
 *
 * @return Number of bytes written, otherwise negative error code:
 *
 * - -EINVAL is returned if some argument is missing or wrong (Please,
 *    type "dmesg" for more info)
 * - -EFAULT is returned if a user <-> kernel transfer went wrong
 * - -ENOMEM is returned if the system is out of memory
 *
 */
int a4l_sync_write(a4l_desc_t * dsc,
          unsigned int idx_subd,
          unsigned int chan_desc,
          unsigned int ns_delay, void *buf, size_t nbyte)
{
   int ret;
   a4l_insn_t insn_tab[2] = {
       {
           .type = A4L_INSN_WRITE,
           .idx_subd = idx_subd,
           .chan_desc = chan_desc,
           .data_size = 0,
           .data = buf
       }, {
           .type = A4L_INSN_WAIT,
           .idx_subd = idx_subd,
           .chan_desc = chan_desc,
           .data_size = 1,
           .data = NULL
       }
   };
 
   /* If some delay needs to be applied,
      the instruction list feature is needed */
   if (ns_delay != 0) {
       int ret;
       lsampl_t _delay = (lsampl_t) ns_delay;
       a4l_insnlst_t insnlst = {
           .count = 2,
           .insns = insn_tab
       };
 
       /* Sets the delay to wait */
       insn_tab[1].data = &_delay;
 
       /* Sends the two instructions (false read + wait)
          to the Analogy layer */
       ret = a4l_snd_insnlist(dsc, &insnlst);
       if (ret < 0)
           return ret;
   }
 
   /* The first instruction structure must be updated so as
      to write the proper data amount */
   insn_tab[0].data_size = nbyte;
 
   /* Sends the write instruction to the Analogy layer */
   ret = a4l_snd_insn(dsc, insn_tab);
 
   return (ret == 0) ? nbyte : ret;
}
 
/**
 * @brief Perform a synchronous acquisition read operation
 *
 * @param[in] dsc Device descriptor filled by a4l_open() (and
 * optionally a4l_fill_desc())
 * @param[in] idx_subd Index of the concerned subdevice
 * @param[in] chan_desc Channel descriptor (channel, range and
 * reference)
 * @param[in] ns_delay Optional delay (in nanoseconds) to wait between
 * the setting of the input channel and sample(s) acquisition(s).
 * @param[in] buf Input buffer
 * @param[in] nbyte Number of bytes to read
 *
 * @return Number of bytes read, otherwise negative error code:
 *
 * - -EINVAL is returned if some argument is missing or wrong (Please,
 *    type "dmesg" for more info)
 * - -EFAULT is returned if a user <-> kernel transfer went wrong
 * - -ENOMEM is returned if the system is out of memory
 *
 */
int a4l_sync_read(a4l_desc_t * dsc,
         unsigned int idx_subd,
         unsigned int chan_desc,
         unsigned int ns_delay, void *buf, size_t nbyte)
{
   int ret;
   a4l_insn_t insn_tab[2] = {
       {
           .type = A4L_INSN_READ,
           .idx_subd = idx_subd,
           .chan_desc = chan_desc,
           .data_size = nbyte,
           .data = buf},
       {
           .type = A4L_INSN_WAIT,
           .idx_subd = idx_subd,
           .chan_desc = chan_desc,
           .data_size = sizeof(unsigned int),
           .data = NULL}
   };
 
   /* If some delay needs to be applied,
      the instruction list feature is needed */
   if (ns_delay != 0) {
       int ret;
       lsampl_t _delay = (lsampl_t) ns_delay;
       a4l_insnlst_t insnlst = {
           .count = 2,
           .insns = insn_tab
       };
 
       /* Sets the delay to wait */
       insn_tab[1].data = &_delay;
 
       /* Sends the two instructions (false read + wait)
          to the Analogy layer */
       ret = a4l_snd_insnlist(dsc, &insnlst);
       if (ret < 0)
           return ret;
   }
 
   /* The first instruction structure must be updated so as
      to retrieve the proper data amount */
   insn_tab[0].data_size = nbyte;
 
   /* Sends the read instruction to the Analogy layer */
   ret = a4l_snd_insn(dsc, insn_tab);
 
   return (ret == 0) ? nbyte : ret;
}
 
/**
 * @brief Perform a synchronous acquisition digital acquisition
 *
 * @param[in] dsc Device descriptor filled by a4l_open() (and
 * optionally a4l_fill_desc())
 * @param[in] idx_subd Index of the concerned subdevice
 * @param[in] mask Write mask which indicates which bit(s) must be
 * modified
 * @param[in,out] buf Input / output buffer
 *
 * @return Number of bytes read, otherwise negative error code:
 *
 * - -EINVAL is returned if some argument is missing or wrong (Please,
 *    type "dmesg" for more info)
 * - -EFAULT is returned if a user <-> kernel transfer went wrong
 * - -ENOMEM is returned if the system is out of memory
 * - -ENOSYS is returned if the driver does not provide any handler
 *    "instruction bits"
 *
 */
int a4l_sync_dio(a4l_desc_t *dsc,
        unsigned int idx_subd, void *mask, void *buf)
{
   unsigned char values[16];
   a4l_insn_t insn = {
       .type = A4L_INSN_BITS,
       .idx_subd = idx_subd,
       .data = values,
   };
 
   int ret, size;
   a4l_sbinfo_t *subd;
 
   /* Get the subdevice descriptor */
   ret = a4l_get_subdinfo(dsc, idx_subd, &subd);
   if (ret < 0)
       return ret;
 
   /* Get the size in memory of a DIO acquisition */
   size = a4l_sizeof_subd(subd);
 
   switch(size) {
   case sizeof(uint32_t): {
       uint32_t *tmp = (uint32_t *)values;
       tmp[0] = *((uint32_t *)mask);
       tmp[1] = *((uint32_t *)buf);
       insn.data_size = 2 * sizeof(uint32_t);
       break;
   }
   case sizeof(uint16_t): {
       uint16_t *tmp = (uint16_t *)values;
       tmp[0] = *((uint16_t *)mask);
       tmp[1] = *((uint16_t *)buf);
       insn.data_size = 2 * sizeof(uint16_t);
       break;
   }
   case sizeof(uint8_t): {
       uint8_t *tmp = (uint8_t *)values;
       tmp[0] = *((uint8_t *)mask);
       tmp[1] = *((uint8_t *)buf);
       insn.data_size = 2 * sizeof(uint8_t);
       break;
   }
   default:
       return -EINVAL;
   }
 
   /* Send the insn_bits instruction */
   ret = a4l_snd_insn(dsc, &insn);
 
   /* Update the buffer if need be */
   switch(size) {
   case sizeof(uint32_t): {
       uint32_t *tmp = (uint32_t *)buf;
       *tmp = ((uint32_t *)values)[1];
       break;
   }
   case sizeof(uint16_t): {
       uint16_t *tmp = (uint16_t *)buf;
       *tmp = ((uint16_t *)values)[1];
       break;
   }
   case sizeof(uint8_t): {
       uint8_t *tmp = (uint8_t *)buf;
       *tmp = ((uint8_t *)values)[1];
       break;
   }
   }
 
   return ret;
}
 
/**
 * @brief Configure a subdevice
 *
 * a4l_config_subd() takes a variable count of arguments. According to
 * the configuration type, some additional argument is necessary:
 * - A4L_INSN_CONFIG_DIO_INPUT: the channel index (unsigned int)
 * - A4L_INSN_CONFIG_DIO_OUTPUT: the channel index (unsigned int)
 * - A4L_INSN_CONFIG_DIO_QUERY: the returned DIO polarity (unsigned
 *   int *)
 *
 * @param[in] dsc Device descriptor filled by a4l_open() (and
 * optionally a4l_fill_desc())
 * @param[in] idx_subd Index of the concerned subdevice
 * @param[in] type Configuration parameter
 *
 * @return 0 on success. Otherwise:
 *
 * - -EINVAL is returned if some argument is missing or wrong (Please,
 *    type "dmesg" for more info)
 * - -ENOSYS is returned if the configuration parameter is not
 *    supported
 *
 */
int a4l_config_subd(a4l_desc_t * dsc,
           unsigned int idx_subd, unsigned int type, ...)
{
   unsigned int values[4] = {type, 0, 0, 0};
   a4l_insn_t insn = {
       .type = A4L_INSN_CONFIG,
       .idx_subd = idx_subd,
       .data = values,
   };
   int ret = 0;
   va_list args;
 
   va_start(args, type);
 
   /* So far, few config types are supported */
   switch (type) {
   case A4L_INSN_CONFIG_DIO_OUTPUT:
   case A4L_INSN_CONFIG_DIO_INPUT:
   case A4L_INSN_CONFIG_DIO_OPENDRAIN:
   {
       unsigned int idx_chan = va_arg(args, unsigned int);
       insn.chan_desc = CHAN(idx_chan);
       insn.data_size = sizeof(unsigned int);
       break;
   }
   case A4L_INSN_CONFIG_DIO_QUERY:
       insn.data_size = 2 * sizeof(unsigned int);
       break;
   default:
       return -ENOSYS;
   }
 
   /* Send the config instruction */
   ret = a4l_snd_insn(dsc, &insn);
   if (ret < 0)
       goto out;
 
   /* Retrieve the result(s), if need be */
   switch (type) {
   case A4L_INSN_CONFIG_DIO_QUERY:
   {
       unsigned int *value = va_arg(args, unsigned int *);
       *value = values[1];
       break;
   }
   default:
       break;
   }
 
out:
   va_end(args);
 
   return ret;
}
 
/** @} Synchronous acquisition API */
 
/** @} Level 2 API */