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];
168 static int hw_set_configuration(int device_index, int capability, void *value);
170 static unsigned int get_memory_size(int type)
172 if (type == MEMORY_SIZE_8K)
174 else if (type == MEMORY_SIZE_64K)
176 else if (type == MEMORY_SIZE_128K)
178 else if (type == MEMORY_SIZE_512K)
184 static int opendev4(struct sr_device_instance **sdi, libusb_device *dev,
185 struct libusb_device_descriptor *des)
191 /* Note: sdi is non-NULL, the caller already checked this. */
193 if (!(zp = (*sdi)->priv)) {
194 sr_err("zp: %s: (*sdi)->priv was NULL", __func__);
198 if ((err = libusb_get_device_descriptor(dev, des))) {
199 sr_warn("failed to get device descriptor: %d", err);
203 if (des->idVendor != USB_VENDOR)
206 if (libusb_get_bus_number(dev) == (*sdi)->usb->bus
207 && libusb_get_device_address(dev) == (*sdi)->usb->address) {
209 for (i = 0; i < ARRAY_SIZE(zeroplus_models); i++) {
210 if (!(des->idProduct == zeroplus_models[i].pid))
213 sr_info("Found PID=%04X (%s)", des->idProduct,
214 zeroplus_models[i].model_name);
215 zp->num_channels = zeroplus_models[i].channels;
216 zp->memory_size = zeroplus_models[i].sample_depth * 1024;
220 if (zp->num_channels == 0) {
221 sr_warn("Unknown ZeroPlus device %04X", des->idProduct);
226 if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) {
227 (*sdi)->status = SR_ST_ACTIVE;
228 sr_info("opened device %d on %d.%d interface %d",
229 (*sdi)->index, (*sdi)->usb->bus,
230 (*sdi)->usb->address, USB_INTERFACE);
232 sr_warn("failed to open device: %d", err);
240 static struct sr_device_instance *zp_open_device(int device_index)
242 struct sr_device_instance *sdi;
243 libusb_device **devlist;
244 struct libusb_device_descriptor des;
247 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
250 libusb_get_device_list(usb_context, &devlist);
251 if (sdi->status == SR_ST_INACTIVE) {
252 /* Find the device by vendor, product, bus and address. */
253 libusb_get_device_list(usb_context, &devlist);
254 for (i = 0; devlist[i]; i++) {
255 /* TODO: Error handling. */
256 err = opendev4(&sdi, devlist[i], &des);
259 /* Status must be SR_ST_ACTIVE, i.e. already in use... */
262 libusb_free_device_list(devlist, 1);
264 if (sdi && sdi->status != SR_ST_ACTIVE)
270 static void close_device(struct sr_device_instance *sdi)
272 if (!sdi->usb->devhdl)
275 sr_info("closing device %d on %d.%d interface %d", sdi->index,
276 sdi->usb->bus, sdi->usb->address, USB_INTERFACE);
277 libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE);
278 libusb_close(sdi->usb->devhdl);
279 sdi->usb->devhdl = NULL;
280 sdi->status = SR_ST_INACTIVE;
283 static int configure_probes(struct sr_device_instance *sdi, GSList *probes)
286 struct sr_probe *probe;
288 int probe_bit, stage, i;
291 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
295 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
296 zp->trigger_mask[i] = 0;
297 zp->trigger_value[i] = 0;
301 for (l = probes; l; l = l->next) {
302 probe = (struct sr_probe *)l->data;
303 if (probe->enabled == FALSE)
305 probe_bit = 1 << (probe->index - 1);
306 zp->probe_mask |= probe_bit;
308 if (probe->trigger) {
310 for (tc = probe->trigger; *tc; tc++) {
311 zp->trigger_mask[stage] |= probe_bit;
313 zp->trigger_value[stage] |= probe_bit;
315 if (stage > NUM_TRIGGER_STAGES)
328 static int hw_init(const char *deviceinfo)
330 struct sr_device_instance *sdi;
331 struct libusb_device_descriptor des;
332 libusb_device **devlist;
336 /* Avoid compiler warnings. */
339 /* Allocate memory for our private driver context. */
340 if (!(zp = g_try_malloc(sizeof(struct zp)))) {
341 sr_err("zp: %s: struct zp malloc failed", __func__);
345 /* Set some sane defaults. */
346 zp->cur_samplerate = 0;
348 zp->limit_samples = 0;
349 zp->num_channels = 32; /* TODO: This isn't initialized before it's needed :( */
352 memset(zp->trigger_mask, 0, NUM_TRIGGER_STAGES);
353 memset(zp->trigger_value, 0, NUM_TRIGGER_STAGES);
354 // memset(zp->trigger_buffer, 0, NUM_TRIGGER_STAGES);
356 if (libusb_init(&usb_context) != 0) {
357 sr_warn("Failed to initialize USB.");
361 /* Find all ZeroPlus analyzers and add them to device list. */
363 libusb_get_device_list(usb_context, &devlist);
365 for (i = 0; devlist[i]; i++) {
366 err = libusb_get_device_descriptor(devlist[i], &des);
368 sr_warn("failed to get device descriptor: %d", err);
372 if (des.idVendor == USB_VENDOR) {
374 * Definitely a Zeroplus.
375 * TODO: Any way to detect specific model/version in
376 * the zeroplus range?
378 /* Register the device with libsigrok. */
379 sdi = sr_device_instance_new(devcnt,
380 SR_ST_INACTIVE, USB_VENDOR_NAME,
381 USB_MODEL_NAME, USB_MODEL_VERSION);
383 sr_err("zp: %s: sr_device_instance_new failed",
391 g_slist_append(device_instances, sdi);
392 sdi->usb = sr_usb_device_instance_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_opendev(int device_index)
405 struct sr_device_instance *sdi;
409 if (!(sdi = zp_open_device(device_index))) {
410 sr_warn("unable to open device");
414 /* TODO: Note: sdi is retrieved in zp_open_device(). */
416 if (!(zp = sdi->priv)) {
417 sr_err("zp: %s: sdi->priv was NULL", __func__);
421 err = libusb_claim_interface(sdi->usb->devhdl, USB_INTERFACE);
423 sr_warn("Unable to claim interface: %d", err);
426 analyzer_reset(sdi->usb->devhdl);
427 analyzer_initialize(sdi->usb->devhdl);
429 analyzer_set_memory_size(MEMORY_SIZE_512K);
430 // analyzer_set_freq(g_freq, g_freq_scale);
431 analyzer_set_trigger_count(1);
432 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
433 // * get_memory_size(g_memory_size)) / 100) >> 2);
434 analyzer_set_ramsize_trigger_address(
435 (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
438 if (g_double_mode == 1)
439 analyzer_set_compression(COMPRESSION_DOUBLE);
440 else if (g_compression == 1)
441 analyzer_set_compression(COMPRESSION_ENABLE);
444 analyzer_set_compression(COMPRESSION_NONE);
446 if (zp->cur_samplerate == 0) {
447 /* Samplerate hasn't been set. Default to the slowest one. */
448 if (hw_set_configuration(device_index, SR_HWCAP_SAMPLERATE,
449 &samplerates.list[0]) == SR_ERR)
456 static int hw_closedev(int device_index)
458 struct sr_device_instance *sdi;
460 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
461 sr_err("zp: %s: sdi was NULL", __func__);
462 return SR_ERR; /* TODO: SR_ERR_ARG? */
471 static void hw_cleanup(void)
475 /* Properly close all devices... */
476 for (l = device_instances; l; l = l->next)
477 close_device((struct sr_device_instance *)l->data);
479 /* ...and free all their memory. */
480 for (l = device_instances; l; l = l->next)
482 g_slist_free(device_instances);
483 device_instances = NULL;
486 libusb_exit(usb_context);
490 static void *hw_get_device_info(int device_index, int device_info_id)
492 struct sr_device_instance *sdi;
496 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
497 sr_err("zp: %s: sdi was NULL", __func__);
501 if (!(zp = sdi->priv)) {
502 sr_err("zp: %s: sdi->priv was NULL", __func__);
506 switch (device_info_id) {
510 case SR_DI_NUM_PROBES:
511 info = GINT_TO_POINTER(zp->num_channels);
513 case SR_DI_PROBE_NAMES:
516 case SR_DI_SAMPLERATES:
519 case SR_DI_TRIGGER_TYPES:
520 info = TRIGGER_TYPES;
522 case SR_DI_CUR_SAMPLERATE:
523 info = &zp->cur_samplerate;
526 /* Unknown device info ID, return NULL. */
527 sr_err("zp: %s: Unknown device info ID", __func__);
535 static int hw_get_status(int device_index)
537 struct sr_device_instance *sdi;
539 sdi = sr_get_device_instance(device_instances, device_index);
543 return SR_ST_NOT_FOUND;
546 static int *hw_get_capabilities(void)
551 static int set_configuration_samplerate(struct sr_device_instance *sdi,
557 sr_err("zp: %s: sdi was NULL", __func__);
561 if (!(zp = sdi->priv)) {
562 sr_err("zp: %s: sdi->priv was NULL", __func__);
566 sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate);
568 if (samplerate > SR_MHZ(1))
569 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
570 else if (samplerate > SR_KHZ(1))
571 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
573 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
575 zp->cur_samplerate = samplerate;
576 zp->period_ps = 1000000000000 / samplerate;
581 static int hw_set_configuration(int device_index, int capability, void *value)
583 struct sr_device_instance *sdi;
587 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
588 sr_err("zp: %s: sdi was NULL", __func__);
592 if (!(zp = sdi->priv)) {
593 sr_err("zp: %s: sdi->priv was NULL", __func__);
597 switch (capability) {
598 case SR_HWCAP_SAMPLERATE:
600 return set_configuration_samplerate(sdi, *tmp_u64);
601 case SR_HWCAP_PROBECONFIG:
602 return configure_probes(sdi, (GSList *)value);
603 case SR_HWCAP_LIMIT_SAMPLES:
605 zp->limit_samples = *tmp_u64;
612 static int hw_start_acquisition(int device_index, gpointer session_data)
614 struct sr_device_instance *sdi;
615 struct sr_datafeed_packet packet;
616 struct sr_datafeed_logic logic;
617 struct sr_datafeed_header header;
618 uint64_t samples_read;
620 unsigned int packet_num;
624 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
625 sr_err("zp: %s: sdi was NULL", __func__);
629 if (!(zp = sdi->priv)) {
630 sr_err("zp: %s: sdi->priv was NULL", __func__);
634 /* push configured settings to device */
635 analyzer_configure(sdi->usb->devhdl);
637 analyzer_start(sdi->usb->devhdl);
638 sr_info("Waiting for data");
639 analyzer_wait_data(sdi->usb->devhdl);
641 sr_info("Stop address = 0x%x", analyzer_get_stop_address(sdi->usb->devhdl));
642 sr_info("Now address = 0x%x", analyzer_get_now_address(sdi->usb->devhdl));
643 sr_info("Trigger address = 0x%x", analyzer_get_trigger_address(sdi->usb->devhdl));
645 packet.type = SR_DF_HEADER;
646 packet.payload = &header;
647 header.feed_version = 1;
648 gettimeofday(&header.starttime, NULL);
649 header.samplerate = zp->cur_samplerate;
650 header.num_logic_probes = zp->num_channels;
651 header.num_analog_probes = 0;
652 sr_session_bus(session_data, &packet);
654 if (!(buf = g_try_malloc(PACKET_SIZE))) {
655 sr_err("zp: %s: buf malloc failed", __func__);
656 return SR_ERR_MALLOC;
660 analyzer_read_start(sdi->usb->devhdl);
661 /* Send the incoming transfer to the session bus. */
662 for (packet_num = 0; packet_num < (zp->memory_size * 4 / PACKET_SIZE);
664 res = analyzer_read_data(sdi->usb->devhdl, buf, PACKET_SIZE);
665 sr_info("Tried to read %llx bytes, actually read %x bytes",
668 packet.type = SR_DF_LOGIC;
669 packet.timeoffset = samples_read * zp->period_ps;
670 packet.duration = res / 4 * zp->period_ps;
671 packet.payload = &logic;
672 logic.length = PACKET_SIZE;
675 sr_session_bus(session_data, &packet);
676 samples_read += res / 4;
678 analyzer_read_stop(sdi->usb->devhdl);
681 packet.type = SR_DF_END;
682 sr_session_bus(session_data, &packet);
687 /* This stops acquisition on ALL devices, ignoring device_index. */
688 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
690 struct sr_datafeed_packet packet;
691 struct sr_device_instance *sdi;
693 packet.type = SR_DF_END;
694 sr_session_bus(session_device_id, &packet);
696 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
697 return; /* TODO: Cry? */
699 analyzer_reset(sdi->usb->devhdl);
700 /* TODO: Need to cancel and free any queued up transfers. */
703 struct sr_device_plugin zeroplus_logic_cube_plugin_info = {
704 .name = "zeroplus-logic-cube",
705 .longname = "Zeroplus Logic Cube LAP-C series",
708 .cleanup = hw_cleanup,
709 .opendev = hw_opendev,
710 .closedev = hw_closedev,
711 .get_device_info = hw_get_device_info,
712 .get_status = hw_get_status,
713 .get_capabilities = hw_get_capabilities,
714 .set_configuration = hw_set_configuration,
715 .start_acquisition = hw_start_acquisition,
716 .stop_acquisition = hw_stop_acquisition,