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,
75 SR_HWCAP_CAPTURE_RATIO,
77 /* These are really implemented in the driver, not the hardware. */
78 SR_HWCAP_LIMIT_SAMPLES,
83 * ZEROPLUS LAP-C (16032) numbers the 16 probes A0-A7 and B0-B7.
84 * We currently ignore other untested/unsupported devices here.
86 static const char *probe_names[NUM_PROBES + 1] = {
106 /* List of struct sr_dev_inst, maintained by dev_open()/dev_close(). */
107 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
108 static struct sr_dev_driver *zdi = &zeroplus_logic_cube_driver_info;
110 static libusb_context *usb_context = NULL;
113 * The hardware supports more samplerates than these, but these are the
114 * options hardcoded into the vendor's Windows GUI.
118 * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up
121 static const uint64_t supported_samplerates[] = {
143 static const struct sr_samplerates samplerates = {
147 supported_samplerates,
150 /* Private driver context. */
155 /* Private, per-device-instance driver context. */
157 uint64_t cur_samplerate;
158 uint64_t limit_samples;
159 int num_channels; /* TODO: This isn't initialized before it's needed :( */
160 uint64_t memory_size;
162 uint8_t trigger_mask[NUM_TRIGGER_STAGES];
163 uint8_t trigger_value[NUM_TRIGGER_STAGES];
164 // uint8_t trigger_buffer[NUM_TRIGGER_STAGES];
166 /* TODO: this belongs in the device instance */
167 struct sr_usb_dev_inst *usb;
170 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
172 static int hw_dev_close(struct sr_dev_inst *sdi);
174 static unsigned int get_memory_size(int type)
176 if (type == MEMORY_SIZE_8K)
178 else if (type == MEMORY_SIZE_64K)
180 else if (type == MEMORY_SIZE_128K)
182 else if (type == MEMORY_SIZE_512K)
188 static int configure_probes(const struct sr_dev_inst *sdi)
190 struct dev_context *devc;
191 const struct sr_probe *probe;
193 int probe_bit, stage, i;
196 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
199 devc->probe_mask = 0;
200 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
201 devc->trigger_mask[i] = 0;
202 devc->trigger_value[i] = 0;
206 for (l = sdi->probes; l; l = l->next) {
207 probe = (struct sr_probe *)l->data;
208 if (probe->enabled == FALSE)
210 probe_bit = 1 << (probe->index);
211 devc->probe_mask |= probe_bit;
213 if (probe->trigger) {
215 for (tc = probe->trigger; *tc; tc++) {
216 devc->trigger_mask[stage] |= probe_bit;
218 devc->trigger_value[stage] |= probe_bit;
220 if (stage > NUM_TRIGGER_STAGES)
229 static int clear_instances(void)
232 struct sr_dev_inst *sdi;
233 struct drv_context *drvc;
234 struct dev_context *devc;
237 for (l = drvc->instances; l; l = l->next) {
239 if (!(devc = sdi->priv)) {
240 /* Log error, but continue cleaning up the rest. */
241 sr_err("zeroplus: %s: sdi->priv was NULL, continuing", __func__);
244 sr_usb_dev_inst_free(devc->usb);
245 /* Properly close all devices... */
247 /* ...and free all their memory. */
248 sr_dev_inst_free(sdi);
250 g_slist_free(drvc->instances);
251 drvc->instances = NULL;
260 static int hw_init(void)
262 struct drv_context *drvc;
264 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
265 sr_err("zeroplus: driver context malloc failed.");
270 if (libusb_init(&usb_context) != 0) {
271 sr_err("zp: Failed to initialize USB.");
278 static GSList *hw_scan(GSList *options)
280 struct sr_dev_inst *sdi;
281 struct sr_probe *probe;
282 struct drv_context *drvc;
283 struct dev_context *devc;
285 struct libusb_device_descriptor des;
286 libusb_device **devlist;
288 int ret, devcnt, i, j;
296 /* Find all ZEROPLUS analyzers and add them to device list. */
298 libusb_get_device_list(usb_context, &devlist); /* TODO: Errors. */
300 for (i = 0; devlist[i]; i++) {
301 ret = libusb_get_device_descriptor(devlist[i], &des);
303 sr_err("zp: failed to get device descriptor: %d", ret);
308 for (j = 0; j < zeroplus_models[j].vid; j++) {
309 if (des.idVendor == zeroplus_models[j].vid &&
310 des.idProduct == zeroplus_models[j].pid) {
311 prof = &zeroplus_models[j];
314 /* Skip if the device was not found */
317 sr_info("zp: Found ZEROPLUS model %s", prof->model_name);
319 /* Register the device with libsigrok. */
320 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
321 VENDOR_NAME, prof->model_name, NULL))) {
322 sr_err("zp: %s: sr_dev_inst_new failed", __func__);
327 /* Allocate memory for our private driver context. */
328 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
329 sr_err("zp: %s: devc malloc failed", __func__);
333 devc->num_channels = prof->channels;
334 devc->memory_size = prof->sample_depth * 1024;
335 // memset(devc->trigger_buffer, 0, NUM_TRIGGER_STAGES);
337 /* Fill in probelist according to this device's profile. */
338 for (j = 0; j < devc->num_channels; j++) {
339 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
342 sdi->probes = g_slist_append(sdi->probes, probe);
345 devices = g_slist_append(devices, sdi);
346 drvc->instances = g_slist_append(drvc->instances, sdi);
347 devc->usb = sr_usb_dev_inst_new(
348 libusb_get_bus_number(devlist[i]),
349 libusb_get_device_address(devlist[i]), NULL);
353 libusb_free_device_list(devlist, 1);
358 static GSList *hw_dev_list(void)
360 struct drv_context *drvc;
364 return drvc->instances;
367 static int hw_dev_open(struct sr_dev_inst *sdi)
369 struct dev_context *devc;
370 libusb_device **devlist, *dev;
371 struct libusb_device_descriptor des;
372 int device_count, ret, i;
374 if (!(devc = sdi->priv)) {
375 sr_err("zp: %s: sdi->priv was NULL", __func__);
379 device_count = libusb_get_device_list(usb_context, &devlist);
380 if (device_count < 0) {
381 sr_err("zp: Failed to retrieve device list");
386 for (i = 0; i < device_count; i++) {
387 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
388 sr_err("fx2lafw: Failed to get device descriptor: %d.",
392 if (libusb_get_bus_number(devlist[i]) == devc->usb->bus
393 && libusb_get_device_address(devlist[i]) == devc->usb->address) {
399 sr_err("device on bus %d address %d disappeared!",
400 devc->usb->bus, devc->usb->address);
404 if (!(ret = libusb_open(dev, &(devc->usb->devhdl)))) {
405 sdi->status = SR_ST_ACTIVE;
406 sr_info("zp: opened device %d on %d.%d interface %d",
407 sdi->index, devc->usb->bus,
408 devc->usb->address, USB_INTERFACE);
410 sr_err("zp: failed to open device: %d", ret);
414 ret = libusb_set_configuration(devc->usb->devhdl, USB_CONFIGURATION);
416 sr_err("zp: Unable to set USB configuration %d: %d",
417 USB_CONFIGURATION, ret);
421 ret = libusb_claim_interface(devc->usb->devhdl, USB_INTERFACE);
423 sr_err("zp: Unable to claim interface: %d", ret);
427 analyzer_reset(devc->usb->devhdl);
428 analyzer_initialize(devc->usb->devhdl);
430 analyzer_set_memory_size(MEMORY_SIZE_512K);
431 // analyzer_set_freq(g_freq, g_freq_scale);
432 analyzer_set_trigger_count(1);
433 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
434 // * get_memory_size(g_memory_size)) / 100) >> 2);
435 analyzer_set_ramsize_trigger_address(
436 (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
439 if (g_double_mode == 1)
440 analyzer_set_compression(COMPRESSION_DOUBLE);
441 else if (g_compression == 1)
442 analyzer_set_compression(COMPRESSION_ENABLE);
445 analyzer_set_compression(COMPRESSION_NONE);
447 if (devc->cur_samplerate == 0) {
448 /* Samplerate hasn't been set. Default to the slowest one. */
449 if (hw_dev_config_set(sdi, SR_HWCAP_SAMPLERATE,
450 &samplerates.list[0]) == SR_ERR)
457 static int hw_dev_close(struct sr_dev_inst *sdi)
459 struct dev_context *devc;
461 if (!(devc = sdi->priv)) {
462 sr_err("zp: %s: sdi->priv was NULL", __func__);
466 if (!devc->usb->devhdl)
469 sr_info("zp: closing device %d on %d.%d interface %d", sdi->index,
470 devc->usb->bus, devc->usb->address, USB_INTERFACE);
471 libusb_release_interface(devc->usb->devhdl, USB_INTERFACE);
472 libusb_reset_device(devc->usb->devhdl);
473 libusb_close(devc->usb->devhdl);
474 devc->usb->devhdl = NULL;
475 sdi->status = SR_ST_INACTIVE;
480 static int hw_cleanup(void)
482 struct drv_context *drvc;
484 if (!(drvc = zdi->priv))
490 libusb_exit(usb_context);
496 static int hw_info_get(int info_id, const void **data,
497 const struct sr_dev_inst *sdi)
499 struct dev_context *devc;
505 case SR_DI_NUM_PROBES:
508 *data = GINT_TO_POINTER(devc->num_channels);
509 sr_spew("zp: %s: Returning number of channels: %d.",
510 __func__, devc->num_channels);
514 case SR_DI_PROBE_NAMES:
516 sr_spew("zp: %s: Returning probenames.", __func__);
518 case SR_DI_SAMPLERATES:
519 *data = &samplerates;
520 sr_spew("zp: %s: Returning samplerates.", __func__);
522 case SR_DI_TRIGGER_TYPES:
523 *data = TRIGGER_TYPES;
524 sr_spew("zp: %s: Returning triggertypes: %s.", __func__, TRIGGER_TYPES);
526 case SR_DI_CUR_SAMPLERATE:
529 *data = &devc->cur_samplerate;
530 sr_spew("zp: %s: Returning samplerate: %" PRIu64 "Hz.",
531 __func__, devc->cur_samplerate);
542 static int set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
544 struct dev_context *devc;
547 sr_err("zp: %s: sdi was NULL", __func__);
551 if (!(devc = sdi->priv)) {
552 sr_err("zp: %s: sdi->priv was NULL", __func__);
556 sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate);
558 if (samplerate > SR_MHZ(1))
559 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
560 else if (samplerate > SR_KHZ(1))
561 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
563 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
565 devc->cur_samplerate = samplerate;
570 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
573 struct dev_context *devc;
575 if (!(devc = sdi->priv)) {
576 sr_err("zp: %s: sdi->priv was NULL", __func__);
581 case SR_HWCAP_SAMPLERATE:
582 return set_samplerate(sdi, *(const uint64_t *)value);
583 case SR_HWCAP_LIMIT_SAMPLES:
584 devc->limit_samples = *(const uint64_t *)value;
591 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
594 struct sr_datafeed_packet packet;
595 struct sr_datafeed_logic logic;
596 struct sr_datafeed_header header;
597 struct sr_datafeed_meta_logic meta;
598 uint64_t samples_read;
600 unsigned int packet_num;
602 struct dev_context *devc;
604 if (!(devc = sdi->priv)) {
605 sr_err("zp: %s: sdi->priv was NULL", __func__);
609 if (configure_probes(sdi) != SR_OK) {
610 sr_err("zp: failed to configured probes");
614 /* push configured settings to device */
615 analyzer_configure(devc->usb->devhdl);
617 analyzer_start(devc->usb->devhdl);
618 sr_info("zp: Waiting for data");
619 analyzer_wait_data(devc->usb->devhdl);
621 sr_info("zp: Stop address = 0x%x",
622 analyzer_get_stop_address(devc->usb->devhdl));
623 sr_info("zp: Now address = 0x%x",
624 analyzer_get_now_address(devc->usb->devhdl));
625 sr_info("zp: Trigger address = 0x%x",
626 analyzer_get_trigger_address(devc->usb->devhdl));
628 packet.type = SR_DF_HEADER;
629 packet.payload = &header;
630 header.feed_version = 1;
631 gettimeofday(&header.starttime, NULL);
632 sr_session_send(cb_data, &packet);
634 /* Send metadata about the SR_DF_LOGIC packets to come. */
635 packet.type = SR_DF_META_LOGIC;
636 packet.payload = &meta;
637 meta.samplerate = devc->cur_samplerate;
638 meta.num_probes = devc->num_channels;
639 sr_session_send(cb_data, &packet);
641 if (!(buf = g_try_malloc(PACKET_SIZE))) {
642 sr_err("zp: %s: buf malloc failed", __func__);
643 return SR_ERR_MALLOC;
647 analyzer_read_start(devc->usb->devhdl);
648 /* Send the incoming transfer to the session bus. */
649 for (packet_num = 0; packet_num < (devc->memory_size * 4 / PACKET_SIZE);
651 res = analyzer_read_data(devc->usb->devhdl, buf, PACKET_SIZE);
652 sr_info("zp: Tried to read %llx bytes, actually read %x bytes",
655 packet.type = SR_DF_LOGIC;
656 packet.payload = &logic;
657 logic.length = PACKET_SIZE;
660 sr_session_send(cb_data, &packet);
661 samples_read += res / 4;
663 analyzer_read_stop(devc->usb->devhdl);
666 packet.type = SR_DF_END;
667 sr_session_send(cb_data, &packet);
672 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
673 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
676 struct sr_datafeed_packet packet;
677 struct dev_context *devc;
679 packet.type = SR_DF_END;
680 sr_session_send(cb_data, &packet);
682 if (!(devc = sdi->priv)) {
683 sr_err("zp: %s: sdi->priv was NULL", __func__);
687 analyzer_reset(devc->usb->devhdl);
688 /* TODO: Need to cancel and free any queued up transfers. */
693 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
694 .name = "zeroplus-logic-cube",
695 .longname = "ZEROPLUS Logic Cube LAP-C series",
698 .cleanup = hw_cleanup,
700 .dev_list = hw_dev_list,
701 .dev_clear = hw_cleanup,
702 .dev_open = hw_dev_open,
703 .dev_close = hw_dev_close,
704 .info_get = hw_info_get,
705 .dev_config_set = hw_dev_config_set,
706 .dev_acquisition_start = hw_dev_acquisition_start,
707 .dev_acquisition_stop = hw_dev_acquisition_stop,