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/>.
28 #include "libsigrok.h"
29 #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_TYPES "01"
44 #define PACKET_SIZE 2048 /* ?? */
49 unsigned int channels;
50 unsigned int sample_depth; /* In Ksamples/channel */
51 unsigned int max_sampling_freq;
55 * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the
56 * same 128K sample depth.
58 static model_t zeroplus_models[] = {
59 {0x7009, "LAP-C(16064)", 16, 64, 100},
60 {0x700A, "LAP-C(16128)", 16, 128, 200},
61 {0x700B, "LAP-C(32128)", 32, 128, 200},
62 {0x700C, "LAP-C(321000)", 32, 1024, 200},
63 {0x700D, "LAP-C(322000)", 32, 2048, 200},
64 {0x700E, "LAP-C(16032)", 16, 32, 100},
65 {0x7016, "LAP-C(162000)", 16, 2048, 200},
68 static const int hwcaps[] = {
69 SR_HWCAP_LOGIC_ANALYZER,
72 SR_HWCAP_CAPTURE_RATIO,
74 /* These are really implemented in the driver, not the hardware. */
75 SR_HWCAP_LIMIT_SAMPLES,
80 * ZEROPLUS LAP-C (16032) numbers the 16 probes A0-A7 and B0-B7.
81 * We currently ignore other untested/unsupported devices here.
83 static const char *probe_names[NUM_PROBES + 1] = {
103 /* List of struct sr_dev_inst, maintained by dev_open()/dev_close(). */
104 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
105 static struct sr_dev_driver *zdi = &zeroplus_logic_cube_driver_info;
107 static libusb_context *usb_context = NULL;
110 * The hardware supports more samplerates than these, but these are the
111 * options hardcoded into the vendor's Windows GUI.
115 * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up
118 static const uint64_t supported_samplerates[] = {
140 static const struct sr_samplerates samplerates = {
144 supported_samplerates,
147 /* Private, per-device-instance driver context. */
149 uint64_t cur_samplerate;
150 uint64_t limit_samples;
151 int num_channels; /* TODO: This isn't initialized before it's needed :( */
152 uint64_t memory_size;
154 uint8_t trigger_mask[NUM_TRIGGER_STAGES];
155 uint8_t trigger_value[NUM_TRIGGER_STAGES];
156 // uint8_t trigger_buffer[NUM_TRIGGER_STAGES];
158 /* TODO: this belongs in the device instance */
159 struct sr_usb_dev_inst *usb;
162 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
164 static int hw_dev_close(struct sr_dev_inst *sdi);
166 static unsigned int get_memory_size(int type)
168 if (type == MEMORY_SIZE_8K)
170 else if (type == MEMORY_SIZE_64K)
172 else if (type == MEMORY_SIZE_128K)
174 else if (type == MEMORY_SIZE_512K)
180 static int opendev4(struct sr_dev_inst **sdi, libusb_device *dev,
181 struct libusb_device_descriptor *des)
187 /* Note: sdi is non-NULL, the caller already checked this. */
189 if (!(ctx = (*sdi)->priv)) {
190 sr_err("zp: %s: (*sdi)->priv was NULL", __func__);
194 if ((ret = libusb_get_device_descriptor(dev, des))) {
195 sr_err("zp: failed to get device descriptor: %d", ret);
199 if (des->idVendor != USB_VENDOR)
202 if (libusb_get_bus_number(dev) == ctx->usb->bus
203 && libusb_get_device_address(dev) == ctx->usb->address) {
205 for (i = 0; i < ARRAY_SIZE(zeroplus_models); i++) {
206 if (!(des->idProduct == zeroplus_models[i].pid))
209 sr_info("zp: Found ZEROPLUS device 0x%04x (%s)",
210 des->idProduct, zeroplus_models[i].model_name);
211 ctx->num_channels = zeroplus_models[i].channels;
212 ctx->memory_size = zeroplus_models[i].sample_depth * 1024;
216 if (ctx->num_channels == 0) {
217 sr_err("zp: Unknown ZEROPLUS device 0x%04x",
223 if (!(ret = libusb_open(dev, &(ctx->usb->devhdl)))) {
224 (*sdi)->status = SR_ST_ACTIVE;
225 sr_info("zp: opened device %d on %d.%d interface %d",
226 (*sdi)->index, ctx->usb->bus,
227 ctx->usb->address, USB_INTERFACE);
229 sr_err("zp: failed to open device: %d", ret);
237 static struct sr_dev_inst *zp_open_dev(int dev_index)
239 struct sr_dev_inst *sdi;
240 libusb_device **devlist;
241 struct libusb_device_descriptor des;
244 if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index)))
247 libusb_get_device_list(usb_context, &devlist);
248 if (sdi->status == SR_ST_INACTIVE) {
249 /* Find the device by vendor, product, bus and address. */
250 libusb_get_device_list(usb_context, &devlist);
251 for (i = 0; devlist[i]; i++) {
252 /* TODO: Error handling. */
253 opendev4(&sdi, devlist[i], &des);
256 /* Status must be SR_ST_ACTIVE, i.e. already in use... */
259 libusb_free_device_list(devlist, 1);
261 if (sdi && sdi->status != SR_ST_ACTIVE)
267 static int configure_probes(const struct sr_dev_inst *sdi, const GSList *probes)
270 const struct sr_probe *probe;
272 int probe_bit, stage, i;
275 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
279 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
280 ctx->trigger_mask[i] = 0;
281 ctx->trigger_value[i] = 0;
285 for (l = probes; l; l = l->next) {
286 probe = (struct sr_probe *)l->data;
287 if (probe->enabled == FALSE)
289 probe_bit = 1 << (probe->index - 1);
290 ctx->probe_mask |= probe_bit;
292 if (probe->trigger) {
294 for (tc = probe->trigger; *tc; tc++) {
295 ctx->trigger_mask[stage] |= probe_bit;
297 ctx->trigger_value[stage] |= probe_bit;
299 if (stage > NUM_TRIGGER_STAGES)
308 static void clear_instances(void)
311 struct sr_dev_inst *sdi;
313 for (l = zdi->instances; l; l = l->next) {
315 /* Properly close all devices... */
317 /* ...and free all their memory. */
318 sr_dev_inst_free(sdi);
320 g_slist_free(zdi->instances);
321 zdi->instances = NULL;
329 static int hw_init(void)
332 if (libusb_init(&usb_context) != 0) {
333 sr_err("zp: Failed to initialize USB.");
340 static GSList *hw_scan(GSList *options)
342 struct sr_dev_inst *sdi;
343 struct libusb_device_descriptor des;
345 libusb_device **devlist;
354 /* Allocate memory for our private driver context. */
355 if (!(ctx = g_try_malloc(sizeof(struct context)))) {
356 sr_err("zp: %s: ctx malloc failed", __func__);
360 /* Set some sane defaults. */
361 ctx->cur_samplerate = 0;
362 ctx->limit_samples = 0;
363 /* TODO: num_channels isn't initialized before it's needed :( */
364 ctx->num_channels = NUM_PROBES;
365 ctx->memory_size = 0;
367 memset(ctx->trigger_mask, 0, NUM_TRIGGER_STAGES);
368 memset(ctx->trigger_value, 0, NUM_TRIGGER_STAGES);
369 // memset(ctx->trigger_buffer, 0, NUM_TRIGGER_STAGES);
371 /* Find all ZEROPLUS analyzers and add them to device list. */
373 libusb_get_device_list(usb_context, &devlist); /* TODO: Errors. */
375 for (i = 0; devlist[i]; i++) {
376 ret = libusb_get_device_descriptor(devlist[i], &des);
378 sr_err("zp: failed to get device descriptor: %d", ret);
382 if (des.idVendor == USB_VENDOR) {
384 * Definitely a ZEROPLUS.
385 * TODO: Any way to detect specific model/version in
386 * the ZEROPLUS range?
388 /* Register the device with libsigrok. */
389 if (!(sdi = sr_dev_inst_new(devcnt,
390 SR_ST_INACTIVE, VENDOR_NAME,
391 MODEL_NAME, MODEL_VERSION))) {
392 sr_err("zp: %s: sr_dev_inst_new failed",
399 devices = g_slist_append(devices, sdi);
400 zdi->instances = g_slist_append(zdi->instances, sdi);
401 ctx->usb = sr_usb_dev_inst_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_dev_open(struct sr_dev_inst *sdi)
417 if (!(ctx = sdi->priv)) {
418 sr_err("zp: %s: sdi->priv was NULL", __func__);
422 ret = libusb_set_configuration(ctx->usb->devhdl, USB_CONFIGURATION);
424 sr_err("zp: Unable to set USB configuration %d: %d",
425 USB_CONFIGURATION, ret);
429 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
431 sr_err("zp: Unable to claim interface: %d", ret);
435 analyzer_reset(ctx->usb->devhdl);
436 analyzer_initialize(ctx->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 (ctx->cur_samplerate == 0) {
456 /* Samplerate hasn't been set. Default to the slowest one. */
457 if (hw_dev_config_set(sdi, SR_HWCAP_SAMPLERATE,
458 &samplerates.list[0]) == SR_ERR)
465 static int hw_dev_close(struct sr_dev_inst *sdi)
469 if (!(ctx = sdi->priv)) {
470 sr_err("zp: %s: sdi->priv was NULL", __func__);
474 if (!ctx->usb->devhdl)
477 sr_info("zp: closing device %d on %d.%d interface %d", sdi->index,
478 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
479 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
480 libusb_reset_device(ctx->usb->devhdl);
481 libusb_close(ctx->usb->devhdl);
482 ctx->usb->devhdl = NULL;
483 sdi->status = SR_ST_INACTIVE;
488 static int hw_cleanup(void)
494 libusb_exit(usb_context);
500 static int hw_info_get(int info_id, const void **data,
501 const struct sr_dev_inst *sdi)
508 sr_spew("zp: %s: Returning sdi.", __func__);
513 case SR_DI_NUM_PROBES:
516 *data = GINT_TO_POINTER(ctx->num_channels);
517 sr_spew("zp: %s: Returning number of channels: %d.",
518 __func__, ctx->num_channels);
522 case SR_DI_PROBE_NAMES:
524 sr_spew("zp: %s: Returning probenames.", __func__);
526 case SR_DI_SAMPLERATES:
527 *data = &samplerates;
528 sr_spew("zp: %s: Returning samplerates.", __func__);
530 case SR_DI_TRIGGER_TYPES:
531 *data = TRIGGER_TYPES;
532 sr_spew("zp: %s: Returning triggertypes: %s.", __func__, TRIGGER_TYPES);
534 case SR_DI_CUR_SAMPLERATE:
537 *data = &ctx->cur_samplerate;
538 sr_spew("zp: %s: Returning samplerate: %" PRIu64 "Hz.",
539 __func__, ctx->cur_samplerate);
550 static int hw_dev_status_get(int dev_index)
552 struct sr_dev_inst *sdi;
554 sdi = sr_dev_inst_get(zdi->instances, dev_index);
558 return SR_ST_NOT_FOUND;
561 static int set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
566 sr_err("zp: %s: sdi was NULL", __func__);
570 if (!(ctx = 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 ctx->cur_samplerate = samplerate;
589 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
594 if (!(ctx = sdi->priv)) {
595 sr_err("zp: %s: sdi->priv was NULL", __func__);
600 case SR_HWCAP_SAMPLERATE:
601 return set_samplerate(sdi, *(const uint64_t *)value);
602 case SR_HWCAP_PROBECONFIG:
603 return configure_probes(sdi, (const GSList *)value);
604 case SR_HWCAP_LIMIT_SAMPLES:
605 ctx->limit_samples = *(const uint64_t *)value;
612 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
615 struct sr_datafeed_packet packet;
616 struct sr_datafeed_logic logic;
617 struct sr_datafeed_header header;
618 struct sr_datafeed_meta_logic meta;
619 uint64_t samples_read;
621 unsigned int packet_num;
625 if (!(ctx = sdi->priv)) {
626 sr_err("zp: %s: sdi->priv was NULL", __func__);
630 /* push configured settings to device */
631 analyzer_configure(ctx->usb->devhdl);
633 analyzer_start(ctx->usb->devhdl);
634 sr_info("zp: Waiting for data");
635 analyzer_wait_data(ctx->usb->devhdl);
637 sr_info("zp: Stop address = 0x%x",
638 analyzer_get_stop_address(ctx->usb->devhdl));
639 sr_info("zp: Now address = 0x%x",
640 analyzer_get_now_address(ctx->usb->devhdl));
641 sr_info("zp: Trigger address = 0x%x",
642 analyzer_get_trigger_address(ctx->usb->devhdl));
644 packet.type = SR_DF_HEADER;
645 packet.payload = &header;
646 header.feed_version = 1;
647 gettimeofday(&header.starttime, NULL);
648 sr_session_send(cb_data, &packet);
650 /* Send metadata about the SR_DF_LOGIC packets to come. */
651 packet.type = SR_DF_META_LOGIC;
652 packet.payload = &meta;
653 meta.samplerate = ctx->cur_samplerate;
654 meta.num_probes = ctx->num_channels;
655 sr_session_send(cb_data, &packet);
657 if (!(buf = g_try_malloc(PACKET_SIZE))) {
658 sr_err("zp: %s: buf malloc failed", __func__);
659 return SR_ERR_MALLOC;
663 analyzer_read_start(ctx->usb->devhdl);
664 /* Send the incoming transfer to the session bus. */
665 for (packet_num = 0; packet_num < (ctx->memory_size * 4 / PACKET_SIZE);
667 res = analyzer_read_data(ctx->usb->devhdl, buf, PACKET_SIZE);
668 sr_info("zp: Tried to read %llx bytes, actually read %x bytes",
671 packet.type = SR_DF_LOGIC;
672 packet.payload = &logic;
673 logic.length = PACKET_SIZE;
676 sr_session_send(cb_data, &packet);
677 samples_read += res / 4;
679 analyzer_read_stop(ctx->usb->devhdl);
682 packet.type = SR_DF_END;
683 sr_session_send(cb_data, &packet);
688 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
689 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
692 struct sr_datafeed_packet packet;
695 packet.type = SR_DF_END;
696 sr_session_send(cb_data, &packet);
698 if (!(ctx = sdi->priv)) {
699 sr_err("zp: %s: sdi->priv was NULL", __func__);
703 analyzer_reset(ctx->usb->devhdl);
704 /* TODO: Need to cancel and free any queued up transfers. */
709 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
710 .name = "zeroplus-logic-cube",
711 .longname = "ZEROPLUS Logic Cube LAP-C series",
714 .cleanup = hw_cleanup,
716 .dev_open = hw_dev_open,
717 .dev_close = hw_dev_close,
718 .info_get = hw_info_get,
719 .dev_status_get = hw_dev_status_get,
720 .dev_config_set = hw_dev_config_set,
721 .dev_acquisition_start = hw_dev_acquisition_start,
722 .dev_acquisition_stop = hw_dev_acquisition_stop,