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 {0x0c12, 0x700B, "LAP-C(32128)", 32, 128, 200},
63 {0x0c12, 0x700C, "LAP-C(321000)", 32, 1024, 200},
64 {0x0c12, 0x700D, "LAP-C(322000)", 32, 2048, 200},
65 {0x0c12, 0x700E, "LAP-C(16032)", 16, 32, 100},
66 {0x0c12, 0x7016, "LAP-C(162000)", 16, 2048, 200},
70 static const int hwcaps[] = {
71 SR_HWCAP_LOGIC_ANALYZER,
74 SR_HWCAP_CAPTURE_RATIO,
76 /* These are really implemented in the driver, not the hardware. */
77 SR_HWCAP_LIMIT_SAMPLES,
82 * ZEROPLUS LAP-C (16032) numbers the 16 probes A0-A7 and B0-B7.
83 * We currently ignore other untested/unsupported devices here.
85 static const char *probe_names[NUM_PROBES + 1] = {
105 /* List of struct sr_dev_inst, maintained by dev_open()/dev_close(). */
106 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
107 static struct sr_dev_driver *zdi = &zeroplus_logic_cube_driver_info;
109 static libusb_context *usb_context = NULL;
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 limit_samples;
153 int num_channels; /* TODO: This isn't initialized before it's needed :( */
154 uint64_t memory_size;
156 uint8_t trigger_mask[NUM_TRIGGER_STAGES];
157 uint8_t trigger_value[NUM_TRIGGER_STAGES];
158 // uint8_t trigger_buffer[NUM_TRIGGER_STAGES];
160 /* TODO: this belongs in the device instance */
161 struct sr_usb_dev_inst *usb;
164 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
166 static int hw_dev_close(struct sr_dev_inst *sdi);
168 static unsigned int get_memory_size(int type)
170 if (type == MEMORY_SIZE_8K)
172 else if (type == MEMORY_SIZE_64K)
174 else if (type == MEMORY_SIZE_128K)
176 else if (type == MEMORY_SIZE_512K)
182 static int configure_probes(const struct sr_dev_inst *sdi, const GSList *probes)
185 const struct sr_probe *probe;
187 int probe_bit, stage, i;
190 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
194 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
195 ctx->trigger_mask[i] = 0;
196 ctx->trigger_value[i] = 0;
200 for (l = probes; l; l = l->next) {
201 probe = (struct sr_probe *)l->data;
202 if (probe->enabled == FALSE)
204 probe_bit = 1 << (probe->index - 1);
205 ctx->probe_mask |= probe_bit;
207 if (probe->trigger) {
209 for (tc = probe->trigger; *tc; tc++) {
210 ctx->trigger_mask[stage] |= probe_bit;
212 ctx->trigger_value[stage] |= probe_bit;
214 if (stage > NUM_TRIGGER_STAGES)
223 static void clear_instances(void)
226 struct sr_dev_inst *sdi;
228 for (l = zdi->instances; l; l = l->next) {
230 /* Properly close all devices... */
232 /* ...and free all their memory. */
233 sr_dev_inst_free(sdi);
235 g_slist_free(zdi->instances);
236 zdi->instances = NULL;
244 static int hw_init(void)
247 if (libusb_init(&usb_context) != 0) {
248 sr_err("zp: Failed to initialize USB.");
255 static GSList *hw_scan(GSList *options)
257 struct sr_dev_inst *sdi;
258 struct sr_probe *probe;
261 struct libusb_device_descriptor des;
262 libusb_device **devlist;
264 int ret, devcnt, i, j;
271 /* Find all ZEROPLUS analyzers and add them to device list. */
273 libusb_get_device_list(usb_context, &devlist); /* TODO: Errors. */
275 for (i = 0; devlist[i]; i++) {
276 ret = libusb_get_device_descriptor(devlist[i], &des);
278 sr_err("zp: failed to get device descriptor: %d", ret);
283 for (j = 0; j < zeroplus_models[j].vid; j++) {
284 if (des.idVendor == zeroplus_models[j].vid &&
285 des.idProduct == zeroplus_models[j].pid) {
286 prof = &zeroplus_models[j];
289 /* Skip if the device was not found */
292 sr_info("zp: Found ZEROPLUS model %s", prof->model_name);
294 /* Register the device with libsigrok. */
295 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
296 VENDOR_NAME, prof->model_name, NULL))) {
297 sr_err("zp: %s: sr_dev_inst_new failed", __func__);
302 /* Allocate memory for our private driver context. */
303 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
304 sr_err("zp: %s: ctx malloc failed", __func__);
308 ctx->num_channels = prof->channels;
309 ctx->memory_size = prof->sample_depth * 1024;
310 // memset(ctx->trigger_buffer, 0, NUM_TRIGGER_STAGES);
312 /* Fill in probelist according to this device's profile. */
313 for (j = 0; j < ctx->num_channels; j++) {
314 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
317 sdi->probes = g_slist_append(sdi->probes, probe);
320 devices = g_slist_append(devices, sdi);
321 zdi->instances = g_slist_append(zdi->instances, sdi);
322 ctx->usb = sr_usb_dev_inst_new(
323 libusb_get_bus_number(devlist[i]),
324 libusb_get_device_address(devlist[i]), NULL);
328 libusb_free_device_list(devlist, 1);
333 static int hw_dev_open(struct sr_dev_inst *sdi)
336 libusb_device **devlist, *dev;
337 struct libusb_device_descriptor des;
338 int device_count, ret, i;
340 if (!(ctx = sdi->priv)) {
341 sr_err("zp: %s: sdi->priv was NULL", __func__);
345 device_count = libusb_get_device_list(usb_context, &devlist);
346 if (device_count < 0) {
347 sr_err("zp: Failed to retrieve device list");
352 for (i = 0; i < device_count; i++) {
353 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
354 sr_err("fx2lafw: Failed to get device descriptor: %d.",
358 if (libusb_get_bus_number(devlist[i]) == ctx->usb->bus
359 && libusb_get_device_address(devlist[i]) == ctx->usb->address) {
365 sr_err("device on bus %d address %d disappeared!",
366 ctx->usb->bus, ctx->usb->address);
370 if (!(ret = libusb_open(dev, &(ctx->usb->devhdl)))) {
371 sdi->status = SR_ST_ACTIVE;
372 sr_info("zp: opened device %d on %d.%d interface %d",
373 sdi->index, ctx->usb->bus,
374 ctx->usb->address, USB_INTERFACE);
376 sr_err("zp: failed to open device: %d", ret);
380 ret = libusb_set_configuration(ctx->usb->devhdl, USB_CONFIGURATION);
382 sr_err("zp: Unable to set USB configuration %d: %d",
383 USB_CONFIGURATION, ret);
387 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
389 sr_err("zp: Unable to claim interface: %d", ret);
393 analyzer_reset(ctx->usb->devhdl);
394 analyzer_initialize(ctx->usb->devhdl);
396 analyzer_set_memory_size(MEMORY_SIZE_512K);
397 // analyzer_set_freq(g_freq, g_freq_scale);
398 analyzer_set_trigger_count(1);
399 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
400 // * get_memory_size(g_memory_size)) / 100) >> 2);
401 analyzer_set_ramsize_trigger_address(
402 (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
405 if (g_double_mode == 1)
406 analyzer_set_compression(COMPRESSION_DOUBLE);
407 else if (g_compression == 1)
408 analyzer_set_compression(COMPRESSION_ENABLE);
411 analyzer_set_compression(COMPRESSION_NONE);
413 if (ctx->cur_samplerate == 0) {
414 /* Samplerate hasn't been set. Default to the slowest one. */
415 if (hw_dev_config_set(sdi, SR_HWCAP_SAMPLERATE,
416 &samplerates.list[0]) == SR_ERR)
423 static int hw_dev_close(struct sr_dev_inst *sdi)
427 if (!(ctx = sdi->priv)) {
428 sr_err("zp: %s: sdi->priv was NULL", __func__);
432 if (!ctx->usb->devhdl)
435 sr_info("zp: closing device %d on %d.%d interface %d", sdi->index,
436 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
437 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
438 libusb_reset_device(ctx->usb->devhdl);
439 libusb_close(ctx->usb->devhdl);
440 ctx->usb->devhdl = NULL;
441 sdi->status = SR_ST_INACTIVE;
446 static int hw_cleanup(void)
452 libusb_exit(usb_context);
458 static int hw_info_get(int info_id, const void **data,
459 const struct sr_dev_inst *sdi)
466 sr_spew("zp: %s: Returning sdi.", __func__);
471 case SR_DI_NUM_PROBES:
474 *data = GINT_TO_POINTER(ctx->num_channels);
475 sr_spew("zp: %s: Returning number of channels: %d.",
476 __func__, ctx->num_channels);
480 case SR_DI_PROBE_NAMES:
482 sr_spew("zp: %s: Returning probenames.", __func__);
484 case SR_DI_SAMPLERATES:
485 *data = &samplerates;
486 sr_spew("zp: %s: Returning samplerates.", __func__);
488 case SR_DI_TRIGGER_TYPES:
489 *data = TRIGGER_TYPES;
490 sr_spew("zp: %s: Returning triggertypes: %s.", __func__, TRIGGER_TYPES);
492 case SR_DI_CUR_SAMPLERATE:
495 *data = &ctx->cur_samplerate;
496 sr_spew("zp: %s: Returning samplerate: %" PRIu64 "Hz.",
497 __func__, ctx->cur_samplerate);
508 static int hw_dev_status_get(int dev_index)
510 struct sr_dev_inst *sdi;
512 sdi = sr_dev_inst_get(zdi->instances, dev_index);
516 return SR_ST_NOT_FOUND;
519 static int set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
524 sr_err("zp: %s: sdi was NULL", __func__);
528 if (!(ctx = sdi->priv)) {
529 sr_err("zp: %s: sdi->priv was NULL", __func__);
533 sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate);
535 if (samplerate > SR_MHZ(1))
536 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
537 else if (samplerate > SR_KHZ(1))
538 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
540 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
542 ctx->cur_samplerate = samplerate;
547 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
552 if (!(ctx = sdi->priv)) {
553 sr_err("zp: %s: sdi->priv was NULL", __func__);
558 case SR_HWCAP_SAMPLERATE:
559 return set_samplerate(sdi, *(const uint64_t *)value);
560 case SR_HWCAP_PROBECONFIG:
561 return configure_probes(sdi, (const GSList *)value);
562 case SR_HWCAP_LIMIT_SAMPLES:
563 ctx->limit_samples = *(const uint64_t *)value;
570 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
573 struct sr_datafeed_packet packet;
574 struct sr_datafeed_logic logic;
575 struct sr_datafeed_header header;
576 struct sr_datafeed_meta_logic meta;
577 uint64_t samples_read;
579 unsigned int packet_num;
583 if (!(ctx = sdi->priv)) {
584 sr_err("zp: %s: sdi->priv was NULL", __func__);
588 /* push configured settings to device */
589 analyzer_configure(ctx->usb->devhdl);
591 analyzer_start(ctx->usb->devhdl);
592 sr_info("zp: Waiting for data");
593 analyzer_wait_data(ctx->usb->devhdl);
595 sr_info("zp: Stop address = 0x%x",
596 analyzer_get_stop_address(ctx->usb->devhdl));
597 sr_info("zp: Now address = 0x%x",
598 analyzer_get_now_address(ctx->usb->devhdl));
599 sr_info("zp: Trigger address = 0x%x",
600 analyzer_get_trigger_address(ctx->usb->devhdl));
602 packet.type = SR_DF_HEADER;
603 packet.payload = &header;
604 header.feed_version = 1;
605 gettimeofday(&header.starttime, NULL);
606 sr_session_send(cb_data, &packet);
608 /* Send metadata about the SR_DF_LOGIC packets to come. */
609 packet.type = SR_DF_META_LOGIC;
610 packet.payload = &meta;
611 meta.samplerate = ctx->cur_samplerate;
612 meta.num_probes = ctx->num_channels;
613 sr_session_send(cb_data, &packet);
615 if (!(buf = g_try_malloc(PACKET_SIZE))) {
616 sr_err("zp: %s: buf malloc failed", __func__);
617 return SR_ERR_MALLOC;
621 analyzer_read_start(ctx->usb->devhdl);
622 /* Send the incoming transfer to the session bus. */
623 for (packet_num = 0; packet_num < (ctx->memory_size * 4 / PACKET_SIZE);
625 res = analyzer_read_data(ctx->usb->devhdl, buf, PACKET_SIZE);
626 sr_info("zp: Tried to read %llx bytes, actually read %x bytes",
629 packet.type = SR_DF_LOGIC;
630 packet.payload = &logic;
631 logic.length = PACKET_SIZE;
634 sr_session_send(cb_data, &packet);
635 samples_read += res / 4;
637 analyzer_read_stop(ctx->usb->devhdl);
640 packet.type = SR_DF_END;
641 sr_session_send(cb_data, &packet);
646 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
647 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
650 struct sr_datafeed_packet packet;
653 packet.type = SR_DF_END;
654 sr_session_send(cb_data, &packet);
656 if (!(ctx = sdi->priv)) {
657 sr_err("zp: %s: sdi->priv was NULL", __func__);
661 analyzer_reset(ctx->usb->devhdl);
662 /* TODO: Need to cancel and free any queued up transfers. */
667 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
668 .name = "zeroplus-logic-cube",
669 .longname = "ZEROPLUS Logic Cube LAP-C series",
672 .cleanup = hw_cleanup,
674 .dev_open = hw_dev_open,
675 .dev_close = hw_dev_close,
676 .info_get = hw_info_get,
677 .dev_status_get = hw_dev_status_get,
678 .dev_config_set = hw_dev_config_set,
679 .dev_acquisition_start = hw_dev_acquisition_start,
680 .dev_acquisition_stop = hw_dev_acquisition_stop,