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_TYPE "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_CONF_LOGIC_ANALYZER,
76 SR_CONF_CAPTURE_RATIO,
78 /* These are really implemented in the driver, not the hardware. */
79 SR_CONF_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] = {
88 "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7",
89 "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7",
93 /* List of struct sr_dev_inst, maintained by dev_open()/dev_close(). */
94 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
95 static struct sr_dev_driver *di = &zeroplus_logic_cube_driver_info;
98 * The hardware supports more samplerates than these, but these are the
99 * options hardcoded into the vendor's Windows GUI.
103 * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up
106 static const uint64_t supported_samplerates[] = {
128 static const struct sr_samplerates samplerates = {
132 supported_samplerates,
135 /* Private, per-device-instance driver context. */
137 uint64_t cur_samplerate;
138 uint64_t max_samplerate;
139 uint64_t limit_samples;
140 int num_channels; /* TODO: This isn't initialized before it's needed :( */
142 unsigned int max_memory_size;
143 //uint8_t probe_mask;
144 //uint8_t trigger_mask[NUM_TRIGGER_STAGES];
145 //uint8_t trigger_value[NUM_TRIGGER_STAGES];
146 // uint8_t trigger_buffer[NUM_TRIGGER_STAGES];
148 unsigned int capture_ratio;
150 /* TODO: this belongs in the device instance */
151 struct sr_usb_dev_inst *usb;
154 static int hw_dev_close(struct sr_dev_inst *sdi);
156 static unsigned int get_memory_size(int type)
158 if (type == MEMORY_SIZE_8K)
160 else if (type == MEMORY_SIZE_64K)
162 else if (type == MEMORY_SIZE_128K)
164 else if (type == MEMORY_SIZE_512K)
171 static int configure_probes(const struct sr_dev_inst *sdi)
173 struct dev_context *devc;
174 const struct sr_probe *probe;
176 int probe_bit, stage, i;
179 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
182 devc->probe_mask = 0;
183 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
184 devc->trigger_mask[i] = 0;
185 devc->trigger_value[i] = 0;
189 for (l = sdi->probes; l; l = l->next) {
190 probe = (struct sr_probe *)l->data;
191 if (probe->enabled == FALSE)
193 probe_bit = 1 << (probe->index);
194 devc->probe_mask |= probe_bit;
196 if (probe->trigger) {
198 for (tc = probe->trigger; *tc; tc++) {
199 devc->trigger_mask[stage] |= probe_bit;
201 devc->trigger_value[stage] |= probe_bit;
203 if (stage > NUM_TRIGGER_STAGES)
213 static int configure_probes(const struct sr_dev_inst *sdi)
215 struct dev_context *devc;
217 const struct sr_probe *probe;
221 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
224 for (l = sdi->probes; l; l = l->next) {
225 probe = (struct sr_probe *)l->data;
226 if (probe->enabled == FALSE)
229 if ((tc = probe->trigger)) {
239 type = TRIGGER_POSEDGE;
242 type = TRIGGER_NEGEDGE;
245 type = TRIGGER_ANYEDGE;
251 analyzer_add_trigger(probe->index, type);
259 static int clear_instances(void)
262 struct sr_dev_inst *sdi;
263 struct drv_context *drvc;
264 struct dev_context *devc;
267 for (l = drvc->instances; l; l = l->next) {
269 if (!(devc = sdi->priv)) {
270 /* Log error, but continue cleaning up the rest. */
271 sr_err("zeroplus: %s: sdi->priv was NULL, continuing", __func__);
274 sr_usb_dev_inst_free(devc->usb);
275 /* Properly close all devices... */
277 /* ...and free all their memory. */
278 sr_dev_inst_free(sdi);
280 g_slist_free(drvc->instances);
281 drvc->instances = NULL;
290 static int hw_init(struct sr_context *sr_ctx)
292 struct drv_context *drvc;
294 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
295 sr_err("Driver context malloc failed.");
296 return SR_ERR_MALLOC;
299 drvc->sr_ctx = sr_ctx;
305 static GSList *hw_scan(GSList *options)
307 struct sr_dev_inst *sdi;
308 struct sr_probe *probe;
309 struct drv_context *drvc;
310 struct dev_context *devc;
312 struct libusb_device_descriptor des;
313 libusb_device **devlist;
315 int ret, devcnt, i, j;
324 /* Find all ZEROPLUS analyzers and add them to device list. */
326 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); /* TODO: Errors. */
328 for (i = 0; devlist[i]; i++) {
329 ret = libusb_get_device_descriptor(devlist[i], &des);
331 sr_err("zp: failed to get device descriptor: %s",
332 libusb_error_name(ret));
337 for (j = 0; j < zeroplus_models[j].vid; j++) {
338 if (des.idVendor == zeroplus_models[j].vid &&
339 des.idProduct == zeroplus_models[j].pid) {
340 prof = &zeroplus_models[j];
343 /* Skip if the device was not found */
346 sr_info("zp: Found ZEROPLUS model %s", prof->model_name);
348 /* Register the device with libsigrok. */
349 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
350 VENDOR_NAME, prof->model_name, NULL))) {
351 sr_err("zp: %s: sr_dev_inst_new failed", __func__);
356 /* Allocate memory for our private driver context. */
357 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
358 sr_err("zp: %s: devc malloc failed", __func__);
362 devc->num_channels = prof->channels;
363 #ifdef ZP_EXPERIMENTAL
364 devc->max_memory_size = 128 * 1024;
365 devc->max_samplerate = 200;
367 devc->max_memory_size = prof->sample_depth * 1024;
368 devc->max_samplerate = prof->max_sampling_freq;
370 devc->max_samplerate *= SR_MHZ(1);
371 devc->memory_size = MEMORY_SIZE_8K;
372 // memset(devc->trigger_buffer, 0, NUM_TRIGGER_STAGES);
374 /* Fill in probelist according to this device's profile. */
375 for (j = 0; j < devc->num_channels; j++) {
376 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
379 sdi->probes = g_slist_append(sdi->probes, probe);
382 devices = g_slist_append(devices, sdi);
383 drvc->instances = g_slist_append(drvc->instances, sdi);
384 devc->usb = sr_usb_dev_inst_new(
385 libusb_get_bus_number(devlist[i]),
386 libusb_get_device_address(devlist[i]), NULL);
390 libusb_free_device_list(devlist, 1);
395 static GSList *hw_dev_list(void)
397 struct drv_context *drvc;
401 return drvc->instances;
404 static int hw_dev_open(struct sr_dev_inst *sdi)
406 struct dev_context *devc;
407 struct drv_context *drvc = di->priv;
408 libusb_device **devlist, *dev;
409 struct libusb_device_descriptor des;
410 int device_count, ret, i;
412 if (!(devc = sdi->priv)) {
413 sr_err("zp: %s: sdi->priv was NULL", __func__);
417 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx,
419 if (device_count < 0) {
420 sr_err("zp: Failed to retrieve device list");
425 for (i = 0; i < device_count; i++) {
426 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
427 sr_err("zp: Failed to get device descriptor: %s.",
428 libusb_error_name(ret));
431 if (libusb_get_bus_number(devlist[i]) == devc->usb->bus
432 && libusb_get_device_address(devlist[i]) == devc->usb->address) {
438 sr_err("device on bus %d address %d disappeared!",
439 devc->usb->bus, devc->usb->address);
443 if (!(ret = libusb_open(dev, &(devc->usb->devhdl)))) {
444 sdi->status = SR_ST_ACTIVE;
445 sr_info("zp: opened device %d on %d.%d interface %d",
446 sdi->index, devc->usb->bus,
447 devc->usb->address, USB_INTERFACE);
449 sr_err("zp: failed to open device: %s", libusb_error_name(ret));
453 ret = libusb_set_configuration(devc->usb->devhdl, USB_CONFIGURATION);
455 sr_err("zp: Unable to set USB configuration %d: %s",
456 USB_CONFIGURATION, libusb_error_name(ret));
460 ret = libusb_claim_interface(devc->usb->devhdl, USB_INTERFACE);
462 sr_err("zp: Unable to claim interface: %s",
463 libusb_error_name(ret));
467 /* Set default configuration after power on */
468 if (analyzer_read_status(devc->usb->devhdl) == 0)
469 analyzer_configure(devc->usb->devhdl);
471 analyzer_reset(devc->usb->devhdl);
472 analyzer_initialize(devc->usb->devhdl);
474 //analyzer_set_memory_size(MEMORY_SIZE_512K);
475 // analyzer_set_freq(g_freq, g_freq_scale);
476 analyzer_set_trigger_count(1);
477 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
478 // * get_memory_size(g_memory_size)) / 100) >> 2);
481 if (g_double_mode == 1)
482 analyzer_set_compression(COMPRESSION_DOUBLE);
483 else if (g_compression == 1)
484 analyzer_set_compression(COMPRESSION_ENABLE);
487 analyzer_set_compression(COMPRESSION_NONE);
489 if (devc->cur_samplerate == 0) {
490 /* Samplerate hasn't been set. Default to 1MHz. */
491 analyzer_set_freq(1, FREQ_SCALE_MHZ);
492 devc->cur_samplerate = SR_MHZ(1);
498 static int hw_dev_close(struct sr_dev_inst *sdi)
500 struct dev_context *devc;
502 if (!(devc = sdi->priv)) {
503 sr_err("zp: %s: sdi->priv was NULL", __func__);
507 if (!devc->usb->devhdl)
510 sr_info("zp: closing device %d on %d.%d interface %d", sdi->index,
511 devc->usb->bus, devc->usb->address, USB_INTERFACE);
512 libusb_release_interface(devc->usb->devhdl, USB_INTERFACE);
513 libusb_reset_device(devc->usb->devhdl);
514 libusb_close(devc->usb->devhdl);
515 devc->usb->devhdl = NULL;
516 sdi->status = SR_ST_INACTIVE;
521 static int hw_cleanup(void)
523 struct drv_context *drvc;
525 if (!(drvc = di->priv))
533 static int config_get(int id, const void **data, const struct sr_dev_inst *sdi)
535 struct dev_context *devc;
538 case SR_CONF_SAMPLERATE:
541 *data = &devc->cur_samplerate;
542 sr_spew("zp: %s: Returning samplerate: %" PRIu64 "Hz.",
543 __func__, devc->cur_samplerate);
554 static int set_samplerate(struct dev_context *devc, uint64_t samplerate)
558 for (i = 0; supported_samplerates[i]; i++)
559 if (samplerate == supported_samplerates[i])
561 if (!supported_samplerates[i] || samplerate > devc->max_samplerate) {
562 sr_err("zp: %s: unsupported samplerate", __func__);
566 sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate);
568 if (samplerate >= SR_MHZ(1))
569 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
570 else if (samplerate >= SR_KHZ(1))
571 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
573 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
575 devc->cur_samplerate = samplerate;
580 static int set_limit_samples(struct dev_context *devc, uint64_t samples)
582 devc->limit_samples = samples;
584 if (samples <= 2 * 1024)
585 devc->memory_size = MEMORY_SIZE_8K;
586 else if (samples <= 16 * 1024)
587 devc->memory_size = MEMORY_SIZE_64K;
588 else if (samples <= 32 * 1024 ||
589 devc->max_memory_size <= 32 * 1024)
590 devc->memory_size = MEMORY_SIZE_128K;
592 devc->memory_size = MEMORY_SIZE_512K;
594 sr_info("zp: Setting memory size to %dK.",
595 get_memory_size(devc->memory_size) / 1024);
597 analyzer_set_memory_size(devc->memory_size);
602 static int set_capture_ratio(struct dev_context *devc, uint64_t ratio)
605 sr_err("zp: %s: invalid capture ratio", __func__);
609 devc->capture_ratio = ratio;
611 sr_info("zp: Setting capture ratio to %d%%.", devc->capture_ratio);
616 static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
618 struct dev_context *devc;
621 sr_err("zp: %s: sdi was NULL", __func__);
625 if (!(devc = sdi->priv)) {
626 sr_err("zp: %s: sdi->priv was NULL", __func__);
631 case SR_CONF_SAMPLERATE:
632 return set_samplerate(devc, *(const uint64_t *)value);
633 case SR_CONF_LIMIT_SAMPLES:
634 return set_limit_samples(devc, *(const uint64_t *)value);
635 case SR_CONF_CAPTURE_RATIO:
636 return set_capture_ratio(devc, *(const uint64_t *)value);
642 static int config_list(int key, const void **data, const struct sr_dev_inst *sdi)
648 case SR_CONF_DEVICE_OPTIONS:
651 case SR_CONF_SAMPLERATE:
652 *data = &samplerates;
654 case SR_CONF_TRIGGER_TYPE:
655 *data = TRIGGER_TYPE;
664 static void set_triggerbar(struct dev_context *devc)
666 unsigned int ramsize;
668 unsigned int triggerbar;
670 ramsize = get_memory_size(devc->memory_size) / 4;
673 if (devc->max_memory_size < n)
674 n = devc->max_memory_size;
675 if (devc->limit_samples < n)
676 n = devc->limit_samples;
677 n = n * devc->capture_ratio / 100;
679 triggerbar = ramsize - 8;
685 analyzer_set_triggerbar_address(triggerbar);
686 analyzer_set_ramsize_trigger_address(ramsize - triggerbar);
688 sr_dbg("zp: triggerbar_address = %d(0x%x)", triggerbar, triggerbar);
689 sr_dbg("zp: ramsize_triggerbar_address = %d(0x%x)",
690 ramsize - triggerbar, ramsize - triggerbar);
693 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
696 struct sr_datafeed_packet packet;
697 struct sr_datafeed_logic logic;
698 struct sr_datafeed_header header;
699 //uint64_t samples_read;
701 unsigned int packet_num;
704 struct dev_context *devc;
706 if (!(devc = sdi->priv)) {
707 sr_err("zp: %s: sdi->priv was NULL", __func__);
711 if (configure_probes(sdi) != SR_OK) {
712 sr_err("zp: failed to configured probes");
716 set_triggerbar(devc);
718 /* push configured settings to device */
719 analyzer_configure(devc->usb->devhdl);
721 analyzer_start(devc->usb->devhdl);
722 sr_info("zp: Waiting for data");
723 analyzer_wait_data(devc->usb->devhdl);
725 sr_info("zp: Stop address = 0x%x",
726 analyzer_get_stop_address(devc->usb->devhdl));
727 sr_info("zp: Now address = 0x%x",
728 analyzer_get_now_address(devc->usb->devhdl));
729 sr_info("zp: Trigger address = 0x%x",
730 analyzer_get_trigger_address(devc->usb->devhdl));
732 packet.type = SR_DF_HEADER;
733 packet.payload = &header;
734 header.feed_version = 1;
735 gettimeofday(&header.starttime, NULL);
736 sr_session_send(cb_data, &packet);
738 if (!(buf = g_try_malloc(PACKET_SIZE))) {
739 sr_err("zp: %s: buf malloc failed", __func__);
740 return SR_ERR_MALLOC;
744 analyzer_read_start(devc->usb->devhdl);
745 /* Send the incoming transfer to the session bus. */
746 n = get_memory_size(devc->memory_size);
747 if (devc->max_memory_size * 4 < n)
748 n = devc->max_memory_size * 4;
749 for (packet_num = 0; packet_num < n / PACKET_SIZE; packet_num++) {
750 res = analyzer_read_data(devc->usb->devhdl, buf, PACKET_SIZE);
751 sr_info("zp: Tried to read %d bytes, actually read %d bytes",
754 packet.type = SR_DF_LOGIC;
755 packet.payload = &logic;
756 logic.length = PACKET_SIZE;
759 sr_session_send(cb_data, &packet);
760 //samples_read += res / 4;
762 analyzer_read_stop(devc->usb->devhdl);
765 packet.type = SR_DF_END;
766 sr_session_send(cb_data, &packet);
771 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
772 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
774 struct sr_datafeed_packet packet;
775 struct dev_context *devc;
777 packet.type = SR_DF_END;
778 sr_session_send(cb_data, &packet);
780 if (!(devc = sdi->priv)) {
781 sr_err("zp: %s: sdi->priv was NULL", __func__);
785 analyzer_reset(devc->usb->devhdl);
786 /* TODO: Need to cancel and free any queued up transfers. */
791 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
792 .name = "zeroplus-logic-cube",
793 .longname = "ZEROPLUS Logic Cube LAP-C series",
796 .cleanup = hw_cleanup,
798 .dev_list = hw_dev_list,
799 .dev_clear = hw_cleanup,
800 .config_get = config_get,
801 .config_set = config_set,
802 .config_list = config_list,
803 .dev_open = hw_dev_open,
804 .dev_close = hw_dev_close,
805 .dev_acquisition_start = hw_dev_acquisition_start,
806 .dev_acquisition_stop = hw_dev_acquisition_stop,