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_TYPES "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_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 max_samplerate;
155 uint64_t limit_samples;
156 int num_channels; /* TODO: This isn't initialized before it's needed :( */
158 unsigned int max_memory_size;
159 //uint8_t probe_mask;
160 //uint8_t trigger_mask[NUM_TRIGGER_STAGES];
161 //uint8_t trigger_value[NUM_TRIGGER_STAGES];
162 // uint8_t trigger_buffer[NUM_TRIGGER_STAGES];
164 unsigned int capture_ratio;
166 /* TODO: this belongs in the device instance */
167 struct sr_usb_dev_inst *usb;
170 static int hw_dev_close(struct sr_dev_inst *sdi);
172 static unsigned int get_memory_size(int type)
174 if (type == MEMORY_SIZE_8K)
176 else if (type == MEMORY_SIZE_64K)
178 else if (type == MEMORY_SIZE_128K)
180 else if (type == MEMORY_SIZE_512K)
187 static int configure_probes(const struct sr_dev_inst *sdi)
189 struct dev_context *devc;
190 const struct sr_probe *probe;
192 int probe_bit, stage, i;
195 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
198 devc->probe_mask = 0;
199 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
200 devc->trigger_mask[i] = 0;
201 devc->trigger_value[i] = 0;
205 for (l = sdi->probes; l; l = l->next) {
206 probe = (struct sr_probe *)l->data;
207 if (probe->enabled == FALSE)
209 probe_bit = 1 << (probe->index);
210 devc->probe_mask |= probe_bit;
212 if (probe->trigger) {
214 for (tc = probe->trigger; *tc; tc++) {
215 devc->trigger_mask[stage] |= probe_bit;
217 devc->trigger_value[stage] |= probe_bit;
219 if (stage > NUM_TRIGGER_STAGES)
229 static int configure_probes(const struct sr_dev_inst *sdi)
231 struct dev_context *devc;
233 const struct sr_probe *probe;
237 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
240 for (l = sdi->probes; l; l = l->next) {
241 probe = (struct sr_probe *)l->data;
242 if (probe->enabled == FALSE)
245 if ((tc = probe->trigger)) {
255 type = TRIGGER_POSEDGE;
258 type = TRIGGER_NEGEDGE;
261 type = TRIGGER_ANYEDGE;
267 analyzer_add_trigger(probe->index, type);
275 static int clear_instances(void)
278 struct sr_dev_inst *sdi;
279 struct drv_context *drvc;
280 struct dev_context *devc;
283 for (l = drvc->instances; l; l = l->next) {
285 if (!(devc = sdi->priv)) {
286 /* Log error, but continue cleaning up the rest. */
287 sr_err("zeroplus: %s: sdi->priv was NULL, continuing", __func__);
290 sr_usb_dev_inst_free(devc->usb);
291 /* Properly close all devices... */
293 /* ...and free all their memory. */
294 sr_dev_inst_free(sdi);
296 g_slist_free(drvc->instances);
297 drvc->instances = NULL;
306 static int hw_init(void)
308 struct drv_context *drvc;
310 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
311 sr_err("zeroplus: driver context malloc failed.");
312 return SR_ERR_MALLOC;
316 if (libusb_init(&usb_context) != 0) {
317 sr_err("zp: Failed to initialize USB.");
324 static GSList *hw_scan(GSList *options)
326 struct sr_dev_inst *sdi;
327 struct sr_probe *probe;
328 struct drv_context *drvc;
329 struct dev_context *devc;
331 struct libusb_device_descriptor des;
332 libusb_device **devlist;
334 int ret, devcnt, i, j;
342 /* Find all ZEROPLUS analyzers and add them to device list. */
344 libusb_get_device_list(usb_context, &devlist); /* TODO: Errors. */
346 for (i = 0; devlist[i]; i++) {
347 ret = libusb_get_device_descriptor(devlist[i], &des);
349 sr_err("zp: failed to get device descriptor: %d", ret);
354 for (j = 0; j < zeroplus_models[j].vid; j++) {
355 if (des.idVendor == zeroplus_models[j].vid &&
356 des.idProduct == zeroplus_models[j].pid) {
357 prof = &zeroplus_models[j];
360 /* Skip if the device was not found */
363 sr_info("zp: Found ZEROPLUS model %s", prof->model_name);
365 /* Register the device with libsigrok. */
366 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
367 VENDOR_NAME, prof->model_name, NULL))) {
368 sr_err("zp: %s: sr_dev_inst_new failed", __func__);
373 /* Allocate memory for our private driver context. */
374 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
375 sr_err("zp: %s: devc malloc failed", __func__);
379 devc->num_channels = prof->channels;
380 #ifdef ZP_EXPERIMENTAL
381 devc->max_memory_size = 128 * 1024;
382 devc->max_samplerate = 200;
384 devc->max_memory_size = prof->sample_depth * 1024;
385 devc->max_samplerate = prof->max_sampling_freq;
387 devc->max_samplerate *= SR_MHZ(1);
388 devc->memory_size = MEMORY_SIZE_8K;
389 // memset(devc->trigger_buffer, 0, NUM_TRIGGER_STAGES);
391 /* Fill in probelist according to this device's profile. */
392 for (j = 0; j < devc->num_channels; j++) {
393 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
396 sdi->probes = g_slist_append(sdi->probes, probe);
399 devices = g_slist_append(devices, sdi);
400 drvc->instances = g_slist_append(drvc->instances, sdi);
401 devc->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 GSList *hw_dev_list(void)
414 struct drv_context *drvc;
418 return drvc->instances;
421 static int hw_dev_open(struct sr_dev_inst *sdi)
423 struct dev_context *devc;
424 libusb_device **devlist, *dev;
425 struct libusb_device_descriptor des;
426 int device_count, ret, i;
428 if (!(devc = sdi->priv)) {
429 sr_err("zp: %s: sdi->priv was NULL", __func__);
433 device_count = libusb_get_device_list(usb_context, &devlist);
434 if (device_count < 0) {
435 sr_err("zp: Failed to retrieve device list");
440 for (i = 0; i < device_count; i++) {
441 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
442 sr_err("fx2lafw: Failed to get device descriptor: %d.",
446 if (libusb_get_bus_number(devlist[i]) == devc->usb->bus
447 && libusb_get_device_address(devlist[i]) == devc->usb->address) {
453 sr_err("device on bus %d address %d disappeared!",
454 devc->usb->bus, devc->usb->address);
458 if (!(ret = libusb_open(dev, &(devc->usb->devhdl)))) {
459 sdi->status = SR_ST_ACTIVE;
460 sr_info("zp: opened device %d on %d.%d interface %d",
461 sdi->index, devc->usb->bus,
462 devc->usb->address, USB_INTERFACE);
464 sr_err("zp: failed to open device: %d", ret);
468 ret = libusb_set_configuration(devc->usb->devhdl, USB_CONFIGURATION);
470 sr_err("zp: Unable to set USB configuration %d: %d",
471 USB_CONFIGURATION, ret);
475 ret = libusb_claim_interface(devc->usb->devhdl, USB_INTERFACE);
477 sr_err("zp: Unable to claim interface: %d", ret);
481 /* Set default configuration after power on */
482 if (analyzer_read_status(devc->usb->devhdl) == 0)
483 analyzer_configure(devc->usb->devhdl);
485 analyzer_reset(devc->usb->devhdl);
486 analyzer_initialize(devc->usb->devhdl);
488 //analyzer_set_memory_size(MEMORY_SIZE_512K);
489 // analyzer_set_freq(g_freq, g_freq_scale);
490 analyzer_set_trigger_count(1);
491 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
492 // * get_memory_size(g_memory_size)) / 100) >> 2);
495 if (g_double_mode == 1)
496 analyzer_set_compression(COMPRESSION_DOUBLE);
497 else if (g_compression == 1)
498 analyzer_set_compression(COMPRESSION_ENABLE);
501 analyzer_set_compression(COMPRESSION_NONE);
503 if (devc->cur_samplerate == 0) {
504 /* Samplerate hasn't been set. Default to 1MHz. */
505 analyzer_set_freq(1, FREQ_SCALE_MHZ);
506 devc->cur_samplerate = SR_MHZ(1);
512 static int hw_dev_close(struct sr_dev_inst *sdi)
514 struct dev_context *devc;
516 if (!(devc = sdi->priv)) {
517 sr_err("zp: %s: sdi->priv was NULL", __func__);
521 if (!devc->usb->devhdl)
524 sr_info("zp: closing device %d on %d.%d interface %d", sdi->index,
525 devc->usb->bus, devc->usb->address, USB_INTERFACE);
526 libusb_release_interface(devc->usb->devhdl, USB_INTERFACE);
527 libusb_reset_device(devc->usb->devhdl);
528 libusb_close(devc->usb->devhdl);
529 devc->usb->devhdl = NULL;
530 sdi->status = SR_ST_INACTIVE;
535 static int hw_cleanup(void)
537 struct drv_context *drvc;
539 if (!(drvc = zdi->priv))
545 libusb_exit(usb_context);
551 static int hw_info_get(int info_id, const void **data,
552 const struct sr_dev_inst *sdi)
554 struct dev_context *devc;
560 case SR_DI_NUM_PROBES:
563 *data = GINT_TO_POINTER(devc->num_channels);
564 sr_spew("zp: %s: Returning number of channels: %d.",
565 __func__, devc->num_channels);
569 case SR_DI_PROBE_NAMES:
571 sr_spew("zp: %s: Returning probenames.", __func__);
573 case SR_DI_SAMPLERATES:
574 *data = &samplerates;
575 sr_spew("zp: %s: Returning samplerates.", __func__);
577 case SR_DI_TRIGGER_TYPES:
578 *data = TRIGGER_TYPES;
579 sr_spew("zp: %s: Returning triggertypes: %s.", __func__, TRIGGER_TYPES);
581 case SR_DI_CUR_SAMPLERATE:
584 *data = &devc->cur_samplerate;
585 sr_spew("zp: %s: Returning samplerate: %" PRIu64 "Hz.",
586 __func__, devc->cur_samplerate);
597 static int set_samplerate(struct dev_context *devc, uint64_t samplerate)
601 for (i = 0; supported_samplerates[i]; i++)
602 if (samplerate == supported_samplerates[i])
604 if (!supported_samplerates[i] || samplerate > devc->max_samplerate) {
605 sr_err("zp: %s: unsupported samplerate", __func__);
609 sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate);
611 if (samplerate >= SR_MHZ(1))
612 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
613 else if (samplerate >= SR_KHZ(1))
614 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
616 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
618 devc->cur_samplerate = samplerate;
623 static int set_limit_samples(struct dev_context *devc, uint64_t samples)
625 devc->limit_samples = samples;
627 if (samples <= 2 * 1024)
628 devc->memory_size = MEMORY_SIZE_8K;
629 else if (samples <= 16 * 1024)
630 devc->memory_size = MEMORY_SIZE_64K;
631 else if (samples <= 32 * 1024 ||
632 devc->max_memory_size <= 32 * 1024)
633 devc->memory_size = MEMORY_SIZE_128K;
635 devc->memory_size = MEMORY_SIZE_512K;
637 sr_info("zp: Setting memory size to %dK.",
638 get_memory_size(devc->memory_size) / 1024);
640 analyzer_set_memory_size(devc->memory_size);
645 static int set_capture_ratio(struct dev_context *devc, uint64_t ratio)
648 sr_err("zp: %s: invalid capture ratio", __func__);
652 devc->capture_ratio = ratio;
654 sr_info("zp: Setting capture ratio to %d%%.", devc->capture_ratio);
659 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
662 struct dev_context *devc;
665 sr_err("zp: %s: sdi was NULL", __func__);
669 if (!(devc = sdi->priv)) {
670 sr_err("zp: %s: sdi->priv was NULL", __func__);
675 case SR_HWCAP_SAMPLERATE:
676 return set_samplerate(devc, *(const uint64_t *)value);
677 case SR_HWCAP_LIMIT_SAMPLES:
678 return set_limit_samples(devc, *(const uint64_t *)value);
679 case SR_HWCAP_CAPTURE_RATIO:
680 return set_capture_ratio(devc, *(const uint64_t *)value);
686 static void set_triggerbar(struct dev_context *devc)
688 unsigned int ramsize;
690 unsigned int triggerbar;
692 ramsize = get_memory_size(devc->memory_size) / 4;
695 if (devc->max_memory_size < n)
696 n = devc->max_memory_size;
697 if (devc->limit_samples < n)
698 n = devc->limit_samples;
699 n = n * devc->capture_ratio / 100;
701 triggerbar = ramsize - 8;
707 analyzer_set_triggerbar_address(triggerbar);
708 analyzer_set_ramsize_trigger_address(ramsize - triggerbar);
710 sr_dbg("zp: triggerbar_address = %d(0x%x)", triggerbar, triggerbar);
711 sr_dbg("zp: ramsize_triggerbar_address = %d(0x%x)",
712 ramsize - triggerbar, ramsize - triggerbar);
715 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
718 struct sr_datafeed_packet packet;
719 struct sr_datafeed_logic logic;
720 struct sr_datafeed_header header;
721 struct sr_datafeed_meta_logic meta;
722 //uint64_t samples_read;
724 unsigned int packet_num;
727 struct dev_context *devc;
729 if (!(devc = sdi->priv)) {
730 sr_err("zp: %s: sdi->priv was NULL", __func__);
734 if (configure_probes(sdi) != SR_OK) {
735 sr_err("zp: failed to configured probes");
739 set_triggerbar(devc);
741 /* push configured settings to device */
742 analyzer_configure(devc->usb->devhdl);
744 analyzer_start(devc->usb->devhdl);
745 sr_info("zp: Waiting for data");
746 analyzer_wait_data(devc->usb->devhdl);
748 sr_info("zp: Stop address = 0x%x",
749 analyzer_get_stop_address(devc->usb->devhdl));
750 sr_info("zp: Now address = 0x%x",
751 analyzer_get_now_address(devc->usb->devhdl));
752 sr_info("zp: Trigger address = 0x%x",
753 analyzer_get_trigger_address(devc->usb->devhdl));
755 packet.type = SR_DF_HEADER;
756 packet.payload = &header;
757 header.feed_version = 1;
758 gettimeofday(&header.starttime, NULL);
759 sr_session_send(cb_data, &packet);
761 /* Send metadata about the SR_DF_LOGIC packets to come. */
762 packet.type = SR_DF_META_LOGIC;
763 packet.payload = &meta;
764 meta.samplerate = devc->cur_samplerate;
765 meta.num_probes = devc->num_channels;
766 sr_session_send(cb_data, &packet);
768 if (!(buf = g_try_malloc(PACKET_SIZE))) {
769 sr_err("zp: %s: buf malloc failed", __func__);
770 return SR_ERR_MALLOC;
774 analyzer_read_start(devc->usb->devhdl);
775 /* Send the incoming transfer to the session bus. */
776 n = get_memory_size(devc->memory_size);
777 if (devc->max_memory_size * 4 < n)
778 n = devc->max_memory_size * 4;
779 for (packet_num = 0; packet_num < n / PACKET_SIZE; packet_num++) {
780 res = analyzer_read_data(devc->usb->devhdl, buf, PACKET_SIZE);
781 sr_info("zp: Tried to read %d bytes, actually read %d bytes",
784 packet.type = SR_DF_LOGIC;
785 packet.payload = &logic;
786 logic.length = PACKET_SIZE;
789 sr_session_send(cb_data, &packet);
790 //samples_read += res / 4;
792 analyzer_read_stop(devc->usb->devhdl);
795 packet.type = SR_DF_END;
796 sr_session_send(cb_data, &packet);
801 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
802 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
804 struct sr_datafeed_packet packet;
805 struct dev_context *devc;
807 packet.type = SR_DF_END;
808 sr_session_send(cb_data, &packet);
810 if (!(devc = sdi->priv)) {
811 sr_err("zp: %s: sdi->priv was NULL", __func__);
815 analyzer_reset(devc->usb->devhdl);
816 /* TODO: Need to cancel and free any queued up transfers. */
821 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
822 .name = "zeroplus-logic-cube",
823 .longname = "ZEROPLUS Logic Cube LAP-C series",
826 .cleanup = hw_cleanup,
828 .dev_list = hw_dev_list,
829 .dev_clear = hw_cleanup,
830 .dev_open = hw_dev_open,
831 .dev_close = hw_dev_close,
832 .info_get = hw_info_get,
833 .dev_config_set = hw_dev_config_set,
834 .dev_acquisition_start = hw_dev_acquisition_start,
835 .dev_acquisition_stop = hw_dev_acquisition_stop,