2 * This file is part of the libsigrok 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, 0x7002, "LAP-16128U", 16, 128, 200},
46 {0x0c12, 0x7009, "LAP-C(16064)", 16, 64, 100},
47 {0x0c12, 0x700a, "LAP-C(16128)", 16, 128, 200},
48 /* TODO: We don't know anything about these.
49 {0x0c12, 0x700b, "LAP-C(32128)", 32, 128, 200},
50 {0x0c12, 0x700c, "LAP-C(321000)", 32, 1024, 200},
51 {0x0c12, 0x700d, "LAP-C(322000)", 32, 2048, 200},
53 {0x0c12, 0x700e, "LAP-C(16032)", 16, 32, 100},
54 {0x0c12, 0x7016, "LAP-C(162000)", 16, 2048, 200},
58 static const int32_t hwcaps[] = {
59 SR_CONF_LOGIC_ANALYZER,
61 SR_CONF_CAPTURE_RATIO,
62 SR_CONF_LIMIT_SAMPLES,
66 * ZEROPLUS LAP-C (16032) numbers the 16 probes A0-A7 and B0-B7.
67 * We currently ignore other untested/unsupported devices here.
69 static const char *probe_names[] = {
70 "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7",
71 "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7",
75 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
76 static struct sr_dev_driver *di = &zeroplus_logic_cube_driver_info;
79 * The hardware supports more samplerates than these, but these are the
80 * options hardcoded into the vendor's Windows GUI.
83 static const uint64_t samplerates_100[] = {
102 const uint64_t samplerates_200[] = {
123 static int hw_dev_close(struct sr_dev_inst *sdi);
126 static int configure_probes(const struct sr_dev_inst *sdi)
128 struct dev_context *devc;
129 const struct sr_probe *probe;
131 int probe_bit, stage, i;
134 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
137 devc->probe_mask = 0;
138 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
139 devc->trigger_mask[i] = 0;
140 devc->trigger_value[i] = 0;
144 for (l = sdi->probes; l; l = l->next) {
145 probe = (struct sr_probe *)l->data;
146 if (probe->enabled == FALSE)
148 probe_bit = 1 << (probe->index);
149 devc->probe_mask |= probe_bit;
151 if (probe->trigger) {
153 for (tc = probe->trigger; *tc; tc++) {
154 devc->trigger_mask[stage] |= probe_bit;
156 devc->trigger_value[stage] |= probe_bit;
158 if (stage > NUM_TRIGGER_STAGES)
168 static int configure_probes(const struct sr_dev_inst *sdi)
170 struct dev_context *devc;
172 const struct sr_probe *probe;
176 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
179 for (l = sdi->probes; l; l = l->next) {
180 probe = (struct sr_probe *)l->data;
181 if (probe->enabled == FALSE)
184 if ((tc = probe->trigger)) {
194 type = TRIGGER_POSEDGE;
197 type = TRIGGER_NEGEDGE;
200 type = TRIGGER_ANYEDGE;
206 analyzer_add_trigger(probe->index, type);
214 SR_PRIV int zp_set_samplerate(struct dev_context *devc, uint64_t samplerate)
218 for (i = 0; ARRAY_SIZE(samplerates_200); i++)
219 if (samplerate == samplerates_200[i])
222 if (i == ARRAY_SIZE(samplerates_200) || samplerate > devc->max_samplerate) {
223 sr_err("Unsupported samplerate: %" PRIu64 "Hz.", samplerate);
227 sr_info("Setting samplerate to %" PRIu64 "Hz.", samplerate);
229 if (samplerate >= SR_MHZ(1))
230 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
231 else if (samplerate >= SR_KHZ(1))
232 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
234 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
236 devc->cur_samplerate = samplerate;
241 static int clear_instances(void)
244 struct sr_dev_inst *sdi;
245 struct drv_context *drvc;
246 struct dev_context *devc;
247 struct sr_usb_dev_inst *usb;
250 for (l = drvc->instances; l; l = l->next) {
252 if (!(devc = sdi->priv)) {
253 /* Log error, but continue cleaning up the rest. */
254 sr_err("%s: sdi->priv was NULL, continuing", __func__);
258 sr_usb_dev_inst_free(usb);
259 /* Properly close all devices... */
261 /* ...and free all their memory. */
262 sr_dev_inst_free(sdi);
264 g_slist_free(drvc->instances);
265 drvc->instances = NULL;
270 static int hw_init(struct sr_context *sr_ctx)
272 return std_hw_init(sr_ctx, di, "zeroplus: ");
275 static GSList *hw_scan(GSList *options)
277 struct sr_dev_inst *sdi;
278 struct sr_probe *probe;
279 struct drv_context *drvc;
280 struct dev_context *devc;
281 const struct zp_model *prof;
282 struct libusb_device_descriptor des;
283 libusb_device **devlist;
285 int ret, devcnt, i, j;
293 /* Find all ZEROPLUS analyzers and add them to device list. */
295 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); /* TODO: Errors. */
297 for (i = 0; devlist[i]; i++) {
298 ret = libusb_get_device_descriptor(devlist[i], &des);
300 sr_err("Failed to get device descriptor: %s.",
301 libusb_error_name(ret));
306 for (j = 0; j < zeroplus_models[j].vid; j++) {
307 if (des.idVendor == zeroplus_models[j].vid &&
308 des.idProduct == zeroplus_models[j].pid) {
309 prof = &zeroplus_models[j];
312 /* Skip if the device was not found. */
315 sr_info("Found ZEROPLUS %s.", prof->model_name);
317 /* Register the device with libsigrok. */
318 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
319 VENDOR_NAME, prof->model_name, NULL))) {
320 sr_err("%s: sr_dev_inst_new failed", __func__);
325 /* Allocate memory for our private driver context. */
326 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
327 sr_err("Device context malloc failed.");
333 devc->num_channels = prof->channels;
334 #ifdef ZP_EXPERIMENTAL
335 devc->max_memory_size = 128 * 1024;
336 devc->max_samplerate = 200;
338 devc->max_memory_size = prof->sample_depth * 1024;
339 devc->max_samplerate = prof->max_sampling_freq;
341 devc->max_samplerate *= SR_MHZ(1);
342 devc->memory_size = MEMORY_SIZE_8K;
343 // memset(devc->trigger_buffer, 0, NUM_TRIGGER_STAGES);
345 /* Fill in probelist according to this device's profile. */
346 for (j = 0; j < devc->num_channels; j++) {
347 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
350 sdi->probes = g_slist_append(sdi->probes, probe);
353 devices = g_slist_append(devices, sdi);
354 drvc->instances = g_slist_append(drvc->instances, sdi);
355 sdi->conn = sr_usb_dev_inst_new(
356 libusb_get_bus_number(devlist[i]),
357 libusb_get_device_address(devlist[i]), NULL);
361 libusb_free_device_list(devlist, 1);
366 static GSList *hw_dev_list(void)
368 return ((struct drv_context *)(di->priv))->instances;
371 static int hw_dev_open(struct sr_dev_inst *sdi)
373 struct dev_context *devc;
374 struct drv_context *drvc;
375 struct sr_usb_dev_inst *usb;
376 libusb_device **devlist, *dev;
377 struct libusb_device_descriptor des;
378 int device_count, ret, i;
383 if (!(devc = sdi->priv)) {
384 sr_err("%s: sdi->priv was NULL", __func__);
388 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx,
390 if (device_count < 0) {
391 sr_err("Failed to retrieve device list.");
396 for (i = 0; i < device_count; i++) {
397 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
398 sr_err("Failed to get device descriptor: %s.",
399 libusb_error_name(ret));
402 if (libusb_get_bus_number(devlist[i]) == usb->bus
403 && libusb_get_device_address(devlist[i]) == usb->address) {
409 sr_err("Device on bus %d address %d disappeared!",
410 usb->bus, usb->address);
414 if (!(ret = libusb_open(dev, &(usb->devhdl)))) {
415 sdi->status = SR_ST_ACTIVE;
416 sr_info("Opened device %d on %d.%d interface %d.",
417 sdi->index, usb->bus, usb->address, USB_INTERFACE);
419 sr_err("Failed to open device: %s.", libusb_error_name(ret));
423 ret = libusb_set_configuration(usb->devhdl, USB_CONFIGURATION);
425 sr_err("Unable to set USB configuration %d: %s.",
426 USB_CONFIGURATION, libusb_error_name(ret));
430 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
432 sr_err("Unable to claim interface: %s.",
433 libusb_error_name(ret));
437 /* Set default configuration after power on. */
438 if (analyzer_read_status(usb->devhdl) == 0)
439 analyzer_configure(usb->devhdl);
441 analyzer_reset(usb->devhdl);
442 analyzer_initialize(usb->devhdl);
444 //analyzer_set_memory_size(MEMORY_SIZE_512K);
445 // analyzer_set_freq(g_freq, g_freq_scale);
446 analyzer_set_trigger_count(1);
447 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
448 // * get_memory_size(g_memory_size)) / 100) >> 2);
451 if (g_double_mode == 1)
452 analyzer_set_compression(COMPRESSION_DOUBLE);
453 else if (g_compression == 1)
454 analyzer_set_compression(COMPRESSION_ENABLE);
457 analyzer_set_compression(COMPRESSION_NONE);
459 if (devc->cur_samplerate == 0) {
460 /* Samplerate hasn't been set. Default to 1MHz. */
461 analyzer_set_freq(1, FREQ_SCALE_MHZ);
462 devc->cur_samplerate = SR_MHZ(1);
468 static int hw_dev_close(struct sr_dev_inst *sdi)
470 struct sr_usb_dev_inst *usb;
477 sr_info("Closing device %d on %d.%d interface %d.", sdi->index,
478 usb->bus, usb->address, USB_INTERFACE);
479 libusb_release_interface(usb->devhdl, USB_INTERFACE);
480 libusb_reset_device(usb->devhdl);
481 libusb_close(usb->devhdl);
483 sdi->status = SR_ST_INACTIVE;
488 static int hw_cleanup(void)
490 struct drv_context *drvc;
492 if (!(drvc = di->priv))
500 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi)
502 struct dev_context *devc;
505 case SR_CONF_SAMPLERATE:
508 *data = g_variant_new_uint64(devc->cur_samplerate);
509 sr_spew("Returning samplerate: %" PRIu64 "Hz.",
510 devc->cur_samplerate);
521 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
523 struct dev_context *devc;
525 if (sdi->status != SR_ST_ACTIVE)
526 return SR_ERR_DEV_CLOSED;
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 dev_context *devc;
591 struct sr_usb_dev_inst *usb;
592 struct sr_datafeed_packet packet;
593 struct sr_datafeed_logic logic;
594 //uint64_t samples_read;
596 unsigned int packet_num, n;
599 if (sdi->status != SR_ST_ACTIVE)
600 return SR_ERR_DEV_CLOSED;
602 if (!(devc = sdi->priv)) {
603 sr_err("%s: sdi->priv was NULL", __func__);
607 if (configure_probes(sdi) != SR_OK) {
608 sr_err("Failed to configure probes.");
614 set_triggerbar(devc);
616 /* Push configured settings to device. */
617 analyzer_configure(usb->devhdl);
619 analyzer_start(usb->devhdl);
620 sr_info("Waiting for data.");
621 analyzer_wait_data(usb->devhdl);
623 sr_info("Stop address = 0x%x.",
624 analyzer_get_stop_address(usb->devhdl));
625 sr_info("Now address = 0x%x.",
626 analyzer_get_now_address(usb->devhdl));
627 sr_info("Trigger address = 0x%x.",
628 analyzer_get_trigger_address(usb->devhdl));
630 /* Send header packet to the session bus. */
631 std_session_send_df_header(cb_data, LOG_PREFIX);
633 if (!(buf = g_try_malloc(PACKET_SIZE))) {
634 sr_err("Packet buffer malloc failed.");
635 return SR_ERR_MALLOC;
639 analyzer_read_start(usb->devhdl);
640 /* Send the incoming transfer to the session bus. */
641 n = get_memory_size(devc->memory_size);
642 if (devc->max_memory_size * 4 < n)
643 n = devc->max_memory_size * 4;
644 for (packet_num = 0; packet_num < n / PACKET_SIZE; packet_num++) {
645 res = analyzer_read_data(usb->devhdl, buf, PACKET_SIZE);
646 sr_info("Tried to read %d bytes, actually read %d bytes.",
649 packet.type = SR_DF_LOGIC;
650 packet.payload = &logic;
651 logic.length = PACKET_SIZE;
654 sr_session_send(cb_data, &packet);
655 //samples_read += res / 4;
657 analyzer_read_stop(usb->devhdl);
660 packet.type = SR_DF_END;
661 sr_session_send(cb_data, &packet);
666 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
667 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
669 struct dev_context *devc;
670 struct sr_usb_dev_inst *usb;
671 struct sr_datafeed_packet packet;
673 packet.type = SR_DF_END;
674 sr_session_send(cb_data, &packet);
676 if (!(devc = sdi->priv)) {
677 sr_err("%s: sdi->priv was NULL", __func__);
682 analyzer_reset(usb->devhdl);
683 /* TODO: Need to cancel and free any queued up transfers. */
688 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
689 .name = "zeroplus-logic-cube",
690 .longname = "ZEROPLUS Logic Cube LAP-C series",
693 .cleanup = hw_cleanup,
695 .dev_list = hw_dev_list,
696 .dev_clear = hw_cleanup,
697 .config_get = config_get,
698 .config_set = config_set,
699 .config_list = config_list,
700 .dev_open = hw_dev_open,
701 .dev_close = hw_dev_close,
702 .dev_acquisition_start = hw_dev_acquisition_start,
703 .dev_acquisition_stop = hw_dev_acquisition_stop,