2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 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;
159 uint64_t limit_samples;
160 int num_channels; /* TODO: This isn't initialized before it's needed :( */
161 uint64_t memory_size;
163 uint8_t trigger_mask[NUM_TRIGGER_STAGES];
164 uint8_t trigger_value[NUM_TRIGGER_STAGES];
165 // uint8_t trigger_buffer[NUM_TRIGGER_STAGES];
167 struct sr_usb_device_instance *usb;
170 static int hw_set_configuration(int device_index, int capability, void *value);
172 static unsigned int get_memory_size(int type)
174 if (type == MEMORY_SIZE_8K)
176 else if (type == MEMORY_SIZE_64K)
178 else if (type == MEMORY_SIZE_128K)
180 else if (type == MEMORY_SIZE_512K)
186 static int opendev4(struct sr_device_instance **sdi, libusb_device *dev,
187 struct libusb_device_descriptor *des)
193 /* Note: sdi is non-NULL, the caller already checked this. */
195 if (!(zp = (*sdi)->priv)) {
196 sr_err("zp: %s: (*sdi)->priv was NULL", __func__);
200 if ((err = libusb_get_device_descriptor(dev, des))) {
201 sr_warn("failed to get device descriptor: %d", err);
205 if (des->idVendor != USB_VENDOR)
208 if (libusb_get_bus_number(dev) == zp->usb->bus
209 && libusb_get_device_address(dev) == zp->usb->address) {
211 for (i = 0; i < ARRAY_SIZE(zeroplus_models); i++) {
212 if (!(des->idProduct == zeroplus_models[i].pid))
215 sr_info("Found PID=%04X (%s)", des->idProduct,
216 zeroplus_models[i].model_name);
217 zp->num_channels = zeroplus_models[i].channels;
218 zp->memory_size = zeroplus_models[i].sample_depth * 1024;
222 if (zp->num_channels == 0) {
223 sr_warn("Unknown ZeroPlus device %04X", des->idProduct);
228 if (!(err = libusb_open(dev, &(zp->usb->devhdl)))) {
229 (*sdi)->status = SR_ST_ACTIVE;
230 sr_info("opened device %d on %d.%d interface %d",
231 (*sdi)->index, zp->usb->bus,
232 zp->usb->address, USB_INTERFACE);
234 sr_warn("failed to open device: %d", err);
242 static struct sr_device_instance *zp_open_device(int device_index)
244 struct sr_device_instance *sdi;
245 libusb_device **devlist;
246 struct libusb_device_descriptor des;
249 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
252 libusb_get_device_list(usb_context, &devlist);
253 if (sdi->status == SR_ST_INACTIVE) {
254 /* Find the device by vendor, product, bus and address. */
255 libusb_get_device_list(usb_context, &devlist);
256 for (i = 0; devlist[i]; i++) {
257 /* TODO: Error handling. */
258 err = opendev4(&sdi, devlist[i], &des);
261 /* Status must be SR_ST_ACTIVE, i.e. already in use... */
264 libusb_free_device_list(devlist, 1);
266 if (sdi && sdi->status != SR_ST_ACTIVE)
272 static void close_device(struct sr_device_instance *sdi)
276 if (!(zp = sdi->priv)) {
277 sr_err("zp: %s: sdi->priv was NULL", __func__);
281 if (!zp->usb->devhdl)
284 sr_info("closing device %d on %d.%d interface %d", sdi->index,
285 zp->usb->bus, zp->usb->address, USB_INTERFACE);
286 libusb_release_interface(zp->usb->devhdl, USB_INTERFACE);
287 libusb_close(zp->usb->devhdl);
288 zp->usb->devhdl = NULL;
289 sdi->status = SR_ST_INACTIVE;
292 static int configure_probes(struct sr_device_instance *sdi, GSList *probes)
295 struct sr_probe *probe;
297 int probe_bit, stage, i;
300 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
304 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
305 zp->trigger_mask[i] = 0;
306 zp->trigger_value[i] = 0;
310 for (l = probes; l; l = l->next) {
311 probe = (struct sr_probe *)l->data;
312 if (probe->enabled == FALSE)
314 probe_bit = 1 << (probe->index - 1);
315 zp->probe_mask |= probe_bit;
317 if (probe->trigger) {
319 for (tc = probe->trigger; *tc; tc++) {
320 zp->trigger_mask[stage] |= probe_bit;
322 zp->trigger_value[stage] |= probe_bit;
324 if (stage > NUM_TRIGGER_STAGES)
337 static int hw_init(const char *deviceinfo)
339 struct sr_device_instance *sdi;
340 struct libusb_device_descriptor des;
341 libusb_device **devlist;
345 /* Avoid compiler warnings. */
348 /* Allocate memory for our private driver context. */
349 if (!(zp = g_try_malloc(sizeof(struct zp)))) {
350 sr_err("zp: %s: struct zp malloc failed", __func__);
354 /* Set some sane defaults. */
355 zp->cur_samplerate = 0;
357 zp->limit_samples = 0;
358 zp->num_channels = 32; /* TODO: This isn't initialized before it's needed :( */
361 memset(zp->trigger_mask, 0, NUM_TRIGGER_STAGES);
362 memset(zp->trigger_value, 0, NUM_TRIGGER_STAGES);
363 // memset(zp->trigger_buffer, 0, NUM_TRIGGER_STAGES);
365 if (libusb_init(&usb_context) != 0) {
366 sr_warn("Failed to initialize USB.");
370 /* Find all ZeroPlus analyzers and add them to device list. */
372 libusb_get_device_list(usb_context, &devlist);
374 for (i = 0; devlist[i]; i++) {
375 err = libusb_get_device_descriptor(devlist[i], &des);
377 sr_warn("failed to get device descriptor: %d", err);
381 if (des.idVendor == USB_VENDOR) {
383 * Definitely a Zeroplus.
384 * TODO: Any way to detect specific model/version in
385 * the zeroplus range?
387 /* Register the device with libsigrok. */
388 sdi = sr_device_instance_new(devcnt,
389 SR_ST_INACTIVE, USB_VENDOR_NAME,
390 USB_MODEL_NAME, USB_MODEL_VERSION);
392 sr_err("zp: %s: sr_device_instance_new failed",
400 g_slist_append(device_instances, sdi);
401 zp->usb = sr_usb_device_instance_new(
402 libusb_get_bus_number(devlist[i]),
403 libusb_get_device_address(devlist[i]), NULL);
407 libusb_free_device_list(devlist, 1);
412 static int hw_opendev(int device_index)
414 struct sr_device_instance *sdi;
418 if (!(sdi = zp_open_device(device_index))) {
419 sr_warn("unable to open device");
423 /* TODO: Note: sdi is retrieved in zp_open_device(). */
425 if (!(zp = sdi->priv)) {
426 sr_err("zp: %s: sdi->priv was NULL", __func__);
430 err = libusb_claim_interface(zp->usb->devhdl, USB_INTERFACE);
432 sr_warn("Unable to claim interface: %d", err);
435 analyzer_reset(zp->usb->devhdl);
436 analyzer_initialize(zp->usb->devhdl);
438 analyzer_set_memory_size(MEMORY_SIZE_512K);
439 // analyzer_set_freq(g_freq, g_freq_scale);
440 analyzer_set_trigger_count(1);
441 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
442 // * get_memory_size(g_memory_size)) / 100) >> 2);
443 analyzer_set_ramsize_trigger_address(
444 (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
447 if (g_double_mode == 1)
448 analyzer_set_compression(COMPRESSION_DOUBLE);
449 else if (g_compression == 1)
450 analyzer_set_compression(COMPRESSION_ENABLE);
453 analyzer_set_compression(COMPRESSION_NONE);
455 if (zp->cur_samplerate == 0) {
456 /* Samplerate hasn't been set. Default to the slowest one. */
457 if (hw_set_configuration(device_index, SR_HWCAP_SAMPLERATE,
458 &samplerates.list[0]) == SR_ERR)
465 static int hw_closedev(int device_index)
467 struct sr_device_instance *sdi;
469 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
470 sr_err("zp: %s: sdi was NULL", __func__);
471 return SR_ERR; /* TODO: SR_ERR_ARG? */
480 static void hw_cleanup(void)
484 /* Properly close all devices... */
485 for (l = device_instances; l; l = l->next)
486 close_device((struct sr_device_instance *)l->data);
488 /* ...and free all their memory. */
489 for (l = device_instances; l; l = l->next)
491 g_slist_free(device_instances);
492 device_instances = NULL;
495 libusb_exit(usb_context);
499 static void *hw_get_device_info(int device_index, int device_info_id)
501 struct sr_device_instance *sdi;
505 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
506 sr_err("zp: %s: sdi was NULL", __func__);
510 if (!(zp = sdi->priv)) {
511 sr_err("zp: %s: sdi->priv was NULL", __func__);
515 switch (device_info_id) {
519 case SR_DI_NUM_PROBES:
520 info = GINT_TO_POINTER(zp->num_channels);
522 case SR_DI_PROBE_NAMES:
525 case SR_DI_SAMPLERATES:
528 case SR_DI_TRIGGER_TYPES:
529 info = TRIGGER_TYPES;
531 case SR_DI_CUR_SAMPLERATE:
532 info = &zp->cur_samplerate;
535 /* Unknown device info ID, return NULL. */
536 sr_err("zp: %s: Unknown device info ID", __func__);
544 static int hw_get_status(int device_index)
546 struct sr_device_instance *sdi;
548 sdi = sr_get_device_instance(device_instances, device_index);
552 return SR_ST_NOT_FOUND;
555 static int *hw_get_capabilities(void)
560 static int set_configuration_samplerate(struct sr_device_instance *sdi,
566 sr_err("zp: %s: sdi was NULL", __func__);
570 if (!(zp = sdi->priv)) {
571 sr_err("zp: %s: sdi->priv was NULL", __func__);
575 sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate);
577 if (samplerate > SR_MHZ(1))
578 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
579 else if (samplerate > SR_KHZ(1))
580 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
582 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
584 zp->cur_samplerate = samplerate;
585 zp->period_ps = 1000000000000 / samplerate;
590 static int hw_set_configuration(int device_index, int capability, void *value)
592 struct sr_device_instance *sdi;
596 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
597 sr_err("zp: %s: sdi was NULL", __func__);
601 if (!(zp = sdi->priv)) {
602 sr_err("zp: %s: sdi->priv was NULL", __func__);
606 switch (capability) {
607 case SR_HWCAP_SAMPLERATE:
609 return set_configuration_samplerate(sdi, *tmp_u64);
610 case SR_HWCAP_PROBECONFIG:
611 return configure_probes(sdi, (GSList *)value);
612 case SR_HWCAP_LIMIT_SAMPLES:
614 zp->limit_samples = *tmp_u64;
621 static int hw_start_acquisition(int device_index, gpointer session_data)
623 struct sr_device_instance *sdi;
624 struct sr_datafeed_packet packet;
625 struct sr_datafeed_logic logic;
626 struct sr_datafeed_header header;
627 uint64_t samples_read;
629 unsigned int packet_num;
633 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
634 sr_err("zp: %s: sdi was NULL", __func__);
638 if (!(zp = sdi->priv)) {
639 sr_err("zp: %s: sdi->priv was NULL", __func__);
643 /* push configured settings to device */
644 analyzer_configure(zp->usb->devhdl);
646 analyzer_start(zp->usb->devhdl);
647 sr_info("Waiting for data");
648 analyzer_wait_data(zp->usb->devhdl);
650 sr_info("Stop address = 0x%x", analyzer_get_stop_address(zp->usb->devhdl));
651 sr_info("Now address = 0x%x", analyzer_get_now_address(zp->usb->devhdl));
652 sr_info("Trigger address = 0x%x", analyzer_get_trigger_address(zp->usb->devhdl));
654 packet.type = SR_DF_HEADER;
655 packet.payload = &header;
656 header.feed_version = 1;
657 gettimeofday(&header.starttime, NULL);
658 header.samplerate = zp->cur_samplerate;
659 header.num_logic_probes = zp->num_channels;
660 header.num_analog_probes = 0;
661 sr_session_bus(session_data, &packet);
663 if (!(buf = g_try_malloc(PACKET_SIZE))) {
664 sr_err("zp: %s: buf malloc failed", __func__);
665 return SR_ERR_MALLOC;
669 analyzer_read_start(zp->usb->devhdl);
670 /* Send the incoming transfer to the session bus. */
671 for (packet_num = 0; packet_num < (zp->memory_size * 4 / PACKET_SIZE);
673 res = analyzer_read_data(zp->usb->devhdl, buf, PACKET_SIZE);
674 sr_info("Tried to read %llx bytes, actually read %x bytes",
677 packet.type = SR_DF_LOGIC;
678 packet.timeoffset = samples_read * zp->period_ps;
679 packet.duration = res / 4 * zp->period_ps;
680 packet.payload = &logic;
681 logic.length = PACKET_SIZE;
684 sr_session_bus(session_data, &packet);
685 samples_read += res / 4;
687 analyzer_read_stop(zp->usb->devhdl);
690 packet.type = SR_DF_END;
691 sr_session_bus(session_data, &packet);
696 /* This stops acquisition on ALL devices, ignoring device_index. */
697 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
699 struct sr_datafeed_packet packet;
700 struct sr_device_instance *sdi;
703 packet.type = SR_DF_END;
704 sr_session_bus(session_device_id, &packet);
706 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
707 sr_err("zp: %s: sdi was NULL", __func__);
711 if (!(zp = sdi->priv)) {
712 sr_err("zp: %s: sdi->priv was NULL", __func__);
716 analyzer_reset(zp->usb->devhdl);
717 /* TODO: Need to cancel and free any queued up transfers. */
720 struct sr_device_plugin zeroplus_logic_cube_plugin_info = {
721 .name = "zeroplus-logic-cube",
722 .longname = "Zeroplus Logic Cube LAP-C series",
725 .cleanup = hw_cleanup,
726 .opendev = hw_opendev,
727 .closedev = hw_closedev,
728 .get_device_info = hw_get_device_info,
729 .get_status = hw_get_status,
730 .get_capabilities = hw_get_capabilities,
731 .set_configuration = hw_set_configuration,
732 .start_acquisition = hw_start_acquisition,
733 .stop_acquisition = hw_stop_acquisition,