2 * This file is part of the sigrok 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/>.
29 #include "sigrok-internal.h"
32 #define USB_VENDOR 0x0c12
33 #define USB_VENDOR_NAME "Zeroplus"
34 #define USB_MODEL_NAME "Logic Cube"
35 #define USB_MODEL_VERSION ""
38 #define USB_INTERFACE 0
39 #define USB_CONFIGURATION 1
40 #define NUM_TRIGGER_STAGES 4
41 #define TRIGGER_TYPES "01"
43 #define PACKET_SIZE 2048 /* ?? */
48 unsigned int channels;
49 unsigned int sample_depth; /* In Ksamples/channel */
50 unsigned int max_sampling_freq;
54 * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the
55 * same 128K sample depth.
57 static model_t zeroplus_models[] = {
58 {0x7009, "LAP-C(16064)", 16, 64, 100},
59 {0x700A, "LAP-C(16128)", 16, 128, 200},
60 {0x700B, "LAP-C(32128)", 32, 128, 200},
61 {0x700C, "LAP-C(321000)", 32, 1024, 200},
62 {0x700D, "LAP-C(322000)", 32, 2048, 200},
63 {0x700E, "LAP-C(16032)", 16, 32, 100},
64 {0x7016, "LAP-C(162000)", 16, 2048, 200},
67 static int hwcaps[] = {
68 SR_HWCAP_LOGIC_ANALYZER,
71 SR_HWCAP_CAPTURE_RATIO,
73 /* These are really implemented in the driver, not the hardware. */
74 SR_HWCAP_LIMIT_SAMPLES,
79 * ZEROPLUS LAP-C (16032) numbers the 16 probes A0-A7 and B0-B7.
80 * We currently ignore other untested/unsupported devices here.
82 static const char *probe_names[NUM_PROBES + 1] = {
102 /* List of struct sr_dev_inst, maintained by dev_open()/dev_close(). */
103 static GSList *dev_insts = NULL;
105 static libusb_context *usb_context = NULL;
108 * The hardware supports more samplerates than these, but these are the
109 * options hardcoded into the vendor's Windows GUI.
113 * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up
116 static uint64_t supported_samplerates[] = {
138 static struct sr_samplerates samplerates = {
142 supported_samplerates,
145 /* Private, per-device-instance driver context. */
147 uint64_t cur_samplerate;
148 uint64_t limit_samples;
149 int num_channels; /* TODO: This isn't initialized before it's needed :( */
150 uint64_t memory_size;
152 uint8_t trigger_mask[NUM_TRIGGER_STAGES];
153 uint8_t trigger_value[NUM_TRIGGER_STAGES];
154 // uint8_t trigger_buffer[NUM_TRIGGER_STAGES];
156 struct sr_usb_dev_inst *usb;
159 static int hw_dev_config_set(int dev_index, int hwcap, void *value);
161 static unsigned int get_memory_size(int type)
163 if (type == MEMORY_SIZE_8K)
165 else if (type == MEMORY_SIZE_64K)
167 else if (type == MEMORY_SIZE_128K)
169 else if (type == MEMORY_SIZE_512K)
175 static int opendev4(struct sr_dev_inst **sdi, libusb_device *dev,
176 struct libusb_device_descriptor *des)
182 /* Note: sdi is non-NULL, the caller already checked this. */
184 if (!(ctx = (*sdi)->priv)) {
185 sr_err("zp: %s: (*sdi)->priv was NULL", __func__);
189 if ((ret = libusb_get_device_descriptor(dev, des))) {
190 sr_err("zp: failed to get device descriptor: %d", ret);
194 if (des->idVendor != USB_VENDOR)
197 if (libusb_get_bus_number(dev) == ctx->usb->bus
198 && libusb_get_device_address(dev) == ctx->usb->address) {
200 for (i = 0; i < ARRAY_SIZE(zeroplus_models); i++) {
201 if (!(des->idProduct == zeroplus_models[i].pid))
204 sr_info("zp: Found ZeroPlus device 0x%04x (%s)",
205 des->idProduct, zeroplus_models[i].model_name);
206 ctx->num_channels = zeroplus_models[i].channels;
207 ctx->memory_size = zeroplus_models[i].sample_depth * 1024;
211 if (ctx->num_channels == 0) {
212 sr_err("zp: Unknown ZeroPlus device 0x%04x",
218 if (!(ret = libusb_open(dev, &(ctx->usb->devhdl)))) {
219 (*sdi)->status = SR_ST_ACTIVE;
220 sr_info("zp: opened device %d on %d.%d interface %d",
221 (*sdi)->index, ctx->usb->bus,
222 ctx->usb->address, USB_INTERFACE);
224 sr_err("zp: failed to open device: %d", ret);
232 static struct sr_dev_inst *zp_open_dev(int dev_index)
234 struct sr_dev_inst *sdi;
235 libusb_device **devlist;
236 struct libusb_device_descriptor des;
239 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
242 libusb_get_device_list(usb_context, &devlist);
243 if (sdi->status == SR_ST_INACTIVE) {
244 /* Find the device by vendor, product, bus and address. */
245 libusb_get_device_list(usb_context, &devlist);
246 for (i = 0; devlist[i]; i++) {
247 /* TODO: Error handling. */
248 opendev4(&sdi, devlist[i], &des);
251 /* Status must be SR_ST_ACTIVE, i.e. already in use... */
254 libusb_free_device_list(devlist, 1);
256 if (sdi && sdi->status != SR_ST_ACTIVE)
262 static void close_dev(struct sr_dev_inst *sdi)
266 if (!(ctx = sdi->priv)) {
267 sr_err("zp: %s: sdi->priv was NULL", __func__);
271 if (!ctx->usb->devhdl)
274 sr_info("zp: closing device %d on %d.%d interface %d", sdi->index,
275 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
276 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
277 libusb_reset_device(ctx->usb->devhdl);
278 libusb_close(ctx->usb->devhdl);
279 ctx->usb->devhdl = NULL;
280 /* TODO: Call libusb_exit() here or only in hw_cleanup()? */
281 sdi->status = SR_ST_INACTIVE;
284 static int configure_probes(struct sr_dev_inst *sdi, GSList *probes)
287 struct sr_probe *probe;
289 int probe_bit, stage, i;
292 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
296 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
297 ctx->trigger_mask[i] = 0;
298 ctx->trigger_value[i] = 0;
302 for (l = probes; l; l = l->next) {
303 probe = (struct sr_probe *)l->data;
304 if (probe->enabled == FALSE)
306 probe_bit = 1 << (probe->index - 1);
307 ctx->probe_mask |= probe_bit;
309 if (probe->trigger) {
311 for (tc = probe->trigger; *tc; tc++) {
312 ctx->trigger_mask[stage] |= probe_bit;
314 ctx->trigger_value[stage] |= probe_bit;
316 if (stage > NUM_TRIGGER_STAGES)
329 static int hw_init(const char *devinfo)
331 struct sr_dev_inst *sdi;
332 struct libusb_device_descriptor des;
333 libusb_device **devlist;
337 /* Avoid compiler warnings. */
340 /* Allocate memory for our private driver context. */
341 if (!(ctx = g_try_malloc(sizeof(struct context)))) {
342 sr_err("zp: %s: ctx malloc failed", __func__);
346 /* Set some sane defaults. */
347 ctx->cur_samplerate = 0;
348 ctx->limit_samples = 0;
349 /* TODO: num_channels isn't initialized before it's needed :( */
350 ctx->num_channels = NUM_PROBES;
351 ctx->memory_size = 0;
353 memset(ctx->trigger_mask, 0, NUM_TRIGGER_STAGES);
354 memset(ctx->trigger_value, 0, NUM_TRIGGER_STAGES);
355 // memset(ctx->trigger_buffer, 0, NUM_TRIGGER_STAGES);
357 if (libusb_init(&usb_context) != 0) {
358 sr_err("zp: Failed to initialize USB.");
362 /* Find all ZeroPlus analyzers and add them to device list. */
364 libusb_get_device_list(usb_context, &devlist); /* TODO: Errors. */
366 for (i = 0; devlist[i]; i++) {
367 ret = libusb_get_device_descriptor(devlist[i], &des);
369 sr_err("zp: failed to get device descriptor: %d", ret);
373 if (des.idVendor == USB_VENDOR) {
375 * Definitely a Zeroplus.
376 * TODO: Any way to detect specific model/version in
377 * the zeroplus range?
379 /* Register the device with libsigrok. */
380 if (!(sdi = sr_dev_inst_new(devcnt,
381 SR_ST_INACTIVE, USB_VENDOR_NAME,
382 USB_MODEL_NAME, USB_MODEL_VERSION))) {
383 sr_err("zp: %s: sr_dev_inst_new failed",
391 g_slist_append(dev_insts, sdi);
392 ctx->usb = sr_usb_dev_inst_new(
393 libusb_get_bus_number(devlist[i]),
394 libusb_get_device_address(devlist[i]), NULL);
398 libusb_free_device_list(devlist, 1);
403 static int hw_dev_open(int dev_index)
405 struct sr_dev_inst *sdi;
409 if (!(sdi = zp_open_dev(dev_index))) {
410 sr_err("zp: unable to open device");
414 /* TODO: Note: sdi is retrieved in zp_open_dev(). */
416 if (!(ctx = sdi->priv)) {
417 sr_err("zp: %s: sdi->priv was NULL", __func__);
421 ret = libusb_set_configuration(ctx->usb->devhdl, USB_CONFIGURATION);
423 sr_err("zp: Unable to set USB configuration %d: %d",
424 USB_CONFIGURATION, ret);
428 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
430 sr_err("zp: Unable to claim interface: %d", ret);
434 analyzer_reset(ctx->usb->devhdl);
435 analyzer_initialize(ctx->usb->devhdl);
437 analyzer_set_memory_size(MEMORY_SIZE_512K);
438 // analyzer_set_freq(g_freq, g_freq_scale);
439 analyzer_set_trigger_count(1);
440 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
441 // * get_memory_size(g_memory_size)) / 100) >> 2);
442 analyzer_set_ramsize_trigger_address(
443 (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
446 if (g_double_mode == 1)
447 analyzer_set_compression(COMPRESSION_DOUBLE);
448 else if (g_compression == 1)
449 analyzer_set_compression(COMPRESSION_ENABLE);
452 analyzer_set_compression(COMPRESSION_NONE);
454 if (ctx->cur_samplerate == 0) {
455 /* Samplerate hasn't been set. Default to the slowest one. */
456 if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE,
457 &samplerates.list[0]) == SR_ERR)
464 static int hw_dev_close(int dev_index)
466 struct sr_dev_inst *sdi;
468 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
469 sr_err("zp: %s: sdi was NULL", __func__);
470 return SR_ERR; /* TODO: SR_ERR_ARG? */
479 static int hw_cleanup(void)
482 struct sr_dev_inst *sdi;
484 for (l = dev_insts; l; l = l->next) {
486 /* Properly close all devices... */
488 /* ...and free all their memory. */
489 sr_dev_inst_free(sdi);
491 g_slist_free(dev_insts);
495 libusb_exit(usb_context);
501 static void *hw_dev_info_get(int dev_index, int dev_info_id)
503 struct sr_dev_inst *sdi;
507 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
508 sr_err("zp: %s: sdi was NULL", __func__);
512 if (!(ctx = sdi->priv)) {
513 sr_err("zp: %s: sdi->priv was NULL", __func__);
517 sr_spew("zp: %s: dev_index %d, dev_info_id %d.", __func__,
518 dev_index, dev_info_id);
520 switch (dev_info_id) {
523 sr_spew("zp: %s: Returning sdi.", __func__);
525 case SR_DI_NUM_PROBES:
526 info = GINT_TO_POINTER(ctx->num_channels);
527 sr_spew("zp: %s: Returning number of probes: %d.", __func__,
530 case SR_DI_PROBE_NAMES:
532 sr_spew("zp: %s: Returning probenames.", __func__);
534 case SR_DI_SAMPLERATES:
536 sr_spew("zp: %s: Returning samplerates.", __func__);
538 case SR_DI_TRIGGER_TYPES:
539 info = TRIGGER_TYPES;
540 sr_spew("zp: %s: Returning triggertypes: %s.", __func__, info);
542 case SR_DI_CUR_SAMPLERATE:
543 info = &ctx->cur_samplerate;
544 sr_spew("zp: %s: Returning samplerate: %" PRIu64 "Hz.",
545 __func__, ctx->cur_samplerate);
548 /* Unknown device info ID, return NULL. */
549 sr_err("zp: %s: Unknown device info ID", __func__);
557 static int hw_dev_status_get(int dev_index)
559 struct sr_dev_inst *sdi;
561 sdi = sr_dev_inst_get(dev_insts, dev_index);
565 return SR_ST_NOT_FOUND;
568 static int *hw_hwcap_get_all(void)
573 static int set_samplerate(struct sr_dev_inst *sdi, uint64_t samplerate)
578 sr_err("zp: %s: sdi was NULL", __func__);
582 if (!(ctx = sdi->priv)) {
583 sr_err("zp: %s: sdi->priv was NULL", __func__);
587 sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate);
589 if (samplerate > SR_MHZ(1))
590 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
591 else if (samplerate > SR_KHZ(1))
592 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
594 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
596 ctx->cur_samplerate = samplerate;
601 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
603 struct sr_dev_inst *sdi;
606 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
607 sr_err("zp: %s: sdi was NULL", __func__);
611 if (!(ctx = sdi->priv)) {
612 sr_err("zp: %s: sdi->priv was NULL", __func__);
617 case SR_HWCAP_SAMPLERATE:
618 return set_samplerate(sdi, *(uint64_t *)value);
619 case SR_HWCAP_PROBECONFIG:
620 return configure_probes(sdi, (GSList *)value);
621 case SR_HWCAP_LIMIT_SAMPLES:
622 ctx->limit_samples = *(uint64_t *)value;
629 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
631 struct sr_dev_inst *sdi;
632 struct sr_datafeed_packet packet;
633 struct sr_datafeed_logic logic;
634 struct sr_datafeed_header header;
635 uint64_t samples_read;
637 unsigned int packet_num;
641 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
642 sr_err("zp: %s: sdi was NULL", __func__);
646 if (!(ctx = sdi->priv)) {
647 sr_err("zp: %s: sdi->priv was NULL", __func__);
651 /* push configured settings to device */
652 analyzer_configure(ctx->usb->devhdl);
654 analyzer_start(ctx->usb->devhdl);
655 sr_info("zp: Waiting for data");
656 analyzer_wait_data(ctx->usb->devhdl);
658 sr_info("zp: Stop address = 0x%x",
659 analyzer_get_stop_address(ctx->usb->devhdl));
660 sr_info("zp: Now address = 0x%x",
661 analyzer_get_now_address(ctx->usb->devhdl));
662 sr_info("zp: Trigger address = 0x%x",
663 analyzer_get_trigger_address(ctx->usb->devhdl));
665 packet.type = SR_DF_HEADER;
666 packet.payload = &header;
667 header.feed_version = 1;
668 gettimeofday(&header.starttime, NULL);
669 header.samplerate = ctx->cur_samplerate;
670 header.num_logic_probes = ctx->num_channels;
671 sr_session_send(cb_data, &packet);
673 if (!(buf = g_try_malloc(PACKET_SIZE))) {
674 sr_err("zp: %s: buf malloc failed", __func__);
675 return SR_ERR_MALLOC;
679 analyzer_read_start(ctx->usb->devhdl);
680 /* Send the incoming transfer to the session bus. */
681 for (packet_num = 0; packet_num < (ctx->memory_size * 4 / PACKET_SIZE);
683 res = analyzer_read_data(ctx->usb->devhdl, buf, PACKET_SIZE);
684 sr_info("zp: Tried to read %llx bytes, actually read %x bytes",
687 packet.type = SR_DF_LOGIC;
688 packet.payload = &logic;
689 logic.length = PACKET_SIZE;
692 sr_session_send(cb_data, &packet);
693 samples_read += res / 4;
695 analyzer_read_stop(ctx->usb->devhdl);
698 packet.type = SR_DF_END;
699 sr_session_send(cb_data, &packet);
704 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
705 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
707 struct sr_datafeed_packet packet;
708 struct sr_dev_inst *sdi;
711 packet.type = SR_DF_END;
712 sr_session_send(cb_data, &packet);
714 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
715 sr_err("zp: %s: sdi was NULL", __func__);
719 if (!(ctx = sdi->priv)) {
720 sr_err("zp: %s: sdi->priv was NULL", __func__);
724 analyzer_reset(ctx->usb->devhdl);
725 /* TODO: Need to cancel and free any queued up transfers. */
730 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
731 .name = "zeroplus-logic-cube",
732 .longname = "Zeroplus Logic Cube LAP-C series",
735 .cleanup = hw_cleanup,
736 .dev_open = hw_dev_open,
737 .dev_close = hw_dev_close,
738 .dev_info_get = hw_dev_info_get,
739 .dev_status_get = hw_dev_status_get,
740 .hwcap_get_all = hw_hwcap_get_all,
741 .dev_config_set = hw_dev_config_set,
742 .dev_acquisition_start = hw_dev_acquisition_start,
743 .dev_acquisition_stop = hw_dev_acquisition_stop,