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"
31 #define USB_VENDOR 0x0c12
33 #define VENDOR_NAME "ZEROPLUS"
34 #define MODEL_NAME "Logic Cube LAP-C"
35 #define MODEL_VERSION NULL
38 #define USB_INTERFACE 0
39 #define USB_CONFIGURATION 1
40 #define NUM_TRIGGER_STAGES 4
41 #define TRIGGER_TYPES "01"
43 #define PACKET_SIZE 2048 /* ?? */
45 //#define ZP_EXPERIMENTAL
51 unsigned int channels;
52 unsigned int sample_depth; /* In Ksamples/channel */
53 unsigned int max_sampling_freq;
57 * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the
58 * same 128K sample depth.
60 static model_t zeroplus_models[] = {
61 {0x0c12, 0x7009, "LAP-C(16064)", 16, 64, 100},
62 {0x0c12, 0x700A, "LAP-C(16128)", 16, 128, 200},
63 /* TODO: we don't know anything about these
64 {0x0c12, 0x700B, "LAP-C(32128)", 32, 128, 200},
65 {0x0c12, 0x700C, "LAP-C(321000)", 32, 1024, 200},
66 {0x0c12, 0x700D, "LAP-C(322000)", 32, 2048, 200},
68 {0x0c12, 0x700E, "LAP-C(16032)", 16, 32, 100},
69 {0x0c12, 0x7016, "LAP-C(162000)", 16, 2048, 200},
73 static const int hwcaps[] = {
74 SR_HWCAP_LOGIC_ANALYZER,
76 SR_HWCAP_CAPTURE_RATIO,
78 /* These are really implemented in the driver, not the hardware. */
79 SR_HWCAP_LIMIT_SAMPLES,
84 * ZEROPLUS LAP-C (16032) numbers the 16 probes A0-A7 and B0-B7.
85 * We currently ignore other untested/unsupported devices here.
87 static const char *probe_names[NUM_PROBES + 1] = {
107 /* List of struct sr_dev_inst, maintained by dev_open()/dev_close(). */
108 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
109 static struct sr_dev_driver *di = &zeroplus_logic_cube_driver_info;
112 * The hardware supports more samplerates than these, but these are the
113 * options hardcoded into the vendor's Windows GUI.
117 * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up
120 static const uint64_t supported_samplerates[] = {
142 static const struct sr_samplerates samplerates = {
146 supported_samplerates,
149 /* Private, per-device-instance driver context. */
151 uint64_t cur_samplerate;
152 uint64_t max_samplerate;
153 uint64_t limit_samples;
154 int num_channels; /* TODO: This isn't initialized before it's needed :( */
156 unsigned int max_memory_size;
157 //uint8_t probe_mask;
158 //uint8_t trigger_mask[NUM_TRIGGER_STAGES];
159 //uint8_t trigger_value[NUM_TRIGGER_STAGES];
160 // uint8_t trigger_buffer[NUM_TRIGGER_STAGES];
162 unsigned int capture_ratio;
164 /* TODO: this belongs in the device instance */
165 struct sr_usb_dev_inst *usb;
168 static int hw_dev_close(struct sr_dev_inst *sdi);
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)
185 static int configure_probes(const struct sr_dev_inst *sdi)
187 struct dev_context *devc;
188 const struct sr_probe *probe;
190 int probe_bit, stage, i;
193 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
196 devc->probe_mask = 0;
197 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
198 devc->trigger_mask[i] = 0;
199 devc->trigger_value[i] = 0;
203 for (l = sdi->probes; l; l = l->next) {
204 probe = (struct sr_probe *)l->data;
205 if (probe->enabled == FALSE)
207 probe_bit = 1 << (probe->index);
208 devc->probe_mask |= probe_bit;
210 if (probe->trigger) {
212 for (tc = probe->trigger; *tc; tc++) {
213 devc->trigger_mask[stage] |= probe_bit;
215 devc->trigger_value[stage] |= probe_bit;
217 if (stage > NUM_TRIGGER_STAGES)
227 static int configure_probes(const struct sr_dev_inst *sdi)
229 struct dev_context *devc;
231 const struct sr_probe *probe;
235 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
238 for (l = sdi->probes; l; l = l->next) {
239 probe = (struct sr_probe *)l->data;
240 if (probe->enabled == FALSE)
243 if ((tc = probe->trigger)) {
253 type = TRIGGER_POSEDGE;
256 type = TRIGGER_NEGEDGE;
259 type = TRIGGER_ANYEDGE;
265 analyzer_add_trigger(probe->index, type);
273 static int clear_instances(void)
276 struct sr_dev_inst *sdi;
277 struct drv_context *drvc;
278 struct dev_context *devc;
281 for (l = drvc->instances; l; l = l->next) {
283 if (!(devc = sdi->priv)) {
284 /* Log error, but continue cleaning up the rest. */
285 sr_err("zeroplus: %s: sdi->priv was NULL, continuing", __func__);
288 sr_usb_dev_inst_free(devc->usb);
289 /* Properly close all devices... */
291 /* ...and free all their memory. */
292 sr_dev_inst_free(sdi);
294 g_slist_free(drvc->instances);
295 drvc->instances = NULL;
304 static int hw_init(struct sr_context *sr_ctx)
306 struct drv_context *drvc;
308 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
309 sr_err("zeroplus: driver context malloc failed.");
310 return SR_ERR_MALLOC;
312 drvc->sr_ctx = sr_ctx;
318 static GSList *hw_scan(GSList *options)
320 struct sr_dev_inst *sdi;
321 struct sr_probe *probe;
322 struct drv_context *drvc;
323 struct dev_context *devc;
325 struct libusb_device_descriptor des;
326 libusb_device **devlist;
328 int ret, devcnt, i, j;
337 /* Find all ZEROPLUS analyzers and add them to device list. */
339 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); /* TODO: Errors. */
341 for (i = 0; devlist[i]; i++) {
342 ret = libusb_get_device_descriptor(devlist[i], &des);
344 sr_err("zp: failed to get device descriptor: %s",
345 libusb_error_name(ret));
350 for (j = 0; j < zeroplus_models[j].vid; j++) {
351 if (des.idVendor == zeroplus_models[j].vid &&
352 des.idProduct == zeroplus_models[j].pid) {
353 prof = &zeroplus_models[j];
356 /* Skip if the device was not found */
359 sr_info("zp: Found ZEROPLUS model %s", prof->model_name);
361 /* Register the device with libsigrok. */
362 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
363 VENDOR_NAME, prof->model_name, NULL))) {
364 sr_err("zp: %s: sr_dev_inst_new failed", __func__);
369 /* Allocate memory for our private driver context. */
370 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
371 sr_err("zp: %s: devc malloc failed", __func__);
375 devc->num_channels = prof->channels;
376 #ifdef ZP_EXPERIMENTAL
377 devc->max_memory_size = 128 * 1024;
378 devc->max_samplerate = 200;
380 devc->max_memory_size = prof->sample_depth * 1024;
381 devc->max_samplerate = prof->max_sampling_freq;
383 devc->max_samplerate *= SR_MHZ(1);
384 devc->memory_size = MEMORY_SIZE_8K;
385 // memset(devc->trigger_buffer, 0, NUM_TRIGGER_STAGES);
387 /* Fill in probelist according to this device's profile. */
388 for (j = 0; j < devc->num_channels; j++) {
389 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
392 sdi->probes = g_slist_append(sdi->probes, probe);
395 devices = g_slist_append(devices, sdi);
396 drvc->instances = g_slist_append(drvc->instances, sdi);
397 devc->usb = sr_usb_dev_inst_new(
398 libusb_get_bus_number(devlist[i]),
399 libusb_get_device_address(devlist[i]), NULL);
403 libusb_free_device_list(devlist, 1);
408 static GSList *hw_dev_list(void)
410 struct drv_context *drvc;
414 return drvc->instances;
417 static int hw_dev_open(struct sr_dev_inst *sdi)
419 struct dev_context *devc;
420 struct drv_context *drvc = di->priv;
421 libusb_device **devlist, *dev;
422 struct libusb_device_descriptor des;
423 int device_count, ret, i;
425 if (!(devc = sdi->priv)) {
426 sr_err("zp: %s: sdi->priv was NULL", __func__);
430 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx,
432 if (device_count < 0) {
433 sr_err("zp: Failed to retrieve device list");
438 for (i = 0; i < device_count; i++) {
439 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
440 sr_err("zp: Failed to get device descriptor: %s.",
441 libusb_error_name(ret));
444 if (libusb_get_bus_number(devlist[i]) == devc->usb->bus
445 && libusb_get_device_address(devlist[i]) == devc->usb->address) {
451 sr_err("device on bus %d address %d disappeared!",
452 devc->usb->bus, devc->usb->address);
456 if (!(ret = libusb_open(dev, &(devc->usb->devhdl)))) {
457 sdi->status = SR_ST_ACTIVE;
458 sr_info("zp: opened device %d on %d.%d interface %d",
459 sdi->index, devc->usb->bus,
460 devc->usb->address, USB_INTERFACE);
462 sr_err("zp: failed to open device: %s", libusb_error_name(ret));
466 ret = libusb_set_configuration(devc->usb->devhdl, USB_CONFIGURATION);
468 sr_err("zp: Unable to set USB configuration %d: %s",
469 USB_CONFIGURATION, libusb_error_name(ret));
473 ret = libusb_claim_interface(devc->usb->devhdl, USB_INTERFACE);
475 sr_err("zp: Unable to claim interface: %s",
476 libusb_error_name(ret));
480 /* Set default configuration after power on */
481 if (analyzer_read_status(devc->usb->devhdl) == 0)
482 analyzer_configure(devc->usb->devhdl);
484 analyzer_reset(devc->usb->devhdl);
485 analyzer_initialize(devc->usb->devhdl);
487 //analyzer_set_memory_size(MEMORY_SIZE_512K);
488 // analyzer_set_freq(g_freq, g_freq_scale);
489 analyzer_set_trigger_count(1);
490 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
491 // * get_memory_size(g_memory_size)) / 100) >> 2);
494 if (g_double_mode == 1)
495 analyzer_set_compression(COMPRESSION_DOUBLE);
496 else if (g_compression == 1)
497 analyzer_set_compression(COMPRESSION_ENABLE);
500 analyzer_set_compression(COMPRESSION_NONE);
502 if (devc->cur_samplerate == 0) {
503 /* Samplerate hasn't been set. Default to 1MHz. */
504 analyzer_set_freq(1, FREQ_SCALE_MHZ);
505 devc->cur_samplerate = SR_MHZ(1);
511 static int hw_dev_close(struct sr_dev_inst *sdi)
513 struct dev_context *devc;
515 if (!(devc = sdi->priv)) {
516 sr_err("zp: %s: sdi->priv was NULL", __func__);
520 if (!devc->usb->devhdl)
523 sr_info("zp: closing device %d on %d.%d interface %d", sdi->index,
524 devc->usb->bus, devc->usb->address, USB_INTERFACE);
525 libusb_release_interface(devc->usb->devhdl, USB_INTERFACE);
526 libusb_reset_device(devc->usb->devhdl);
527 libusb_close(devc->usb->devhdl);
528 devc->usb->devhdl = NULL;
529 sdi->status = SR_ST_INACTIVE;
534 static int hw_cleanup(void)
536 struct drv_context *drvc;
538 if (!(drvc = di->priv))
546 static int hw_info_get(int info_id, const void **data,
547 const struct sr_dev_inst *sdi)
549 struct dev_context *devc;
555 case SR_DI_NUM_PROBES:
558 *data = GINT_TO_POINTER(devc->num_channels);
559 sr_spew("zp: %s: Returning number of channels: %d.",
560 __func__, devc->num_channels);
564 case SR_DI_PROBE_NAMES:
566 sr_spew("zp: %s: Returning probenames.", __func__);
568 case SR_DI_SAMPLERATES:
569 *data = &samplerates;
570 sr_spew("zp: %s: Returning samplerates.", __func__);
572 case SR_DI_TRIGGER_TYPES:
573 *data = TRIGGER_TYPES;
574 sr_spew("zp: %s: Returning triggertypes: %s.", __func__, TRIGGER_TYPES);
576 case SR_DI_CUR_SAMPLERATE:
579 *data = &devc->cur_samplerate;
580 sr_spew("zp: %s: Returning samplerate: %" PRIu64 "Hz.",
581 __func__, devc->cur_samplerate);
592 static int set_samplerate(struct dev_context *devc, uint64_t samplerate)
596 for (i = 0; supported_samplerates[i]; i++)
597 if (samplerate == supported_samplerates[i])
599 if (!supported_samplerates[i] || samplerate > devc->max_samplerate) {
600 sr_err("zp: %s: unsupported samplerate", __func__);
604 sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate);
606 if (samplerate >= SR_MHZ(1))
607 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
608 else if (samplerate >= SR_KHZ(1))
609 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
611 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
613 devc->cur_samplerate = samplerate;
618 static int set_limit_samples(struct dev_context *devc, uint64_t samples)
620 devc->limit_samples = samples;
622 if (samples <= 2 * 1024)
623 devc->memory_size = MEMORY_SIZE_8K;
624 else if (samples <= 16 * 1024)
625 devc->memory_size = MEMORY_SIZE_64K;
626 else if (samples <= 32 * 1024 ||
627 devc->max_memory_size <= 32 * 1024)
628 devc->memory_size = MEMORY_SIZE_128K;
630 devc->memory_size = MEMORY_SIZE_512K;
632 sr_info("zp: Setting memory size to %dK.",
633 get_memory_size(devc->memory_size) / 1024);
635 analyzer_set_memory_size(devc->memory_size);
640 static int set_capture_ratio(struct dev_context *devc, uint64_t ratio)
643 sr_err("zp: %s: invalid capture ratio", __func__);
647 devc->capture_ratio = ratio;
649 sr_info("zp: Setting capture ratio to %d%%.", devc->capture_ratio);
654 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
657 struct dev_context *devc;
660 sr_err("zp: %s: sdi was NULL", __func__);
664 if (!(devc = sdi->priv)) {
665 sr_err("zp: %s: sdi->priv was NULL", __func__);
670 case SR_HWCAP_SAMPLERATE:
671 return set_samplerate(devc, *(const uint64_t *)value);
672 case SR_HWCAP_LIMIT_SAMPLES:
673 return set_limit_samples(devc, *(const uint64_t *)value);
674 case SR_HWCAP_CAPTURE_RATIO:
675 return set_capture_ratio(devc, *(const uint64_t *)value);
681 static void set_triggerbar(struct dev_context *devc)
683 unsigned int ramsize;
685 unsigned int triggerbar;
687 ramsize = get_memory_size(devc->memory_size) / 4;
690 if (devc->max_memory_size < n)
691 n = devc->max_memory_size;
692 if (devc->limit_samples < n)
693 n = devc->limit_samples;
694 n = n * devc->capture_ratio / 100;
696 triggerbar = ramsize - 8;
702 analyzer_set_triggerbar_address(triggerbar);
703 analyzer_set_ramsize_trigger_address(ramsize - triggerbar);
705 sr_dbg("zp: triggerbar_address = %d(0x%x)", triggerbar, triggerbar);
706 sr_dbg("zp: ramsize_triggerbar_address = %d(0x%x)",
707 ramsize - triggerbar, ramsize - triggerbar);
710 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
713 struct sr_datafeed_packet packet;
714 struct sr_datafeed_logic logic;
715 struct sr_datafeed_header header;
716 struct sr_datafeed_meta_logic meta;
717 //uint64_t samples_read;
719 unsigned int packet_num;
722 struct dev_context *devc;
724 if (!(devc = sdi->priv)) {
725 sr_err("zp: %s: sdi->priv was NULL", __func__);
729 if (configure_probes(sdi) != SR_OK) {
730 sr_err("zp: failed to configured probes");
734 set_triggerbar(devc);
736 /* push configured settings to device */
737 analyzer_configure(devc->usb->devhdl);
739 analyzer_start(devc->usb->devhdl);
740 sr_info("zp: Waiting for data");
741 analyzer_wait_data(devc->usb->devhdl);
743 sr_info("zp: Stop address = 0x%x",
744 analyzer_get_stop_address(devc->usb->devhdl));
745 sr_info("zp: Now address = 0x%x",
746 analyzer_get_now_address(devc->usb->devhdl));
747 sr_info("zp: Trigger address = 0x%x",
748 analyzer_get_trigger_address(devc->usb->devhdl));
750 packet.type = SR_DF_HEADER;
751 packet.payload = &header;
752 header.feed_version = 1;
753 gettimeofday(&header.starttime, NULL);
754 sr_session_send(cb_data, &packet);
756 /* Send metadata about the SR_DF_LOGIC packets to come. */
757 packet.type = SR_DF_META_LOGIC;
758 packet.payload = &meta;
759 meta.samplerate = devc->cur_samplerate;
760 meta.num_probes = devc->num_channels;
761 sr_session_send(cb_data, &packet);
763 if (!(buf = g_try_malloc(PACKET_SIZE))) {
764 sr_err("zp: %s: buf malloc failed", __func__);
765 return SR_ERR_MALLOC;
769 analyzer_read_start(devc->usb->devhdl);
770 /* Send the incoming transfer to the session bus. */
771 n = get_memory_size(devc->memory_size);
772 if (devc->max_memory_size * 4 < n)
773 n = devc->max_memory_size * 4;
774 for (packet_num = 0; packet_num < n / PACKET_SIZE; packet_num++) {
775 res = analyzer_read_data(devc->usb->devhdl, buf, PACKET_SIZE);
776 sr_info("zp: Tried to read %d bytes, actually read %d bytes",
779 packet.type = SR_DF_LOGIC;
780 packet.payload = &logic;
781 logic.length = PACKET_SIZE;
784 sr_session_send(cb_data, &packet);
785 //samples_read += res / 4;
787 analyzer_read_stop(devc->usb->devhdl);
790 packet.type = SR_DF_END;
791 sr_session_send(cb_data, &packet);
796 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
797 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
799 struct sr_datafeed_packet packet;
800 struct dev_context *devc;
802 packet.type = SR_DF_END;
803 sr_session_send(cb_data, &packet);
805 if (!(devc = sdi->priv)) {
806 sr_err("zp: %s: sdi->priv was NULL", __func__);
810 analyzer_reset(devc->usb->devhdl);
811 /* TODO: Need to cancel and free any queued up transfers. */
816 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
817 .name = "zeroplus-logic-cube",
818 .longname = "ZEROPLUS Logic Cube LAP-C series",
821 .cleanup = hw_cleanup,
823 .dev_list = hw_dev_list,
824 .dev_clear = hw_cleanup,
825 .dev_open = hw_dev_open,
826 .dev_close = hw_dev_close,
827 .info_get = hw_info_get,
828 .dev_config_set = hw_dev_config_set,
829 .dev_acquisition_start = hw_dev_acquisition_start,
830 .dev_acquisition_stop = hw_dev_acquisition_stop,