]> sigrok.org Git - libsigrok.git/commitdiff
uni-t-dmm: Make driver more similar to serial-dmm.
authorUwe Hermann <redacted>
Wed, 19 Dec 2012 18:01:54 +0000 (19:01 +0100)
committerUwe Hermann <redacted>
Fri, 26 Apr 2013 18:05:44 +0000 (20:05 +0200)
hardware/uni-t-dmm/api.c
hardware/uni-t-dmm/protocol.c
hardware/uni-t-dmm/protocol.h

index d210304bb6e87c5301eb69321bcd163e5b9e82e4..cfb6ae6be8b06602b305db8e88ef87bb0a61e3ec 100644 (file)
@@ -40,14 +40,27 @@ static const int32_t hwcaps[] = {
 SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info;
 SR_PRIV struct sr_dev_driver voltcraft_vc820_driver_info;
 
-static struct sr_dev_driver *di_ut61d = &uni_t_ut61d_driver_info;
-static struct sr_dev_driver *di_vc820 = &voltcraft_vc820_driver_info;
-
-/* After hw_init() this will point to a device-specific entry (see above). */
-static struct sr_dev_driver *di = NULL;
+SR_PRIV struct dmm_info udmms[] = {
+       {
+               "UNI-T", "UT61D", 19230 /* TODO */,
+               FS9922_PACKET_SIZE, NULL,
+               sr_fs9922_packet_valid, sr_fs9922_parse,
+               NULL,
+               &uni_t_ut61d_driver_info, receive_data_UNI_T_UT61D,
+       },
+       {
+               "Voltcraft VC-820", "VC-820", 19200 /* TODO */,
+               FS9721_PACKET_SIZE, NULL,
+               sr_fs9721_packet_valid, sr_fs9721_parse,
+               NULL,
+               &voltcraft_vc820_driver_info, receive_data_VOLTCRAFT_VC820,
+       },
+};
 
-static int clear_instances(void)
+static int clear_instances(int dmm)
 {
+       (void)dmm;
+
        /* TODO: Use common code later. */
 
        return SR_OK;
@@ -55,26 +68,12 @@ static int clear_instances(void)
 
 static int hw_init(struct sr_context *sr_ctx, int dmm)
 {
-       if (dmm == UNI_T_UT61D)
-               di = di_ut61d;
-       else if (dmm == VOLTCRAFT_VC820)
-               di = di_vc820;
-       sr_dbg("Selected '%s' subdriver.", di->name);
+       sr_dbg("Selected '%s' subdriver.", udmms[dmm].di->name);
 
-       return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
-}
-
-static int hw_init_ut61d(struct sr_context *sr_ctx)
-{
-       return hw_init(sr_ctx, UNI_T_UT61D);
+       return std_hw_init(sr_ctx, uddms[dmm].di, DRIVER_LOG_DOMAIN);
 }
 
-static int hw_init_vc820(struct sr_context *sr_ctx)
-{
-       return hw_init(sr_ctx, VOLTCRAFT_VC820);
-}
-
-static GSList *hw_scan(GSList *options)
+static GSList *hw_scan(GSList *options, int dmm)
 {
        GSList *usb_devices, *devices, *l;
        struct sr_dev_inst *sdi;
@@ -87,10 +86,10 @@ static GSList *hw_scan(GSList *options)
 
        (void)options;
 
-       drvc = di->priv;
+       drvc = udmms[dmm].di->priv;
 
        /* USB scan is always authoritative. */
-       clear_instances();
+       clear_instances(dmm);
 
        conn = NULL;
        for (l = options; l; l = l->next) {
@@ -119,12 +118,12 @@ static GSList *hw_scan(GSList *options)
                }
 
                if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE,
-                               di->longname, NULL, NULL))) {
+                               udmms[dmm].vendor, udmms[dmm].device, NULL))) {
                        sr_err("sr_dev_inst_new returned NULL.");
                        return NULL;
                }
                sdi->priv = devc;
-               sdi->driver = di;
+               sdi->driver = udmms[dmm].di;
                if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
                        return NULL;
                sdi->probes = g_slist_append(sdi->probes, probe);
@@ -138,18 +137,18 @@ static GSList *hw_scan(GSList *options)
        return devices;
 }
 
-static GSList *hw_dev_list(void)
+static GSList *hw_dev_list(int dmm)
 {
-       return ((struct drv_context *)(di->priv))->instances;
+       return ((struct drv_context *)(udmms[dmm]->priv))->instances;
 }
 
-static int hw_dev_open(struct sr_dev_inst *sdi)
+static int hw_dev_open(struct sr_dev_inst *sdi, int dmm)
 {
        struct drv_context *drvc;
        struct dev_context *devc;
     int ret;
 
-       drvc = di->priv;
+       drvc = udmms[dmm].di->priv;
        devc = sdi->priv;
 
     if ((ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, devc->usb)) == SR_OK)
@@ -169,9 +168,9 @@ static int hw_dev_close(struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
-static int hw_cleanup(void)
+static int hw_cleanup(int dmm)
 {
-       clear_instances();
+       clear_instances(dmm);
 
        return SR_OK;
 }
@@ -231,7 +230,7 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
 }
 
 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
-                                   void *cb_data)
+                                   void *cb_data, int dmm)
 {
        struct dev_context *devc;
 
@@ -242,13 +241,8 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
        /* Send header packet to the session bus. */
        std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
 
-       if (!strcmp(di->name, "uni-t-ut61d")) {
-               sr_source_add(0, 0, 10 /* poll_timeout */,
-                             uni_t_ut61d_receive_data, (void *)sdi);
-       } else if (!strcmp(di->name, "voltcraft-vc820")) {
-               sr_source_add(0, 0, 10 /* poll_timeout */,
-                             voltcraft_vc820_receive_data, (void *)sdi);
-       }
+       sr_source_add(0, 0, 10 /* poll_timeout */,
+                     udmms[dmm].receive_data, (void *)sdi);
 
        return SR_OK;
 }
@@ -272,40 +266,50 @@ static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
        return SR_OK;
 }
 
-SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info = {
-       .name = "uni-t-ut61d",
-       .longname = "UNI-T UT61D",
-       .api_version = 1,
-       .init = hw_init_ut61d,
-       .cleanup = hw_cleanup,
-       .scan = hw_scan,
-       .dev_list = hw_dev_list,
-       .dev_clear = clear_instances,
-       .config_get = NULL,
-       .config_set = config_set,
-       .config_list = config_list,
-       .dev_open = hw_dev_open,
-       .dev_close = hw_dev_close,
-       .dev_acquisition_start = hw_dev_acquisition_start,
-       .dev_acquisition_stop = hw_dev_acquisition_stop,
-       .priv = NULL,
+/* Driver-specific API function wrappers */
+#define HW_INIT(X) \
+static int hw_init_##X(struct sr_context *sr_ctx) { return hw_init(sr_ctx, X); }
+#define HW_CLEANUP(X) \
+static int hw_cleanup_##X(void) { return hw_cleanup(X); }
+#define HW_SCAN(X) \
+static GSList *hw_scan_##X(GSList *options) { return hw_scan(options, X); }
+#define HW_DEV_LIST(X) \
+static GSList *hw_dev_list_##X(void) { return hw_dev_list(X); }
+#define CLEAR_INSTANCES(X) \
+static int clear_instances_##X(void) { return clear_instances(X); }
+#define HW_DEV_ACQUISITION_START(X) \
+static int hw_dev_acquisition_start_##X(const struct sr_dev_inst *sdi, \
+void *cb_data) { return hw_dev_acquisition_start(sdi, cb_data, X); }
+#define HW_DEV_OPEN(X) \
+static int hw_dev_open_##X(struct sr_dev_inst *sdi) { return hw_dev_open(sdi, X); }
+
+/* Driver structs and API function wrappers */
+#define DRV(ID, ID_UPPER, NAME, LONGNAME) \
+HW_INIT(ID_UPPER) \
+HW_CLEANUP(ID_UPPER) \
+HW_SCAN(ID_UPPER) \
+HW_DEV_LIST(ID_UPPER) \
+CLEAR_INSTANCES(ID_UPPER) \
+HW_DEV_ACQUISITION_START(ID_UPPER) \
+HW_DEV_OPEN(ID_UPPER) \
+SR_PRIV struct sr_dev_driver ID##_driver_info = { \
+       .name = NAME, \
+       .longname = LONGNAME, \
+       .api_version = 1, \
+       .init = hw_init_##ID_UPPER, \
+       .cleanup = hw_cleanup_##ID_UPPER, \
+       .scan = hw_scan_##ID_UPPER, \
+       .dev_list = hw_dev_list_##ID_UPPER, \
+       .dev_clear = clear_instances_##ID_UPPER, \
+       .config_get = NULL, \
+       .config_set = config_set, \
+       .config_list = config_list, \
+       .dev_open = hw_dev_open_##ID_UPPER, \
+       .dev_close = hw_dev_close, \
+       .dev_acquisition_start = hw_dev_acquisition_start_##ID_UPPER, \
+       .dev_acquisition_stop = hw_dev_acquisition_stop, \
+       .priv = NULL, \
 };
 
-SR_PRIV struct sr_dev_driver voltcraft_vc820_driver_info = {
-       .name = "voltcraft-vc820",
-       .longname = "Voltcraft VC-820",
-       .api_version = 1,
-       .init = hw_init_vc820,
-       .cleanup = hw_cleanup,
-       .scan = hw_scan,
-       .dev_list = hw_dev_list,
-       .dev_clear = clear_instances,
-       .config_get = NULL,
-       .config_set = config_set,
-       .config_list = config_list,
-       .dev_open = hw_dev_open,
-       .dev_close = hw_dev_close,
-       .dev_acquisition_start = hw_dev_acquisition_start,
-       .dev_acquisition_stop = hw_dev_acquisition_stop,
-       .priv = NULL,
-};
+DRV(uni_t_ut61d, UNI_T_UT61D, "uni-t-ut61d", "UNI-T UT61D")
+DRV(voltcraft_vc820, VOLTCRAFT_VC820, "voltcraft-vc820", "Voltcraft VC-820")
index 8b3cf79749df1e4c4bb2045427d1af92634541d0..2d644dadfd4a53c342a9af5d7a7ddeb8676bcdea 100644 (file)
  * Most UNI-T DMMs can be used with two (three) different PC interface cables:
  *  - The UT-D04 USB/HID cable, old version with Hoitek HE2325U chip.
  *  - The UT-D04 USB/HID cable, new version with WCH CH9325 chip.
- *  - The UT-D01 RS232 cable.
+ *  - The UT-D02 RS232 cable.
  *
- * This driver is meant to support all three cables, and various DMMs that
+ * This driver is meant to support all USB/HID cables, and various DMMs that
  * can be attached to a PC via these cables. Currently only the UT-D04 cable
- * (new version) is supported.
+ * (new version) is supported/tested.
+ * The UT-D02 RS232 cable is handled by the 'serial-dmm' driver.
  *
  * The data for one DMM packet (e.g. 14 bytes if the respective DMM uses a
  * Fortune Semiconductor FS9922-DMM4 chip) is spread across multiple
  *  - ...
  */
 
-static void decode_packet(struct sr_dev_inst *sdi, int dmm, const uint8_t *buf)
+static void decode_packet(struct sr_dev_inst *sdi, int dmm, const uint8_t *buf,
+                         void *info)
 {
        struct sr_datafeed_packet packet;
        struct sr_datafeed_analog analog;
-       struct dev_context *devc;
-       struct fs9721_info info;
+       /// struct fs9721_info info;
        float floatval;
        int ret;
 
@@ -79,16 +80,16 @@ static void decode_packet(struct sr_dev_inst *sdi, int dmm, const uint8_t *buf)
        memset(&analog, 0, sizeof(struct sr_datafeed_analog));
 
        /* Parse the protocol packet. */
-       ret = SR_ERR;
-       if (dmm == UNI_T_UT61D)
-               ret = sr_dmm_parse_fs9922(buf, &floatval, &analog);
-       else if (dmm == VOLTCRAFT_VC820)
-               ret = sr_fs9721_parse(buf, &floatval, &analog, &info);
+       ret = udmms[dmm].packet_parse(buf, &floatval, &analog, &info);
        if (ret != SR_OK) {
                sr_err("Invalid DMM packet, ignoring.");
                return;
        }
 
+       /* If this DMM needs additional handling, call the resp. function. */
+       if (udmms[dmm].dmm_details)
+               udmms[dmm].dmm_details(&analog, info);
+
        /* Send a sample packet with one analog value. */
        analog.probes = sdi->probes;
        analog.num_samples = 1;
@@ -181,7 +182,7 @@ static void log_dmm_packet(const uint8_t *buf)
               buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]);
 }
 
-static int uni_t_dmm_receive_data(int fd, int revents, int dmm, void *cb_data)
+static int receive_data(int fd, int revents, int dmm, void *info, void *cb_data)
 {
        struct sr_dev_inst *sdi;
        struct dev_context *devc;
@@ -201,8 +202,8 @@ static int uni_t_dmm_receive_data(int fd, int revents, int dmm, void *cb_data)
 
        /* On the first run, we need to init the HID chip. */
        if (first_run) {
-               /* TODO: The baudrate is DMM-specific (UT61D: 19230). */
-               if ((ret = hid_chip_init(devc, 19230)) != SR_OK) {
+               /* Note: The baudrate is DMM-specific (UT61D: 19230). */
+               if ((ret = hid_chip_init(devc, udmms[dmm].baudrate)) != SR_OK) {
                        sr_err("HID chip init failed: %d.", ret);
                        return FALSE;
                }
@@ -237,15 +238,8 @@ static int uni_t_dmm_receive_data(int fd, int revents, int dmm, void *cb_data)
        if (buf[0] != 0xf0) {
                /* First time: Synchronize to the start of a packet. */
                if (!synced_on_first_packet) {
-                       if (dmm == UNI_T_UT61D) {
-                               /* Valid packets start with '+' or '-'. */
-                               if ((buf[1] != '+') && buf[1] != '-')
-                                       return TRUE;
-                       } else if (dmm == VOLTCRAFT_VC820) {
-                               /* Valid packets have 0x1 as high nibble. */
-                               if (!sr_fs9721_is_packet_start(buf[1]))
-                                       return TRUE;
-                       }
+                       if (!udmms[dmm].packet_valid(buf))
+                               return TRUE;
                        synced_on_first_packet = TRUE;
                        sr_spew("Successfully synchronized on first packet.");
                }
@@ -258,13 +252,14 @@ static int uni_t_dmm_receive_data(int fd, int revents, int dmm, void *cb_data)
                if (data_byte_counter == NUM_DATA_BYTES) {
                        log_dmm_packet(pbuf);
                        data_byte_counter = 0;
-                       if (dmm == VOLTCRAFT_VC820) {
-                               if (!sr_fs9721_packet_valid(pbuf)) {
-                                       sr_err("Invalid packet.");
-                                       return TRUE;
-                               }
+
+                       if (!udmms[dmm].packet_valid(pbuf)) {
+                               sr_err("Invalid packet.");
+                               return TRUE;
                        }
-                       decode_packet(sdi, dmm, pbuf);
+
+                       decode_packet(sdi, dmm, pbuf, info);
+
                        memset(pbuf, 0x00, NUM_DATA_BYTES);
                }
        }
@@ -278,12 +273,11 @@ static int uni_t_dmm_receive_data(int fd, int revents, int dmm, void *cb_data)
        return TRUE;
 }
 
-SR_PRIV int uni_t_ut61d_receive_data(int fd, int revents, void *cb_data)
-{
-       return uni_t_dmm_receive_data(fd, revents, UNI_T_UT61D, cb_data);
-}
+#define RECEIVE_DATA(ID_UPPER, DMM_DRIVER) \
+SR_PRIV int receive_data_##ID_UPPER(int fd, int revents, void *cb_data) { \
+       struct DMM_DRIVER##_info info; \
+       return receive_data(fd, revents, ID_UPPER, &info, cb_data); }
 
-SR_PRIV int voltcraft_vc820_receive_data(int fd, int revents, void *cb_data)
-{
-       return uni_t_dmm_receive_data(fd, revents, VOLTCRAFT_VC820, cb_data);
-}
+/* Driver-specific receive_data() wrappers */
+RECEIVE_DATA(UNI_T_UT61D, fs9922)
+RECEIVE_DATA(VOLTCRAFT_VC820, fs9721)
index 389df3fa5af80621acfbf7f4cadc92759101306e..73fa75825825498e37ddfe50a604c143109e8256 100644 (file)
 #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
 #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
 
+/* Note: When adding entries here, don't forget to update DMM_COUNT. */
 enum {
        UNI_T_UT61D,
        VOLTCRAFT_VC820,
 };
 
+#define DMM_COUNT 2
+
+struct dmm_info {
+       char *vendor;
+       char *device;
+       uint32_t baudrate;
+       int packet_size;
+       int (*packet_request)(struct sr_serial_dev_inst *);
+       gboolean (*packet_valid)(const uint8_t *);
+       int (*packet_parse)(const uint8_t *, float *,
+                           struct sr_datafeed_analog *, void *);
+       void (*dmm_details)(struct sr_datafeed_analog *, void *);
+       struct sr_dev_driver *di;
+       int (*receive_data)(int, int, void *);
+};
+
+extern SR_PRIV struct dmm_info udmms[DMM_COUNT];
+
 #define UT_D04_CABLE_USB_VID   0x1a86
 #define UT_D04_CABLE_USB_DID   0xe008
 
@@ -66,7 +85,7 @@ struct dev_context {
        uint8_t protocol_buf[14];
 };
 
-SR_PRIV int uni_t_ut61d_receive_data(int fd, int revents, void *cb_data);
-SR_PRIV int voltcraft_vc820_receive_data(int fd, int revents, void *cb_data);
+SR_PRIV int receive_data_UNI_T_UT61D(int fd, int revents, void *cb_data);
+SR_PRIV int receive_data_VOLTCRAFT_VC820(int fd, int revents, void *cb_data);
 
 #endif