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/>.
22 #define VENDOR_NAME "ZEROPLUS"
23 #define USB_INTERFACE 0
24 #define USB_CONFIGURATION 1
25 #define NUM_TRIGGER_STAGES 4
26 #define TRIGGER_TYPE "01"
27 #define PACKET_SIZE 2048 /* ?? */
29 //#define ZP_EXPERIMENTAL
35 unsigned int channels;
36 unsigned int sample_depth; /* In Ksamples/channel */
37 unsigned int max_sampling_freq;
41 * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the
42 * same 128K sample depth.
44 static const struct zp_model zeroplus_models[] = {
45 {0x0c12, 0x7009, "LAP-C(16064)", 16, 64, 100},
46 {0x0c12, 0x700a, "LAP-C(16128)", 16, 128, 200},
47 /* TODO: We don't know anything about these.
48 {0x0c12, 0x700b, "LAP-C(32128)", 32, 128, 200},
49 {0x0c12, 0x700c, "LAP-C(321000)", 32, 1024, 200},
50 {0x0c12, 0x700d, "LAP-C(322000)", 32, 2048, 200},
52 {0x0c12, 0x700e, "LAP-C(16032)", 16, 32, 100},
53 {0x0c12, 0x7016, "LAP-C(162000)", 16, 2048, 200},
57 static const int32_t hwcaps[] = {
58 SR_CONF_LOGIC_ANALYZER,
60 SR_CONF_CAPTURE_RATIO,
61 SR_CONF_LIMIT_SAMPLES,
65 * ZEROPLUS LAP-C (16032) numbers the 16 probes A0-A7 and B0-B7.
66 * We currently ignore other untested/unsupported devices here.
68 static const char *probe_names[] = {
69 "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7",
70 "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7",
74 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
75 static struct sr_dev_driver *di = &zeroplus_logic_cube_driver_info;
78 * The hardware supports more samplerates than these, but these are the
79 * options hardcoded into the vendor's Windows GUI.
82 static const uint64_t samplerates_100[] = {
101 const uint64_t samplerates_200[] = {
122 static int hw_dev_close(struct sr_dev_inst *sdi);
125 static int configure_probes(const struct sr_dev_inst *sdi)
127 struct dev_context *devc;
128 const struct sr_probe *probe;
130 int probe_bit, stage, i;
133 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
136 devc->probe_mask = 0;
137 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
138 devc->trigger_mask[i] = 0;
139 devc->trigger_value[i] = 0;
143 for (l = sdi->probes; l; l = l->next) {
144 probe = (struct sr_probe *)l->data;
145 if (probe->enabled == FALSE)
147 probe_bit = 1 << (probe->index);
148 devc->probe_mask |= probe_bit;
150 if (probe->trigger) {
152 for (tc = probe->trigger; *tc; tc++) {
153 devc->trigger_mask[stage] |= probe_bit;
155 devc->trigger_value[stage] |= probe_bit;
157 if (stage > NUM_TRIGGER_STAGES)
167 static int configure_probes(const struct sr_dev_inst *sdi)
169 struct dev_context *devc;
171 const struct sr_probe *probe;
175 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
178 for (l = sdi->probes; l; l = l->next) {
179 probe = (struct sr_probe *)l->data;
180 if (probe->enabled == FALSE)
183 if ((tc = probe->trigger)) {
193 type = TRIGGER_POSEDGE;
196 type = TRIGGER_NEGEDGE;
199 type = TRIGGER_ANYEDGE;
205 analyzer_add_trigger(probe->index, type);
213 SR_PRIV int zp_set_samplerate(struct dev_context *devc, uint64_t samplerate)
217 for (i = 0; ARRAY_SIZE(samplerates_200); i++)
218 if (samplerate == samplerates_200[i])
221 if (i == ARRAY_SIZE(samplerates_200) || samplerate > devc->max_samplerate) {
222 sr_err("Unsupported samplerate: %" PRIu64 "Hz.", samplerate);
226 sr_info("Setting samplerate to %" PRIu64 "Hz.", samplerate);
228 if (samplerate >= SR_MHZ(1))
229 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
230 else if (samplerate >= SR_KHZ(1))
231 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
233 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
235 devc->cur_samplerate = samplerate;
240 static int clear_instances(void)
243 struct sr_dev_inst *sdi;
244 struct drv_context *drvc;
245 struct dev_context *devc;
248 for (l = drvc->instances; l; l = l->next) {
250 if (!(devc = sdi->priv)) {
251 /* Log error, but continue cleaning up the rest. */
252 sr_err("%s: sdi->priv was NULL, continuing", __func__);
255 sr_usb_dev_inst_free(devc->usb);
256 /* Properly close all devices... */
258 /* ...and free all their memory. */
259 sr_dev_inst_free(sdi);
261 g_slist_free(drvc->instances);
262 drvc->instances = NULL;
267 static int hw_init(struct sr_context *sr_ctx)
269 return std_hw_init(sr_ctx, di, "zeroplus: ");
272 static GSList *hw_scan(GSList *options)
274 struct sr_dev_inst *sdi;
275 struct sr_probe *probe;
276 struct drv_context *drvc;
277 struct dev_context *devc;
278 const struct zp_model *prof;
279 struct libusb_device_descriptor des;
280 libusb_device **devlist;
282 int ret, devcnt, i, j;
292 /* Find all ZEROPLUS analyzers and add them to device list. */
294 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); /* TODO: Errors. */
296 for (i = 0; devlist[i]; i++) {
297 ret = libusb_get_device_descriptor(devlist[i], &des);
299 sr_err("Failed to get device descriptor: %s.",
300 libusb_error_name(ret));
305 for (j = 0; j < zeroplus_models[j].vid; j++) {
306 if (des.idVendor == zeroplus_models[j].vid &&
307 des.idProduct == zeroplus_models[j].pid) {
308 prof = &zeroplus_models[j];
311 /* Skip if the device was not found. */
314 sr_info("Found ZEROPLUS %s.", prof->model_name);
316 /* Register the device with libsigrok. */
317 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
318 VENDOR_NAME, prof->model_name, NULL))) {
319 sr_err("%s: sr_dev_inst_new failed", __func__);
324 /* Allocate memory for our private driver context. */
325 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
326 sr_err("Device context malloc failed.");
332 devc->num_channels = prof->channels;
333 #ifdef ZP_EXPERIMENTAL
334 devc->max_memory_size = 128 * 1024;
335 devc->max_samplerate = 200;
337 devc->max_memory_size = prof->sample_depth * 1024;
338 devc->max_samplerate = prof->max_sampling_freq;
340 devc->max_samplerate *= SR_MHZ(1);
341 devc->memory_size = MEMORY_SIZE_8K;
342 // memset(devc->trigger_buffer, 0, NUM_TRIGGER_STAGES);
344 /* Fill in probelist according to this device's profile. */
345 for (j = 0; j < devc->num_channels; j++) {
346 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
349 sdi->probes = g_slist_append(sdi->probes, probe);
352 devices = g_slist_append(devices, sdi);
353 drvc->instances = g_slist_append(drvc->instances, sdi);
354 devc->usb = sr_usb_dev_inst_new(
355 libusb_get_bus_number(devlist[i]),
356 libusb_get_device_address(devlist[i]), NULL);
360 libusb_free_device_list(devlist, 1);
365 static GSList *hw_dev_list(void)
367 return ((struct drv_context *)(di->priv))->instances;
370 static int hw_dev_open(struct sr_dev_inst *sdi)
372 struct dev_context *devc;
373 struct drv_context *drvc;
374 libusb_device **devlist, *dev;
375 struct libusb_device_descriptor des;
376 int device_count, ret, i;
380 if (!(devc = sdi->priv)) {
381 sr_err("%s: sdi->priv was NULL", __func__);
385 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx,
387 if (device_count < 0) {
388 sr_err("Failed to retrieve device list.");
393 for (i = 0; i < device_count; i++) {
394 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
395 sr_err("Failed to get device descriptor: %s.",
396 libusb_error_name(ret));
399 if (libusb_get_bus_number(devlist[i]) == devc->usb->bus
400 && libusb_get_device_address(devlist[i]) == devc->usb->address) {
406 sr_err("Device on bus %d address %d disappeared!",
407 devc->usb->bus, devc->usb->address);
411 if (!(ret = libusb_open(dev, &(devc->usb->devhdl)))) {
412 sdi->status = SR_ST_ACTIVE;
413 sr_info("Opened device %d on %d.%d interface %d.",
414 sdi->index, devc->usb->bus,
415 devc->usb->address, USB_INTERFACE);
417 sr_err("Failed to open device: %s.", libusb_error_name(ret));
421 ret = libusb_set_configuration(devc->usb->devhdl, USB_CONFIGURATION);
423 sr_err("Unable to set USB configuration %d: %s.",
424 USB_CONFIGURATION, libusb_error_name(ret));
428 ret = libusb_claim_interface(devc->usb->devhdl, USB_INTERFACE);
430 sr_err("Unable to claim interface: %s.",
431 libusb_error_name(ret));
435 /* Set default configuration after power on. */
436 if (analyzer_read_status(devc->usb->devhdl) == 0)
437 analyzer_configure(devc->usb->devhdl);
439 analyzer_reset(devc->usb->devhdl);
440 analyzer_initialize(devc->usb->devhdl);
442 //analyzer_set_memory_size(MEMORY_SIZE_512K);
443 // analyzer_set_freq(g_freq, g_freq_scale);
444 analyzer_set_trigger_count(1);
445 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
446 // * get_memory_size(g_memory_size)) / 100) >> 2);
449 if (g_double_mode == 1)
450 analyzer_set_compression(COMPRESSION_DOUBLE);
451 else if (g_compression == 1)
452 analyzer_set_compression(COMPRESSION_ENABLE);
455 analyzer_set_compression(COMPRESSION_NONE);
457 if (devc->cur_samplerate == 0) {
458 /* Samplerate hasn't been set. Default to 1MHz. */
459 analyzer_set_freq(1, FREQ_SCALE_MHZ);
460 devc->cur_samplerate = SR_MHZ(1);
466 static int hw_dev_close(struct sr_dev_inst *sdi)
468 struct dev_context *devc;
472 if (!devc->usb->devhdl)
475 sr_info("Closing device %d on %d.%d interface %d.", sdi->index,
476 devc->usb->bus, devc->usb->address, USB_INTERFACE);
477 libusb_release_interface(devc->usb->devhdl, USB_INTERFACE);
478 libusb_reset_device(devc->usb->devhdl);
479 libusb_close(devc->usb->devhdl);
480 devc->usb->devhdl = NULL;
481 sdi->status = SR_ST_INACTIVE;
486 static int hw_cleanup(void)
488 struct drv_context *drvc;
490 if (!(drvc = di->priv))
498 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi)
500 struct dev_context *devc;
503 case SR_CONF_SAMPLERATE:
506 *data = g_variant_new_uint64(devc->cur_samplerate);
507 sr_spew("Returning samplerate: %" PRIu64 "Hz.",
508 devc->cur_samplerate);
519 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
521 struct dev_context *devc;
524 sr_err("%s: sdi was NULL", __func__);
528 if (!(devc = sdi->priv)) {
529 sr_err("%s: sdi->priv was NULL", __func__);
534 case SR_CONF_SAMPLERATE:
535 return zp_set_samplerate(devc, g_variant_get_uint64(data));
536 case SR_CONF_LIMIT_SAMPLES:
537 return set_limit_samples(devc, g_variant_get_uint64(data));
538 case SR_CONF_CAPTURE_RATIO:
539 return set_capture_ratio(devc, g_variant_get_uint64(data));
547 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
549 struct dev_context *devc;
554 case SR_CONF_DEVICE_OPTIONS:
555 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
556 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
558 case SR_CONF_SAMPLERATE:
560 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
561 if (devc->prof->max_sampling_freq == 100) {
562 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
563 samplerates_100, ARRAY_SIZE(samplerates_100),
565 } else if (devc->prof->max_sampling_freq == 200) {
566 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
567 samplerates_200, ARRAY_SIZE(samplerates_200),
570 sr_err("Internal error: Unknown max. samplerate: %d.",
571 devc->prof->max_sampling_freq);
574 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
575 *data = g_variant_builder_end(&gvb);
577 case SR_CONF_TRIGGER_TYPE:
578 *data = g_variant_new_string(TRIGGER_TYPE);
587 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
590 struct sr_datafeed_packet packet;
591 struct sr_datafeed_logic logic;
592 //uint64_t samples_read;
594 unsigned int packet_num, n;
596 struct dev_context *devc;
598 if (!(devc = sdi->priv)) {
599 sr_err("%s: sdi->priv was NULL", __func__);
603 if (configure_probes(sdi) != SR_OK) {
604 sr_err("Failed to configure probes.");
608 set_triggerbar(devc);
610 /* Push configured settings to device. */
611 analyzer_configure(devc->usb->devhdl);
613 analyzer_start(devc->usb->devhdl);
614 sr_info("Waiting for data.");
615 analyzer_wait_data(devc->usb->devhdl);
617 sr_info("Stop address = 0x%x.",
618 analyzer_get_stop_address(devc->usb->devhdl));
619 sr_info("Now address = 0x%x.",
620 analyzer_get_now_address(devc->usb->devhdl));
621 sr_info("Trigger address = 0x%x.",
622 analyzer_get_trigger_address(devc->usb->devhdl));
624 /* Send header packet to the session bus. */
625 std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
627 if (!(buf = g_try_malloc(PACKET_SIZE))) {
628 sr_err("Packet buffer malloc failed.");
629 return SR_ERR_MALLOC;
633 analyzer_read_start(devc->usb->devhdl);
634 /* Send the incoming transfer to the session bus. */
635 n = get_memory_size(devc->memory_size);
636 if (devc->max_memory_size * 4 < n)
637 n = devc->max_memory_size * 4;
638 for (packet_num = 0; packet_num < n / PACKET_SIZE; packet_num++) {
639 res = analyzer_read_data(devc->usb->devhdl, buf, PACKET_SIZE);
640 sr_info("Tried to read %d bytes, actually read %d bytes.",
643 packet.type = SR_DF_LOGIC;
644 packet.payload = &logic;
645 logic.length = PACKET_SIZE;
648 sr_session_send(cb_data, &packet);
649 //samples_read += res / 4;
651 analyzer_read_stop(devc->usb->devhdl);
654 packet.type = SR_DF_END;
655 sr_session_send(cb_data, &packet);
660 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
661 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
663 struct sr_datafeed_packet packet;
664 struct dev_context *devc;
666 packet.type = SR_DF_END;
667 sr_session_send(cb_data, &packet);
669 if (!(devc = sdi->priv)) {
670 sr_err("%s: sdi->priv was NULL", __func__);
674 analyzer_reset(devc->usb->devhdl);
675 /* TODO: Need to cancel and free any queued up transfers. */
680 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
681 .name = "zeroplus-logic-cube",
682 .longname = "ZEROPLUS Logic Cube LAP-C series",
685 .cleanup = hw_cleanup,
687 .dev_list = hw_dev_list,
688 .dev_clear = hw_cleanup,
689 .config_get = config_get,
690 .config_set = config_set,
691 .config_list = config_list,
692 .dev_open = hw_dev_open,
693 .dev_close = hw_dev_close,
694 .dev_acquisition_start = hw_dev_acquisition_start,
695 .dev_acquisition_stop = hw_dev_acquisition_stop,