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 /* ?? */
50 unsigned int channels;
51 unsigned int sample_depth; /* In Ksamples/channel */
52 unsigned int max_sampling_freq;
56 * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the
57 * same 128K sample depth.
59 static model_t zeroplus_models[] = {
60 {0x0c12, 0x7009, "LAP-C(16064)", 16, 64, 100},
61 {0x0c12, 0x700A, "LAP-C(16128)", 16, 128, 200},
62 /* TODO: we don't know anything about these
63 {0x0c12, 0x700B, "LAP-C(32128)", 32, 128, 200},
64 {0x0c12, 0x700C, "LAP-C(321000)", 32, 1024, 200},
65 {0x0c12, 0x700D, "LAP-C(322000)", 32, 2048, 200},
67 {0x0c12, 0x700E, "LAP-C(16032)", 16, 32, 100},
68 {0x0c12, 0x7016, "LAP-C(162000)", 16, 2048, 200},
72 static const int hwcaps[] = {
73 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 *zdi = &zeroplus_logic_cube_driver_info;
111 static libusb_context *usb_context = NULL;
114 * The hardware supports more samplerates than these, but these are the
115 * options hardcoded into the vendor's Windows GUI.
119 * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up
122 static const uint64_t supported_samplerates[] = {
144 static const struct sr_samplerates samplerates = {
148 supported_samplerates,
151 /* Private, per-device-instance driver context. */
153 uint64_t cur_samplerate;
154 uint64_t limit_samples;
155 int num_channels; /* TODO: This isn't initialized before it's needed :( */
156 uint64_t memory_size;
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 /* TODO: this belongs in the device instance */
163 struct sr_usb_dev_inst *usb;
166 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
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)
184 static int configure_probes(const struct sr_dev_inst *sdi, const GSList *probes)
187 const struct sr_probe *probe;
189 int probe_bit, stage, i;
192 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
196 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
197 ctx->trigger_mask[i] = 0;
198 ctx->trigger_value[i] = 0;
202 for (l = probes; l; l = l->next) {
203 probe = (struct sr_probe *)l->data;
204 if (probe->enabled == FALSE)
206 probe_bit = 1 << (probe->index);
207 ctx->probe_mask |= probe_bit;
209 if (probe->trigger) {
211 for (tc = probe->trigger; *tc; tc++) {
212 ctx->trigger_mask[stage] |= probe_bit;
214 ctx->trigger_value[stage] |= probe_bit;
216 if (stage > NUM_TRIGGER_STAGES)
225 static void clear_instances(void)
228 struct sr_dev_inst *sdi;
230 for (l = zdi->instances; l; l = l->next) {
232 /* Properly close all devices... */
234 /* ...and free all their memory. */
235 sr_dev_inst_free(sdi);
237 g_slist_free(zdi->instances);
238 zdi->instances = NULL;
246 static int hw_init(void)
249 if (libusb_init(&usb_context) != 0) {
250 sr_err("zp: Failed to initialize USB.");
257 static GSList *hw_scan(GSList *options)
259 struct sr_dev_inst *sdi;
260 struct sr_probe *probe;
263 struct libusb_device_descriptor des;
264 libusb_device **devlist;
266 int ret, devcnt, i, j;
273 /* Find all ZEROPLUS analyzers and add them to device list. */
275 libusb_get_device_list(usb_context, &devlist); /* TODO: Errors. */
277 for (i = 0; devlist[i]; i++) {
278 ret = libusb_get_device_descriptor(devlist[i], &des);
280 sr_err("zp: failed to get device descriptor: %d", ret);
285 for (j = 0; j < zeroplus_models[j].vid; j++) {
286 if (des.idVendor == zeroplus_models[j].vid &&
287 des.idProduct == zeroplus_models[j].pid) {
288 prof = &zeroplus_models[j];
291 /* Skip if the device was not found */
294 sr_info("zp: Found ZEROPLUS model %s", prof->model_name);
296 /* Register the device with libsigrok. */
297 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
298 VENDOR_NAME, prof->model_name, NULL))) {
299 sr_err("zp: %s: sr_dev_inst_new failed", __func__);
304 /* Allocate memory for our private driver context. */
305 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
306 sr_err("zp: %s: ctx malloc failed", __func__);
310 ctx->num_channels = prof->channels;
311 ctx->memory_size = prof->sample_depth * 1024;
312 // memset(ctx->trigger_buffer, 0, NUM_TRIGGER_STAGES);
314 /* Fill in probelist according to this device's profile. */
315 for (j = 0; j < ctx->num_channels; j++) {
316 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
319 sdi->probes = g_slist_append(sdi->probes, probe);
322 devices = g_slist_append(devices, sdi);
323 zdi->instances = g_slist_append(zdi->instances, sdi);
324 ctx->usb = sr_usb_dev_inst_new(
325 libusb_get_bus_number(devlist[i]),
326 libusb_get_device_address(devlist[i]), NULL);
330 libusb_free_device_list(devlist, 1);
335 static int hw_dev_open(struct sr_dev_inst *sdi)
338 libusb_device **devlist, *dev;
339 struct libusb_device_descriptor des;
340 int device_count, ret, i;
342 if (!(ctx = sdi->priv)) {
343 sr_err("zp: %s: sdi->priv was NULL", __func__);
347 device_count = libusb_get_device_list(usb_context, &devlist);
348 if (device_count < 0) {
349 sr_err("zp: Failed to retrieve device list");
354 for (i = 0; i < device_count; i++) {
355 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
356 sr_err("fx2lafw: Failed to get device descriptor: %d.",
360 if (libusb_get_bus_number(devlist[i]) == ctx->usb->bus
361 && libusb_get_device_address(devlist[i]) == ctx->usb->address) {
367 sr_err("device on bus %d address %d disappeared!",
368 ctx->usb->bus, ctx->usb->address);
372 if (!(ret = libusb_open(dev, &(ctx->usb->devhdl)))) {
373 sdi->status = SR_ST_ACTIVE;
374 sr_info("zp: opened device %d on %d.%d interface %d",
375 sdi->index, ctx->usb->bus,
376 ctx->usb->address, USB_INTERFACE);
378 sr_err("zp: failed to open device: %d", ret);
382 ret = libusb_set_configuration(ctx->usb->devhdl, USB_CONFIGURATION);
384 sr_err("zp: Unable to set USB configuration %d: %d",
385 USB_CONFIGURATION, ret);
389 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
391 sr_err("zp: Unable to claim interface: %d", ret);
395 analyzer_reset(ctx->usb->devhdl);
396 analyzer_initialize(ctx->usb->devhdl);
398 analyzer_set_memory_size(MEMORY_SIZE_512K);
399 // analyzer_set_freq(g_freq, g_freq_scale);
400 analyzer_set_trigger_count(1);
401 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
402 // * get_memory_size(g_memory_size)) / 100) >> 2);
403 analyzer_set_ramsize_trigger_address(
404 (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
407 if (g_double_mode == 1)
408 analyzer_set_compression(COMPRESSION_DOUBLE);
409 else if (g_compression == 1)
410 analyzer_set_compression(COMPRESSION_ENABLE);
413 analyzer_set_compression(COMPRESSION_NONE);
415 if (ctx->cur_samplerate == 0) {
416 /* Samplerate hasn't been set. Default to the slowest one. */
417 if (hw_dev_config_set(sdi, SR_HWCAP_SAMPLERATE,
418 &samplerates.list[0]) == SR_ERR)
425 static int hw_dev_close(struct sr_dev_inst *sdi)
429 if (!(ctx = sdi->priv)) {
430 sr_err("zp: %s: sdi->priv was NULL", __func__);
434 if (!ctx->usb->devhdl)
437 sr_info("zp: closing device %d on %d.%d interface %d", sdi->index,
438 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
439 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
440 libusb_reset_device(ctx->usb->devhdl);
441 libusb_close(ctx->usb->devhdl);
442 ctx->usb->devhdl = NULL;
443 sdi->status = SR_ST_INACTIVE;
448 static int hw_cleanup(void)
454 libusb_exit(usb_context);
460 static int hw_info_get(int info_id, const void **data,
461 const struct sr_dev_inst *sdi)
469 case SR_DI_NUM_PROBES:
472 *data = GINT_TO_POINTER(ctx->num_channels);
473 sr_spew("zp: %s: Returning number of channels: %d.",
474 __func__, ctx->num_channels);
478 case SR_DI_PROBE_NAMES:
480 sr_spew("zp: %s: Returning probenames.", __func__);
482 case SR_DI_SAMPLERATES:
483 *data = &samplerates;
484 sr_spew("zp: %s: Returning samplerates.", __func__);
486 case SR_DI_TRIGGER_TYPES:
487 *data = TRIGGER_TYPES;
488 sr_spew("zp: %s: Returning triggertypes: %s.", __func__, TRIGGER_TYPES);
490 case SR_DI_CUR_SAMPLERATE:
493 *data = &ctx->cur_samplerate;
494 sr_spew("zp: %s: Returning samplerate: %" PRIu64 "Hz.",
495 __func__, ctx->cur_samplerate);
506 static int set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
511 sr_err("zp: %s: sdi was NULL", __func__);
515 if (!(ctx = sdi->priv)) {
516 sr_err("zp: %s: sdi->priv was NULL", __func__);
520 sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate);
522 if (samplerate > SR_MHZ(1))
523 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
524 else if (samplerate > SR_KHZ(1))
525 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
527 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
529 ctx->cur_samplerate = samplerate;
534 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
539 if (!(ctx = sdi->priv)) {
540 sr_err("zp: %s: sdi->priv was NULL", __func__);
545 case SR_HWCAP_SAMPLERATE:
546 return set_samplerate(sdi, *(const uint64_t *)value);
547 case SR_HWCAP_PROBECONFIG:
548 return configure_probes(sdi, (const GSList *)value);
549 case SR_HWCAP_LIMIT_SAMPLES:
550 ctx->limit_samples = *(const uint64_t *)value;
557 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
560 struct sr_datafeed_packet packet;
561 struct sr_datafeed_logic logic;
562 struct sr_datafeed_header header;
563 struct sr_datafeed_meta_logic meta;
564 uint64_t samples_read;
566 unsigned int packet_num;
570 if (!(ctx = sdi->priv)) {
571 sr_err("zp: %s: sdi->priv was NULL", __func__);
575 /* push configured settings to device */
576 analyzer_configure(ctx->usb->devhdl);
578 analyzer_start(ctx->usb->devhdl);
579 sr_info("zp: Waiting for data");
580 analyzer_wait_data(ctx->usb->devhdl);
582 sr_info("zp: Stop address = 0x%x",
583 analyzer_get_stop_address(ctx->usb->devhdl));
584 sr_info("zp: Now address = 0x%x",
585 analyzer_get_now_address(ctx->usb->devhdl));
586 sr_info("zp: Trigger address = 0x%x",
587 analyzer_get_trigger_address(ctx->usb->devhdl));
589 packet.type = SR_DF_HEADER;
590 packet.payload = &header;
591 header.feed_version = 1;
592 gettimeofday(&header.starttime, NULL);
593 sr_session_send(cb_data, &packet);
595 /* Send metadata about the SR_DF_LOGIC packets to come. */
596 packet.type = SR_DF_META_LOGIC;
597 packet.payload = &meta;
598 meta.samplerate = ctx->cur_samplerate;
599 meta.num_probes = ctx->num_channels;
600 sr_session_send(cb_data, &packet);
602 if (!(buf = g_try_malloc(PACKET_SIZE))) {
603 sr_err("zp: %s: buf malloc failed", __func__);
604 return SR_ERR_MALLOC;
608 analyzer_read_start(ctx->usb->devhdl);
609 /* Send the incoming transfer to the session bus. */
610 for (packet_num = 0; packet_num < (ctx->memory_size * 4 / PACKET_SIZE);
612 res = analyzer_read_data(ctx->usb->devhdl, buf, PACKET_SIZE);
613 sr_info("zp: Tried to read %llx bytes, actually read %x bytes",
616 packet.type = SR_DF_LOGIC;
617 packet.payload = &logic;
618 logic.length = PACKET_SIZE;
621 sr_session_send(cb_data, &packet);
622 samples_read += res / 4;
624 analyzer_read_stop(ctx->usb->devhdl);
627 packet.type = SR_DF_END;
628 sr_session_send(cb_data, &packet);
633 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
634 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
637 struct sr_datafeed_packet packet;
640 packet.type = SR_DF_END;
641 sr_session_send(cb_data, &packet);
643 if (!(ctx = sdi->priv)) {
644 sr_err("zp: %s: sdi->priv was NULL", __func__);
648 analyzer_reset(ctx->usb->devhdl);
649 /* TODO: Need to cancel and free any queued up transfers. */
654 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
655 .name = "zeroplus-logic-cube",
656 .longname = "ZEROPLUS Logic Cube LAP-C series",
659 .cleanup = hw_cleanup,
661 .dev_open = hw_dev_open,
662 .dev_close = hw_dev_close,
663 .info_get = hw_info_get,
664 .dev_config_set = hw_dev_config_set,
665 .dev_acquisition_start = hw_dev_acquisition_start,
666 .dev_acquisition_stop = hw_dev_acquisition_stop,