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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/**
 * @file
 * Analogy for Linux, range 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 <errno.h>
#include <math.h>
#include "internal.h"
#include <rtdm/analogy.h>
 
#ifndef DOXYGEN_CPP
 
static lsampl_t data32_get(void *src)
{
   return (lsampl_t) * ((lsampl_t *) (src));
}
 
static lsampl_t data16_get(void *src)
{
   return (lsampl_t) * ((sampl_t *) (src));
}
 
static lsampl_t data8_get(void *src)
{
   return (lsampl_t) * ((unsigned char *)(src));
}
 
static void data32_set(void *dst, lsampl_t val)
{
   *((lsampl_t *) (dst)) = val;
}
 
static void data16_set(void *dst, lsampl_t val)
{
   *((sampl_t *) (dst)) = (sampl_t) (0xffff & val);
}
 
static void data8_set(void *dst, lsampl_t val)
{
   *((unsigned char *)(dst)) = (unsigned char)(0xff & val);
}
 
#endif /* !DOXYGEN_CPP */
 
/*!
 * @ingroup analogy_lib_level2
 * @defgroup analogy_lib_rng2 Range / conversion API
 * @{
 */
 
/**
 * @brief Get the size in memory of an acquired element
 *
 * According to the board, the channels have various acquisition
 * widths. With values like 8, 16 or 32, there is no problem finding
 * out the size in memory (1, 2, 4); however with widths like 12 or
 * 24, this function might be helpful to guess the size needed in RAM
 * for a single acquired element.
 *
 * @param[in] chan Channel descriptor
 *
 * @return the size in memory of an acquired element, otherwise a negative
 * error code:
 *
 * - -EINVAL is returned if the argument chan is NULL
 *
 */
int a4l_sizeof_chan(a4l_chinfo_t * chan)
{
   /* So far, it seems there is no 64 bit acquistion stuff */
   int i = 0, sizes[3] = {8, 16, 32};
 
   if (chan == NULL)
       return -EINVAL;
 
   while (i < 3 && sizes[i] < chan->nb_bits)
       i++;
 
   return (i == 3) ? -EINVAL : sizes[i] / 8;
}
 
/**
 * @brief Get the size in memory of a digital acquired element
 *
 * This function is only useful for DIO subdevices. Digital subdevices
 * are a specific kind of subdevice on which channels are regarded as
 * bits composing the subdevice's bitfield. During a DIO acquisition,
 * all bits are sampled. Therefore, a4l_sizeof_chan() is useless in
 * this case and we have to use a4l_sizeof_subd().
 * With bitfields which sizes are 8, 16 or 32, there is no problem
 * finding out the size in memory (1, 2, 4); however with widths like
 * 12 or 24, this function might be helpful to guess the size needed
 * in RAM for a single acquired element.
 *
 * @param[in] subd Subdevice descriptor
 *
 * @return the size in memory of an acquired element, otherwise a negative
 * error code:
 *
 * - -EINVAL is returned if the argument chan is NULL or if the
 *    subdevice is not a digital subdevice
 *
 */
int a4l_sizeof_subd(a4l_sbinfo_t *subd)
{
   /* So far, it seems there is no 64 bit acquistion stuff */
   int i = 0, sizes[3] = {8, 16, 32};
 
   if (subd == NULL)
       return -EINVAL;
 
   /* This function is only useful for DIO subdevice (all
      channels are acquired in one shot); for other kind of
      subdevice, the user must use a4l_sizeof_chan() so as to
      find out the size of the channel he wants to use */
   if ((subd->flags & A4L_SUBD_TYPES) != A4L_SUBD_DIO &&
       (subd->flags & A4L_SUBD_TYPES) != A4L_SUBD_DI &&
       (subd->flags & A4L_SUBD_TYPES) != A4L_SUBD_DO)
       return -EINVAL;
 
   while (i < 3 && sizes[i] < subd->nb_chan)
       i++;
 
   return (i == 3) ? -EINVAL : sizes[i] / 8;
}
 
/**
 * @brief Find the must suitable range
 *
 * @param[in] dsc Device descriptor filled by a4l_open() and
 * a4l_fill_desc()
 *
 * @param[in] idx_subd Index of the concerned subdevice
 * @param[in] idx_chan Index of the concerned channel
 * @param[in] unit Unit type used in the range
 * @param[in] min Minimal limit value
 * @param[in] max Maximal limit value
 * @param[out] rng Found range
 *
 * @return The index of the most suitable range on success. Otherwise:
 *
 * - -ENOENT is returned if a suitable range is not found.
 * - -EINVAL is returned if some argument is missing or wrong;
 *    idx_subd, idx_chan and the dsc pointer should be checked; check
 *    also the kernel log ("dmesg"); WARNING: a4l_fill_desc() should
 *    be called before using a4l_find_range()
 *
 */
int a4l_find_range(a4l_desc_t * dsc,
          unsigned int idx_subd,
          unsigned int idx_chan,
          unsigned long unit,
          double min, double max, a4l_rnginfo_t ** rng)
{
   int i, ret;
   long lmin, lmax;
   a4l_chinfo_t *chinfo;
   a4l_rnginfo_t *rnginfo, *tmp = NULL;
   unsigned int idx_rng = -ENOENT;
 
   if (rng != NULL)
       *rng = NULL;
 
   /* Basic checkings */
   if (dsc == NULL)
       return -EINVAL;
 
   /* a4l_fill_desc() must have been called on this descriptor */
   if (dsc->magic != MAGIC_CPLX_DESC)
       return -EINVAL;
 
   /* Retrieves the ranges count */
   ret = a4l_get_chinfo(dsc, idx_subd, idx_chan, &chinfo);
   if (ret < 0)
       return ret;
 
   /* Initializes variables */
   lmin = (long)(min * A4L_RNG_FACTOR);
   lmax = (long)(max * A4L_RNG_FACTOR);
 
   /* Performs the research */
   for (i = 0; i < chinfo->nb_rng; i++) {
 
       ret = a4l_get_rnginfo(dsc, idx_subd, idx_chan, i, &rnginfo);
       if (ret < 0)
           return ret;
 
       if (A4L_RNG_UNIT(rnginfo->flags) == unit &&
           rnginfo->min <= lmin && rnginfo->max >= lmax) {
 
           if (tmp != NULL) {
               if (rnginfo->min >= tmp->min &&
                   rnginfo->max <= tmp->max) {
                   idx_rng = i;
                   tmp = rnginfo;
               }
           } else {
               idx_rng = i;
               tmp = rnginfo;
           }
       }
   }
 
   if (rng != NULL)
       *rng = tmp;
 
   return idx_rng;
}
 
/**
 * @brief Unpack raw data (from the driver) into unsigned long values
 *
 * This function takes as input driver-specific data and scatters each
 * element into an entry of an unsigned long table. It is a
 * convenience routine which performs no conversion, just copy.
 *
 * @param[in] chan Channel descriptor
 * @param[out] dst Ouput buffer
 * @param[in] src Input buffer
 * @param[in] cnt Count of transfer to copy
 *
 * @return the count of copy performed, otherwise a negative error
 * code:
 *
 * - -EINVAL is returned if some argument is missing or wrong;
 *    chan, dst and src pointers should be checked; check also the
 *    kernel log ("dmesg"); WARNING: a4l_fill_desc() should be called
 *    before using a4l_ultoraw()
 *
 */
int a4l_rawtoul(a4l_chinfo_t * chan, unsigned long *dst, void *src, int cnt)
{
   int size, i = 0, j = 0;
 
   /* Temporary data accessor */
   lsampl_t(*datax_get) (void *);
 
   /* Basic checking */
   if (chan == NULL)
       return -EINVAL;
 
   /* Find out the size in memory */
   size = a4l_sizeof_chan(chan);
 
   /* Get the suitable accessor */
   switch (size) {
   case 4:
       datax_get = data32_get;
       break;
   case 2:
       datax_get = data16_get;
       break;
   case 1:
       datax_get = data8_get;
       break;
   default:
       return -EINVAL;
   };
 
   while (j < cnt) {
 
       /* Properly copy the data */
       dst[j] = (unsigned long)datax_get(src + i);
 
       /* Update the counters */
       i += size;
       j++;
   }
 
   return j;
}
 
/**
 * @brief Convert raw data (from the driver) to float-typed samples
 *
 * @param[in] chan Channel descriptor
 * @param[in] rng Range descriptor
 * @param[out] dst Ouput buffer
 * @param[in] src Input buffer
 * @param[in] cnt Count of conversion to perform
 *
 * @return the count of conversion performed, otherwise a negative
 * error code:
 *
 * - -EINVAL is returned if some argument is missing or wrong;
 *    chan, rng and the pointers should be checked; check also the
 *    kernel log ("dmesg"); WARNING: a4l_fill_desc() should be called
 *    before using a4l_rawtod()
 *
 */
int a4l_rawtof(a4l_chinfo_t * chan,
          a4l_rnginfo_t * rng, float *dst, void *src, int cnt)
{
   int size, i = 0, j = 0;
   lsampl_t tmp;
 
   /* Temporary values used for conversion
      (phys = a * src + b) */
   float a, b;
   /* Temporary data accessor */
   lsampl_t(*datax_get) (void *);
 
   /* Basic checking */
   if (rng == NULL || chan == NULL)
       return -EINVAL;
 
   /* Find out the size in memory */
   size = a4l_sizeof_chan(chan);
 
   /* Get the suitable accessor */
   switch (a4l_sizeof_chan(chan)) {
   case 4:
       datax_get = data32_get;
       break;
   case 2:
       datax_get = data16_get;
       break;
   case 1:
       datax_get = data8_get;
       break;
   default:
       return -EINVAL;
   };
 
   /* Compute the translation factor and the constant only once */
   a = ((float)(rng->max - rng->min)) /
       (((1ULL << chan->nb_bits) - 1) * A4L_RNG_FACTOR);
   b = ((float)rng->min) / A4L_RNG_FACTOR;
 
   while (j < cnt) {
 
       /* Properly retrieve the data */
       tmp = datax_get(src + i);
 
       /* Perform the conversion */
       dst[j] = a * tmp + b;
 
       /* Update the counters */
       i += size;
       j++;
   }
 
   return j;
}
 
/**
 * @brief Convert raw data (from the driver) to double-typed samples
 *
 * @param[in] chan Channel descriptor
 * @param[in] rng Range descriptor
 * @param[out] dst Ouput buffer
 * @param[in] src Input buffer
 * @param[in] cnt Count of conversion to perform
 *
 * @return the count of conversion performed, otherwise a negative
 * error code:
 *
 * - -EINVAL is returned if some argument is missing or wrong;
 *    chan, rng and the pointers should be checked; check also the
 *    kernel log ("dmesg"); WARNING: a4l_fill_desc() should be called
 *    before using a4l_rawtod()
 *
 */
int a4l_rawtod(a4l_chinfo_t * chan,
          a4l_rnginfo_t * rng, double *dst, void *src, int cnt)
{
   int size, i = 0, j = 0;
   lsampl_t tmp;
 
   /* Temporary values used for conversion
      (phys = a * src + b) */
   double a, b;
   /* Temporary data accessor */
   lsampl_t(*datax_get) (void *);
 
   /* Basic checking */
   if (rng == NULL || chan == NULL)
       return -EINVAL;
 
   /* Find out the size in memory */
   size = a4l_sizeof_chan(chan);
 
   /* Get the suitable accessor */
   switch (a4l_sizeof_chan(chan)) {
   case 4:
       datax_get = data32_get;
       break;
   case 2:
       datax_get = data16_get;
       break;
   case 1:
       datax_get = data8_get;
       break;
   default:
       return -EINVAL;
   };
 
   /* Computes the translation factor and the constant only once */
   a = ((double)(rng->max - rng->min)) /
       (((1ULL << chan->nb_bits) - 1) * A4L_RNG_FACTOR);
   b = ((double)rng->min) / A4L_RNG_FACTOR;
 
   while (j < cnt) {
 
       /* Properly retrieve the data */
       tmp = datax_get(src + i);
 
       /* Perform the conversion */
       dst[j] = a * tmp + b;
 
       /* Update the counters */
       i += size;
       j++;
   }
 
   return j;
}
 
/**
 * @brief Pack unsigned long values into raw data (for the driver)
 *
 * This function takes as input a table of unsigned long values and
 * gather them according to the channel width. It is a convenience
 * routine which performs no conversion, just formatting.
 *
 * @param[in] chan Channel descriptor
 * @param[out] dst Ouput buffer
 * @param[in] src Input buffer
 * @param[in] cnt Count of transfer to copy
 *
 * @return the count of copy performed, otherwise a negative error
 * code:
 *
 * - -EINVAL is returned if some argument is missing or wrong; chan,
 *    dst and src pointers should be checked; check also the kernel
 *    log ("dmesg"); WARNING: a4l_fill_desc() should be called before
 *    using a4l_ultoraw()
 *
 */
int a4l_ultoraw(a4l_chinfo_t * chan, void *dst, unsigned long *src, int cnt)
{
   int size, i = 0, j = 0;
 
   /* Temporary data accessor */
   void (*datax_set) (void *, lsampl_t);
 
   /* Basic checking */
   if (chan == NULL)
       return -EINVAL;
 
   /* Find out the size in memory */
   size = a4l_sizeof_chan(chan);
 
   /* Select the suitable accessor */
   switch (size) {
   case 4:
       datax_set = data32_set;
       break;
   case 2:
       datax_set = data16_set;
       break;
   case 1:
       datax_set = data8_set;
       break;
   default:
       return -EINVAL;
   };
 
   while (j < cnt) {
 
       /* Perform the copy */
       datax_set(dst + i, (lsampl_t)src[j]);
 
       /* Update the counters */
       i += size;
       j++;
   }
 
   return j;
}
 
/**
 * @brief Convert float-typed samples to raw data (for the driver)
 *
 * @param[in] chan Channel descriptor
 * @param[in] rng Range descriptor
 * @param[out] dst Ouput buffer
 * @param[in] src Input buffer
 * @param[in] cnt Count of conversion to perform
 *
 * @return the count of conversion performed, otherwise a negative
 * error code:
 *
 * - -EINVAL is returned if some argument is missing or wrong;
 *    chan, rng and the pointers should be checked; check also the
 *    kernel log ("dmesg"); WARNING: a4l_fill_desc() should be called
 *    before using a4l_ftoraw()
 *
 */
int a4l_ftoraw(a4l_chinfo_t * chan,
          a4l_rnginfo_t * rng, void *dst, float *src, int cnt)
{
   int size, i = 0, j = 0;
 
   /* Temporary values used for conversion
      (dst = a * phys - b) */
   float a, b;
   /* Temporary data accessor */
   void (*datax_set) (void *, lsampl_t);
 
   /* Basic checking */
   if (rng == NULL || chan == NULL)
       return -EINVAL;
 
   /* Find out the size in memory */
   size = a4l_sizeof_chan(chan);
 
   /* Select the suitable accessor */
   switch (size) {
   case 4:
       datax_set = data32_set;
       break;
   case 2:
       datax_set = data16_set;
       break;
   case 1:
       datax_set = data8_set;
       break;
   default:
       return -EINVAL;
   };
 
   /* Computes the translation factor and the constant only once */
   a = (((float)A4L_RNG_FACTOR) / (rng->max - rng->min)) *
       ((1ULL << chan->nb_bits) - 1);
   b = ((float)(rng->min) / (rng->max - rng->min)) *
       ((1ULL << chan->nb_bits) - 1);
 
   while (j < cnt) {
 
       /* Performs the conversion */
       datax_set(dst + i, (lsampl_t) (a * src[j] - b));
 
       /* Updates the counters */
       i += size;
       j++;
   }
 
   return j;
}
 
/**
 * @brief Convert double-typed samples to raw data (for the driver)
 *
 * @param[in] chan Channel descriptor
 * @param[in] rng Range descriptor
 * @param[out] dst Ouput buffer
 * @param[in] src Input buffer
 * @param[in] cnt Count of conversion to perform
 *
 * @return the count of conversion performed, otherwise a negative
 * error code:
 *
 * - -EINVAL is returned if some argument is missing or wrong;
 *    chan, rng and the pointers should be checked; check also the
 *    kernel log ("dmesg"); WARNING: a4l_fill_desc() should be called
 *    before using a4l_dtoraw()
 *
 */
int a4l_dtoraw(a4l_chinfo_t * chan,
          a4l_rnginfo_t * rng, void *dst, double *src, int cnt)
{
   int size, i = 0, j = 0;
 
   /* Temporary values used for conversion
      (dst = a * phys - b) */
   double a, b;
   /* Temporary data accessor */
   void (*datax_set) (void *, lsampl_t);
 
   /* Basic checking */
   if (rng == NULL || chan == NULL)
       return -EINVAL;
 
   /* Find out the size in memory */
   size = a4l_sizeof_chan(chan);
 
   /* Select the suitable accessor */
   switch (size) {
   case 4:
       datax_set = data32_set;
       break;
   case 2:
       datax_set = data16_set;
       break;
   case 1:
       datax_set = data8_set;
       break;
   default:
       return -EINVAL;
   };
 
   /* Computes the translation factor and the constant only once */
   a = (((double)A4L_RNG_FACTOR) / (rng->max - rng->min)) *
       ((1ULL << chan->nb_bits) - 1);
   b = ((double)(rng->min) / (rng->max - rng->min)) *
       ((1ULL << chan->nb_bits) - 1);
 
   while (j < cnt) {
 
       /* Performs the conversion */
       datax_set(dst + i, (lsampl_t) (a * src[j] - b));
 
       /* Updates the counters */
       i += size;
       j++;
   }
 
   return j;
}
/** @} Range / conversion  API */