2 * This file is part of the libsigrok project.
4 * Copyright (C) 2016 Andreas Zschunke <andreas.zschunke@gmx.net>
5 * Copyright (C) 2017 Andrej Valek <andy@skyrain.eu>
6 * Copyright (C) 2017 Uwe Hermann <uwe@hermann-uwe.de>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #define USB_INTERFACE 0
26 #define NUM_CHANNELS 32
28 static const uint32_t scanopts[] = {
32 static const uint32_t drvopts[] = {
33 SR_CONF_LOGIC_ANALYZER,
36 static const uint32_t devopts[] = {
37 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
38 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
39 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
40 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
41 SR_CONF_CONN | SR_CONF_GET,
42 SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_SET | SR_CONF_LIST,
45 static const int32_t trigger_matches[] = {
53 static const uint64_t samplerates[] = {
89 static const uint64_t samplerates_hw[] = {
128 SR_PRIV struct sr_dev_driver hantek_4032l_driver_info;
130 static GSList *scan(struct sr_dev_driver *di, GSList *options)
132 struct drv_context *drvc = di->context;
133 GSList *l, *devices, *conn_devices;
134 libusb_device **devlist;
135 struct libusb_device_descriptor des;
138 char connection_id[64];
139 struct sr_channel_group *cg;
140 struct sr_dev_inst *sdi;
141 struct sr_channel *ch;
145 drvc->instances = NULL;
148 for (l = options; l; l = l->next) {
149 struct sr_config *src = l->data;
150 if (src->key == SR_CONF_CONN) {
151 conn = g_variant_get_string(src->data, NULL);
157 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
161 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
162 for (i = 0; devlist[i]; i++) {
164 struct sr_usb_dev_inst *usb = NULL;
165 for (l = conn_devices; l; l = l->next) {
167 if (usb->bus == libusb_get_bus_number(devlist[i]) &&
168 usb->address == libusb_get_device_address(devlist[i]))
172 /* This device matched none of the ones that
173 * matched the conn specification. */
177 libusb_get_device_descriptor(devlist[i], &des);
179 if (des.idVendor != H4032L_USB_VENDOR ||
180 des.idProduct != H4032L_USB_PRODUCT)
183 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
186 sdi = g_malloc0(sizeof(struct sr_dev_inst));
187 sdi->driver = &hantek_4032l_driver_info;
188 sdi->vendor = g_strdup("Hantek");
189 sdi->model = g_strdup("4032L");
190 sdi->connection_id = g_strdup(connection_id);
192 struct sr_channel_group *channel_groups[2];
193 for (int j = 0; j < 2; j++) {
194 cg = g_malloc0(sizeof(struct sr_channel_group));
195 cg->name = g_strdup_printf("%c", 'A' + j);
196 channel_groups[j] = cg;
197 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
200 /* Assemble channel list and add channel to channel groups. */
201 for (int j = 0; j < NUM_CHANNELS; j++) {
202 char channel_name[4];
203 sprintf(channel_name, "%c%d", 'A' + (j & 1), j / 2);
204 ch = sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE, channel_name);
205 cg = channel_groups[j & 1];
206 cg->channels = g_slist_append(cg->channels, ch);
209 struct dev_context *devc = g_malloc0(sizeof(struct dev_context));
211 /* Initialize command packet. */
212 devc->cmd_pkt.magic = H4032L_CMD_PKT_MAGIC;
213 devc->cmd_pkt.pwm_a = h4032l_voltage2pwm(2.5);
214 devc->cmd_pkt.pwm_b = h4032l_voltage2pwm(2.5);
215 devc->cmd_pkt.sample_size = 16384;
217 devc->status = H4032L_STATUS_IDLE;
219 devc->capture_ratio = 5;
222 devices = g_slist_append(devices, sdi);
224 sdi->status = SR_ST_INACTIVE;
225 sdi->inst_type = SR_INST_USB;
226 sdi->conn = sr_usb_dev_inst_new(
227 libusb_get_bus_number(devlist[i]),
228 libusb_get_device_address(devlist[i]), NULL);
231 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
232 libusb_free_device_list(devlist, 1);
234 return std_scan_complete(di, devices);
237 static int dev_open(struct sr_dev_inst *sdi)
239 struct sr_usb_dev_inst *usb = sdi->conn;
242 ret = h4032l_dev_open(sdi);
244 sr_err("Unable to open device.");
248 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
251 case LIBUSB_ERROR_BUSY:
252 sr_err("Unable to claim USB interface. Another "
253 "program or driver has already claimed it.");
255 case LIBUSB_ERROR_NO_DEVICE:
256 sr_err("Device has been disconnected.");
259 sr_err("Unable to claim interface: %s.",
260 libusb_error_name(ret));
267 /* Get FPGA version. */
268 if ((ret = h4032l_get_fpga_version(sdi)) != SR_OK)
274 static int dev_close(struct sr_dev_inst *sdi)
276 struct sr_usb_dev_inst *usb;
283 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
284 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
285 libusb_release_interface(usb->devhdl, USB_INTERFACE);
286 libusb_close(usb->devhdl);
292 static int config_get(uint32_t key, GVariant **data,
293 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
295 struct dev_context *devc = sdi->priv;
296 struct sr_usb_dev_inst *usb;
301 case SR_CONF_SAMPLERATE:
302 *data = g_variant_new_uint64(samplerates_hw[devc->cmd_pkt.sample_rate]);
304 case SR_CONF_CAPTURE_RATIO:
305 *data = g_variant_new_uint64(devc->capture_ratio);
307 case SR_CONF_LIMIT_SAMPLES:
308 *data = g_variant_new_uint64(devc->cmd_pkt.sample_size);
311 if (!sdi || !(usb = sdi->conn))
313 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
322 static int config_set(uint32_t key, GVariant *data,
323 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
325 struct dev_context *devc = sdi->priv;
326 struct h4032l_cmd_pkt *cmd_pkt = &devc->cmd_pkt;
331 case SR_CONF_SAMPLERATE: {
332 uint64_t sample_rate = g_variant_get_uint64(data);
334 while (i < ARRAY_SIZE(samplerates_hw) && samplerates_hw[i] != sample_rate)
337 if (i == ARRAY_SIZE(samplerates_hw) || sample_rate == 0) {
338 sr_err("Invalid sample rate.");
339 return SR_ERR_SAMPLERATE;
341 cmd_pkt->sample_rate = i;
344 case SR_CONF_CAPTURE_RATIO: {
345 uint64_t capture_ratio = g_variant_get_uint64(data);
346 if (capture_ratio > 99) {
347 sr_err("Invalid capture ratio.");
350 devc->capture_ratio = capture_ratio;
353 case SR_CONF_LIMIT_SAMPLES: {
354 uint64_t number_samples = g_variant_get_uint64(data);
355 number_samples += 511;
356 number_samples &= 0xfffffe00;
357 if (number_samples < H4043L_NUM_SAMPLES_MIN ||
358 number_samples > H4032L_NUM_SAMPLES_MAX) {
359 sr_err("Invalid sample range 2k...64M: %"
360 PRIu64 ".", number_samples);
363 cmd_pkt->sample_size = number_samples;
366 case SR_CONF_VOLTAGE_THRESHOLD: {
368 g_variant_get(data, "(dd)", &d1, &d2);
369 devc->cmd_pkt.pwm_a = h4032l_voltage2pwm(d1);
370 devc->cmd_pkt.pwm_b = h4032l_voltage2pwm(d2);
380 static int config_list(uint32_t key, GVariant **data,
381 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
384 case SR_CONF_SCAN_OPTIONS:
385 case SR_CONF_DEVICE_OPTIONS:
386 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
387 case SR_CONF_SAMPLERATE:
388 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates));
390 case SR_CONF_TRIGGER_MATCH:
391 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
393 case SR_CONF_VOLTAGE_THRESHOLD:
394 *data = std_gvar_tuple_double(2.5, 2.5);
396 case SR_CONF_LIMIT_SAMPLES:
397 *data = std_gvar_tuple_u64(H4043L_NUM_SAMPLES_MIN, H4032L_NUM_SAMPLES_MAX);
406 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
408 struct sr_dev_driver *di = sdi->driver;
409 struct drv_context *drvc = di->context;
410 struct dev_context *devc = sdi->priv;
411 struct sr_trigger *trigger = sr_session_trigger_get(sdi->session);
412 struct h4032l_cmd_pkt *cmd_pkt = &devc->cmd_pkt;
414 /* Initialize variables. */
415 devc->acq_aborted = FALSE;
416 devc->submitted_transfers = 0;
417 devc->sent_samples = 0;
419 /* Calculate packet ratio. */
420 cmd_pkt->pre_trigger_size = (cmd_pkt->sample_size * devc->capture_ratio) / 100;
421 devc->trigger_pos = cmd_pkt->pre_trigger_size;
423 cmd_pkt->trig_flags.enable_trigger1 = 0;
424 cmd_pkt->trig_flags.enable_trigger2 = 0;
425 cmd_pkt->trig_flags.trigger_and_logic = 0;
427 if (trigger && trigger->stages) {
428 GSList *stages = trigger->stages;
429 struct sr_trigger_stage *stage1 = stages->data;
431 sr_err("Only one trigger stage supported for now.");
434 cmd_pkt->trig_flags.enable_trigger1 = 1;
435 cmd_pkt->trigger[0].flags.edge_type = H4032L_TRIGGER_EDGE_TYPE_DISABLED;
436 cmd_pkt->trigger[0].flags.data_range_enabled = 0;
437 cmd_pkt->trigger[0].flags.time_range_enabled = 0;
438 cmd_pkt->trigger[0].flags.combined_enabled = 0;
439 cmd_pkt->trigger[0].flags.data_range_type = H4032L_TRIGGER_DATA_RANGE_TYPE_MAX;
440 cmd_pkt->trigger[0].data_range_mask = 0;
441 cmd_pkt->trigger[0].data_range_max = 0;
443 /* Initialize range mask values. */
444 uint32_t range_mask = 0;
445 uint32_t range_value = 0;
447 GSList *channel = stage1->matches;
449 struct sr_trigger_match *match = channel->data;
451 switch (match->match) {
452 case SR_TRIGGER_ZERO:
453 range_mask |= (1 << match->channel->index);
456 range_mask |= (1 << match->channel->index);
457 range_value |= (1 << match->channel->index);
459 case SR_TRIGGER_RISING:
460 if (cmd_pkt->trigger[0].flags.edge_type != H4032L_TRIGGER_EDGE_TYPE_DISABLED) {
461 sr_err("Only one trigger signal with fall/rising/edge allowed.");
464 cmd_pkt->trigger[0].flags.edge_type = H4032L_TRIGGER_EDGE_TYPE_RISE;
465 cmd_pkt->trigger[0].flags.edge_signal = match->channel->index;
467 case SR_TRIGGER_FALLING:
468 if (cmd_pkt->trigger[0].flags.edge_type != H4032L_TRIGGER_EDGE_TYPE_DISABLED) {
469 sr_err("Only one trigger signal with fall/rising/edge allowed.");
472 cmd_pkt->trigger[0].flags.edge_type = H4032L_TRIGGER_EDGE_TYPE_FALL;
473 cmd_pkt->trigger[0].flags.edge_signal = match->channel->index;
475 case SR_TRIGGER_EDGE:
476 if (cmd_pkt->trigger[0].flags.edge_type != H4032L_TRIGGER_EDGE_TYPE_DISABLED) {
477 sr_err("Only one trigger signal with fall/rising/edge allowed.");
480 cmd_pkt->trigger[0].flags.edge_type = H4032L_TRIGGER_EDGE_TYPE_TOGGLE;
481 cmd_pkt->trigger[0].flags.edge_signal = match->channel->index;
484 sr_err("Unknown trigger value.");
488 channel = channel->next;
491 /* Compress range mask value and apply range settings. */
493 cmd_pkt->trigger[0].flags.data_range_enabled = 1;
494 cmd_pkt->trigger[0].data_range_mask |= (range_mask);
496 uint32_t new_range_value = 0;
497 uint32_t bit_mask = 1;
499 if ((range_mask & 1) != 0) {
500 new_range_value <<= 1;
501 if ((range_value & 1) != 0)
502 new_range_value |= bit_mask;
508 cmd_pkt->trigger[0].data_range_max |= range_value;
512 usb_source_add(sdi->session, drvc->sr_ctx, 1000,
513 h4032l_receive_data, sdi->driver->context);
515 /* Start capturing. */
516 return h4032l_start(sdi);
519 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
521 /* Stop capturing. */
522 return h4032l_stop(sdi);
525 SR_PRIV struct sr_dev_driver hantek_4032l_driver_info = {
526 .name = "hantek-4032l",
527 .longname = "Hantek 4032L",
530 .cleanup = std_cleanup,
532 .dev_list = std_dev_list,
533 .dev_clear = std_dev_clear,
534 .config_get = config_get,
535 .config_set = config_set,
536 .config_list = config_list,
537 .dev_open = dev_open,
538 .dev_close = dev_close,
539 .dev_acquisition_start = dev_acquisition_start,
540 .dev_acquisition_stop = dev_acquisition_stop,
543 SR_REGISTER_DEV_DRIVER(hantek_4032l_driver_info);