2 * This file is part of the libsigrok project.
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #define VENDOR_NAME "ZEROPLUS"
23 #define USB_INTERFACE 0
24 #define USB_CONFIGURATION 1
25 #define NUM_TRIGGER_STAGES 4
26 #define TRIGGER_TYPE "01"
27 #define PACKET_SIZE 2048 /* ?? */
29 //#define ZP_EXPERIMENTAL
35 unsigned int channels;
36 unsigned int sample_depth; /* In Ksamples/channel */
37 unsigned int max_sampling_freq;
41 * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the
42 * same 128K sample depth.
44 static const struct zp_model zeroplus_models[] = {
45 {0x0c12, 0x7002, "LAP-16128U", 16, 128, 200},
46 {0x0c12, 0x7009, "LAP-C(16064)", 16, 64, 100},
47 {0x0c12, 0x700a, "LAP-C(16128)", 16, 128, 200},
48 {0x0c12, 0x700b, "LAP-C(32128)", 32, 128, 200},
49 {0x0c12, 0x700c, "LAP-C(321000)", 32, 1024, 200},
50 {0x0c12, 0x700d, "LAP-C(322000)", 32, 2048, 200},
51 {0x0c12, 0x700e, "LAP-C(16032)", 16, 32, 100},
52 {0x0c12, 0x7016, "LAP-C(162000)", 16, 2048, 200},
56 static const int32_t hwcaps[] = {
57 SR_CONF_LOGIC_ANALYZER,
59 SR_CONF_CAPTURE_RATIO,
60 SR_CONF_VOLTAGE_THRESHOLD,
61 SR_CONF_LIMIT_SAMPLES,
65 * ZEROPLUS LAP-C (16032) numbers the 16 probes A0-A7 and B0-B7.
66 * We currently ignore other untested/unsupported devices here.
68 static const char *probe_names[] = {
69 "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7",
70 "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7",
71 "C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7",
72 "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
76 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
77 static struct sr_dev_driver *di = &zeroplus_logic_cube_driver_info;
80 * The hardware supports more samplerates than these, but these are the
81 * options hardcoded into the vendor's Windows GUI.
84 static const uint64_t samplerates_100[] = {
103 const uint64_t samplerates_200[] = {
124 static int dev_close(struct sr_dev_inst *sdi);
127 static int configure_probes(const struct sr_dev_inst *sdi)
129 struct dev_context *devc;
130 const struct sr_probe *probe;
132 int probe_bit, stage, i;
135 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
138 devc->probe_mask = 0;
139 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
140 devc->trigger_mask[i] = 0;
141 devc->trigger_value[i] = 0;
145 for (l = sdi->probes; l; l = l->next) {
146 probe = (struct sr_probe *)l->data;
147 if (probe->enabled == FALSE)
149 probe_bit = 1 << (probe->index);
150 devc->probe_mask |= probe_bit;
152 if (probe->trigger) {
154 for (tc = probe->trigger; *tc; tc++) {
155 devc->trigger_mask[stage] |= probe_bit;
157 devc->trigger_value[stage] |= probe_bit;
159 if (stage > NUM_TRIGGER_STAGES)
169 static int configure_probes(const struct sr_dev_inst *sdi)
171 struct dev_context *devc;
173 const struct sr_probe *probe;
177 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
180 for (l = sdi->probes; l; l = l->next) {
181 probe = (struct sr_probe *)l->data;
182 if (probe->enabled == FALSE)
185 if ((tc = probe->trigger)) {
195 type = TRIGGER_POSEDGE;
198 type = TRIGGER_NEGEDGE;
201 type = TRIGGER_ANYEDGE;
207 analyzer_add_trigger(probe->index, type);
215 SR_PRIV int zp_set_samplerate(struct dev_context *devc, uint64_t samplerate)
219 for (i = 0; ARRAY_SIZE(samplerates_200); i++)
220 if (samplerate == samplerates_200[i])
223 if (i == ARRAY_SIZE(samplerates_200) || samplerate > devc->max_samplerate) {
224 sr_err("Unsupported samplerate: %" PRIu64 "Hz.", samplerate);
228 sr_info("Setting samplerate to %" PRIu64 "Hz.", samplerate);
230 if (samplerate >= SR_MHZ(1))
231 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
232 else if (samplerate >= SR_KHZ(1))
233 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
235 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
237 devc->cur_samplerate = samplerate;
242 static int dev_clear(void)
244 return std_dev_clear(di, NULL);
247 static int init(struct sr_context *sr_ctx)
249 return std_init(sr_ctx, di, LOG_PREFIX);
252 static GSList *scan(GSList *options)
254 struct sr_dev_inst *sdi;
255 struct sr_probe *probe;
256 struct drv_context *drvc;
257 struct dev_context *devc;
258 const struct zp_model *prof;
259 struct libusb_device_descriptor des;
260 libusb_device **devlist;
262 int ret, devcnt, i, j;
270 /* Find all ZEROPLUS analyzers and add them to device list. */
272 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); /* TODO: Errors. */
274 for (i = 0; devlist[i]; i++) {
275 ret = libusb_get_device_descriptor(devlist[i], &des);
277 sr_err("Failed to get device descriptor: %s.",
278 libusb_error_name(ret));
283 for (j = 0; j < zeroplus_models[j].vid; j++) {
284 if (des.idVendor == zeroplus_models[j].vid &&
285 des.idProduct == zeroplus_models[j].pid) {
286 prof = &zeroplus_models[j];
289 /* Skip if the device was not found. */
292 sr_info("Found ZEROPLUS %s.", prof->model_name);
294 /* Register the device with libsigrok. */
295 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
296 VENDOR_NAME, prof->model_name, NULL))) {
297 sr_err("%s: sr_dev_inst_new failed", __func__);
302 /* Allocate memory for our private driver context. */
303 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
304 sr_err("Device context malloc failed.");
310 devc->num_channels = prof->channels;
311 #ifdef ZP_EXPERIMENTAL
312 devc->max_sample_depth = 128 * 1024;
313 devc->max_samplerate = 200;
315 devc->max_sample_depth = prof->sample_depth * 1024;
316 devc->max_samplerate = prof->max_sampling_freq;
318 devc->max_samplerate *= SR_MHZ(1);
319 devc->memory_size = MEMORY_SIZE_8K;
320 // memset(devc->trigger_buffer, 0, NUM_TRIGGER_STAGES);
322 /* Fill in probelist according to this device's profile. */
323 for (j = 0; j < devc->num_channels; j++) {
324 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
327 sdi->probes = g_slist_append(sdi->probes, probe);
330 devices = g_slist_append(devices, sdi);
331 drvc->instances = g_slist_append(drvc->instances, sdi);
332 sdi->inst_type = SR_INST_USB;
333 sdi->conn = sr_usb_dev_inst_new(
334 libusb_get_bus_number(devlist[i]),
335 libusb_get_device_address(devlist[i]), NULL);
339 libusb_free_device_list(devlist, 1);
344 static GSList *dev_list(void)
346 return ((struct drv_context *)(di->priv))->instances;
349 static int dev_open(struct sr_dev_inst *sdi)
351 struct dev_context *devc;
352 struct drv_context *drvc;
353 struct sr_usb_dev_inst *usb;
354 libusb_device **devlist, *dev;
355 struct libusb_device_descriptor des;
356 int device_count, ret, i;
361 if (!(devc = sdi->priv)) {
362 sr_err("%s: sdi->priv was NULL", __func__);
366 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx,
368 if (device_count < 0) {
369 sr_err("Failed to retrieve device list.");
374 for (i = 0; i < device_count; i++) {
375 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
376 sr_err("Failed to get device descriptor: %s.",
377 libusb_error_name(ret));
380 if (libusb_get_bus_number(devlist[i]) == usb->bus
381 && libusb_get_device_address(devlist[i]) == usb->address) {
387 sr_err("Device on bus %d address %d disappeared!",
388 usb->bus, usb->address);
392 if (!(ret = libusb_open(dev, &(usb->devhdl)))) {
393 sdi->status = SR_ST_ACTIVE;
394 sr_info("Opened device %d on %d.%d interface %d.",
395 sdi->index, usb->bus, usb->address, USB_INTERFACE);
397 sr_err("Failed to open device: %s.", libusb_error_name(ret));
401 ret = libusb_set_configuration(usb->devhdl, USB_CONFIGURATION);
403 sr_err("Unable to set USB configuration %d: %s.",
404 USB_CONFIGURATION, libusb_error_name(ret));
408 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
410 sr_err("Unable to claim interface: %s.",
411 libusb_error_name(ret));
415 /* Set default configuration after power on. */
416 if (analyzer_read_status(usb->devhdl) == 0)
417 analyzer_configure(usb->devhdl);
419 analyzer_reset(usb->devhdl);
420 analyzer_initialize(usb->devhdl);
422 //analyzer_set_memory_size(MEMORY_SIZE_512K);
423 // analyzer_set_freq(g_freq, g_freq_scale);
424 analyzer_set_trigger_count(1);
425 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
426 // * get_memory_size(g_memory_size)) / 100) >> 2);
429 if (g_double_mode == 1)
430 analyzer_set_compression(COMPRESSION_DOUBLE);
431 else if (g_compression == 1)
432 analyzer_set_compression(COMPRESSION_ENABLE);
435 analyzer_set_compression(COMPRESSION_NONE);
437 if (devc->cur_samplerate == 0) {
438 /* Samplerate hasn't been set. Default to 1MHz. */
439 analyzer_set_freq(1, FREQ_SCALE_MHZ);
440 devc->cur_samplerate = SR_MHZ(1);
443 if (devc->cur_threshold == 0)
444 set_voltage_threshold(devc, 1.5);
449 static int dev_close(struct sr_dev_inst *sdi)
451 struct sr_usb_dev_inst *usb;
458 sr_info("Closing device %d on %d.%d interface %d.", sdi->index,
459 usb->bus, usb->address, USB_INTERFACE);
460 libusb_release_interface(usb->devhdl, USB_INTERFACE);
461 libusb_reset_device(usb->devhdl);
462 libusb_close(usb->devhdl);
464 sdi->status = SR_ST_INACTIVE;
469 static int cleanup(void)
474 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
475 const struct sr_probe_group *probe_group)
477 struct dev_context *devc;
482 case SR_CONF_SAMPLERATE:
485 *data = g_variant_new_uint64(devc->cur_samplerate);
486 sr_spew("Returning samplerate: %" PRIu64 "Hz.",
487 devc->cur_samplerate);
491 case SR_CONF_CAPTURE_RATIO:
494 *data = g_variant_new_uint64(devc->capture_ratio);
498 case SR_CONF_VOLTAGE_THRESHOLD:
502 range[0] = g_variant_new_double(devc->cur_threshold);
503 range[1] = g_variant_new_double(devc->cur_threshold);
504 *data = g_variant_new_tuple(range, 2);
515 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
516 const struct sr_probe_group *probe_group)
518 struct dev_context *devc;
523 if (sdi->status != SR_ST_ACTIVE)
524 return SR_ERR_DEV_CLOSED;
526 if (!(devc = sdi->priv)) {
527 sr_err("%s: sdi->priv was NULL", __func__);
532 case SR_CONF_SAMPLERATE:
533 return zp_set_samplerate(devc, g_variant_get_uint64(data));
534 case SR_CONF_LIMIT_SAMPLES:
535 return set_limit_samples(devc, g_variant_get_uint64(data));
536 case SR_CONF_CAPTURE_RATIO:
537 return set_capture_ratio(devc, g_variant_get_uint64(data));
538 case SR_CONF_VOLTAGE_THRESHOLD:
539 g_variant_get(data, "(dd)", &low, &high);
540 return set_voltage_threshold(devc, (low + high) / 2.0);
548 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
549 const struct sr_probe_group *probe_group)
551 struct dev_context *devc;
560 case SR_CONF_DEVICE_OPTIONS:
561 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
562 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
564 case SR_CONF_SAMPLERATE:
566 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
567 if (devc->prof->max_sampling_freq == 100) {
568 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
569 samplerates_100, ARRAY_SIZE(samplerates_100),
571 } else if (devc->prof->max_sampling_freq == 200) {
572 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
573 samplerates_200, ARRAY_SIZE(samplerates_200),
576 sr_err("Internal error: Unknown max. samplerate: %d.",
577 devc->prof->max_sampling_freq);
580 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
581 *data = g_variant_builder_end(&gvb);
583 case SR_CONF_TRIGGER_TYPE:
584 *data = g_variant_new_string(TRIGGER_TYPE);
586 case SR_CONF_VOLTAGE_THRESHOLD:
587 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
588 for (v = -6.0; v <= 6.0; v += 0.1) {
589 range[0] = g_variant_new_double(v);
590 range[1] = g_variant_new_double(v);
591 gvar = g_variant_new_tuple(range, 2);
592 g_variant_builder_add_value(&gvb, gvar);
594 *data = g_variant_builder_end(&gvb);
603 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
606 struct dev_context *devc;
607 struct sr_usb_dev_inst *usb;
608 struct sr_datafeed_packet packet;
609 struct sr_datafeed_logic logic;
610 unsigned int samples_read;
612 unsigned int packet_num, n;
615 unsigned int stop_address;
616 unsigned int now_address;
617 unsigned int trigger_address;
618 unsigned int trigger_offset;
619 unsigned int triggerbar;
620 unsigned int ramsize_trigger;
621 unsigned int memory_size;
622 unsigned int valid_samples;
623 unsigned int discard;
626 if (sdi->status != SR_ST_ACTIVE)
627 return SR_ERR_DEV_CLOSED;
629 if (!(devc = sdi->priv)) {
630 sr_err("%s: sdi->priv was NULL", __func__);
634 if (configure_probes(sdi) != SR_OK) {
635 sr_err("Failed to configure probes.");
641 set_triggerbar(devc);
643 /* Push configured settings to device. */
644 analyzer_configure(usb->devhdl);
646 analyzer_start(usb->devhdl);
647 sr_info("Waiting for data.");
648 analyzer_wait_data(usb->devhdl);
650 status = analyzer_read_status(usb->devhdl);
651 stop_address = analyzer_get_stop_address(usb->devhdl);
652 now_address = analyzer_get_now_address(usb->devhdl);
653 trigger_address = analyzer_get_trigger_address(usb->devhdl);
655 triggerbar = analyzer_get_triggerbar_address();
656 ramsize_trigger = analyzer_get_ramsize_trigger_address();
658 n = get_memory_size(devc->memory_size);
661 sr_info("Status = 0x%x.", status);
662 sr_info("Stop address = 0x%x.", stop_address);
663 sr_info("Now address = 0x%x.", now_address);
664 sr_info("Trigger address = 0x%x.", trigger_address);
665 sr_info("Triggerbar address = 0x%x.", triggerbar);
666 sr_info("Ramsize trigger = 0x%x.", ramsize_trigger);
667 sr_info("Memory size = 0x%x.", memory_size);
669 /* Send header packet to the session bus. */
670 std_session_send_df_header(cb_data, LOG_PREFIX);
672 /* Check for empty capture */
673 if ((status & STATUS_READY) && !stop_address) {
674 packet.type = SR_DF_END;
675 sr_session_send(cb_data, &packet);
679 if (!(buf = g_try_malloc(PACKET_SIZE))) {
680 sr_err("Packet buffer malloc failed.");
681 return SR_ERR_MALLOC;
684 /* Check if the trigger is in the samples we are throwing away */
685 trigger_now = now_address == trigger_address ||
686 ((now_address + 1) % memory_size) == trigger_address;
689 * STATUS_READY doesn't clear until now_address advances past
690 * addr 0, but for our logic, clear it in that case
693 status &= ~STATUS_READY;
695 analyzer_read_start(usb->devhdl);
697 /* Calculate how much data to discard */
699 if (status & STATUS_READY) {
701 * We haven't wrapped around, we need to throw away data from
702 * our current position to the end of the buffer.
703 * Additionally, the first two samples captured are always
706 discard += memory_size - now_address + 2;
710 /* If we have more samples than we need, discard them */
711 valid_samples = (stop_address - now_address) % memory_size;
712 if (valid_samples > ramsize_trigger + triggerbar) {
713 discard += valid_samples - (ramsize_trigger + triggerbar);
714 now_address += valid_samples - (ramsize_trigger + triggerbar);
717 sr_info("Need to discard %d samples.", discard);
719 /* Calculate how far in the trigger is */
723 trigger_offset = (trigger_address - now_address) % memory_size;
725 /* Recalculate the number of samples available */
726 valid_samples = (stop_address - now_address) % memory_size;
728 /* Send the incoming transfer to the session bus. */
730 for (packet_num = 0; packet_num < n / PACKET_SIZE; packet_num++) {
732 unsigned int buf_offset;
734 res = analyzer_read_data(usb->devhdl, buf, PACKET_SIZE);
735 sr_info("Tried to read %d bytes, actually read %d bytes.",
738 if (discard >= PACKET_SIZE / 4) {
739 discard -= PACKET_SIZE / 4;
743 len = PACKET_SIZE - discard * 4;
744 buf_offset = discard * 4;
747 /* Check if we've read all the samples */
748 if (samples_read + len / 4 >= valid_samples)
749 len = (valid_samples - samples_read) * 4;
753 if (samples_read < trigger_offset &&
754 samples_read + len / 4 > trigger_offset) {
755 /* Send out samples remaining before trigger */
756 packet.type = SR_DF_LOGIC;
757 packet.payload = &logic;
758 logic.length = (trigger_offset - samples_read) * 4;
760 logic.data = buf + buf_offset;
761 sr_session_send(cb_data, &packet);
763 samples_read += logic.length / 4;
764 buf_offset += logic.length;
767 if (samples_read == trigger_offset) {
768 /* Send out trigger */
769 packet.type = SR_DF_TRIGGER;
770 packet.payload = NULL;
771 sr_session_send(cb_data, &packet);
774 /* Send out data (or data after trigger) */
775 packet.type = SR_DF_LOGIC;
776 packet.payload = &logic;
779 logic.data = buf + buf_offset;
780 sr_session_send(cb_data, &packet);
781 samples_read += len / 4;
783 analyzer_read_stop(usb->devhdl);
786 packet.type = SR_DF_END;
787 sr_session_send(cb_data, &packet);
792 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
793 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
795 struct dev_context *devc;
796 struct sr_usb_dev_inst *usb;
797 struct sr_datafeed_packet packet;
799 packet.type = SR_DF_END;
800 sr_session_send(cb_data, &packet);
802 if (!(devc = sdi->priv)) {
803 sr_err("%s: sdi->priv was NULL", __func__);
808 analyzer_reset(usb->devhdl);
809 /* TODO: Need to cancel and free any queued up transfers. */
814 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
815 .name = "zeroplus-logic-cube",
816 .longname = "ZEROPLUS Logic Cube LAP-C series",
821 .dev_list = dev_list,
822 .dev_clear = dev_clear,
823 .config_get = config_get,
824 .config_set = config_set,
825 .config_list = config_list,
826 .dev_open = dev_open,
827 .dev_close = dev_close,
828 .dev_acquisition_start = dev_acquisition_start,
829 .dev_acquisition_stop = dev_acquisition_stop,