2 * This file is part of the sigrok project.
4 * Copyright (C) 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 ""
37 #define USB_INTERFACE 0
38 #define USB_CONFIGURATION 1
39 #define NUM_TRIGGER_STAGES 4
40 #define TRIGGER_TYPES "01"
42 #define PACKET_SIZE 2048 /* ?? */
47 unsigned int channels;
48 unsigned int sample_depth; /* In Ksamples/channel */
49 unsigned int max_sampling_freq;
53 * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the
54 * same 128K sample depth.
56 static model_t zeroplus_models[] = {
57 {0x7009, "LAP-C(16064)", 16, 64, 100},
58 {0x700A, "LAP-C(16128)", 16, 128, 200},
59 {0x700B, "LAP-C(32128)", 32, 128, 200},
60 {0x700C, "LAP-C(321000)", 32, 1024, 200},
61 {0x700D, "LAP-C(322000)", 32, 2048, 200},
62 {0x700E, "LAP-C(16032)", 16, 32, 100},
63 {0x7016, "LAP-C(162000)", 16, 2048, 200},
66 static int capabilities[] = {
67 SR_HWCAP_LOGIC_ANALYZER,
70 SR_HWCAP_CAPTURE_RATIO,
72 /* These are really implemented in the driver, not the hardware. */
73 SR_HWCAP_LIMIT_SAMPLES,
77 static const char *probe_names[] = {
113 /* List of struct sr_device_instance, maintained by opendev()/closedev(). */
114 static GSList *device_instances = NULL;
116 static libusb_context *usb_context = NULL;
119 * The hardware supports more samplerates than these, but these are the
120 * options hardcoded into the vendor's Windows GUI.
124 * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up
127 static uint64_t supported_samplerates[] = {
149 static struct sr_samplerates samplerates = {
153 supported_samplerates,
157 uint64_t cur_samplerate;
158 uint64_t limit_samples;
159 int num_channels; /* TODO: This isn't initialized before it's needed :( */
160 uint64_t memory_size;
162 uint8_t trigger_mask[NUM_TRIGGER_STAGES];
163 uint8_t trigger_value[NUM_TRIGGER_STAGES];
164 // uint8_t trigger_buffer[NUM_TRIGGER_STAGES];
166 struct sr_usb_device_instance *usb;
169 static int hw_set_configuration(int device_index, int capability, void *value);
171 static unsigned int get_memory_size(int type)
173 if (type == MEMORY_SIZE_8K)
175 else if (type == MEMORY_SIZE_64K)
177 else if (type == MEMORY_SIZE_128K)
179 else if (type == MEMORY_SIZE_512K)
185 static int opendev4(struct sr_device_instance **sdi, libusb_device *dev,
186 struct libusb_device_descriptor *des)
192 /* Note: sdi is non-NULL, the caller already checked this. */
194 if (!(zp = (*sdi)->priv)) {
195 sr_err("zp: %s: (*sdi)->priv was NULL", __func__);
199 if ((err = libusb_get_device_descriptor(dev, des))) {
200 sr_warn("failed to get device descriptor: %d", err);
204 if (des->idVendor != USB_VENDOR)
207 if (libusb_get_bus_number(dev) == zp->usb->bus
208 && libusb_get_device_address(dev) == zp->usb->address) {
210 for (i = 0; i < ARRAY_SIZE(zeroplus_models); i++) {
211 if (!(des->idProduct == zeroplus_models[i].pid))
214 sr_info("Found PID=%04X (%s)", des->idProduct,
215 zeroplus_models[i].model_name);
216 zp->num_channels = zeroplus_models[i].channels;
217 zp->memory_size = zeroplus_models[i].sample_depth * 1024;
221 if (zp->num_channels == 0) {
222 sr_warn("Unknown ZeroPlus device %04X", des->idProduct);
227 if (!(err = libusb_open(dev, &(zp->usb->devhdl)))) {
228 (*sdi)->status = SR_ST_ACTIVE;
229 sr_info("opened device %d on %d.%d interface %d",
230 (*sdi)->index, zp->usb->bus,
231 zp->usb->address, USB_INTERFACE);
233 sr_warn("failed to open device: %d", err);
241 static struct sr_device_instance *zp_open_device(int device_index)
243 struct sr_device_instance *sdi;
244 libusb_device **devlist;
245 struct libusb_device_descriptor des;
248 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
251 libusb_get_device_list(usb_context, &devlist);
252 if (sdi->status == SR_ST_INACTIVE) {
253 /* Find the device by vendor, product, bus and address. */
254 libusb_get_device_list(usb_context, &devlist);
255 for (i = 0; devlist[i]; i++) {
256 /* TODO: Error handling. */
257 err = opendev4(&sdi, devlist[i], &des);
260 /* Status must be SR_ST_ACTIVE, i.e. already in use... */
263 libusb_free_device_list(devlist, 1);
265 if (sdi && sdi->status != SR_ST_ACTIVE)
271 static void close_device(struct sr_device_instance *sdi)
275 if (!(zp = sdi->priv)) {
276 sr_err("zp: %s: sdi->priv was NULL", __func__);
280 if (!zp->usb->devhdl)
283 sr_info("closing device %d on %d.%d interface %d", sdi->index,
284 zp->usb->bus, zp->usb->address, USB_INTERFACE);
285 libusb_release_interface(zp->usb->devhdl, USB_INTERFACE);
286 libusb_close(zp->usb->devhdl);
287 zp->usb->devhdl = NULL;
288 sdi->status = SR_ST_INACTIVE;
291 static int configure_probes(struct sr_device_instance *sdi, GSList *probes)
294 struct sr_probe *probe;
296 int probe_bit, stage, i;
299 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
303 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
304 zp->trigger_mask[i] = 0;
305 zp->trigger_value[i] = 0;
309 for (l = probes; l; l = l->next) {
310 probe = (struct sr_probe *)l->data;
311 if (probe->enabled == FALSE)
313 probe_bit = 1 << (probe->index - 1);
314 zp->probe_mask |= probe_bit;
316 if (probe->trigger) {
318 for (tc = probe->trigger; *tc; tc++) {
319 zp->trigger_mask[stage] |= probe_bit;
321 zp->trigger_value[stage] |= probe_bit;
323 if (stage > NUM_TRIGGER_STAGES)
336 static int hw_init(const char *deviceinfo)
338 struct sr_device_instance *sdi;
339 struct libusb_device_descriptor des;
340 libusb_device **devlist;
344 /* Avoid compiler warnings. */
347 /* Allocate memory for our private driver context. */
348 if (!(zp = g_try_malloc(sizeof(struct zp)))) {
349 sr_err("zp: %s: struct zp malloc failed", __func__);
353 /* Set some sane defaults. */
354 zp->cur_samplerate = 0;
355 zp->limit_samples = 0;
356 zp->num_channels = 32; /* TODO: This isn't initialized before it's needed :( */
359 memset(zp->trigger_mask, 0, NUM_TRIGGER_STAGES);
360 memset(zp->trigger_value, 0, NUM_TRIGGER_STAGES);
361 // memset(zp->trigger_buffer, 0, NUM_TRIGGER_STAGES);
363 if (libusb_init(&usb_context) != 0) {
364 sr_warn("Failed to initialize USB.");
368 /* Find all ZeroPlus analyzers and add them to device list. */
370 libusb_get_device_list(usb_context, &devlist);
372 for (i = 0; devlist[i]; i++) {
373 err = libusb_get_device_descriptor(devlist[i], &des);
375 sr_warn("failed to get device descriptor: %d", err);
379 if (des.idVendor == USB_VENDOR) {
381 * Definitely a Zeroplus.
382 * TODO: Any way to detect specific model/version in
383 * the zeroplus range?
385 /* Register the device with libsigrok. */
386 sdi = sr_device_instance_new(devcnt,
387 SR_ST_INACTIVE, USB_VENDOR_NAME,
388 USB_MODEL_NAME, USB_MODEL_VERSION);
390 sr_err("zp: %s: sr_device_instance_new failed",
398 g_slist_append(device_instances, sdi);
399 zp->usb = sr_usb_device_instance_new(
400 libusb_get_bus_number(devlist[i]),
401 libusb_get_device_address(devlist[i]), NULL);
405 libusb_free_device_list(devlist, 1);
410 static int hw_opendev(int device_index)
412 struct sr_device_instance *sdi;
416 if (!(sdi = zp_open_device(device_index))) {
417 sr_warn("unable to open device");
421 /* TODO: Note: sdi is retrieved in zp_open_device(). */
423 if (!(zp = sdi->priv)) {
424 sr_err("zp: %s: sdi->priv was NULL", __func__);
428 err = libusb_claim_interface(zp->usb->devhdl, USB_INTERFACE);
430 sr_warn("Unable to claim interface: %d", err);
433 analyzer_reset(zp->usb->devhdl);
434 analyzer_initialize(zp->usb->devhdl);
436 analyzer_set_memory_size(MEMORY_SIZE_512K);
437 // analyzer_set_freq(g_freq, g_freq_scale);
438 analyzer_set_trigger_count(1);
439 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
440 // * get_memory_size(g_memory_size)) / 100) >> 2);
441 analyzer_set_ramsize_trigger_address(
442 (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
445 if (g_double_mode == 1)
446 analyzer_set_compression(COMPRESSION_DOUBLE);
447 else if (g_compression == 1)
448 analyzer_set_compression(COMPRESSION_ENABLE);
451 analyzer_set_compression(COMPRESSION_NONE);
453 if (zp->cur_samplerate == 0) {
454 /* Samplerate hasn't been set. Default to the slowest one. */
455 if (hw_set_configuration(device_index, SR_HWCAP_SAMPLERATE,
456 &samplerates.list[0]) == SR_ERR)
463 static int hw_closedev(int device_index)
465 struct sr_device_instance *sdi;
467 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
468 sr_err("zp: %s: sdi was NULL", __func__);
469 return SR_ERR; /* TODO: SR_ERR_ARG? */
478 static void hw_cleanup(void)
482 /* Properly close all devices... */
483 for (l = device_instances; l; l = l->next)
484 close_device((struct sr_device_instance *)l->data);
486 /* ...and free all their memory. */
487 for (l = device_instances; l; l = l->next)
489 g_slist_free(device_instances);
490 device_instances = NULL;
493 libusb_exit(usb_context);
497 static void *hw_get_device_info(int device_index, int device_info_id)
499 struct sr_device_instance *sdi;
503 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
504 sr_err("zp: %s: sdi was NULL", __func__);
508 if (!(zp = sdi->priv)) {
509 sr_err("zp: %s: sdi->priv was NULL", __func__);
513 switch (device_info_id) {
517 case SR_DI_NUM_PROBES:
518 info = GINT_TO_POINTER(zp->num_channels);
520 case SR_DI_PROBE_NAMES:
523 case SR_DI_SAMPLERATES:
526 case SR_DI_TRIGGER_TYPES:
527 info = TRIGGER_TYPES;
529 case SR_DI_CUR_SAMPLERATE:
530 info = &zp->cur_samplerate;
533 /* Unknown device info ID, return NULL. */
534 sr_err("zp: %s: Unknown device info ID", __func__);
542 static int hw_get_status(int device_index)
544 struct sr_device_instance *sdi;
546 sdi = sr_get_device_instance(device_instances, device_index);
550 return SR_ST_NOT_FOUND;
553 static int *hw_get_capabilities(void)
558 static int set_configuration_samplerate(struct sr_device_instance *sdi,
564 sr_err("zp: %s: sdi was NULL", __func__);
568 if (!(zp = sdi->priv)) {
569 sr_err("zp: %s: sdi->priv was NULL", __func__);
573 sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate);
575 if (samplerate > SR_MHZ(1))
576 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
577 else if (samplerate > SR_KHZ(1))
578 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
580 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
582 zp->cur_samplerate = samplerate;
587 static int hw_set_configuration(int device_index, int capability, void *value)
589 struct sr_device_instance *sdi;
593 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
594 sr_err("zp: %s: sdi was NULL", __func__);
598 if (!(zp = sdi->priv)) {
599 sr_err("zp: %s: sdi->priv was NULL", __func__);
603 switch (capability) {
604 case SR_HWCAP_SAMPLERATE:
606 return set_configuration_samplerate(sdi, *tmp_u64);
607 case SR_HWCAP_PROBECONFIG:
608 return configure_probes(sdi, (GSList *)value);
609 case SR_HWCAP_LIMIT_SAMPLES:
611 zp->limit_samples = *tmp_u64;
618 static int hw_start_acquisition(int device_index, gpointer session_data)
620 struct sr_device_instance *sdi;
621 struct sr_datafeed_packet packet;
622 struct sr_datafeed_logic logic;
623 struct sr_datafeed_header header;
624 uint64_t samples_read;
626 unsigned int packet_num;
630 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
631 sr_err("zp: %s: sdi was NULL", __func__);
635 if (!(zp = sdi->priv)) {
636 sr_err("zp: %s: sdi->priv was NULL", __func__);
640 /* push configured settings to device */
641 analyzer_configure(zp->usb->devhdl);
643 analyzer_start(zp->usb->devhdl);
644 sr_info("Waiting for data");
645 analyzer_wait_data(zp->usb->devhdl);
647 sr_info("Stop address = 0x%x", analyzer_get_stop_address(zp->usb->devhdl));
648 sr_info("Now address = 0x%x", analyzer_get_now_address(zp->usb->devhdl));
649 sr_info("Trigger address = 0x%x", analyzer_get_trigger_address(zp->usb->devhdl));
651 packet.type = SR_DF_HEADER;
652 packet.payload = &header;
653 header.feed_version = 1;
654 gettimeofday(&header.starttime, NULL);
655 header.samplerate = zp->cur_samplerate;
656 header.num_logic_probes = zp->num_channels;
657 header.num_analog_probes = 0;
658 sr_session_bus(session_data, &packet);
660 if (!(buf = g_try_malloc(PACKET_SIZE))) {
661 sr_err("zp: %s: buf malloc failed", __func__);
662 return SR_ERR_MALLOC;
666 analyzer_read_start(zp->usb->devhdl);
667 /* Send the incoming transfer to the session bus. */
668 for (packet_num = 0; packet_num < (zp->memory_size * 4 / PACKET_SIZE);
670 res = analyzer_read_data(zp->usb->devhdl, buf, PACKET_SIZE);
671 sr_info("Tried to read %llx bytes, actually read %x bytes",
674 packet.type = SR_DF_LOGIC;
675 packet.payload = &logic;
676 logic.length = PACKET_SIZE;
679 sr_session_bus(session_data, &packet);
680 samples_read += res / 4;
682 analyzer_read_stop(zp->usb->devhdl);
685 packet.type = SR_DF_END;
686 sr_session_bus(session_data, &packet);
691 /* This stops acquisition on ALL devices, ignoring device_index. */
692 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
694 struct sr_datafeed_packet packet;
695 struct sr_device_instance *sdi;
698 packet.type = SR_DF_END;
699 sr_session_bus(session_device_id, &packet);
701 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
702 sr_err("zp: %s: sdi was NULL", __func__);
706 if (!(zp = sdi->priv)) {
707 sr_err("zp: %s: sdi->priv was NULL", __func__);
711 analyzer_reset(zp->usb->devhdl);
712 /* TODO: Need to cancel and free any queued up transfers. */
715 struct sr_device_plugin zeroplus_logic_cube_plugin_info = {
716 .name = "zeroplus-logic-cube",
717 .longname = "Zeroplus Logic Cube LAP-C series",
720 .cleanup = hw_cleanup,
721 .opendev = hw_opendev,
722 .closedev = hw_closedev,
723 .get_device_info = hw_get_device_info,
724 .get_status = hw_get_status,
725 .get_capabilities = hw_get_capabilities,
726 .set_configuration = hw_set_configuration,
727 .start_acquisition = hw_start_acquisition,
728 .stop_acquisition = hw_stop_acquisition,