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/>.
28 #include "libsigrok.h"
29 #include "libsigrok-internal.h"
32 #define USB_VENDOR 0x0c12
34 #define VENDOR_NAME "ZEROPLUS"
35 #define MODEL_NAME "Logic Cube LAP-C"
36 #define MODEL_VERSION NULL
39 #define USB_INTERFACE 0
40 #define USB_CONFIGURATION 1
41 #define NUM_TRIGGER_STAGES 4
42 #define TRIGGER_TYPES "01"
44 #define PACKET_SIZE 2048 /* ?? */
49 unsigned int channels;
50 unsigned int sample_depth; /* In Ksamples/channel */
51 unsigned int max_sampling_freq;
55 * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the
56 * same 128K sample depth.
58 static model_t zeroplus_models[] = {
59 {0x7009, "LAP-C(16064)", 16, 64, 100},
60 {0x700A, "LAP-C(16128)", 16, 128, 200},
61 {0x700B, "LAP-C(32128)", 32, 128, 200},
62 {0x700C, "LAP-C(321000)", 32, 1024, 200},
63 {0x700D, "LAP-C(322000)", 32, 2048, 200},
64 {0x700E, "LAP-C(16032)", 16, 32, 100},
65 {0x7016, "LAP-C(162000)", 16, 2048, 200},
68 static const int hwcaps[] = {
69 SR_HWCAP_LOGIC_ANALYZER,
72 SR_HWCAP_CAPTURE_RATIO,
74 /* These are really implemented in the driver, not the hardware. */
75 SR_HWCAP_LIMIT_SAMPLES,
80 * ZEROPLUS LAP-C (16032) numbers the 16 probes A0-A7 and B0-B7.
81 * We currently ignore other untested/unsupported devices here.
83 static const char *probe_names[NUM_PROBES + 1] = {
103 /* List of struct sr_dev_inst, maintained by dev_open()/dev_close(). */
104 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
105 static struct sr_dev_driver *zdi = &zeroplus_logic_cube_driver_info;
107 static libusb_context *usb_context = NULL;
110 * The hardware supports more samplerates than these, but these are the
111 * options hardcoded into the vendor's Windows GUI.
115 * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up
118 static const uint64_t supported_samplerates[] = {
140 static const struct sr_samplerates samplerates = {
144 supported_samplerates,
147 /* Private, per-device-instance driver context. */
149 uint64_t cur_samplerate;
150 uint64_t limit_samples;
151 int num_channels; /* TODO: This isn't initialized before it's needed :( */
152 uint64_t memory_size;
154 uint8_t trigger_mask[NUM_TRIGGER_STAGES];
155 uint8_t trigger_value[NUM_TRIGGER_STAGES];
156 // uint8_t trigger_buffer[NUM_TRIGGER_STAGES];
158 /* TODO: this belongs in the device instance */
159 struct sr_usb_dev_inst *usb;
162 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
165 static unsigned int get_memory_size(int type)
167 if (type == MEMORY_SIZE_8K)
169 else if (type == MEMORY_SIZE_64K)
171 else if (type == MEMORY_SIZE_128K)
173 else if (type == MEMORY_SIZE_512K)
179 static int opendev4(struct sr_dev_inst **sdi, libusb_device *dev,
180 struct libusb_device_descriptor *des)
186 /* Note: sdi is non-NULL, the caller already checked this. */
188 if (!(ctx = (*sdi)->priv)) {
189 sr_err("zp: %s: (*sdi)->priv was NULL", __func__);
193 if ((ret = libusb_get_device_descriptor(dev, des))) {
194 sr_err("zp: failed to get device descriptor: %d", ret);
198 if (des->idVendor != USB_VENDOR)
201 if (libusb_get_bus_number(dev) == ctx->usb->bus
202 && libusb_get_device_address(dev) == ctx->usb->address) {
204 for (i = 0; i < ARRAY_SIZE(zeroplus_models); i++) {
205 if (!(des->idProduct == zeroplus_models[i].pid))
208 sr_info("zp: Found ZEROPLUS device 0x%04x (%s)",
209 des->idProduct, zeroplus_models[i].model_name);
210 ctx->num_channels = zeroplus_models[i].channels;
211 ctx->memory_size = zeroplus_models[i].sample_depth * 1024;
215 if (ctx->num_channels == 0) {
216 sr_err("zp: Unknown ZEROPLUS device 0x%04x",
222 if (!(ret = libusb_open(dev, &(ctx->usb->devhdl)))) {
223 (*sdi)->status = SR_ST_ACTIVE;
224 sr_info("zp: opened device %d on %d.%d interface %d",
225 (*sdi)->index, ctx->usb->bus,
226 ctx->usb->address, USB_INTERFACE);
228 sr_err("zp: failed to open device: %d", ret);
236 static struct sr_dev_inst *zp_open_dev(int dev_index)
238 struct sr_dev_inst *sdi;
239 libusb_device **devlist;
240 struct libusb_device_descriptor des;
243 if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index)))
246 libusb_get_device_list(usb_context, &devlist);
247 if (sdi->status == SR_ST_INACTIVE) {
248 /* Find the device by vendor, product, bus and address. */
249 libusb_get_device_list(usb_context, &devlist);
250 for (i = 0; devlist[i]; i++) {
251 /* TODO: Error handling. */
252 opendev4(&sdi, devlist[i], &des);
255 /* Status must be SR_ST_ACTIVE, i.e. already in use... */
258 libusb_free_device_list(devlist, 1);
260 if (sdi && sdi->status != SR_ST_ACTIVE)
266 static void close_dev(struct sr_dev_inst *sdi)
270 if (!(ctx = sdi->priv)) {
271 sr_err("zp: %s: sdi->priv was NULL", __func__);
275 if (!ctx->usb->devhdl)
278 sr_info("zp: closing device %d on %d.%d interface %d", sdi->index,
279 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
280 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
281 libusb_reset_device(ctx->usb->devhdl);
282 libusb_close(ctx->usb->devhdl);
283 ctx->usb->devhdl = NULL;
284 /* TODO: Call libusb_exit() here or only in hw_cleanup()? */
285 sdi->status = SR_ST_INACTIVE;
288 static int configure_probes(const struct sr_dev_inst *sdi, const GSList *probes)
291 const struct sr_probe *probe;
293 int probe_bit, stage, i;
296 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
300 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
301 ctx->trigger_mask[i] = 0;
302 ctx->trigger_value[i] = 0;
306 for (l = probes; l; l = l->next) {
307 probe = (struct sr_probe *)l->data;
308 if (probe->enabled == FALSE)
310 probe_bit = 1 << (probe->index - 1);
311 ctx->probe_mask |= probe_bit;
313 if (probe->trigger) {
315 for (tc = probe->trigger; *tc; tc++) {
316 ctx->trigger_mask[stage] |= probe_bit;
318 ctx->trigger_value[stage] |= probe_bit;
320 if (stage > NUM_TRIGGER_STAGES)
329 static void clear_instances(void)
332 struct sr_dev_inst *sdi;
334 for (l = zdi->instances; l; l = l->next) {
336 /* Properly close all devices... */
338 /* ...and free all their memory. */
339 sr_dev_inst_free(sdi);
341 g_slist_free(zdi->instances);
342 zdi->instances = NULL;
350 static int hw_init(void)
353 if (libusb_init(&usb_context) != 0) {
354 sr_err("zp: Failed to initialize USB.");
361 static GSList *hw_scan(GSList *options)
363 struct sr_dev_inst *sdi;
364 struct libusb_device_descriptor des;
366 libusb_device **devlist;
375 /* Allocate memory for our private driver context. */
376 if (!(ctx = g_try_malloc(sizeof(struct context)))) {
377 sr_err("zp: %s: ctx malloc failed", __func__);
381 /* Set some sane defaults. */
382 ctx->cur_samplerate = 0;
383 ctx->limit_samples = 0;
384 /* TODO: num_channels isn't initialized before it's needed :( */
385 ctx->num_channels = NUM_PROBES;
386 ctx->memory_size = 0;
388 memset(ctx->trigger_mask, 0, NUM_TRIGGER_STAGES);
389 memset(ctx->trigger_value, 0, NUM_TRIGGER_STAGES);
390 // memset(ctx->trigger_buffer, 0, NUM_TRIGGER_STAGES);
392 /* Find all ZEROPLUS analyzers and add them to device list. */
394 libusb_get_device_list(usb_context, &devlist); /* TODO: Errors. */
396 for (i = 0; devlist[i]; i++) {
397 ret = libusb_get_device_descriptor(devlist[i], &des);
399 sr_err("zp: failed to get device descriptor: %d", ret);
403 if (des.idVendor == USB_VENDOR) {
405 * Definitely a ZEROPLUS.
406 * TODO: Any way to detect specific model/version in
407 * the ZEROPLUS range?
409 /* Register the device with libsigrok. */
410 if (!(sdi = sr_dev_inst_new(devcnt,
411 SR_ST_INACTIVE, VENDOR_NAME,
412 MODEL_NAME, MODEL_VERSION))) {
413 sr_err("zp: %s: sr_dev_inst_new failed",
420 devices = g_slist_append(devices, sdi);
421 zdi->instances = g_slist_append(zdi->instances, sdi);
422 ctx->usb = sr_usb_dev_inst_new(
423 libusb_get_bus_number(devlist[i]),
424 libusb_get_device_address(devlist[i]), NULL);
428 libusb_free_device_list(devlist, 1);
433 static int hw_dev_open(int dev_index)
435 struct sr_dev_inst *sdi;
439 if (!(sdi = zp_open_dev(dev_index))) {
440 sr_err("zp: unable to open device");
444 /* TODO: Note: sdi is retrieved in zp_open_dev(). */
446 if (!(ctx = sdi->priv)) {
447 sr_err("zp: %s: sdi->priv was NULL", __func__);
451 ret = libusb_set_configuration(ctx->usb->devhdl, USB_CONFIGURATION);
453 sr_err("zp: Unable to set USB configuration %d: %d",
454 USB_CONFIGURATION, ret);
458 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
460 sr_err("zp: Unable to claim interface: %d", ret);
464 analyzer_reset(ctx->usb->devhdl);
465 analyzer_initialize(ctx->usb->devhdl);
467 analyzer_set_memory_size(MEMORY_SIZE_512K);
468 // analyzer_set_freq(g_freq, g_freq_scale);
469 analyzer_set_trigger_count(1);
470 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
471 // * get_memory_size(g_memory_size)) / 100) >> 2);
472 analyzer_set_ramsize_trigger_address(
473 (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
476 if (g_double_mode == 1)
477 analyzer_set_compression(COMPRESSION_DOUBLE);
478 else if (g_compression == 1)
479 analyzer_set_compression(COMPRESSION_ENABLE);
482 analyzer_set_compression(COMPRESSION_NONE);
484 if (ctx->cur_samplerate == 0) {
485 /* Samplerate hasn't been set. Default to the slowest one. */
486 if (hw_dev_config_set(sdi, SR_HWCAP_SAMPLERATE,
487 &samplerates.list[0]) == SR_ERR)
494 static int hw_dev_close(int dev_index)
496 struct sr_dev_inst *sdi;
498 if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index))) {
499 sr_err("zp: %s: sdi was NULL", __func__);
500 return SR_ERR; /* TODO: SR_ERR_ARG? */
509 static int hw_cleanup(void)
515 libusb_exit(usb_context);
521 static int hw_info_get(int info_id, const void **data,
522 const struct sr_dev_inst *sdi)
529 sr_spew("zp: %s: Returning sdi.", __func__);
534 case SR_DI_NUM_PROBES:
537 *data = GINT_TO_POINTER(ctx->num_channels);
538 sr_spew("zp: %s: Returning number of channels: %d.",
539 __func__, ctx->num_channels);
543 case SR_DI_PROBE_NAMES:
545 sr_spew("zp: %s: Returning probenames.", __func__);
547 case SR_DI_SAMPLERATES:
548 *data = &samplerates;
549 sr_spew("zp: %s: Returning samplerates.", __func__);
551 case SR_DI_TRIGGER_TYPES:
552 *data = TRIGGER_TYPES;
553 sr_spew("zp: %s: Returning triggertypes: %s.", __func__, TRIGGER_TYPES);
555 case SR_DI_CUR_SAMPLERATE:
558 *data = &ctx->cur_samplerate;
559 sr_spew("zp: %s: Returning samplerate: %" PRIu64 "Hz.",
560 __func__, ctx->cur_samplerate);
571 static int hw_dev_status_get(int dev_index)
573 struct sr_dev_inst *sdi;
575 sdi = sr_dev_inst_get(zdi->instances, dev_index);
579 return SR_ST_NOT_FOUND;
582 static int set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
587 sr_err("zp: %s: sdi was NULL", __func__);
591 if (!(ctx = sdi->priv)) {
592 sr_err("zp: %s: sdi->priv was NULL", __func__);
596 sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate);
598 if (samplerate > SR_MHZ(1))
599 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
600 else if (samplerate > SR_KHZ(1))
601 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
603 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
605 ctx->cur_samplerate = samplerate;
610 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
615 if (!(ctx = sdi->priv)) {
616 sr_err("zp: %s: sdi->priv was NULL", __func__);
621 case SR_HWCAP_SAMPLERATE:
622 return set_samplerate(sdi, *(const uint64_t *)value);
623 case SR_HWCAP_PROBECONFIG:
624 return configure_probes(sdi, (const GSList *)value);
625 case SR_HWCAP_LIMIT_SAMPLES:
626 ctx->limit_samples = *(const uint64_t *)value;
633 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
635 struct sr_dev_inst *sdi;
636 struct sr_datafeed_packet packet;
637 struct sr_datafeed_logic logic;
638 struct sr_datafeed_header header;
639 struct sr_datafeed_meta_logic meta;
640 uint64_t samples_read;
642 unsigned int packet_num;
646 if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index))) {
647 sr_err("zp: %s: sdi was NULL", __func__);
651 if (!(ctx = sdi->priv)) {
652 sr_err("zp: %s: sdi->priv was NULL", __func__);
656 /* push configured settings to device */
657 analyzer_configure(ctx->usb->devhdl);
659 analyzer_start(ctx->usb->devhdl);
660 sr_info("zp: Waiting for data");
661 analyzer_wait_data(ctx->usb->devhdl);
663 sr_info("zp: Stop address = 0x%x",
664 analyzer_get_stop_address(ctx->usb->devhdl));
665 sr_info("zp: Now address = 0x%x",
666 analyzer_get_now_address(ctx->usb->devhdl));
667 sr_info("zp: Trigger address = 0x%x",
668 analyzer_get_trigger_address(ctx->usb->devhdl));
670 packet.type = SR_DF_HEADER;
671 packet.payload = &header;
672 header.feed_version = 1;
673 gettimeofday(&header.starttime, NULL);
674 sr_session_send(cb_data, &packet);
676 /* Send metadata about the SR_DF_LOGIC packets to come. */
677 packet.type = SR_DF_META_LOGIC;
678 packet.payload = &meta;
679 meta.samplerate = ctx->cur_samplerate;
680 meta.num_probes = ctx->num_channels;
681 sr_session_send(cb_data, &packet);
683 if (!(buf = g_try_malloc(PACKET_SIZE))) {
684 sr_err("zp: %s: buf malloc failed", __func__);
685 return SR_ERR_MALLOC;
689 analyzer_read_start(ctx->usb->devhdl);
690 /* Send the incoming transfer to the session bus. */
691 for (packet_num = 0; packet_num < (ctx->memory_size * 4 / PACKET_SIZE);
693 res = analyzer_read_data(ctx->usb->devhdl, buf, PACKET_SIZE);
694 sr_info("zp: Tried to read %llx bytes, actually read %x bytes",
697 packet.type = SR_DF_LOGIC;
698 packet.payload = &logic;
699 logic.length = PACKET_SIZE;
702 sr_session_send(cb_data, &packet);
703 samples_read += res / 4;
705 analyzer_read_stop(ctx->usb->devhdl);
708 packet.type = SR_DF_END;
709 sr_session_send(cb_data, &packet);
714 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
715 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
717 struct sr_datafeed_packet packet;
718 struct sr_dev_inst *sdi;
721 packet.type = SR_DF_END;
722 sr_session_send(cb_data, &packet);
724 if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index))) {
725 sr_err("zp: %s: sdi was NULL", __func__);
729 if (!(ctx = sdi->priv)) {
730 sr_err("zp: %s: sdi->priv was NULL", __func__);
734 analyzer_reset(ctx->usb->devhdl);
735 /* TODO: Need to cancel and free any queued up transfers. */
740 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
741 .name = "zeroplus-logic-cube",
742 .longname = "ZEROPLUS Logic Cube LAP-C series",
745 .cleanup = hw_cleanup,
747 .dev_open = hw_dev_open,
748 .dev_close = hw_dev_close,
749 .info_get = hw_info_get,
750 .dev_status_get = hw_dev_status_get,
751 .dev_config_set = hw_dev_config_set,
752 .dev_acquisition_start = hw_dev_acquisition_start,
753 .dev_acquisition_stop = hw_dev_acquisition_stop,