]> sigrok.org Git - libsigrok.git/commitdiff
hantek-4032l: Add initial driver implementation.
authorAndreas Zschunke <redacted>
Sat, 5 Mar 2016 20:55:02 +0000 (21:55 +0100)
committerUwe Hermann <redacted>
Fri, 5 Jan 2018 19:28:14 +0000 (20:28 +0100)
[Note: This patch is basically a squashed version of the initial driver
commits by Andreas Zschunke <redacted>, two fixes by
Andrej Valek <redacted>, and various coding style / cosmetic
fixes by Uwe Hermann <redacted> to make the driver a lot more
consistent with the rest of the libsigrok code-base.]

Makefile.am
configure.ac
src/hardware/hantek-4032l/api.c
src/hardware/hantek-4032l/protocol.c
src/hardware/hantek-4032l/protocol.h

index 4303e0c9065a35b6b694a9bcf13091be047e6766..2d153af9e03112a763683a85ee507b75be7a04fa 100644 (file)
@@ -324,18 +324,18 @@ src_libdrivers_la_SOURCES += \
        src/hardware/hameg-hmo/protocol.c \
        src/hardware/hameg-hmo/api.c
 endif
-if HW_HANTEK_6XXX
-src_libdrivers_la_SOURCES += \
-       src/hardware/hantek-6xxx/protocol.h \
-       src/hardware/hantek-6xxx/protocol.c \
-       src/hardware/hantek-6xxx/api.c
-endif
 if HW_HANTEK_4032L
 src_libdrivers_la_SOURCES += \
        src/hardware/hantek-4032l/protocol.h \
        src/hardware/hantek-4032l/protocol.c \
        src/hardware/hantek-4032l/api.c
 endif
+if HW_HANTEK_6XXX
+src_libdrivers_la_SOURCES += \
+       src/hardware/hantek-6xxx/protocol.h \
+       src/hardware/hantek-6xxx/protocol.c \
+       src/hardware/hantek-6xxx/api.c
+endif
 if HW_HANTEK_DSO
 src_libdrivers_la_SOURCES += \
        src/hardware/hantek-dso/protocol.h \
index d18a19ab96599c7a9c9998009d2d811fced56a52..2e148b7ef56484a99e45c81db47ba29a3d054626 100644 (file)
@@ -240,7 +240,7 @@ SR_DRIVER([fx2lafw], [fx2lafw], [libusb])
 SR_DRIVER([GMC MH 1x/2x], [gmc-mh-1x-2x], [libserialport])
 SR_DRIVER([GW Instek GDS-800], [gwinstek-gds-800], [libserialport])
 SR_DRIVER([Hameg HMO], [hameg-hmo], [libserialport])
-SR_DRIVER([Hantek 4032L], [hantek-4032l])
+SR_DRIVER([Hantek 4032L], [hantek-4032l], [libusb])
 SR_DRIVER([Hantek 6xxx], [hantek-6xxx], [libusb])
 SR_DRIVER([Hantek DSO], [hantek-dso], [libusb])
 SR_DRIVER([HP 3457A], [hp-3457a])
index b6e47ce1f736ba0525c622844ae75ae88326b309..9576fabccb068ad9fcc6ac3d5b6951e2769fabe7 100644 (file)
@@ -1,7 +1,9 @@
 /*
  * This file is part of the libsigrok project.
  *
- * Copyright (C) 2016 Andreas Zschunke <andreasz77@gmx.net>
+ * Copyright (C) 2016 Andreas Zschunke <andreas.zschunke@gmx.net>
+ * Copyright (C) 2017 Andrej Valek <andy@skyrain.eu>
+ * Copyright (C) 2017 Uwe Hermann <uwe@hermann-uwe.de>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #include <config.h>
 #include "protocol.h"
 
-SR_PRIV struct sr_dev_driver hantek_4032l_driver_info;
+#define USB_INTERFACE 0
+#define NUM_CHANNELS 32
 
-static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
-{
-       return std_init(sr_ctx, di, LOG_PREFIX);
-}
+static const uint32_t scanopts[] = {
+       SR_CONF_CONN,
+};
+
+static const uint32_t drvopts[] = {
+       SR_CONF_LOGIC_ANALYZER,
+};
+
+static const uint32_t devopts[] = {
+       SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
+       SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
+       SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
+       SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
+       SR_CONF_CONN | SR_CONF_GET,
+       SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_SET | SR_CONF_LIST,
+};
+
+static const int32_t trigger_matches[] = {
+       SR_TRIGGER_ZERO,
+       SR_TRIGGER_ONE,
+       SR_TRIGGER_RISING,
+       SR_TRIGGER_FALLING,
+       SR_TRIGGER_EDGE,
+};
+
+static const uint64_t samplerates[] = {
+       SR_KHZ(1),
+       SR_KHZ(2),
+       SR_KHZ(4),
+       SR_KHZ(8),
+       SR_KHZ(16),
+       SR_HZ(31250),
+       SR_HZ(62500),
+       SR_KHZ(125),
+       SR_KHZ(250),
+       SR_KHZ(500),
+       SR_KHZ(625),
+       SR_HZ(781250),
+       SR_MHZ(1),
+       SR_KHZ(1250),
+       SR_HZ(1562500),
+       SR_MHZ(2),
+       SR_KHZ(2500),
+       SR_KHZ(3125),
+       SR_MHZ(4),
+       SR_MHZ(5),
+       SR_KHZ(6250),
+       SR_MHZ(10),
+       SR_KHZ(12500),
+       SR_MHZ(20),
+       SR_MHZ(25),
+       SR_MHZ(40),
+       SR_MHZ(50),
+       SR_MHZ(80),
+       SR_MHZ(100),
+       SR_MHZ(160),
+       SR_MHZ(200),
+       SR_MHZ(320),
+       SR_MHZ(400),
+};
+
+static const uint64_t samplerates_hw[] = {
+       SR_MHZ(100),
+       SR_MHZ(50),
+       SR_MHZ(25),
+       SR_KHZ(12500),
+       SR_KHZ(6250),
+       SR_KHZ(3125),
+       SR_HZ(1562500),
+       SR_HZ(781250),
+       SR_MHZ(80),
+       SR_MHZ(40),
+       SR_MHZ(20),
+       SR_MHZ(10),
+       SR_MHZ(5),
+       SR_KHZ(2500),
+       SR_KHZ(1250),
+       SR_KHZ(625),
+       SR_MHZ(4),
+       SR_MHZ(2),
+       SR_MHZ(1),
+       SR_KHZ(500),
+       SR_KHZ(250),
+       SR_KHZ(125),
+       SR_HZ(62500),
+       SR_HZ(31250),
+       SR_KHZ(16),
+       SR_KHZ(8),
+       SR_KHZ(4),
+       SR_KHZ(2),
+       SR_KHZ(1),
+       0,
+       0,
+       0,
+       SR_MHZ(200),
+       SR_MHZ(160),
+       SR_MHZ(400),
+       SR_MHZ(320),
+};
+
+SR_PRIV struct sr_dev_driver hantek_4032l_driver_info;
 
 static GSList *scan(struct sr_dev_driver *di, GSList *options)
 {
-       struct drv_context *drvc;
-       GSList *devices;
-
-       (void)options;
+       struct drv_context *drvc = di->context;
+       GSList *l, *devices, *conn_devices;
+       libusb_device **devlist;
+       struct libusb_device_descriptor des;
+       const char *conn;
+       int i;
+       char connection_id[64];
+       struct sr_channel_group *cg;
+       struct sr_dev_inst *sdi;
+       struct sr_channel *ch;
 
        devices = NULL;
-       drvc = di->context;
+       conn_devices = NULL;
        drvc->instances = NULL;
+       conn = NULL;
+
+       for (l = options; l; l = l->next) {
+               struct sr_config *src = l->data;
+               if (src->key == SR_CONF_CONN) {
+                       conn = g_variant_get_string(src->data, NULL);
+                       break;
+               }
+       }
 
-       /* TODO: scan for devices, either based on a SR_CONF_CONN option
-        * or on a USB scan. */
-
-       return devices;
-}
+       if (conn)
+               conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
+       else
+               conn_devices = NULL;
+
+       libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
+       for (i = 0; devlist[i]; i++) {
+               if (conn) {
+                       struct sr_usb_dev_inst *usb = NULL;
+                       for (l = conn_devices; l; l = l->next) {
+                               usb = l->data;
+                               if (usb->bus == libusb_get_bus_number(devlist[i])
+                                       && usb->address == libusb_get_device_address(devlist[i]))
+                                       break;
+                       }
+                       if (!l)
+                               /* This device matched none of the ones that
+                                * matched the conn specification. */
+                               continue;
+               }
+
+               libusb_get_device_descriptor(devlist[i], &des);
+
+               if (des.idVendor != H4032L_USB_VENDOR ||
+                   des.idProduct != H4032L_USB_PRODUCT)
+                       continue;
+
+               usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
+
+               sdi = g_malloc0(sizeof(struct sr_dev_inst));
+               sdi->driver = &hantek_4032l_driver_info;
+               sdi->vendor = g_strdup("Hantek");
+               sdi->model = g_strdup("4032L");
+               sdi->connection_id = g_strdup(connection_id);
+
+               struct sr_channel_group *channel_groups[2];
+               for (int j = 0; j < 2; j++) {
+                       cg = g_malloc0(sizeof(struct sr_channel_group));
+                       cg->name = g_strdup_printf("%c", 'A' + j);
+                       channel_groups[j] = cg;
+                       sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
+               }
+
+               /* Assemble channel list and add channel to channel groups. */
+               for (int j = 0; j < NUM_CHANNELS; j++) {
+                       char channel_name[4];
+                       sprintf(channel_name, "%c%d", 'A' + (j & 1), j / 2);
+                       ch = sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE, channel_name);
+                       cg = channel_groups[j & 1];
+                       cg->channels = g_slist_append(cg->channels, ch);
+               }
+
+               struct dev_context *devc = g_malloc0(sizeof(struct dev_context));
+
+               /* Initialize command packet. */
+               devc->cmd_pkt.magic = H4032L_CMD_PKT_MAGIC;
+               devc->cmd_pkt.pwm_a = h4032l_voltage2pwm(2.5);
+               devc->cmd_pkt.pwm_b = h4032l_voltage2pwm(2.5);
+               devc->cmd_pkt.sample_size = 16384;
+               devc->cmd_pkt.pre_trigger_size = 1024;
+
+               devc->status = H4032L_STATUS_IDLE;
+
+               devc->capture_ratio = 5;
+
+               devc->usb_transfer = libusb_alloc_transfer(0);
+
+               sdi->priv = devc;
+               devices = g_slist_append(devices, sdi);
+
+               sdi->status = SR_ST_INACTIVE;
+               sdi->inst_type = SR_INST_USB;
+               sdi->conn = sr_usb_dev_inst_new(
+                       libusb_get_bus_number(devlist[i]),
+                       libusb_get_device_address(devlist[i]), NULL);
+       }
 
-static GSList *dev_list(const struct sr_dev_driver *di)
-{
-       return ((struct drv_context *)(di->context))->instances;
-}
+       g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
+       libusb_free_device_list(devlist, 1);
 
-static int dev_clear(const struct sr_dev_driver *di)
-{
-       return std_dev_clear(di, NULL);
+       return std_scan_complete(di, devices);
 }
 
 static int dev_open(struct sr_dev_inst *sdi)
 {
-       (void)sdi;
+       struct sr_usb_dev_inst *usb = sdi->conn;
+       int ret;
 
-       /* TODO: get handle from sdi->conn and open it. */
+       ret = h4032l_dev_open(sdi);
+       if (ret != SR_OK) {
+               sr_err("Unable to open device.");
+               return SR_ERR;
+       }
 
-       sdi->status = SR_ST_ACTIVE;
+       ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
+       if (ret != 0) {
+               switch (ret) {
+               case LIBUSB_ERROR_BUSY:
+                       sr_err("Unable to claim USB interface. Another "
+                              "program or driver has already claimed it.");
+                       break;
+               case LIBUSB_ERROR_NO_DEVICE:
+                       sr_err("Device has been disconnected.");
+                       break;
+               default:
+                       sr_err("Unable to claim interface: %s.",
+                              libusb_error_name(ret));
+                       break;
+               }
+
+               return SR_ERR;
+       }
 
        return SR_OK;
 }
 
 static int dev_close(struct sr_dev_inst *sdi)
 {
-       (void)sdi;
+       struct sr_usb_dev_inst *usb;
 
-       /* TODO: get handle from sdi->conn and close it. */
+       usb = sdi->conn;
 
-       sdi->status = SR_ST_INACTIVE;
+       if (!usb->devhdl)
+               return SR_ERR_BUG;
 
-       return SR_OK;
-}
-
-static int cleanup(const struct sr_dev_driver *di)
-{
-       dev_clear(di);
-
-       /* TODO: free other driver resources, if any. */
+       sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
+               usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
+       libusb_release_interface(usb->devhdl, USB_INTERFACE);
+       libusb_close(usb->devhdl);
+       usb->devhdl = NULL;
 
        return SR_OK;
 }
@@ -88,82 +290,217 @@ static int cleanup(const struct sr_dev_driver *di)
 static int config_get(uint32_t key, GVariant **data,
        const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
-       int ret;
+       struct dev_context *devc = sdi->priv;
+       struct sr_usb_dev_inst *usb;
 
-       (void)sdi;
-       (void)data;
        (void)cg;
 
-       ret = SR_OK;
        switch (key) {
-       /* TODO */
+       case SR_CONF_SAMPLERATE:
+               *data = g_variant_new_uint64(samplerates_hw[devc->cmd_pkt.sample_rate]);
+               break;
+       case SR_CONF_CAPTURE_RATIO:
+               *data = g_variant_new_uint64(devc->capture_ratio);
+               break;
+       case SR_CONF_LIMIT_SAMPLES:
+               *data = g_variant_new_uint64(devc->cmd_pkt.sample_size);
+               break;
+       case SR_CONF_CONN:
+               if (!sdi || !(usb = sdi->conn))
+                       return SR_ERR_ARG;
+               *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
+               break;
        default:
                return SR_ERR_NA;
        }
 
-       return ret;
+       return SR_OK;
 }
 
 static int config_set(uint32_t key, GVariant *data,
        const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
-       int ret;
+       struct dev_context *devc = sdi->priv;
+       struct h4032l_cmd_pkt *cmd_pkt = &devc->cmd_pkt;
 
-       (void)data;
        (void)cg;
 
-       if (sdi->status != SR_ST_ACTIVE)
-               return SR_ERR_DEV_CLOSED;
-
-       ret = SR_OK;
        switch (key) {
-       /* TODO */
-       default:
-               ret = SR_ERR_NA;
+       case SR_CONF_SAMPLERATE: {
+                       uint64_t sample_rate = g_variant_get_uint64(data);
+                       uint8_t i = 0;
+                       while (i < ARRAY_SIZE(samplerates_hw) && samplerates_hw[i] != sample_rate)
+                               i++;
+
+                       if (i == ARRAY_SIZE(samplerates_hw) || sample_rate == 0) {
+                               sr_err("invalid sample rate");
+                               return SR_ERR_SAMPLERATE;
+                       }
+                       cmd_pkt->sample_rate = i;
+
+                       return SR_OK;
+               }
+       case SR_CONF_CAPTURE_RATIO:
+               devc->capture_ratio = g_variant_get_uint64(data);
+               return SR_OK;
+       case SR_CONF_LIMIT_SAMPLES: {
+                       uint64_t number_samples = g_variant_get_uint64(data);
+                       number_samples += 511;
+                       number_samples &= 0xfffffe00;
+                       if (number_samples < 2048
+                           || number_samples > 64 * 1024 * 1024) {
+                               sr_err("invalid sample range 2k...64M: %ld",
+                                      number_samples);
+                               return SR_ERR;
+                       }
+                       cmd_pkt->sample_size = number_samples;
+                       return SR_OK;
+               }
+       case SR_CONF_VOLTAGE_THRESHOLD: {
+                       double d1, d2;
+                       g_variant_get(data, "(dd)", &d1, &d2);
+                       devc->cmd_pkt.pwm_a = h4032l_voltage2pwm(d1);
+                       devc->cmd_pkt.pwm_b = h4032l_voltage2pwm(d2);
+                       return SR_OK;
+               }
        }
 
-       return ret;
+       return SR_ERR_NA;
 }
 
 static int config_list(uint32_t key, GVariant **data,
        const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
-       int ret;
-
-       (void)sdi;
-       (void)data;
-       (void)cg;
-
-       ret = SR_OK;
        switch (key) {
-       /* TODO */
+       case SR_CONF_SCAN_OPTIONS:
+       case SR_CONF_DEVICE_OPTIONS:
+               return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
+       case SR_CONF_SAMPLERATE:
+               *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates));
+               break;
+       case SR_CONF_TRIGGER_MATCH:
+               *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
+               break;
+       case SR_CONF_VOLTAGE_THRESHOLD:
+               *data = std_gvar_tuple_double(2.5, 2.5);
+               break;
        default:
                return SR_ERR_NA;
        }
 
-       return ret;
+       return SR_OK;
 }
 
-static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
+static int dev_acquisition_start(const struct sr_dev_inst *sdi)
 {
-       (void)sdi;
-       (void)cb_data;
-
-       if (sdi->status != SR_ST_ACTIVE)
-               return SR_ERR_DEV_CLOSED;
+       struct sr_dev_driver *di = sdi->driver;
+       struct drv_context *drvc = di->context;
+       struct dev_context *devc = sdi->priv;
+       struct sr_trigger *trigger = sr_session_trigger_get(sdi->session);
+       struct h4032l_cmd_pkt *cmd_pkt = &devc->cmd_pkt;
+
+       /* Calculate packet ratio. */
+       cmd_pkt->pre_trigger_size = (cmd_pkt->sample_size * devc->capture_ratio) / 100;
+
+       cmd_pkt->trig_flags.enable_trigger1 = 0;
+       cmd_pkt->trig_flags.enable_trigger2 = 0;
+       cmd_pkt->trig_flags.trigger_and_logic = 0;
+
+       if (trigger && trigger->stages) {
+               GSList *stages = trigger->stages;
+               struct sr_trigger_stage *stage1 = stages->data;
+               if (stages->next) {
+                       sr_err("only one trigger stage supported for now");
+                       return SR_ERR;
+               }
+               cmd_pkt->trig_flags.enable_trigger1 = 1;
+               cmd_pkt->trigger[0].flags.edge_type = H4032L_TRIGGER_EDGE_TYPE_DISABLED;
+               cmd_pkt->trigger[0].flags.data_range_enabled = 0;
+               cmd_pkt->trigger[0].flags.time_range_enabled = 0;
+               cmd_pkt->trigger[0].flags.combined_enabled = 0;
+               cmd_pkt->trigger[0].flags.data_range_type = H4032L_TRIGGER_DATA_RANGE_TYPE_MAX;
+               cmd_pkt->trigger[0].data_range_mask = 0;
+               cmd_pkt->trigger[0].data_range_max = 0;
+
+               /* Initialize range mask values. */
+               uint32_t range_mask = 0;
+               uint32_t range_value = 0;
+
+               GSList *channel = stage1->matches;
+               while (channel) {
+                       struct sr_trigger_match *match = channel->data;
+
+                       switch (match->match) {
+                       case SR_TRIGGER_ZERO:
+                               range_mask |= (1 << match->channel->index);
+                               break;
+                       case SR_TRIGGER_ONE:
+                               range_mask |= (1 << match->channel->index);
+                               range_value |= (1 << match->channel->index);
+                               break;
+                       case SR_TRIGGER_RISING:
+                               if (cmd_pkt->trigger[0].flags.edge_type != H4032L_TRIGGER_EDGE_TYPE_DISABLED) {
+                                       sr_err("only one trigger signal with fall/rising/edge allowed");
+                                       return SR_ERR;
+                               }
+                               cmd_pkt->trigger[0].flags.edge_type = H4032L_TRIGGER_EDGE_TYPE_RISE;
+                               cmd_pkt->trigger[0].flags.edge_signal = match->channel->index;
+                               break;
+                       case SR_TRIGGER_FALLING:
+                               if (cmd_pkt->trigger[0].flags.edge_type != H4032L_TRIGGER_EDGE_TYPE_DISABLED) {
+                                       sr_err("only one trigger signal with fall/rising/edge allowed");
+                                       return SR_ERR;
+                               }
+                               cmd_pkt->trigger[0].flags.edge_type = H4032L_TRIGGER_EDGE_TYPE_FALL;
+                               cmd_pkt->trigger[0].flags.edge_signal = match->channel->index;
+                               break;
+                       case SR_TRIGGER_EDGE:
+                               if (cmd_pkt->trigger[0].flags.edge_type != H4032L_TRIGGER_EDGE_TYPE_DISABLED) {
+                                       sr_err("only one trigger signal with fall/rising/edge allowed");
+                                       return SR_ERR;
+                               }
+                               cmd_pkt->trigger[0].flags.edge_type = H4032L_TRIGGER_EDGE_TYPE_TOGGLE;
+                               cmd_pkt->trigger[0].flags.edge_signal = match->channel->index;
+                               break;
+                       default:
+                               sr_err("unknown trigger value");
+                               return SR_ERR;
+                       }
+
+                       channel = channel->next;
+               }
+
+               /* Compress range mask value and apply range settings. */
+               if (range_mask) {
+                       cmd_pkt->trigger[0].flags.data_range_enabled = 1;
+                       cmd_pkt->trigger[0].data_range_mask |= (range_mask);
+
+                       uint32_t new_range_value = 0;
+                       uint32_t bit_mask = 1;
+                       while (range_mask) {
+                               if ((range_mask & 1) != 0) {
+                                       new_range_value <<= 1;
+                                       if ((range_value & 1) != 0)
+                                               new_range_value |= bit_mask;
+                                       bit_mask <<= 1;
+                               }
+                               range_mask >>= 1;
+                               range_value >>= 1;
+                       }
+                       cmd_pkt->trigger[0].data_range_max |= range_value;
+               }
+       }
 
-       /* TODO: configure hardware, reset acquisition state, set up
-        * callbacks and send header packet. */
+       usb_source_add(sdi->session, drvc->sr_ctx, 10000,
+               h4032l_receive_data, sdi->driver->context);
 
-       return SR_OK;
+       /* Start capturing. */
+       return h4032l_start(sdi);
 }
 
-static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
+static int dev_acquisition_stop(struct sr_dev_inst *sdi)
 {
-       (void)cb_data;
-
-       if (sdi->status != SR_ST_ACTIVE)
-               return SR_ERR_DEV_CLOSED;
+       (void)sdi;
 
        /* TODO: stop acquisition. */
 
@@ -172,13 +509,13 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
 
 SR_PRIV struct sr_dev_driver hantek_4032l_driver_info = {
        .name = "hantek-4032l",
-       .longname = "Hantek 4032l",
+       .longname = "Hantek 4032L",
        .api_version = 1,
-       .init = init,
-       .cleanup = cleanup,
+       .init = std_init,
+       .cleanup = std_cleanup,
        .scan = scan,
-       .dev_list = dev_list,
-       .dev_clear = dev_clear,
+       .dev_list = std_dev_list,
+       .dev_clear = std_dev_clear,
        .config_get = config_get,
        .config_set = config_set,
        .config_list = config_list,
@@ -188,3 +525,4 @@ SR_PRIV struct sr_dev_driver hantek_4032l_driver_info = {
        .dev_acquisition_stop = dev_acquisition_stop,
        .context = NULL,
 };
+SR_REGISTER_DEV_DRIVER(hantek_4032l_driver_info);
index aa54be839b6959c415deaab98e054cd52888c971..509a196e4b5f67bd550346aaab5d6d7622f89bd6 100644 (file)
@@ -1,7 +1,9 @@
 /*
  * This file is part of the libsigrok project.
  *
- * Copyright (C) 2016 Andreas Zschunke <andreasz77@gmx.net>
+ * Copyright (C) 2016 Andreas Zschunke <andreas.zschunke@gmx.net>
+ * Copyright (C) 2017 Andrej Valek <andy@skyrain.eu>
+ * Copyright (C) 2017 Uwe Hermann <uwe@hermann-uwe.de>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #include <config.h>
 #include "protocol.h"
 
-SR_PRIV int hantek_4032l_receive_data(int fd, int revents, void *cb_data)
+#define H4032L_USB_TIMEOUT 500
+
+enum h4032l_cmd {
+       CMD_CONFIGURE = 0x2b1a, /* Also arms the logic analyzer. */
+       CMD_STATUS = 0x4b3a,
+       CMD_GET = 0x6b5a
+};
+
+struct __attribute__((__packed__)) h4032l_status_packet {
+       uint32_t magic;
+       uint32_t values;
+       uint32_t status;
+};
+
+SR_PRIV int h4032l_receive_data(int fd, int revents, void *cb_data)
 {
-       const struct sr_dev_inst *sdi;
-       struct dev_context *devc;
+       struct timeval tv;
+       struct drv_context *drvc;
 
        (void)fd;
+       (void)revents;
+
+       drvc = (struct drv_context *)cb_data;
+
+       tv.tv_sec = tv.tv_usec = 0;
+       libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
 
-       if (!(sdi = cb_data))
-               return TRUE;
+       return TRUE;
+}
 
-       if (!(devc = sdi->priv))
-               return TRUE;
+void LIBUSB_CALL h4032l_usb_callback(struct libusb_transfer *transfer)
+{
+       const struct sr_dev_inst *sdi = transfer->user_data;
+       struct dev_context *devc = sdi->priv;
+       struct drv_context *drvc = sdi->driver->context;
+       struct sr_usb_dev_inst *usb = sdi->conn;
+       gboolean cmd = FALSE;
+       uint32_t max_samples = 512 / sizeof(uint32_t);
+       uint32_t *buffer;
+       struct h4032l_status_packet *status;
+       struct sr_datafeed_packet packet;
+       struct sr_datafeed_logic logic;
+       uint32_t number_samples;
+       int ret;
 
-       if (revents == G_IO_IN) {
-               /* TODO */
+       if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
+               sr_err("%s error: %d.", __func__, transfer->status);
+               return;
        }
 
-       return TRUE;
+       buffer = (uint32_t *)transfer->buffer;
+
+       switch (devc->status) {
+       case H4032L_STATUS_IDLE:
+               sr_err("USB callback called in idle.");
+               break;
+       case H4032L_STATUS_CMD_CONFIGURE:
+               /* Select status request as next. */
+               cmd = TRUE;
+               devc->cmd_pkt.cmd = CMD_STATUS;
+               devc->status = H4032L_STATUS_CMD_STATUS;
+               break;
+       case H4032L_STATUS_CMD_STATUS:
+               /* Select status request as next. */
+               devc->status = H4032L_STATUS_RESPONSE_STATUS;
+               break;
+       case H4032L_STATUS_RESPONSE_STATUS:
+               /*
+                * Check magic and if status is complete, then select
+                * First Transfer as next.
+                */
+               status = (struct h4032l_status_packet *)transfer->buffer;
+               if (status->magic != H4032L_STATUS_PACKET_MAGIC) {
+                       devc->status = H4032L_STATUS_CMD_STATUS;
+                       devc->cmd_pkt.cmd = CMD_STATUS;
+                       cmd = TRUE;
+               } else if (status->status == 2) {
+                       devc->status = H4032L_STATUS_RESPONSE_STATUS_CONTINUE;
+               } else {
+                       devc->status = H4032L_STATUS_RESPONSE_STATUS_RETRY;
+               }
+               break;
+       case H4032L_STATUS_RESPONSE_STATUS_RETRY:
+               devc->status = H4032L_STATUS_CMD_STATUS;
+               devc->cmd_pkt.cmd = CMD_STATUS;
+               cmd = TRUE;
+               break;
+       case H4032L_STATUS_RESPONSE_STATUS_CONTINUE:
+               devc->status = H4032L_STATUS_CMD_GET;
+               devc->cmd_pkt.cmd = CMD_GET;
+               cmd = TRUE;
+               break;
+       case H4032L_STATUS_CMD_GET:
+               devc->status = H4032L_STATUS_FIRST_TRANSFER;
+               break;
+       case H4032L_STATUS_FIRST_TRANSFER:
+               if (buffer[0] != H4032L_START_PACKET_MAGIC) {
+                       sr_err("Mismatch magic number of start poll.");
+                       devc->status = H4032L_STATUS_IDLE;
+                       break;
+               }
+               devc->status = H4032L_STATUS_TRANSFER;
+               max_samples--;
+               buffer++;
+               break;
+       case H4032L_STATUS_TRANSFER:
+               number_samples = (devc->remaining_samples < max_samples) ? devc->remaining_samples : max_samples;
+               devc->remaining_samples -= number_samples;
+               packet.type = SR_DF_LOGIC;
+               packet.payload = &logic;
+               logic.length = number_samples * sizeof(uint32_t);
+               logic.unitsize = sizeof(uint32_t);
+               logic.data = buffer;
+               sr_session_send(sdi, &packet);
+               sr_dbg("Remaining: %d %08X %08X.", devc->remaining_samples,
+                       buffer[0], buffer[1]);
+               if (devc->remaining_samples == 0) {
+                       std_session_send_df_end(sdi);
+                       usb_source_remove(sdi->session, drvc->sr_ctx);
+                       devc->status = H4032L_STATUS_IDLE;
+                       if (buffer[number_samples] != H4032L_END_PACKET_MAGIC)
+                               sr_err("Mismatch magic number of end poll.");
+               }
+               break;
+       }
+
+       if (devc->status != H4032L_STATUS_IDLE) {
+               if (cmd) {
+                       /* Setup new USB cmd packet, reuse transfer object. */
+                       sr_dbg("New command: %d.", devc->status);
+                       libusb_fill_bulk_transfer(transfer, usb->devhdl,
+                               2 | LIBUSB_ENDPOINT_OUT,
+                               (unsigned char *)&devc->cmd_pkt,
+                               sizeof(struct h4032l_cmd_pkt),
+                               h4032l_usb_callback, (void *)sdi,
+                               H4032L_USB_TIMEOUT);
+               } else {
+                       /* Setup new USB poll packet, reuse transfer object. */
+                       sr_dbg("Poll: %d.", devc->status);
+                       libusb_fill_bulk_transfer(transfer, usb->devhdl,
+                               6 | LIBUSB_ENDPOINT_IN,
+                               devc->buffer, ARRAY_SIZE(devc->buffer),
+                               h4032l_usb_callback,
+                               (void *)sdi, H4032L_USB_TIMEOUT);
+               }
+               /* Send prepared usb packet. */
+               if ((ret = libusb_submit_transfer(transfer)) != 0) {
+                       sr_err("Failed to submit transfer: %s.",
+                              libusb_error_name(ret));
+                       devc->status = H4032L_STATUS_IDLE;
+               }
+       } else {
+               sr_dbg("Now idle.");
+       }
+
+       if (devc->status == H4032L_STATUS_IDLE)
+               libusb_free_transfer(transfer);
+}
+
+uint16_t h4032l_voltage2pwm(double voltage)
+{
+       /*
+        * word PwmA - channel A Vref PWM value, pseudocode:
+        * -6V < ThresholdVoltage < +6V
+        * Vref = 1.8 - ThresholdVoltage
+        * if Vref > 10.0
+        *      Vref = 10.0
+        * if Vref < -5.0
+        *      Vref = -5.0
+        * pwm = ToInt((Vref + 5.0) / 15.0 * 4096.0)
+        * if pwm > 4095
+        *      pwm = 4095
+        */
+       voltage = 1.8 - voltage;
+       if (voltage > 10.0)
+               voltage = 10.0;
+       else if (voltage < -5.0)
+               voltage = -5.0;
+
+       return (uint16_t) ((voltage + 5.0) * (4096.0 / 15.0));
+}
+
+SR_PRIV int h4032l_start(const struct sr_dev_inst *sdi)
+{
+       struct dev_context *devc = sdi->priv;
+       struct sr_usb_dev_inst *usb = sdi->conn;
+       struct libusb_transfer *transfer;
+       int ret;
+
+       /* Send configure command to arm the logic analyzer. */
+       devc->cmd_pkt.cmd = CMD_CONFIGURE;
+       devc->status = H4032L_STATUS_CMD_CONFIGURE;
+       devc->remaining_samples = devc->cmd_pkt.sample_size;
+
+       transfer = libusb_alloc_transfer(0);
+
+       libusb_fill_bulk_transfer(transfer, usb->devhdl,
+               2 | LIBUSB_ENDPOINT_OUT, (unsigned char *)&devc->cmd_pkt,
+               sizeof(struct h4032l_cmd_pkt), h4032l_usb_callback,
+               (void *)sdi, H4032L_USB_TIMEOUT);
+
+       if ((ret = libusb_submit_transfer(transfer)) != 0) {
+               sr_err("Failed to submit transfer: %s.", libusb_error_name(ret));
+               libusb_free_transfer(transfer);
+               return SR_ERR;
+       }
+
+       std_session_send_df_header(sdi);
+
+       return SR_OK;
+}
+
+SR_PRIV int h4032l_dev_open(struct sr_dev_inst *sdi)
+{
+       struct drv_context *drvc = sdi->driver->context;
+       struct sr_usb_dev_inst *usb = sdi->conn;
+       struct libusb_device_descriptor des;
+       libusb_device **devlist;
+       int ret = SR_ERR, i, device_count;
+       char connection_id[64];
+
+       device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
+       if (device_count < 0) {
+               sr_err("Failed to get device list: %s.",
+                      libusb_error_name(device_count));
+               return SR_ERR;
+       }
+
+       for (i = 0; i < device_count; i++) {
+               libusb_get_device_descriptor(devlist[i], &des);
+
+               if (des.idVendor != H4032L_USB_VENDOR ||
+                   des.idProduct != H4032L_USB_PRODUCT)
+                       continue;
+
+               if ((sdi->status == SR_ST_INITIALIZING) ||
+                   (sdi->status == SR_ST_INACTIVE)) {
+                       /* Check device by its physical USB bus/port address. */
+                       usb_get_port_path(devlist[i], connection_id,
+                                         sizeof(connection_id));
+                       if (strcmp(sdi->connection_id, connection_id))
+                               /* This is not the one. */
+                               continue;
+               }
+
+               if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
+                       if (usb->address == 0xff)
+                               /*
+                                * First time we touch this device after FW
+                                * upload, so we don't know the address yet.
+                                */
+                               usb->address =
+                                   libusb_get_device_address(devlist[i]);
+               } else {
+                       sr_err("Failed to open device: %s.",
+                              libusb_error_name(ret));
+                       ret = SR_ERR;
+                       break;
+               }
+
+               ret = SR_OK;
+               break;
+       }
+
+       libusb_free_device_list(devlist, 1);
+       return ret;
 }
index 732b9f9bef27de428a27db20f77679534709ca00..b5017ed23000ff8905161c7a595761f3ace07b5d 100644 (file)
@@ -1,7 +1,9 @@
 /*
  * This file is part of the libsigrok project.
  *
- * Copyright (C) 2016 Andreas Zschunke <andreasz77@gmx.net>
+ * Copyright (C) 2016 Andreas Zschunke <andreas.zschunke@gmx.net>
+ * Copyright (C) 2017 Andrej Valek <andy@skyrain.eu>
+ * Copyright (C) 2017 Uwe Hermann <uwe@hermann-uwe.de>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 #include <stdint.h>
 #include <glib.h>
+#include <string.h>
 #include <libsigrok/libsigrok.h>
 #include "libsigrok-internal.h"
 
 #define LOG_PREFIX "hantek-4032l"
 
-/** Private, per-device-instance driver context. */
-struct dev_context {
-       /* Model-specific information */
+#define H4032L_USB_VENDOR 0x04b5
+#define H4032L_USB_PRODUCT 0x4032
+
+#define H4032L_CMD_PKT_MAGIC 0x017f
+#define H4032L_STATUS_PACKET_MAGIC 0x2B1A037F
+#define H4032L_START_PACKET_MAGIC 0x2B1A027F
+#define H4032L_END_PACKET_MAGIC 0x4D3C037F
+
+enum h4032l_trigger_edge_type {
+       H4032L_TRIGGER_EDGE_TYPE_RISE = 0,
+       H4032L_TRIGGER_EDGE_TYPE_FALL,
+       H4032L_TRIGGER_EDGE_TYPE_TOGGLE,
+       H4032L_TRIGGER_EDGE_TYPE_DISABLED
+};
+
+enum h4032l_trigger_data_range_type {
+       H4032L_TRIGGER_DATA_RANGE_TYPE_MAX = 0,
+       H4032L_TRIGGER_DATA_RANGE_TYPE_MIN_OR_MAX,
+       H4032L_TRIGGER_DATA_RANGE_TYPE_OUT_OF_RANGE,
+       H4032L_TRIGGER_DATA_RANGE_TYPE_WITHIN_RANGE
+};
+
+enum h4032l_trigger_time_range_type {
+       H4032L_TRIGGER_TIME_RANGE_TYPE_MAX = 0,
+       H4032L_TRIGGER_TIME_RANGE_TYPE_MIN_OR_MAX,
+       H4032L_TRIGGER_TIME_RANGE_TYPE_OUT_OF_RANGE,
+       H4032L_TRIGGER_TIME_RANGE_TYPE_WITHIN_RANGE
+};
 
-       /* Acquisition settings */
+enum h4032l_trigger_data_selection {
+       H4032L_TRIGGER_DATA_SELECTION_NEXT = 0,
+       H4032L_TRIGGER_DATA_SELECTION_CURRENT,
+       H4032L_TRIGGER_DATA_SELECTION_PREV
+};
 
-       /* Operational state */
+enum h4032l_status {
+       H4032L_STATUS_IDLE,
+       H4032L_STATUS_CMD_CONFIGURE,
+       H4032L_STATUS_CMD_STATUS,
+       H4032L_STATUS_RESPONSE_STATUS,
+       H4032L_STATUS_RESPONSE_STATUS_RETRY,
+       H4032L_STATUS_RESPONSE_STATUS_CONTINUE,
+       H4032L_STATUS_CMD_GET,
+       H4032L_STATUS_FIRST_TRANSFER,
+       H4032L_STATUS_TRANSFER,
+};
 
-       /* Temporary state across callbacks */
+struct __attribute__((__packed__)) h4032l_trigger {
+       struct {
+               uint32_t edge_signal:5;
+               uint32_t edge_type:2;
+               uint32_t :1;
+               uint32_t data_range_type:2;
+               uint32_t time_range_type:2;
+               uint32_t data_range_enabled:1;
+               uint32_t time_range_enabled:1;
+               uint32_t :2;
+               uint32_t data_sel:2;
+               uint32_t combined_enabled:1;
+       } flags;
+       uint32_t data_range_min;
+       uint32_t data_range_max;
+       uint32_t time_range_min;
+       uint32_t time_range_max;
+       uint32_t data_range_mask;
+       uint32_t combine_mask;
+       uint32_t combine_data;
+};
 
+struct __attribute__((__packed__)) h4032l_cmd_pkt {
+       uint16_t magic; /* 0x017f */
+       uint8_t sample_rate;
+       struct {
+               uint8_t enable_trigger1:1;
+               uint8_t enable_trigger2:1;
+               uint8_t trigger_and_logic:1;
+       } trig_flags;
+       uint16_t pwm_a;
+       uint16_t pwm_b;
+       uint16_t reserved;
+       uint32_t sample_size; /* Sample depth in bits per channel, 2k-64M, must be multiple of 512. */
+       uint32_t pre_trigger_size; /* Pretrigger buffer depth in bits, must be < sample_size. */
+       struct h4032l_trigger trigger[2];
+       uint16_t cmd;
+};
+
+struct dev_context {
+       enum h4032l_status status;
+       uint32_t remaining_samples;
+       struct h4032l_cmd_pkt cmd_pkt;
+       struct libusb_transfer *usb_transfer;
+       uint8_t buffer[512];
+       uint64_t capture_ratio;
 };
 
-SR_PRIV int hantek_4032l_receive_data(int fd, int revents, void *cb_data);
+SR_PRIV int h4032l_receive_data(int fd, int revents, void *cb_data);
+SR_PRIV uint16_t h4032l_voltage2pwm(double voltage);
+SR_PRIV void LIBUSB_CALL h4032l_usb_callback(struct libusb_transfer *transfer);
+SR_PRIV int h4032l_start(const struct sr_dev_inst *sdi);
+SR_PRIV int h4032l_dev_open(struct sr_dev_inst *sdi);
 
 #endif