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 USB_VENDOR 0x0c12
24 #define VENDOR_NAME "ZEROPLUS"
25 #define MODEL_NAME "Logic Cube LAP-C"
26 #define MODEL_VERSION NULL
29 #define USB_INTERFACE 0
30 #define USB_CONFIGURATION 1
31 #define NUM_TRIGGER_STAGES 4
32 #define TRIGGER_TYPE "01"
34 #define PACKET_SIZE 2048 /* ?? */
36 //#define ZP_EXPERIMENTAL
42 unsigned int channels;
43 unsigned int sample_depth; /* In Ksamples/channel */
44 unsigned int max_sampling_freq;
48 * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the
49 * same 128K sample depth.
51 static model_t zeroplus_models[] = {
52 {0x0c12, 0x7009, "LAP-C(16064)", 16, 64, 100},
53 {0x0c12, 0x700A, "LAP-C(16128)", 16, 128, 200},
54 /* TODO: we don't know anything about these
55 {0x0c12, 0x700B, "LAP-C(32128)", 32, 128, 200},
56 {0x0c12, 0x700C, "LAP-C(321000)", 32, 1024, 200},
57 {0x0c12, 0x700D, "LAP-C(322000)", 32, 2048, 200},
59 {0x0c12, 0x700E, "LAP-C(16032)", 16, 32, 100},
60 {0x0c12, 0x7016, "LAP-C(162000)", 16, 2048, 200},
64 static const int hwcaps[] = {
65 SR_CONF_LOGIC_ANALYZER,
67 SR_CONF_CAPTURE_RATIO,
69 /* These are really implemented in the driver, not the hardware. */
70 SR_CONF_LIMIT_SAMPLES,
75 * ZEROPLUS LAP-C (16032) numbers the 16 probes A0-A7 and B0-B7.
76 * We currently ignore other untested/unsupported devices here.
78 static const char *probe_names[NUM_PROBES + 1] = {
79 "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7",
80 "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7",
84 /* List of struct sr_dev_inst, maintained by dev_open()/dev_close(). */
85 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
86 static struct sr_dev_driver *di = &zeroplus_logic_cube_driver_info;
89 * The hardware supports more samplerates than these, but these are the
90 * options hardcoded into the vendor's Windows GUI.
94 * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up
97 const uint64_t zp_supported_samplerates[] = {
119 static const struct sr_samplerates samplerates = {
123 .list = zp_supported_samplerates,
126 static int hw_dev_close(struct sr_dev_inst *sdi);
129 static int configure_probes(const struct sr_dev_inst *sdi)
131 struct dev_context *devc;
132 const struct sr_probe *probe;
134 int probe_bit, stage, i;
137 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
140 devc->probe_mask = 0;
141 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
142 devc->trigger_mask[i] = 0;
143 devc->trigger_value[i] = 0;
147 for (l = sdi->probes; l; l = l->next) {
148 probe = (struct sr_probe *)l->data;
149 if (probe->enabled == FALSE)
151 probe_bit = 1 << (probe->index);
152 devc->probe_mask |= probe_bit;
154 if (probe->trigger) {
156 for (tc = probe->trigger; *tc; tc++) {
157 devc->trigger_mask[stage] |= probe_bit;
159 devc->trigger_value[stage] |= probe_bit;
161 if (stage > NUM_TRIGGER_STAGES)
171 static int configure_probes(const struct sr_dev_inst *sdi)
173 struct dev_context *devc;
175 const struct sr_probe *probe;
179 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
182 for (l = sdi->probes; l; l = l->next) {
183 probe = (struct sr_probe *)l->data;
184 if (probe->enabled == FALSE)
187 if ((tc = probe->trigger)) {
197 type = TRIGGER_POSEDGE;
200 type = TRIGGER_NEGEDGE;
203 type = TRIGGER_ANYEDGE;
209 analyzer_add_trigger(probe->index, type);
217 static int clear_instances(void)
220 struct sr_dev_inst *sdi;
221 struct drv_context *drvc;
222 struct dev_context *devc;
225 for (l = drvc->instances; l; l = l->next) {
227 if (!(devc = sdi->priv)) {
228 /* Log error, but continue cleaning up the rest. */
229 sr_err("%s: sdi->priv was NULL, continuing", __func__);
232 sr_usb_dev_inst_free(devc->usb);
233 /* Properly close all devices... */
235 /* ...and free all their memory. */
236 sr_dev_inst_free(sdi);
238 g_slist_free(drvc->instances);
239 drvc->instances = NULL;
244 static int hw_init(struct sr_context *sr_ctx)
246 return std_hw_init(sr_ctx, di, "zeroplus: ");
249 static GSList *hw_scan(GSList *options)
251 struct sr_dev_inst *sdi;
252 struct sr_probe *probe;
253 struct drv_context *drvc;
254 struct dev_context *devc;
256 struct libusb_device_descriptor des;
257 libusb_device **devlist;
259 int ret, devcnt, i, j;
269 /* Find all ZEROPLUS analyzers and add them to device list. */
271 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); /* TODO: Errors. */
273 for (i = 0; devlist[i]; i++) {
274 ret = libusb_get_device_descriptor(devlist[i], &des);
276 sr_err("Failed to get device descriptor: %s.",
277 libusb_error_name(ret));
282 for (j = 0; j < zeroplus_models[j].vid; j++) {
283 if (des.idVendor == zeroplus_models[j].vid &&
284 des.idProduct == zeroplus_models[j].pid) {
285 prof = &zeroplus_models[j];
288 /* Skip if the device was not found */
291 sr_info("Found ZEROPLUS model %s.", prof->model_name);
293 /* Register the device with libsigrok. */
294 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
295 VENDOR_NAME, prof->model_name, NULL))) {
296 sr_err("%s: sr_dev_inst_new failed", __func__);
301 /* Allocate memory for our private driver context. */
302 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
303 sr_err("Device context malloc failed.");
307 devc->num_channels = prof->channels;
308 #ifdef ZP_EXPERIMENTAL
309 devc->max_memory_size = 128 * 1024;
310 devc->max_samplerate = 200;
312 devc->max_memory_size = prof->sample_depth * 1024;
313 devc->max_samplerate = prof->max_sampling_freq;
315 devc->max_samplerate *= SR_MHZ(1);
316 devc->memory_size = MEMORY_SIZE_8K;
317 // memset(devc->trigger_buffer, 0, NUM_TRIGGER_STAGES);
319 /* Fill in probelist according to this device's profile. */
320 for (j = 0; j < devc->num_channels; j++) {
321 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
324 sdi->probes = g_slist_append(sdi->probes, probe);
327 devices = g_slist_append(devices, sdi);
328 drvc->instances = g_slist_append(drvc->instances, sdi);
329 devc->usb = sr_usb_dev_inst_new(
330 libusb_get_bus_number(devlist[i]),
331 libusb_get_device_address(devlist[i]), NULL);
335 libusb_free_device_list(devlist, 1);
340 static GSList *hw_dev_list(void)
342 return ((struct drv_context *)(di->priv))->instances;
345 static int hw_dev_open(struct sr_dev_inst *sdi)
347 struct dev_context *devc;
348 struct drv_context *drvc = di->priv;
349 libusb_device **devlist, *dev;
350 struct libusb_device_descriptor des;
351 int device_count, ret, i;
353 if (!(devc = sdi->priv)) {
354 sr_err("%s: sdi->priv was NULL", __func__);
358 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx,
360 if (device_count < 0) {
361 sr_err("Failed to retrieve device list.");
366 for (i = 0; i < device_count; i++) {
367 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
368 sr_err("Failed to get device descriptor: %s.",
369 libusb_error_name(ret));
372 if (libusb_get_bus_number(devlist[i]) == devc->usb->bus
373 && libusb_get_device_address(devlist[i]) == devc->usb->address) {
379 sr_err("Device on bus %d address %d disappeared!",
380 devc->usb->bus, devc->usb->address);
384 if (!(ret = libusb_open(dev, &(devc->usb->devhdl)))) {
385 sdi->status = SR_ST_ACTIVE;
386 sr_info("Opened device %d on %d.%d interface %d.",
387 sdi->index, devc->usb->bus,
388 devc->usb->address, USB_INTERFACE);
390 sr_err("Failed to open device: %s.", libusb_error_name(ret));
394 ret = libusb_set_configuration(devc->usb->devhdl, USB_CONFIGURATION);
396 sr_err("Unable to set USB configuration %d: %s.",
397 USB_CONFIGURATION, libusb_error_name(ret));
401 ret = libusb_claim_interface(devc->usb->devhdl, USB_INTERFACE);
403 sr_err("Unable to claim interface: %s.",
404 libusb_error_name(ret));
408 /* Set default configuration after power on */
409 if (analyzer_read_status(devc->usb->devhdl) == 0)
410 analyzer_configure(devc->usb->devhdl);
412 analyzer_reset(devc->usb->devhdl);
413 analyzer_initialize(devc->usb->devhdl);
415 //analyzer_set_memory_size(MEMORY_SIZE_512K);
416 // analyzer_set_freq(g_freq, g_freq_scale);
417 analyzer_set_trigger_count(1);
418 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
419 // * get_memory_size(g_memory_size)) / 100) >> 2);
422 if (g_double_mode == 1)
423 analyzer_set_compression(COMPRESSION_DOUBLE);
424 else if (g_compression == 1)
425 analyzer_set_compression(COMPRESSION_ENABLE);
428 analyzer_set_compression(COMPRESSION_NONE);
430 if (devc->cur_samplerate == 0) {
431 /* Samplerate hasn't been set. Default to 1MHz. */
432 analyzer_set_freq(1, FREQ_SCALE_MHZ);
433 devc->cur_samplerate = SR_MHZ(1);
439 static int hw_dev_close(struct sr_dev_inst *sdi)
441 struct dev_context *devc;
445 if (!devc->usb->devhdl)
448 sr_info("Closing device %d on %d.%d interface %d.", sdi->index,
449 devc->usb->bus, devc->usb->address, USB_INTERFACE);
450 libusb_release_interface(devc->usb->devhdl, USB_INTERFACE);
451 libusb_reset_device(devc->usb->devhdl);
452 libusb_close(devc->usb->devhdl);
453 devc->usb->devhdl = NULL;
454 sdi->status = SR_ST_INACTIVE;
459 static int hw_cleanup(void)
461 struct drv_context *drvc;
463 if (!(drvc = di->priv))
471 static int config_get(int id, const void **data, const struct sr_dev_inst *sdi)
473 struct dev_context *devc;
476 case SR_CONF_SAMPLERATE:
479 *data = &devc->cur_samplerate;
480 sr_spew("Returning samplerate: %" PRIu64 "Hz.",
481 devc->cur_samplerate);
492 static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
494 struct dev_context *devc;
497 sr_err("%s: sdi was NULL", __func__);
501 if (!(devc = sdi->priv)) {
502 sr_err("%s: sdi->priv was NULL", __func__);
507 case SR_CONF_SAMPLERATE:
508 return zp_set_samplerate(devc, *(const uint64_t *)value);
509 case SR_CONF_LIMIT_SAMPLES:
510 return set_limit_samples(devc, *(const uint64_t *)value);
511 case SR_CONF_CAPTURE_RATIO:
512 return set_capture_ratio(devc, *(const uint64_t *)value);
518 static int config_list(int key, const void **data, const struct sr_dev_inst *sdi)
523 case SR_CONF_DEVICE_OPTIONS:
526 case SR_CONF_SAMPLERATE:
527 *data = &samplerates;
529 case SR_CONF_TRIGGER_TYPE:
530 *data = TRIGGER_TYPE;
539 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
542 struct sr_datafeed_packet packet;
543 struct sr_datafeed_logic logic;
544 //uint64_t samples_read;
546 unsigned int packet_num;
549 struct dev_context *devc;
551 if (!(devc = sdi->priv)) {
552 sr_err("%s: sdi->priv was NULL", __func__);
556 if (configure_probes(sdi) != SR_OK) {
557 sr_err("Failed to configure probes.");
561 set_triggerbar(devc);
563 /* push configured settings to device */
564 analyzer_configure(devc->usb->devhdl);
566 analyzer_start(devc->usb->devhdl);
567 sr_info("Waiting for data.");
568 analyzer_wait_data(devc->usb->devhdl);
570 sr_info("Stop address = 0x%x.",
571 analyzer_get_stop_address(devc->usb->devhdl));
572 sr_info("Now address = 0x%x.",
573 analyzer_get_now_address(devc->usb->devhdl));
574 sr_info("Trigger address = 0x%x.",
575 analyzer_get_trigger_address(devc->usb->devhdl));
577 /* Send header packet to the session bus. */
578 std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
580 if (!(buf = g_try_malloc(PACKET_SIZE))) {
581 sr_err("Packet buffer malloc failed.");
582 return SR_ERR_MALLOC;
586 analyzer_read_start(devc->usb->devhdl);
587 /* Send the incoming transfer to the session bus. */
588 n = get_memory_size(devc->memory_size);
589 if (devc->max_memory_size * 4 < n)
590 n = devc->max_memory_size * 4;
591 for (packet_num = 0; packet_num < n / PACKET_SIZE; packet_num++) {
592 res = analyzer_read_data(devc->usb->devhdl, buf, PACKET_SIZE);
593 sr_info("Tried to read %d bytes, actually read %d bytes.",
596 packet.type = SR_DF_LOGIC;
597 packet.payload = &logic;
598 logic.length = PACKET_SIZE;
601 sr_session_send(cb_data, &packet);
602 //samples_read += res / 4;
604 analyzer_read_stop(devc->usb->devhdl);
607 packet.type = SR_DF_END;
608 sr_session_send(cb_data, &packet);
613 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
614 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
616 struct sr_datafeed_packet packet;
617 struct dev_context *devc;
619 packet.type = SR_DF_END;
620 sr_session_send(cb_data, &packet);
622 if (!(devc = sdi->priv)) {
623 sr_err("%s: sdi->priv was NULL", __func__);
627 analyzer_reset(devc->usb->devhdl);
628 /* TODO: Need to cancel and free any queued up transfers. */
633 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
634 .name = "zeroplus-logic-cube",
635 .longname = "ZEROPLUS Logic Cube LAP-C series",
638 .cleanup = hw_cleanup,
640 .dev_list = hw_dev_list,
641 .dev_clear = hw_cleanup,
642 .config_get = config_get,
643 .config_set = config_set,
644 .config_list = config_list,
645 .dev_open = hw_dev_open,
646 .dev_close = hw_dev_close,
647 .dev_acquisition_start = hw_dev_acquisition_start,
648 .dev_acquisition_stop = hw_dev_acquisition_stop,