Module: xenomai-head
Branch: master
Commit: cca3eaa0adf25bc27bba449601c7d01a0080e495
URL:    
http://git.xenomai.org/?p=xenomai-head.git;a=commit;h=cca3eaa0adf25bc27bba449601c7d01a0080e495

Author: Alexis Berlemont <alexis.berlem...@gmail.com>
Date:   Sat Dec 19 20:59:09 2009 +0100

analogy: review the conversion routines in the user library

The conversion routines (raw data from driver <-> physical values: V,
mA) did not behave well with specific acquisition widths (12 and 24
bits). Furthermore, there was an ambiguity in the functions
declarations; the functions are now more explicit.

---

 include/analogy/analogy.h      |   40 +++--
 src/drvlib/analogy/range.c     |  335 +++++++++++++++++++++++++++++++++++-----
 src/utils/analogy/insn_read.c  |  180 +++++++++++++++++-----
 src/utils/analogy/insn_write.c |    8 +-
 4 files changed, 466 insertions(+), 97 deletions(-)

diff --git a/include/analogy/analogy.h b/include/analogy/analogy.h
index 79bd69c..bc778b1 100644
--- a/include/analogy/analogy.h
+++ b/include/analogy/analogy.h
@@ -68,33 +68,33 @@ int a4l_sys_rnginfo(int fd,
 
 /* --- Level 1 API (supposed to be used) --- */
     
-int a4l_get_desc(int fd, a4l_desc_t * dsc, int pass);
+int a4l_get_desc(int fd, a4l_desc_t *dsc, int pass);
 
-int a4l_open(a4l_desc_t * dsc, const char *fname);
+int a4l_open(a4l_desc_t *dsc, const char *fname);
 
-int a4l_close(a4l_desc_t * dsc);
+int a4l_close(a4l_desc_t *dsc);
 
-int a4l_fill_desc(a4l_desc_t * dsc);
+int a4l_fill_desc(a4l_desc_t *dsc);
 
-int a4l_get_subdinfo(a4l_desc_t * dsc,
-                    unsigned int subd, a4l_sbinfo_t ** info);
+int a4l_get_subdinfo(a4l_desc_t *dsc,
+                    unsigned int subd, a4l_sbinfo_t **info);
 
-int a4l_get_chinfo(a4l_desc_t * dsc,
+int a4l_get_chinfo(a4l_desc_t *dsc,
                   unsigned int subd,
-                  unsigned int chan, a4l_chinfo_t ** info);
+                  unsigned int chan, a4l_chinfo_t **info);
 
 #define a4l_get_chan_max(x) (1ULL << (x)->nb_bits)
 
 #define a4l_is_chan_global(x) ((x)->chan_flags & A4L_CHAN_GLOBAL)
 
-int a4l_get_rnginfo(a4l_desc_t * dsc,
+int a4l_get_rnginfo(a4l_desc_t *dsc,
                    unsigned int subd,
                    unsigned int chan,
                    unsigned int rng, a4l_rnginfo_t ** info);
 
 #define a4l_is_rng_global(x) ((x)->flags & A4L_RNG_GLOBAL)
 
-int a4l_snd_command(a4l_desc_t * dsc, a4l_cmd_t * cmd);
+int a4l_snd_command(a4l_desc_t * dsc, a4l_cmd_t *cmd);
     
 int a4l_snd_cancel(a4l_desc_t * dsc, unsigned int idx_subd);
 
@@ -116,7 +116,7 @@ int a4l_mmap(a4l_desc_t * dsc,
 
 int a4l_snd_insnlist(a4l_desc_t * dsc, a4l_insnlst_t * arg);
 
-int a4l_snd_insn(a4l_desc_t * dsc, a4l_insn_t * arg);
+int a4l_snd_insn(a4l_desc_t * dsc, a4l_insn_t *arg);
 
 /* --- Level 2 API (supposed to be used) --- */
 
@@ -138,11 +138,21 @@ int a4l_find_range(a4l_desc_t * dsc,
                   unsigned long unit,
                   double min, double max, a4l_rnginfo_t ** rng);
 
-int a4l_from_phys(a4l_chinfo_t * chan,
-                 a4l_rnginfo_t * rng, double *dst, void *src, int cnt);
+int a4l_rawtoul(a4l_chinfo_t * chan, unsigned long *dst, void *src, int cnt);
 
-int a4l_to_phys(a4l_chinfo_t * chan,
-               a4l_rnginfo_t * rng, void *dst, double *src, int cnt);
+int a4l_rawtof(a4l_chinfo_t * chan,
+              a4l_rnginfo_t * rng, float *dst, void *src, int cnt);
+
+int a4l_rawtod(a4l_chinfo_t * chan,
+              a4l_rnginfo_t * rng, double *dst, void *src, int cnt);
+
+int a4l_ultoraw(a4l_chinfo_t * chan, void *dst, unsigned long *src, int cnt);
+
+int a4l_ftoraw(a4l_chinfo_t * chan,
+              a4l_rnginfo_t * rng, void *dst, float *src, int cnt);
+
+int a4l_dtoraw(a4l_chinfo_t * chan,
+              a4l_rnginfo_t * rng, void *dst, double *src, int cnt);
 
 #endif /* !DOXYGEN_CPP */
 
diff --git a/src/drvlib/analogy/range.c b/src/drvlib/analogy/range.c
index 05615bd..9490c71 100644
--- a/src/drvlib/analogy/range.c
+++ b/src/drvlib/analogy/range.c
@@ -96,7 +96,6 @@ int a4l_sizeof_chan(a4l_chinfo_t * chan)
        return (i == 3) ? -EINVAL : sizes[i] / 8;
 }
 
-
 /**
  * @brief Find the must suitable range
  *
@@ -175,7 +174,71 @@ out_get_range:
 }
 
 /**
- * @brief Convert physical units to samples
+ * @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[in] rng Range 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, rng and the 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
@@ -189,19 +252,87 @@ out_get_range:
  * - -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_to_phys()
+ *    before using a4l_rawtod()
  *
  */
-int a4l_from_phys(a4l_chinfo_t * chan,
-                 a4l_rnginfo_t * rng, double *dst, void *src, int cnt)
+int a4l_rawtof(a4l_chinfo_t * chan,
+              a4l_rnginfo_t * rng, float *dst, void *src, int cnt)
 {
-       int i = 0, j = 0;
+       int size, i = 0, j = 0;
        lsampl_t tmp;
 
-       /* Bytes count used for conversion; the bit width may be
-          different from the acquisition bits size:
-          Ex.: acq_size = 12 bits => conv_size = 16 bits */
-       int conv_size;
+       /* 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) */
@@ -211,15 +342,13 @@ int a4l_from_phys(a4l_chinfo_t * chan,
 
        /* Basic checking */
        if (rng == NULL || chan == NULL)
-               return 0;
+               return -EINVAL;
 
-       /* Computes the conversion width */
-       conv_size = (chan->nb_bits % 8 == 0) ? 
-               chan->nb_bits / 8 : (chan->nb_bits / 8) + 1;
+       /* Find out the size in memory */ 
+       size = a4l_sizeof_chan(chan);
 
-       /* This converting function only works 
-          if acquired data width is 8, 16 or 32 */
-       switch (conv_size) {
+       /* Get the suitable accessor */
+       switch (a4l_sizeof_chan(chan)) {
        case 4:
                datax_get = data32_get;
                break;
@@ -236,18 +365,18 @@ int a4l_from_phys(a4l_chinfo_t * chan,
        /* 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;
+       b = ((double)rng->min) / A4L_RNG_FACTOR;
 
        while (j < cnt) {
 
-               /* Properly retrieves the data */
+               /* Properly retrieve the data */
                tmp = datax_get(src + i);
 
-               /* Performs the conversion */
+               /* Perform the conversion */
                dst[j] = a * tmp + b;
 
-               /* Updates the counters */
-               i += conv_size;
+               /* Update the counters */
+               i += size;
                j++;
        }
 
@@ -255,7 +384,71 @@ int a4l_from_phys(a4l_chinfo_t * chan,
 }
 
 /**
- * @brief Convert samples to physical units
+ * @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[in] rng Range 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, rng and the 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
@@ -269,18 +462,83 @@ int a4l_from_phys(a4l_chinfo_t * chan,
  * - -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_from_phys()
+ *    before using a4l_ftoraw()
  *
  */
-int a4l_to_phys(a4l_chinfo_t * chan,
-               a4l_rnginfo_t * rng, void *dst, double *src, int cnt)
+int a4l_ftoraw(a4l_chinfo_t * chan,
+              a4l_rnginfo_t * rng, void *dst, float *src, int cnt)
 {
-       int i = 0, j = 0;
+       int size, i = 0, j = 0;
 
-       /* Bytes count used for conversion; the bit width may be
-          different from the acquisition bits size:
-          Ex.: acq_size = 12 bits => conv_size = 16 bits */
-       int conv_size;
+       /* 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) */
@@ -290,15 +548,13 @@ int a4l_to_phys(a4l_chinfo_t * chan,
 
        /* Basic checking */
        if (rng == NULL || chan == NULL)
-               return 0;
+               return -EINVAL;
 
-       /* Computes the conversion width */
-       conv_size = (chan->nb_bits % 8 == 0) ? 
-               chan->nb_bits / 8 : (chan->nb_bits / 8) + 1;
+       /* Find out the size in memory */
+       size = a4l_sizeof_chan(chan);
 
-       /* This converting function only works 
-          if acquired data width is 8, 16 or 32 */
-       switch (conv_size) {
+       /* Select the suitable accessor */
+       switch (size) {
        case 4:
                datax_set = data32_set;
                break;
@@ -324,11 +580,10 @@ int a4l_to_phys(a4l_chinfo_t * chan,
                datax_set(dst + i, (lsampl_t) (a * src[j] - b));
 
                /* Updates the counters */
-               i += conv_size;
+               i += size;
                j++;
        }
 
        return j;
 }
-
 /** @} Range / conversion  API */
diff --git a/src/utils/analogy/insn_read.c b/src/utils/analogy/insn_read.c
index d5e025d..eec0fd3 100644
--- a/src/utils/analogy/insn_read.c
+++ b/src/utils/analogy/insn_read.c
@@ -22,6 +22,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 #include <sys/mman.h>
 #include <errno.h>
 #include <getopt.h>
@@ -35,7 +36,6 @@
 #define SCAN_CNT 10
 
 static unsigned char buf[BUF_SIZE];
-static double dbuf[BUF_SIZE];
 static char *filename = FILENAME;
 static int verbose;
 static int real_time;
@@ -54,6 +54,7 @@ struct option insn_read_opts[] = {
        {"scan-count", required_argument, NULL, 'S'},
        {"channel", required_argument, NULL, 'c'},
        {"range", required_argument, NULL, 'R'},
+       {"raw", no_argument, NULL, 'w'},
        {"help", no_argument, NULL, 'h'},
        {0},
 };
@@ -70,21 +71,140 @@ void do_print_usage(void)
        fprintf(stdout, "\t\t -S, --scan-count: count of scan to perform\n");
        fprintf(stdout, "\t\t -c, --channel: channel to use\n");
        fprintf(stdout, "\t\t -R, --range: range to use\n");
+       fprintf(stdout, "\t\t -w, --raw: dump data in raw format\n");
        fprintf(stdout, "\t\t -h, --help: print this help\n");
 }
 
+int dump_raw(a4l_desc_t *dsc, unsigned char *buf, int size)
+{
+       return fwrite(buf, size, 1, stdout);
+}
+
+int dump_text(a4l_desc_t *dsc, unsigned char *buf, int size)
+{
+       int err = 0, width, tmp_size = 0;
+       char *fmt;
+       a4l_chinfo_t *chan;     
+
+       /* Retrieve the subdevice data size */
+       err = a4l_get_chinfo(dsc, idx_subd, idx_chan, &chan);
+       if (err < 0) {
+               fprintf(stderr,
+                       "insn_read: info for channel %d "
+                       "on subdevice %d not available (err=%d)\n",
+                       idx_chan, idx_subd, err);
+               goto out;
+       }
+
+       width = a4l_sizeof_chan(chan);
+       if (width < 0) {
+               fprintf(stderr,
+                       "insn_read: incoherent info for channel %d\n",
+                       idx_chan);
+               err = width;
+               goto out;
+       }
+
+       switch(width) {
+       case 1:
+               fmt = "0x%02x\n";
+               break;
+       case 2:
+               fmt = "0x%04x\n";
+               break;
+       case 4:
+       default:
+               fmt = "0x%08x\n";
+               break;
+       }
+
+       while (size - tmp_size > 0) {
+               unsigned long values[64];
+               int i, tmp_cnt = ((size - tmp_size) / width > 64) ? 
+                       64 : ((size - tmp_size) / width);
+
+               err = a4l_rawtoul(chan, values, buf + tmp_size, tmp_cnt);
+               if (err < 0)
+                       goto out;
+
+               for (i = 0; i < tmp_cnt; i++) {
+                       fprintf(stdout, fmt, values[i]);
+               }
+               
+               tmp_size += tmp_cnt * width;
+       }
+
+out:
+       return err;
+}
+
+int dump_converted(a4l_desc_t *dsc, unsigned char *buf, int size)
+{
+       int err = 0, width, tmp_size = 0;
+       a4l_chinfo_t *chan;
+       a4l_rnginfo_t *rng;
+
+       /* Retrieve the channel info */
+       err = a4l_get_chinfo(dsc, idx_subd, idx_chan, &chan);
+       if (err < 0) {
+               fprintf(stderr,
+                       "insn_read: info for channel %d "
+                       "on subdevice %d not available (err=%d)\n",
+                       idx_chan, idx_subd, err);
+               goto out;
+       }
+
+       /* Retrieve the range info */
+       err = a4l_get_rnginfo(dsc, idx_subd, idx_chan, idx_rng, &rng);
+       if (err < 0) {
+               fprintf(stderr,
+                       "insn_read: failed to recover range descriptor\n");
+               goto out;
+       }
+
+       width = a4l_sizeof_chan(chan);
+       if (width < 0) {
+               fprintf(stderr,
+                       "insn_read: incoherent info for channel %d\n",
+                       idx_chan);
+               err = width;
+               goto out;
+       }
+
+       while (size - tmp_size > 0) {
+               double values[64];
+               int i, tmp_cnt = ((size - tmp_size) / width > 64) ? 
+                       64 : ((size - tmp_size) / width);
+
+               err = a4l_rawtod(chan, rng, values, buf + tmp_size, tmp_cnt);
+               if (err < 0)
+                       goto out;
+
+               for (i = 0; i < tmp_cnt; i++) {
+                       fprintf(stdout, "%F\n", values[i]);
+               }
+               
+               tmp_size += tmp_cnt * width;
+       }
+
+out:
+       return err;
+}
+
 int main(int argc, char *argv[])
 {
-       int ret = 0, i;
+       int ret = 0;
        unsigned int cnt = 0;
        a4l_desc_t dsc = { .sbdata = NULL };
        a4l_chinfo_t *chinfo;
        a4l_rnginfo_t *rnginfo;
 
+       int (*dump_function) (a4l_desc_t *, unsigned char *, int) = dump_text;
+
        /* Compute arguments */
        while ((ret = getopt_long(argc,
                                  argv,
-                                 "vrd:s:S:c:R:h", insn_read_opts,
+                                 "vrd:s:S:c:R:wh", insn_read_opts,
                                  NULL)) >= 0) {
                switch (ret) {
                case 'v':
@@ -107,6 +227,10 @@ int main(int argc, char *argv[])
                        break;
                case 'R':
                        idx_rng = strtoul(optarg, NULL, 0);
+                       dump_function = dump_converted;
+                       break;
+               case 'w':
+                       dump_function = dump_raw;
                        break;
                case 'h':
                default:
@@ -115,6 +239,12 @@ int main(int argc, char *argv[])
                }
        }
 
+       if (isatty(STDOUT_FILENO) && dump_function == dump_raw) {
+               fprintf(stderr, 
+                       "insn_read: cannot dump raw data on a terminal\n\n");
+               return -EINVAL;
+       }
+       
        if (real_time != 0) {
 
                if (verbose != 0)
@@ -186,9 +316,8 @@ int main(int argc, char *argv[])
 
        if (idx_rng >= 0) {
 
-               ret =
-                       a4l_get_rnginfo(&dsc, idx_subd, idx_chan, idx_rng,
-                                       &rnginfo);
+               ret = a4l_get_rnginfo(&dsc, 
+                                     idx_subd, idx_chan, idx_rng, &rnginfo);
                if (ret < 0) {
                        fprintf(stderr,
                                "insn_read: failed to recover range 
descriptor\n");
@@ -212,8 +341,7 @@ int main(int argc, char *argv[])
        }
 
        /* Set the data size to read */
-       scan_size *= (chinfo->nb_bits % 8 == 0) ? 
-               chinfo->nb_bits / 8 : (chinfo->nb_bits / 8) + 1;
+       scan_size *= a4l_sizeof_chan(chinfo);
 
        if (verbose != 0) {
                printf("insn_read: channel width is %u bits\n",
@@ -238,40 +366,16 @@ int main(int argc, char *argv[])
 
                /* Perform the synchronous read */
                ret = a4l_sync_read(&dsc,
-                                   idx_subd, 0, CHAN(idx_chan), buf, tmp);
+                                   idx_subd, CHAN(idx_chan), 0, buf, tmp);
 
                if (ret < 0)
                        goto out_insn_read;
 
-               /* If a range was selected, converts the samples */
-               if (idx_rng >= 0) {
-                       if (a4l_from_phys(chinfo, rnginfo, dbuf, buf, ret) < 0) 
{
-                               fprintf(stderr,
-                                       "insn_read: data conversion failed 
(ret=%d)\n",
-                                       ret);
-                               goto out_insn_read;
-                       }
-               }
-
-               /* Dump the results */
-               for (i = 0; i < ret; i++) {
-
-                       /* Print the output byte by byte */
-                       printf("0x%x ", buf[i]);
-
-                       /* Unlike a4l_async_read(), a4l_sync_read() cannot
-                          retrieve data which are not aligned with the channel
-                          width; so, it is easier to properly print data.
-                       */
-                       if ((i + 1) % (chinfo->nb_bits / 8) == 0) {
-
-                               /* If a range was selected, prints the 
converted value */
-                               if (idx_rng >= 0)
-                                       printf("\t-> %F",
-                                              dbuf[i / (chinfo->nb_bits / 8)]);
-
-                               printf("\n");
-                       }
+               /* Dump the read data */
+               tmp = dump_function(&dsc, buf, ret);
+               if (tmp < 0) {
+                       ret = tmp;
+                       goto out_insn_read;
                }
 
                /* Update the count */
diff --git a/src/utils/analogy/insn_write.c b/src/utils/analogy/insn_write.c
index 637a56a..2fcd7f4 100644
--- a/src/utils/analogy/insn_write.c
+++ b/src/utils/analogy/insn_write.c
@@ -246,7 +246,7 @@ int main(int argc, char *argv[])
 
        /* If a range was selected, converts the samples */
        if (idx_rng >= 0) {
-               if (a4l_to_phys(chinfo, rnginfo, &value, &dvalue, 1) < 0) {
+               if (a4l_dtoraw(chinfo, rnginfo, &value, &dvalue, 1) < 0) {
                        fprintf(stderr,
                                "insn_write: data conversion failed (ret=%d)\n",
                                ret);
@@ -254,11 +254,11 @@ int main(int argc, char *argv[])
                }
 
                if (verbose != 0)
-                       printf("insn_write: writing value %F (phys=0x%x)\n",
+                       printf("insn_write: writing value %F (raw=0x%x)\n",
                               dvalue, value);
 
        } else if (verbose != 0)
-               printf("insn_write: writing physical value 0x%x\n", value);
+               printf("insn_write: writing raw value 0x%x\n", value);
 
        /* Handle little endian case with bit range < 32 */
        if (scan_size == sizeof(char))
@@ -279,7 +279,7 @@ int main(int argc, char *argv[])
 
        /* Perform the write operation */
        ret = a4l_sync_write(&dsc, 
-                            idx_subd, 0, CHAN(idx_chan), &value, scan_size);
+                            idx_subd, CHAN(idx_chan), 0, &value, scan_size);
 
        if (ret < 0)
                goto out_insn_write;


_______________________________________________
Xenomai-git mailing list
Xenomai-git@gna.org
https://mail.gna.org/listinfo/xenomai-git

Reply via email to