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/>.
27 #include "libsigrok.h"
28 #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_TYPE "01"
44 #define PACKET_SIZE 2048 /* ?? */
46 //#define ZP_EXPERIMENTAL
52 unsigned int channels;
53 unsigned int sample_depth; /* In Ksamples/channel */
54 unsigned int max_sampling_freq;
58 * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the
59 * same 128K sample depth.
61 static model_t zeroplus_models[] = {
62 {0x0c12, 0x7009, "LAP-C(16064)", 16, 64, 100},
63 {0x0c12, 0x700A, "LAP-C(16128)", 16, 128, 200},
64 /* TODO: we don't know anything about these
65 {0x0c12, 0x700B, "LAP-C(32128)", 32, 128, 200},
66 {0x0c12, 0x700C, "LAP-C(321000)", 32, 1024, 200},
67 {0x0c12, 0x700D, "LAP-C(322000)", 32, 2048, 200},
69 {0x0c12, 0x700E, "LAP-C(16032)", 16, 32, 100},
70 {0x0c12, 0x7016, "LAP-C(162000)", 16, 2048, 200},
74 static const int hwcaps[] = {
75 SR_CONF_LOGIC_ANALYZER,
77 SR_CONF_CAPTURE_RATIO,
79 /* These are really implemented in the driver, not the hardware. */
80 SR_CONF_LIMIT_SAMPLES,
85 * ZEROPLUS LAP-C (16032) numbers the 16 probes A0-A7 and B0-B7.
86 * We currently ignore other untested/unsupported devices here.
88 static const char *probe_names[NUM_PROBES + 1] = {
89 "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7",
90 "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7",
94 /* List of struct sr_dev_inst, maintained by dev_open()/dev_close(). */
95 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
96 static struct sr_dev_driver *di = &zeroplus_logic_cube_driver_info;
99 * The hardware supports more samplerates than these, but these are the
100 * options hardcoded into the vendor's Windows GUI.
104 * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up
107 static const uint64_t supported_samplerates[] = {
129 static const struct sr_samplerates samplerates = {
133 .list = supported_samplerates,
136 /* Private, per-device-instance driver context. */
138 uint64_t cur_samplerate;
139 uint64_t max_samplerate;
140 uint64_t limit_samples;
141 int num_channels; /* TODO: This isn't initialized before it's needed :( */
143 unsigned int max_memory_size;
144 //uint8_t probe_mask;
145 //uint8_t trigger_mask[NUM_TRIGGER_STAGES];
146 //uint8_t trigger_value[NUM_TRIGGER_STAGES];
147 // uint8_t trigger_buffer[NUM_TRIGGER_STAGES];
149 unsigned int capture_ratio;
151 /* TODO: this belongs in the device instance */
152 struct sr_usb_dev_inst *usb;
155 static int hw_dev_close(struct sr_dev_inst *sdi);
157 static unsigned int get_memory_size(int type)
159 if (type == MEMORY_SIZE_8K)
161 else if (type == MEMORY_SIZE_64K)
163 else if (type == MEMORY_SIZE_128K)
165 else if (type == MEMORY_SIZE_512K)
172 static int configure_probes(const struct sr_dev_inst *sdi)
174 struct dev_context *devc;
175 const struct sr_probe *probe;
177 int probe_bit, stage, i;
180 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
183 devc->probe_mask = 0;
184 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
185 devc->trigger_mask[i] = 0;
186 devc->trigger_value[i] = 0;
190 for (l = sdi->probes; l; l = l->next) {
191 probe = (struct sr_probe *)l->data;
192 if (probe->enabled == FALSE)
194 probe_bit = 1 << (probe->index);
195 devc->probe_mask |= probe_bit;
197 if (probe->trigger) {
199 for (tc = probe->trigger; *tc; tc++) {
200 devc->trigger_mask[stage] |= probe_bit;
202 devc->trigger_value[stage] |= probe_bit;
204 if (stage > NUM_TRIGGER_STAGES)
214 static int configure_probes(const struct sr_dev_inst *sdi)
216 struct dev_context *devc;
218 const struct sr_probe *probe;
222 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
225 for (l = sdi->probes; l; l = l->next) {
226 probe = (struct sr_probe *)l->data;
227 if (probe->enabled == FALSE)
230 if ((tc = probe->trigger)) {
240 type = TRIGGER_POSEDGE;
243 type = TRIGGER_NEGEDGE;
246 type = TRIGGER_ANYEDGE;
252 analyzer_add_trigger(probe->index, type);
260 static int clear_instances(void)
263 struct sr_dev_inst *sdi;
264 struct drv_context *drvc;
265 struct dev_context *devc;
268 for (l = drvc->instances; l; l = l->next) {
270 if (!(devc = sdi->priv)) {
271 /* Log error, but continue cleaning up the rest. */
272 sr_err("%s: sdi->priv was NULL, continuing", __func__);
275 sr_usb_dev_inst_free(devc->usb);
276 /* Properly close all devices... */
278 /* ...and free all their memory. */
279 sr_dev_inst_free(sdi);
281 g_slist_free(drvc->instances);
282 drvc->instances = NULL;
287 static int hw_init(struct sr_context *sr_ctx)
289 return std_hw_init(sr_ctx, di, "zeroplus: ");
292 static GSList *hw_scan(GSList *options)
294 struct sr_dev_inst *sdi;
295 struct sr_probe *probe;
296 struct drv_context *drvc;
297 struct dev_context *devc;
299 struct libusb_device_descriptor des;
300 libusb_device **devlist;
302 int ret, devcnt, i, j;
312 /* Find all ZEROPLUS analyzers and add them to device list. */
314 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); /* TODO: Errors. */
316 for (i = 0; devlist[i]; i++) {
317 ret = libusb_get_device_descriptor(devlist[i], &des);
319 sr_err("Failed to get device descriptor: %s.",
320 libusb_error_name(ret));
325 for (j = 0; j < zeroplus_models[j].vid; j++) {
326 if (des.idVendor == zeroplus_models[j].vid &&
327 des.idProduct == zeroplus_models[j].pid) {
328 prof = &zeroplus_models[j];
331 /* Skip if the device was not found */
334 sr_info("Found ZEROPLUS model %s.", prof->model_name);
336 /* Register the device with libsigrok. */
337 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
338 VENDOR_NAME, prof->model_name, NULL))) {
339 sr_err("%s: sr_dev_inst_new failed", __func__);
344 /* Allocate memory for our private driver context. */
345 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
346 sr_err("Device context malloc failed.");
350 devc->num_channels = prof->channels;
351 #ifdef ZP_EXPERIMENTAL
352 devc->max_memory_size = 128 * 1024;
353 devc->max_samplerate = 200;
355 devc->max_memory_size = prof->sample_depth * 1024;
356 devc->max_samplerate = prof->max_sampling_freq;
358 devc->max_samplerate *= SR_MHZ(1);
359 devc->memory_size = MEMORY_SIZE_8K;
360 // memset(devc->trigger_buffer, 0, NUM_TRIGGER_STAGES);
362 /* Fill in probelist according to this device's profile. */
363 for (j = 0; j < devc->num_channels; j++) {
364 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
367 sdi->probes = g_slist_append(sdi->probes, probe);
370 devices = g_slist_append(devices, sdi);
371 drvc->instances = g_slist_append(drvc->instances, sdi);
372 devc->usb = sr_usb_dev_inst_new(
373 libusb_get_bus_number(devlist[i]),
374 libusb_get_device_address(devlist[i]), NULL);
378 libusb_free_device_list(devlist, 1);
383 static GSList *hw_dev_list(void)
385 struct drv_context *drvc;
389 return drvc->instances;
392 static int hw_dev_open(struct sr_dev_inst *sdi)
394 struct dev_context *devc;
395 struct drv_context *drvc = di->priv;
396 libusb_device **devlist, *dev;
397 struct libusb_device_descriptor des;
398 int device_count, ret, i;
400 if (!(devc = sdi->priv)) {
401 sr_err("%s: sdi->priv was NULL", __func__);
405 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx,
407 if (device_count < 0) {
408 sr_err("Failed to retrieve device list.");
413 for (i = 0; i < device_count; i++) {
414 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
415 sr_err("Failed to get device descriptor: %s.",
416 libusb_error_name(ret));
419 if (libusb_get_bus_number(devlist[i]) == devc->usb->bus
420 && libusb_get_device_address(devlist[i]) == devc->usb->address) {
426 sr_err("Device on bus %d address %d disappeared!",
427 devc->usb->bus, devc->usb->address);
431 if (!(ret = libusb_open(dev, &(devc->usb->devhdl)))) {
432 sdi->status = SR_ST_ACTIVE;
433 sr_info("Opened device %d on %d.%d interface %d.",
434 sdi->index, devc->usb->bus,
435 devc->usb->address, USB_INTERFACE);
437 sr_err("Failed to open device: %s.", libusb_error_name(ret));
441 ret = libusb_set_configuration(devc->usb->devhdl, USB_CONFIGURATION);
443 sr_err("Unable to set USB configuration %d: %s.",
444 USB_CONFIGURATION, libusb_error_name(ret));
448 ret = libusb_claim_interface(devc->usb->devhdl, USB_INTERFACE);
450 sr_err("Unable to claim interface: %s.",
451 libusb_error_name(ret));
455 /* Set default configuration after power on */
456 if (analyzer_read_status(devc->usb->devhdl) == 0)
457 analyzer_configure(devc->usb->devhdl);
459 analyzer_reset(devc->usb->devhdl);
460 analyzer_initialize(devc->usb->devhdl);
462 //analyzer_set_memory_size(MEMORY_SIZE_512K);
463 // analyzer_set_freq(g_freq, g_freq_scale);
464 analyzer_set_trigger_count(1);
465 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
466 // * get_memory_size(g_memory_size)) / 100) >> 2);
469 if (g_double_mode == 1)
470 analyzer_set_compression(COMPRESSION_DOUBLE);
471 else if (g_compression == 1)
472 analyzer_set_compression(COMPRESSION_ENABLE);
475 analyzer_set_compression(COMPRESSION_NONE);
477 if (devc->cur_samplerate == 0) {
478 /* Samplerate hasn't been set. Default to 1MHz. */
479 analyzer_set_freq(1, FREQ_SCALE_MHZ);
480 devc->cur_samplerate = SR_MHZ(1);
486 static int hw_dev_close(struct sr_dev_inst *sdi)
488 struct dev_context *devc;
490 if (!(devc = sdi->priv)) {
491 sr_err("%s: sdi->priv was NULL", __func__);
495 if (!devc->usb->devhdl)
498 sr_info("Closing device %d on %d.%d interface %d.", sdi->index,
499 devc->usb->bus, devc->usb->address, USB_INTERFACE);
500 libusb_release_interface(devc->usb->devhdl, USB_INTERFACE);
501 libusb_reset_device(devc->usb->devhdl);
502 libusb_close(devc->usb->devhdl);
503 devc->usb->devhdl = NULL;
504 sdi->status = SR_ST_INACTIVE;
509 static int hw_cleanup(void)
511 struct drv_context *drvc;
513 if (!(drvc = di->priv))
521 static int config_get(int id, const void **data, const struct sr_dev_inst *sdi)
523 struct dev_context *devc;
526 case SR_CONF_SAMPLERATE:
529 *data = &devc->cur_samplerate;
530 sr_spew("Returning samplerate: %" PRIu64 "Hz.",
531 devc->cur_samplerate);
542 static int set_samplerate(struct dev_context *devc, uint64_t samplerate)
546 for (i = 0; supported_samplerates[i]; i++)
547 if (samplerate == supported_samplerates[i])
550 if (!supported_samplerates[i] || samplerate > devc->max_samplerate) {
551 sr_err("Unsupported samplerate.");
555 sr_info("Setting samplerate to %" PRIu64 "Hz.", samplerate);
557 if (samplerate >= SR_MHZ(1))
558 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
559 else if (samplerate >= SR_KHZ(1))
560 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
562 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
564 devc->cur_samplerate = samplerate;
569 static int set_limit_samples(struct dev_context *devc, uint64_t samples)
571 devc->limit_samples = samples;
573 if (samples <= 2 * 1024)
574 devc->memory_size = MEMORY_SIZE_8K;
575 else if (samples <= 16 * 1024)
576 devc->memory_size = MEMORY_SIZE_64K;
577 else if (samples <= 32 * 1024 ||
578 devc->max_memory_size <= 32 * 1024)
579 devc->memory_size = MEMORY_SIZE_128K;
581 devc->memory_size = MEMORY_SIZE_512K;
583 sr_info("Setting memory size to %dK.",
584 get_memory_size(devc->memory_size) / 1024);
586 analyzer_set_memory_size(devc->memory_size);
591 static int set_capture_ratio(struct dev_context *devc, uint64_t ratio)
594 sr_err("Invalid capture ratio: %" PRIu64 ".", ratio);
598 devc->capture_ratio = ratio;
600 sr_info("Setting capture ratio to %d%%.", devc->capture_ratio);
605 static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
607 struct dev_context *devc;
610 sr_err("%s: sdi was NULL", __func__);
614 if (!(devc = sdi->priv)) {
615 sr_err("%s: sdi->priv was NULL", __func__);
620 case SR_CONF_SAMPLERATE:
621 return set_samplerate(devc, *(const uint64_t *)value);
622 case SR_CONF_LIMIT_SAMPLES:
623 return set_limit_samples(devc, *(const uint64_t *)value);
624 case SR_CONF_CAPTURE_RATIO:
625 return set_capture_ratio(devc, *(const uint64_t *)value);
631 static int config_list(int key, const void **data, const struct sr_dev_inst *sdi)
636 case SR_CONF_DEVICE_OPTIONS:
639 case SR_CONF_SAMPLERATE:
640 *data = &samplerates;
642 case SR_CONF_TRIGGER_TYPE:
643 *data = TRIGGER_TYPE;
652 static void set_triggerbar(struct dev_context *devc)
654 unsigned int ramsize;
656 unsigned int triggerbar;
658 ramsize = get_memory_size(devc->memory_size) / 4;
661 if (devc->max_memory_size < n)
662 n = devc->max_memory_size;
663 if (devc->limit_samples < n)
664 n = devc->limit_samples;
665 n = n * devc->capture_ratio / 100;
667 triggerbar = ramsize - 8;
673 analyzer_set_triggerbar_address(triggerbar);
674 analyzer_set_ramsize_trigger_address(ramsize - triggerbar);
676 sr_dbg("triggerbar_address = %d(0x%x)", triggerbar, triggerbar);
677 sr_dbg("ramsize_triggerbar_address = %d(0x%x)",
678 ramsize - triggerbar, ramsize - triggerbar);
681 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
684 struct sr_datafeed_packet packet;
685 struct sr_datafeed_logic logic;
686 struct sr_datafeed_header header;
687 //uint64_t samples_read;
689 unsigned int packet_num;
692 struct dev_context *devc;
694 if (!(devc = sdi->priv)) {
695 sr_err("%s: sdi->priv was NULL", __func__);
699 if (configure_probes(sdi) != SR_OK) {
700 sr_err("Failed to configure probes.");
704 set_triggerbar(devc);
706 /* push configured settings to device */
707 analyzer_configure(devc->usb->devhdl);
709 analyzer_start(devc->usb->devhdl);
710 sr_info("Waiting for data.");
711 analyzer_wait_data(devc->usb->devhdl);
713 sr_info("Stop address = 0x%x.",
714 analyzer_get_stop_address(devc->usb->devhdl));
715 sr_info("Now address = 0x%x.",
716 analyzer_get_now_address(devc->usb->devhdl));
717 sr_info("Trigger address = 0x%x.",
718 analyzer_get_trigger_address(devc->usb->devhdl));
720 packet.type = SR_DF_HEADER;
721 packet.payload = &header;
722 header.feed_version = 1;
723 gettimeofday(&header.starttime, NULL);
724 sr_session_send(cb_data, &packet);
726 if (!(buf = g_try_malloc(PACKET_SIZE))) {
727 sr_err("Packet buffer malloc failed.");
728 return SR_ERR_MALLOC;
732 analyzer_read_start(devc->usb->devhdl);
733 /* Send the incoming transfer to the session bus. */
734 n = get_memory_size(devc->memory_size);
735 if (devc->max_memory_size * 4 < n)
736 n = devc->max_memory_size * 4;
737 for (packet_num = 0; packet_num < n / PACKET_SIZE; packet_num++) {
738 res = analyzer_read_data(devc->usb->devhdl, buf, PACKET_SIZE);
739 sr_info("Tried to read %d bytes, actually read %d bytes.",
742 packet.type = SR_DF_LOGIC;
743 packet.payload = &logic;
744 logic.length = PACKET_SIZE;
747 sr_session_send(cb_data, &packet);
748 //samples_read += res / 4;
750 analyzer_read_stop(devc->usb->devhdl);
753 packet.type = SR_DF_END;
754 sr_session_send(cb_data, &packet);
759 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
760 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
762 struct sr_datafeed_packet packet;
763 struct dev_context *devc;
765 packet.type = SR_DF_END;
766 sr_session_send(cb_data, &packet);
768 if (!(devc = sdi->priv)) {
769 sr_err("%s: sdi->priv was NULL", __func__);
773 analyzer_reset(devc->usb->devhdl);
774 /* TODO: Need to cancel and free any queued up transfers. */
779 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
780 .name = "zeroplus-logic-cube",
781 .longname = "ZEROPLUS Logic Cube LAP-C series",
784 .cleanup = hw_cleanup,
786 .dev_list = hw_dev_list,
787 .dev_clear = hw_cleanup,
788 .config_get = config_get,
789 .config_set = config_set,
790 .config_list = config_list,
791 .dev_open = hw_dev_open,
792 .dev_close = hw_dev_close,
793 .dev_acquisition_start = hw_dev_acquisition_start,
794 .dev_acquisition_stop = hw_dev_acquisition_stop,