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/>.
28 #include "sigrok-internal.h"
31 #define USB_VENDOR 0x0c12
32 #define USB_VENDOR_NAME "Zeroplus"
33 #define USB_MODEL_NAME "Logic Cube"
34 #define USB_MODEL_VERSION ""
36 #define USB_INTERFACE 0
37 #define USB_CONFIGURATION 1
38 #define NUM_TRIGGER_STAGES 4
39 #define TRIGGER_TYPES "01"
41 #define PACKET_SIZE 2048 /* ?? */
46 unsigned int channels;
47 unsigned int sample_depth; /* In Ksamples/channel */
48 unsigned int max_sampling_freq;
52 * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the
53 * same 128K sample depth.
55 static model_t zeroplus_models[] = {
56 {0x7009, "LAP-C(16064)", 16, 64, 100},
57 {0x700A, "LAP-C(16128)", 16, 128, 200},
58 {0x700B, "LAP-C(32128)", 32, 128, 200},
59 {0x700C, "LAP-C(321000)", 32, 1024, 200},
60 {0x700D, "LAP-C(322000)", 32, 2048, 200},
61 {0x700E, "LAP-C(16032)", 16, 32, 100},
62 {0x7016, "LAP-C(162000)", 16, 2048, 200},
65 static int capabilities[] = {
66 SR_HWCAP_LOGIC_ANALYZER,
69 SR_HWCAP_CAPTURE_RATIO,
71 /* These are really implemented in the driver, not the hardware. */
72 SR_HWCAP_LIMIT_SAMPLES,
76 static const char *probe_names[] = {
112 /* List of struct sr_device_instance, maintained by opendev()/closedev(). */
113 static GSList *device_instances = NULL;
115 static libusb_context *usb_context = NULL;
118 * The hardware supports more samplerates than these, but these are the
119 * options hardcoded into the vendor's Windows GUI.
123 * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up
126 static uint64_t supported_samplerates[] = {
148 static struct sr_samplerates samplerates = {
152 supported_samplerates,
156 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];
167 static int hw_set_configuration(int device_index, int capability, void *value);
169 static unsigned int get_memory_size(int type)
171 if (type == MEMORY_SIZE_8K)
173 else if (type == MEMORY_SIZE_64K)
175 else if (type == MEMORY_SIZE_128K)
177 else if (type == MEMORY_SIZE_512K)
183 static int opendev4(struct sr_device_instance **sdi, libusb_device *dev,
184 struct libusb_device_descriptor *des)
190 /* Note: sdi is non-NULL, the caller already checked this. */
192 if (!(zp = (*sdi)->priv)) {
193 sr_err("zp: %s: (*sdi)->priv was NULL", __func__);
197 if ((err = libusb_get_device_descriptor(dev, des))) {
198 sr_warn("failed to get device descriptor: %d", err);
202 if (des->idVendor != USB_VENDOR)
205 if (libusb_get_bus_number(dev) == (*sdi)->usb->bus
206 && libusb_get_device_address(dev) == (*sdi)->usb->address) {
208 for (i = 0; i < ARRAY_SIZE(zeroplus_models); i++) {
209 if (!(des->idProduct == zeroplus_models[i].pid))
212 sr_info("Found PID=%04X (%s)", des->idProduct,
213 zeroplus_models[i].model_name);
214 zp->num_channels = zeroplus_models[i].channels;
215 zp->memory_size = zeroplus_models[i].sample_depth * 1024;
219 if (zp->num_channels == 0) {
220 sr_warn("Unknown ZeroPlus device %04X", des->idProduct);
225 if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) {
226 (*sdi)->status = SR_ST_ACTIVE;
227 sr_info("opened device %d on %d.%d interface %d",
228 (*sdi)->index, (*sdi)->usb->bus,
229 (*sdi)->usb->address, USB_INTERFACE);
231 sr_warn("failed to open device: %d", err);
239 static struct sr_device_instance *zp_open_device(int device_index)
241 struct sr_device_instance *sdi;
242 libusb_device **devlist;
243 struct libusb_device_descriptor des;
246 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
249 libusb_get_device_list(usb_context, &devlist);
250 if (sdi->status == SR_ST_INACTIVE) {
251 /* Find the device by vendor, product, bus and address. */
252 libusb_get_device_list(usb_context, &devlist);
253 for (i = 0; devlist[i]; i++) {
254 /* TODO: Error handling. */
255 err = opendev4(&sdi, devlist[i], &des);
258 /* Status must be SR_ST_ACTIVE, i.e. already in use... */
261 libusb_free_device_list(devlist, 1);
263 if (sdi && sdi->status != SR_ST_ACTIVE)
269 static void close_device(struct sr_device_instance *sdi)
271 if (!sdi->usb->devhdl)
274 sr_info("closing device %d on %d.%d interface %d", sdi->index,
275 sdi->usb->bus, sdi->usb->address, USB_INTERFACE);
276 libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE);
277 libusb_close(sdi->usb->devhdl);
278 sdi->usb->devhdl = NULL;
279 sdi->status = SR_ST_INACTIVE;
282 static int configure_probes(struct sr_device_instance *sdi, GSList *probes)
285 struct sr_probe *probe;
287 int probe_bit, stage, i;
290 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
294 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
295 zp->trigger_mask[i] = 0;
296 zp->trigger_value[i] = 0;
300 for (l = probes; l; l = l->next) {
301 probe = (struct sr_probe *)l->data;
302 if (probe->enabled == FALSE)
304 probe_bit = 1 << (probe->index - 1);
305 zp->probe_mask |= probe_bit;
307 if (probe->trigger) {
309 for (tc = probe->trigger; *tc; tc++) {
310 zp->trigger_mask[stage] |= probe_bit;
312 zp->trigger_value[stage] |= probe_bit;
314 if (stage > NUM_TRIGGER_STAGES)
327 static int hw_init(const char *deviceinfo)
329 struct sr_device_instance *sdi;
330 struct libusb_device_descriptor des;
331 libusb_device **devlist;
335 /* Avoid compiler warnings. */
338 /* Allocate memory for our private driver context. */
339 if (!(zp = g_try_malloc(sizeof(struct zp)))) {
340 sr_err("zp: %s: struct zp malloc failed", __func__);
344 /* Set some sane defaults. */
345 zp->cur_samplerate = 0;
347 zp->limit_samples = 0;
348 zp->num_channels = 32; /* TODO: This isn't initialized before it's needed :( */
351 memset(zp->trigger_mask, 0, NUM_TRIGGER_STAGES);
352 memset(zp->trigger_value, 0, NUM_TRIGGER_STAGES);
353 // memset(zp->trigger_buffer, 0, NUM_TRIGGER_STAGES);
355 if (libusb_init(&usb_context) != 0) {
356 sr_warn("Failed to initialize USB.");
360 /* Find all ZeroPlus analyzers and add them to device list. */
362 libusb_get_device_list(usb_context, &devlist);
364 for (i = 0; devlist[i]; i++) {
365 err = libusb_get_device_descriptor(devlist[i], &des);
367 sr_warn("failed to get device descriptor: %d", err);
371 if (des.idVendor == USB_VENDOR) {
373 * Definitely a Zeroplus.
374 * TODO: Any way to detect specific model/version in
375 * the zeroplus range?
377 /* Register the device with libsigrok. */
378 sdi = sr_device_instance_new(devcnt,
379 SR_ST_INACTIVE, USB_VENDOR_NAME,
380 USB_MODEL_NAME, USB_MODEL_VERSION);
382 sr_err("zp: %s: sr_device_instance_new failed",
390 g_slist_append(device_instances, sdi);
391 sdi->usb = sr_usb_device_instance_new(
392 libusb_get_bus_number(devlist[i]),
393 libusb_get_device_address(devlist[i]), NULL);
397 libusb_free_device_list(devlist, 1);
402 static int hw_opendev(int device_index)
404 struct sr_device_instance *sdi;
408 if (!(sdi = zp_open_device(device_index))) {
409 sr_warn("unable to open device");
413 /* TODO: Note: sdi is retrieved in zp_open_device(). */
415 if (!(zp = sdi->priv)) {
416 sr_err("zp: %s: sdi->priv was NULL", __func__);
420 err = libusb_claim_interface(sdi->usb->devhdl, USB_INTERFACE);
422 sr_warn("Unable to claim interface: %d", err);
425 analyzer_reset(sdi->usb->devhdl);
426 analyzer_initialize(sdi->usb->devhdl);
428 analyzer_set_memory_size(MEMORY_SIZE_512K);
429 // analyzer_set_freq(g_freq, g_freq_scale);
430 analyzer_set_trigger_count(1);
431 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
432 // * get_memory_size(g_memory_size)) / 100) >> 2);
433 analyzer_set_ramsize_trigger_address(
434 (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
437 if (g_double_mode == 1)
438 analyzer_set_compression(COMPRESSION_DOUBLE);
439 else if (g_compression == 1)
440 analyzer_set_compression(COMPRESSION_ENABLE);
443 analyzer_set_compression(COMPRESSION_NONE);
445 if (zp->cur_samplerate == 0) {
446 /* Samplerate hasn't been set. Default to the slowest one. */
447 if (hw_set_configuration(device_index, SR_HWCAP_SAMPLERATE,
448 &samplerates.list[0]) == SR_ERR)
455 static int hw_closedev(int device_index)
457 struct sr_device_instance *sdi;
459 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
460 sr_err("zp: %s: sdi was NULL", __func__);
461 return SR_ERR; /* TODO: SR_ERR_ARG? */
470 static void hw_cleanup(void)
474 /* Properly close all devices... */
475 for (l = device_instances; l; l = l->next)
476 close_device((struct sr_device_instance *)l->data);
478 /* ...and free all their memory. */
479 for (l = device_instances; l; l = l->next)
481 g_slist_free(device_instances);
482 device_instances = NULL;
485 libusb_exit(usb_context);
489 static void *hw_get_device_info(int device_index, int device_info_id)
491 struct sr_device_instance *sdi;
495 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
496 sr_err("zp: %s: sdi was NULL", __func__);
500 if (!(zp = sdi->priv)) {
501 sr_err("zp: %s: sdi->priv was NULL", __func__);
505 switch (device_info_id) {
509 case SR_DI_NUM_PROBES:
510 info = GINT_TO_POINTER(zp->num_channels);
512 case SR_DI_PROBE_NAMES:
515 case SR_DI_SAMPLERATES:
518 case SR_DI_TRIGGER_TYPES:
519 info = TRIGGER_TYPES;
521 case SR_DI_CUR_SAMPLERATE:
522 info = &zp->cur_samplerate;
525 /* Unknown device info ID, return NULL. */
526 sr_err("zp: %s: Unknown device info ID", __func__);
534 static int hw_get_status(int device_index)
536 struct sr_device_instance *sdi;
538 sdi = sr_get_device_instance(device_instances, device_index);
542 return SR_ST_NOT_FOUND;
545 static int *hw_get_capabilities(void)
550 static int set_configuration_samplerate(struct sr_device_instance *sdi,
556 sr_err("zp: %s: sdi was NULL", __func__);
560 if (!(zp = sdi->priv)) {
561 sr_err("zp: %s: sdi->priv was NULL", __func__);
565 sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate);
567 if (samplerate > SR_MHZ(1))
568 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
569 else if (samplerate > SR_KHZ(1))
570 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
572 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
574 zp->cur_samplerate = samplerate;
575 zp->period_ps = 1000000000000 / samplerate;
580 static int hw_set_configuration(int device_index, int capability, void *value)
582 struct sr_device_instance *sdi;
586 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
587 sr_err("zp: %s: sdi was NULL", __func__);
591 if (!(zp = sdi->priv)) {
592 sr_err("zp: %s: sdi->priv was NULL", __func__);
596 switch (capability) {
597 case SR_HWCAP_SAMPLERATE:
599 return set_configuration_samplerate(sdi, *tmp_u64);
600 case SR_HWCAP_PROBECONFIG:
601 return configure_probes(sdi, (GSList *)value);
602 case SR_HWCAP_LIMIT_SAMPLES:
604 zp->limit_samples = *tmp_u64;
611 static int hw_start_acquisition(int device_index, gpointer session_data)
613 struct sr_device_instance *sdi;
614 struct sr_datafeed_packet packet;
615 struct sr_datafeed_logic logic;
616 struct sr_datafeed_header header;
617 uint64_t samples_read;
619 unsigned int packet_num;
623 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
624 sr_err("zp: %s: sdi was NULL", __func__);
628 if (!(zp = sdi->priv)) {
629 sr_err("zp: %s: sdi->priv was NULL", __func__);
633 /* push configured settings to device */
634 analyzer_configure(sdi->usb->devhdl);
636 analyzer_start(sdi->usb->devhdl);
637 sr_info("Waiting for data");
638 analyzer_wait_data(sdi->usb->devhdl);
640 sr_info("Stop address = 0x%x", analyzer_get_stop_address(sdi->usb->devhdl));
641 sr_info("Now address = 0x%x", analyzer_get_now_address(sdi->usb->devhdl));
642 sr_info("Trigger address = 0x%x", analyzer_get_trigger_address(sdi->usb->devhdl));
644 packet.type = SR_DF_HEADER;
645 packet.payload = &header;
646 header.feed_version = 1;
647 gettimeofday(&header.starttime, NULL);
648 header.samplerate = zp->cur_samplerate;
649 header.num_logic_probes = zp->num_channels;
650 header.num_analog_probes = 0;
651 sr_session_bus(session_data, &packet);
653 if (!(buf = g_try_malloc(PACKET_SIZE))) {
654 sr_err("zp: %s: buf malloc failed", __func__);
655 return SR_ERR_MALLOC;
659 analyzer_read_start(sdi->usb->devhdl);
660 /* Send the incoming transfer to the session bus. */
661 for (packet_num = 0; packet_num < (zp->memory_size * 4 / PACKET_SIZE);
663 res = analyzer_read_data(sdi->usb->devhdl, buf, PACKET_SIZE);
664 sr_info("Tried to read %llx bytes, actually read %x bytes",
667 packet.type = SR_DF_LOGIC;
668 packet.timeoffset = samples_read * zp->period_ps;
669 packet.duration = res / 4 * zp->period_ps;
670 packet.payload = &logic;
671 logic.length = PACKET_SIZE;
674 sr_session_bus(session_data, &packet);
675 samples_read += res / 4;
677 analyzer_read_stop(sdi->usb->devhdl);
680 packet.type = SR_DF_END;
681 sr_session_bus(session_data, &packet);
686 /* This stops acquisition on ALL devices, ignoring device_index. */
687 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
689 struct sr_datafeed_packet packet;
690 struct sr_device_instance *sdi;
692 packet.type = SR_DF_END;
693 sr_session_bus(session_device_id, &packet);
695 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
696 return; /* TODO: Cry? */
698 analyzer_reset(sdi->usb->devhdl);
699 /* TODO: Need to cancel and free any queued up transfers. */
702 struct sr_device_plugin zeroplus_logic_cube_plugin_info = {
703 .name = "zeroplus-logic-cube",
704 .longname = "Zeroplus Logic Cube LAP-C series",
707 .cleanup = hw_cleanup,
708 .opendev = hw_opendev,
709 .closedev = hw_closedev,
710 .get_device_info = hw_get_device_info,
711 .get_status = hw_get_status,
712 .get_capabilities = hw_get_capabilities,
713 .set_configuration = hw_set_configuration,
714 .start_acquisition = hw_start_acquisition,
715 .stop_acquisition = hw_stop_acquisition,