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 int hwcaps[] = {
58 SR_CONF_LOGIC_ANALYZER,
60 SR_CONF_CAPTURE_RATIO,
61 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.
84 * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up
87 const uint64_t zp_supported_samplerates[] = {
109 static const struct sr_samplerates samplerates = {
113 .list = zp_supported_samplerates,
116 static int hw_dev_close(struct sr_dev_inst *sdi);
119 static int configure_probes(const struct sr_dev_inst *sdi)
121 struct dev_context *devc;
122 const struct sr_probe *probe;
124 int probe_bit, stage, i;
127 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
130 devc->probe_mask = 0;
131 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
132 devc->trigger_mask[i] = 0;
133 devc->trigger_value[i] = 0;
137 for (l = sdi->probes; l; l = l->next) {
138 probe = (struct sr_probe *)l->data;
139 if (probe->enabled == FALSE)
141 probe_bit = 1 << (probe->index);
142 devc->probe_mask |= probe_bit;
144 if (probe->trigger) {
146 for (tc = probe->trigger; *tc; tc++) {
147 devc->trigger_mask[stage] |= probe_bit;
149 devc->trigger_value[stage] |= probe_bit;
151 if (stage > NUM_TRIGGER_STAGES)
161 static int configure_probes(const struct sr_dev_inst *sdi)
163 struct dev_context *devc;
165 const struct sr_probe *probe;
169 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
172 for (l = sdi->probes; l; l = l->next) {
173 probe = (struct sr_probe *)l->data;
174 if (probe->enabled == FALSE)
177 if ((tc = probe->trigger)) {
187 type = TRIGGER_POSEDGE;
190 type = TRIGGER_NEGEDGE;
193 type = TRIGGER_ANYEDGE;
199 analyzer_add_trigger(probe->index, type);
207 static int clear_instances(void)
210 struct sr_dev_inst *sdi;
211 struct drv_context *drvc;
212 struct dev_context *devc;
215 for (l = drvc->instances; l; l = l->next) {
217 if (!(devc = sdi->priv)) {
218 /* Log error, but continue cleaning up the rest. */
219 sr_err("%s: sdi->priv was NULL, continuing", __func__);
222 sr_usb_dev_inst_free(devc->usb);
223 /* Properly close all devices... */
225 /* ...and free all their memory. */
226 sr_dev_inst_free(sdi);
228 g_slist_free(drvc->instances);
229 drvc->instances = NULL;
234 static int hw_init(struct sr_context *sr_ctx)
236 return std_hw_init(sr_ctx, di, "zeroplus: ");
239 static GSList *hw_scan(GSList *options)
241 struct sr_dev_inst *sdi;
242 struct sr_probe *probe;
243 struct drv_context *drvc;
244 struct dev_context *devc;
245 const struct zp_model *prof;
246 struct libusb_device_descriptor des;
247 libusb_device **devlist;
249 int ret, devcnt, i, j;
259 /* Find all ZEROPLUS analyzers and add them to device list. */
261 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); /* TODO: Errors. */
263 for (i = 0; devlist[i]; i++) {
264 ret = libusb_get_device_descriptor(devlist[i], &des);
266 sr_err("Failed to get device descriptor: %s.",
267 libusb_error_name(ret));
272 for (j = 0; j < zeroplus_models[j].vid; j++) {
273 if (des.idVendor == zeroplus_models[j].vid &&
274 des.idProduct == zeroplus_models[j].pid) {
275 prof = &zeroplus_models[j];
278 /* Skip if the device was not found. */
281 sr_info("Found ZEROPLUS %s.", prof->model_name);
283 /* Register the device with libsigrok. */
284 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
285 VENDOR_NAME, prof->model_name, NULL))) {
286 sr_err("%s: sr_dev_inst_new failed", __func__);
291 /* Allocate memory for our private driver context. */
292 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
293 sr_err("Device context malloc failed.");
298 devc->num_channels = prof->channels;
299 #ifdef ZP_EXPERIMENTAL
300 devc->max_memory_size = 128 * 1024;
301 devc->max_samplerate = 200;
303 devc->max_memory_size = prof->sample_depth * 1024;
304 devc->max_samplerate = prof->max_sampling_freq;
306 devc->max_samplerate *= SR_MHZ(1);
307 devc->memory_size = MEMORY_SIZE_8K;
308 // memset(devc->trigger_buffer, 0, NUM_TRIGGER_STAGES);
310 /* Fill in probelist according to this device's profile. */
311 for (j = 0; j < devc->num_channels; j++) {
312 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
315 sdi->probes = g_slist_append(sdi->probes, probe);
318 devices = g_slist_append(devices, sdi);
319 drvc->instances = g_slist_append(drvc->instances, sdi);
320 devc->usb = sr_usb_dev_inst_new(
321 libusb_get_bus_number(devlist[i]),
322 libusb_get_device_address(devlist[i]), NULL);
326 libusb_free_device_list(devlist, 1);
331 static GSList *hw_dev_list(void)
333 return ((struct drv_context *)(di->priv))->instances;
336 static int hw_dev_open(struct sr_dev_inst *sdi)
338 struct dev_context *devc;
339 struct drv_context *drvc;
340 libusb_device **devlist, *dev;
341 struct libusb_device_descriptor des;
342 int device_count, ret, i;
346 if (!(devc = sdi->priv)) {
347 sr_err("%s: sdi->priv was NULL", __func__);
351 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx,
353 if (device_count < 0) {
354 sr_err("Failed to retrieve device list.");
359 for (i = 0; i < device_count; i++) {
360 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
361 sr_err("Failed to get device descriptor: %s.",
362 libusb_error_name(ret));
365 if (libusb_get_bus_number(devlist[i]) == devc->usb->bus
366 && libusb_get_device_address(devlist[i]) == devc->usb->address) {
372 sr_err("Device on bus %d address %d disappeared!",
373 devc->usb->bus, devc->usb->address);
377 if (!(ret = libusb_open(dev, &(devc->usb->devhdl)))) {
378 sdi->status = SR_ST_ACTIVE;
379 sr_info("Opened device %d on %d.%d interface %d.",
380 sdi->index, devc->usb->bus,
381 devc->usb->address, USB_INTERFACE);
383 sr_err("Failed to open device: %s.", libusb_error_name(ret));
387 ret = libusb_set_configuration(devc->usb->devhdl, USB_CONFIGURATION);
389 sr_err("Unable to set USB configuration %d: %s.",
390 USB_CONFIGURATION, libusb_error_name(ret));
394 ret = libusb_claim_interface(devc->usb->devhdl, USB_INTERFACE);
396 sr_err("Unable to claim interface: %s.",
397 libusb_error_name(ret));
401 /* Set default configuration after power on. */
402 if (analyzer_read_status(devc->usb->devhdl) == 0)
403 analyzer_configure(devc->usb->devhdl);
405 analyzer_reset(devc->usb->devhdl);
406 analyzer_initialize(devc->usb->devhdl);
408 //analyzer_set_memory_size(MEMORY_SIZE_512K);
409 // analyzer_set_freq(g_freq, g_freq_scale);
410 analyzer_set_trigger_count(1);
411 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
412 // * get_memory_size(g_memory_size)) / 100) >> 2);
415 if (g_double_mode == 1)
416 analyzer_set_compression(COMPRESSION_DOUBLE);
417 else if (g_compression == 1)
418 analyzer_set_compression(COMPRESSION_ENABLE);
421 analyzer_set_compression(COMPRESSION_NONE);
423 if (devc->cur_samplerate == 0) {
424 /* Samplerate hasn't been set. Default to 1MHz. */
425 analyzer_set_freq(1, FREQ_SCALE_MHZ);
426 devc->cur_samplerate = SR_MHZ(1);
432 static int hw_dev_close(struct sr_dev_inst *sdi)
434 struct dev_context *devc;
438 if (!devc->usb->devhdl)
441 sr_info("Closing device %d on %d.%d interface %d.", sdi->index,
442 devc->usb->bus, devc->usb->address, USB_INTERFACE);
443 libusb_release_interface(devc->usb->devhdl, USB_INTERFACE);
444 libusb_reset_device(devc->usb->devhdl);
445 libusb_close(devc->usb->devhdl);
446 devc->usb->devhdl = NULL;
447 sdi->status = SR_ST_INACTIVE;
452 static int hw_cleanup(void)
454 struct drv_context *drvc;
456 if (!(drvc = di->priv))
464 static int config_get(int id, const void **data, const struct sr_dev_inst *sdi)
466 struct dev_context *devc;
469 case SR_CONF_SAMPLERATE:
472 *data = &devc->cur_samplerate;
473 sr_spew("Returning samplerate: %" PRIu64 "Hz.",
474 devc->cur_samplerate);
485 static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
487 struct dev_context *devc;
490 sr_err("%s: sdi was NULL", __func__);
494 if (!(devc = sdi->priv)) {
495 sr_err("%s: sdi->priv was NULL", __func__);
500 case SR_CONF_SAMPLERATE:
501 return zp_set_samplerate(devc, *(const uint64_t *)value);
502 case SR_CONF_LIMIT_SAMPLES:
503 return set_limit_samples(devc, *(const uint64_t *)value);
504 case SR_CONF_CAPTURE_RATIO:
505 return set_capture_ratio(devc, *(const uint64_t *)value);
513 static int config_list(int key, const void **data, const struct sr_dev_inst *sdi)
518 case SR_CONF_DEVICE_OPTIONS:
521 case SR_CONF_SAMPLERATE:
522 *data = &samplerates;
524 case SR_CONF_TRIGGER_TYPE:
525 *data = TRIGGER_TYPE;
534 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
537 struct sr_datafeed_packet packet;
538 struct sr_datafeed_logic logic;
539 //uint64_t samples_read;
541 unsigned int packet_num, n;
543 struct dev_context *devc;
545 if (!(devc = sdi->priv)) {
546 sr_err("%s: sdi->priv was NULL", __func__);
550 if (configure_probes(sdi) != SR_OK) {
551 sr_err("Failed to configure probes.");
555 set_triggerbar(devc);
557 /* Push configured settings to device. */
558 analyzer_configure(devc->usb->devhdl);
560 analyzer_start(devc->usb->devhdl);
561 sr_info("Waiting for data.");
562 analyzer_wait_data(devc->usb->devhdl);
564 sr_info("Stop address = 0x%x.",
565 analyzer_get_stop_address(devc->usb->devhdl));
566 sr_info("Now address = 0x%x.",
567 analyzer_get_now_address(devc->usb->devhdl));
568 sr_info("Trigger address = 0x%x.",
569 analyzer_get_trigger_address(devc->usb->devhdl));
571 /* Send header packet to the session bus. */
572 std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
574 if (!(buf = g_try_malloc(PACKET_SIZE))) {
575 sr_err("Packet buffer malloc failed.");
576 return SR_ERR_MALLOC;
580 analyzer_read_start(devc->usb->devhdl);
581 /* Send the incoming transfer to the session bus. */
582 n = get_memory_size(devc->memory_size);
583 if (devc->max_memory_size * 4 < n)
584 n = devc->max_memory_size * 4;
585 for (packet_num = 0; packet_num < n / PACKET_SIZE; packet_num++) {
586 res = analyzer_read_data(devc->usb->devhdl, buf, PACKET_SIZE);
587 sr_info("Tried to read %d bytes, actually read %d bytes.",
590 packet.type = SR_DF_LOGIC;
591 packet.payload = &logic;
592 logic.length = PACKET_SIZE;
595 sr_session_send(cb_data, &packet);
596 //samples_read += res / 4;
598 analyzer_read_stop(devc->usb->devhdl);
601 packet.type = SR_DF_END;
602 sr_session_send(cb_data, &packet);
607 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
608 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
610 struct sr_datafeed_packet packet;
611 struct dev_context *devc;
613 packet.type = SR_DF_END;
614 sr_session_send(cb_data, &packet);
616 if (!(devc = sdi->priv)) {
617 sr_err("%s: sdi->priv was NULL", __func__);
621 analyzer_reset(devc->usb->devhdl);
622 /* TODO: Need to cancel and free any queued up transfers. */
627 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
628 .name = "zeroplus-logic-cube",
629 .longname = "ZEROPLUS Logic Cube LAP-C series",
632 .cleanup = hw_cleanup,
634 .dev_list = hw_dev_list,
635 .dev_clear = hw_cleanup,
636 .config_get = config_get,
637 .config_set = config_set,
638 .config_list = config_list,
639 .dev_open = hw_dev_open,
640 .dev_close = hw_dev_close,
641 .dev_acquisition_start = hw_dev_acquisition_start,
642 .dev_acquisition_stop = hw_dev_acquisition_stop,