2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 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/>.
28 #include <sigrok-internal.h>
31 #define USB_VENDOR 0x0c12
32 #define USB_VENDOR_NAME "Zeroplus"
33 #define USB_MODEL_NAME "Logic Cube"
34 #define USB_MODEL_VERSION ""
36 #define USB_INTERFACE 0
37 #define USB_CONFIGURATION 1
38 #define NUM_TRIGGER_STAGES 4
39 #define TRIGGER_TYPES "01"
41 #define PACKET_SIZE 2048 /* ?? */
46 unsigned int channels;
47 unsigned int sample_depth; /* In Ksamples/channel */
48 unsigned int max_sampling_freq;
52 * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the
53 * same 128K sample depth.
55 static model_t zeroplus_models[] = {
56 {0x7009, "LAP-C(16064)", 16, 64, 100},
57 {0x700A, "LAP-C(16128)", 16, 128, 200},
58 {0x700B, "LAP-C(32128)", 32, 128, 200},
59 {0x700C, "LAP-C(321000)", 32, 1024, 200},
60 {0x700D, "LAP-C(322000)", 32, 2048, 200},
61 {0x700E, "LAP-C(16032)", 16, 32, 100},
62 {0x7016, "LAP-C(162000)", 16, 2048, 200},
65 static int capabilities[] = {
66 SR_HWCAP_LOGIC_ANALYZER,
69 SR_HWCAP_CAPTURE_RATIO,
71 /* These are really implemented in the driver, not the hardware. */
72 SR_HWCAP_LIMIT_SAMPLES,
76 /* List of struct sr_device_instance, maintained by opendev()/closedev(). */
77 static GSList *device_instances = NULL;
79 static libusb_context *usb_context = NULL;
82 * The hardware supports more samplerates than these, but these are the
83 * options hardcoded into the vendor's Windows GUI.
87 * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up
90 static uint64_t supported_samplerates[] = {
112 static struct sr_samplerates samplerates = {
116 supported_samplerates,
119 /* TODO: All of these should go in a device-specific struct. */
120 static uint64_t cur_samplerate = 0;
121 static uint64_t limit_samples = 0;
122 static int num_channels = 32; /* TODO: This isn't initialized before it's needed :( */
123 static uint64_t memory_size = 0;
124 static uint8_t probe_mask = 0;
125 static uint8_t trigger_mask[NUM_TRIGGER_STAGES] = { 0 };
126 static uint8_t trigger_value[NUM_TRIGGER_STAGES] = { 0 };
128 // static uint8_t trigger_buffer[NUM_TRIGGER_STAGES] = { 0 };
130 static int hw_set_configuration(int device_index, int capability, void *value);
132 static unsigned int get_memory_size(int type)
134 if (type == MEMORY_SIZE_8K)
136 else if (type == MEMORY_SIZE_64K)
138 else if (type == MEMORY_SIZE_128K)
140 else if (type == MEMORY_SIZE_512K)
146 static int opendev4(struct sr_device_instance **sdi, libusb_device *dev,
147 struct libusb_device_descriptor *des)
152 if ((err = libusb_get_device_descriptor(dev, des))) {
153 g_warning("failed to get device descriptor: %d", err);
157 if (des->idVendor != USB_VENDOR)
160 if (libusb_get_bus_number(dev) == (*sdi)->usb->bus
161 && libusb_get_device_address(dev) == (*sdi)->usb->address) {
163 for (i = 0; i < ARRAY_SIZE(zeroplus_models); i++) {
164 if (!(des->idProduct == zeroplus_models[i].pid))
167 g_message("Found PID=%04X (%s)", des->idProduct,
168 zeroplus_models[i].model_name);
169 num_channels = zeroplus_models[i].channels;
170 memory_size = zeroplus_models[i].sample_depth * 1024;
174 if (num_channels == 0) {
175 g_warning("Unknown ZeroPlus device %04X",
181 if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) {
182 (*sdi)->status = SR_ST_ACTIVE;
183 g_message("opened device %d on %d.%d interface %d",
184 (*sdi)->index, (*sdi)->usb->bus,
185 (*sdi)->usb->address, USB_INTERFACE);
187 g_warning("failed to open device: %d", err);
195 static struct sr_device_instance *zp_open_device(int device_index)
197 struct sr_device_instance *sdi;
198 libusb_device **devlist;
199 struct libusb_device_descriptor des;
202 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
205 libusb_get_device_list(usb_context, &devlist);
206 if (sdi->status == SR_ST_INACTIVE) {
207 /* Find the device by vendor, product, bus and address. */
208 libusb_get_device_list(usb_context, &devlist);
209 for (i = 0; devlist[i]; i++) {
210 /* TODO: Error handling. */
211 err = opendev4(&sdi, devlist[i], &des);
214 /* Status must be SR_ST_ACTIVE, i.e. already in use... */
217 libusb_free_device_list(devlist, 1);
219 if (sdi && sdi->status != SR_ST_ACTIVE)
225 static void close_device(struct sr_device_instance *sdi)
227 if (!sdi->usb->devhdl)
230 g_message("closing device %d on %d.%d interface %d", sdi->index,
231 sdi->usb->bus, sdi->usb->address, USB_INTERFACE);
232 libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE);
233 libusb_close(sdi->usb->devhdl);
234 sdi->usb->devhdl = NULL;
235 sdi->status = SR_ST_INACTIVE;
238 static int configure_probes(GSList *probes)
240 struct sr_probe *probe;
242 int probe_bit, stage, i;
246 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
248 trigger_value[i] = 0;
252 for (l = probes; l; l = l->next) {
253 probe = (struct sr_probe *)l->data;
254 if (probe->enabled == FALSE)
256 probe_bit = 1 << (probe->index - 1);
257 probe_mask |= probe_bit;
259 if (probe->trigger) {
261 for (tc = probe->trigger; *tc; tc++) {
262 trigger_mask[stage] |= probe_bit;
264 trigger_value[stage] |= probe_bit;
266 if (stage > NUM_TRIGGER_STAGES)
279 static int hw_init(const char *deviceinfo)
281 struct sr_device_instance *sdi;
282 struct libusb_device_descriptor des;
283 libusb_device **devlist;
286 /* Avoid compiler warnings. */
287 deviceinfo = deviceinfo;
289 if (libusb_init(&usb_context) != 0) {
290 g_warning("Failed to initialize USB.");
294 /* Find all ZeroPlus analyzers and add them to device list. */
296 libusb_get_device_list(usb_context, &devlist);
298 for (i = 0; devlist[i]; i++) {
299 err = libusb_get_device_descriptor(devlist[i], &des);
301 g_warning("failed to get device descriptor: %d", err);
305 if (des.idVendor == USB_VENDOR) {
307 * Definitely a Zeroplus.
308 * TODO: Any way to detect specific model/version in
309 * the zeroplus range?
311 sdi = sr_device_instance_new(devcnt,
312 SR_ST_INACTIVE, USB_VENDOR_NAME,
313 USB_MODEL_NAME, USB_MODEL_VERSION);
317 g_slist_append(device_instances, sdi);
318 sdi->usb = sr_usb_device_instance_new(
319 libusb_get_bus_number(devlist[i]),
320 libusb_get_device_address(devlist[i]), NULL);
324 libusb_free_device_list(devlist, 1);
329 static int hw_opendev(int device_index)
331 struct sr_device_instance *sdi;
334 if (!(sdi = zp_open_device(device_index))) {
335 g_warning("unable to open device");
339 err = libusb_claim_interface(sdi->usb->devhdl, USB_INTERFACE);
341 g_warning("Unable to claim interface: %d", err);
344 analyzer_reset(sdi->usb->devhdl);
345 analyzer_initialize(sdi->usb->devhdl);
347 analyzer_set_memory_size(MEMORY_SIZE_512K);
348 // analyzer_set_freq(g_freq, g_freq_scale);
349 analyzer_set_trigger_count(1);
350 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
351 // * get_memory_size(g_memory_size)) / 100) >> 2);
352 analyzer_set_ramsize_trigger_address(
353 (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
356 if (g_double_mode == 1)
357 analyzer_set_compression(COMPRESSION_DOUBLE);
358 else if (g_compression == 1)
359 analyzer_set_compression(COMPRESSION_ENABLE);
362 analyzer_set_compression(COMPRESSION_NONE);
364 if (cur_samplerate == 0) {
365 /* Samplerate hasn't been set. Default to the slowest one. */
366 if (hw_set_configuration(device_index, SR_HWCAP_SAMPLERATE,
367 &samplerates.low) == SR_ERR)
374 static void hw_closedev(int device_index)
376 struct sr_device_instance *sdi;
378 if ((sdi = sr_get_device_instance(device_instances, device_index)))
382 static void hw_cleanup(void)
386 /* Properly close all devices... */
387 for (l = device_instances; l; l = l->next)
388 close_device((struct sr_device_instance *)l->data);
390 /* ...and free all their memory. */
391 for (l = device_instances; l; l = l->next)
393 g_slist_free(device_instances);
394 device_instances = NULL;
397 libusb_exit(usb_context);
401 static void *hw_get_device_info(int device_index, int device_info_id)
403 struct sr_device_instance *sdi;
406 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
409 switch (device_info_id) {
413 case SR_DI_NUM_PROBES:
414 info = GINT_TO_POINTER(num_channels);
416 case SR_DI_SAMPLERATES:
419 case SR_DI_TRIGGER_TYPES:
420 info = TRIGGER_TYPES;
422 case SR_DI_CUR_SAMPLERATE:
423 info = &cur_samplerate;
430 static int hw_get_status(int device_index)
432 struct sr_device_instance *sdi;
434 sdi = sr_get_device_instance(device_instances, device_index);
438 return SR_ST_NOT_FOUND;
441 static int *hw_get_capabilities(void)
446 /* TODO: This will set the same samplerate for all devices. */
447 static int set_configuration_samplerate(uint64_t samplerate)
449 g_message("%s(%" PRIu64 ")", __FUNCTION__, samplerate);
450 if (samplerate > SR_MHZ(1))
451 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
452 else if (samplerate > SR_KHZ(1))
453 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
455 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
457 cur_samplerate = samplerate;
462 static int hw_set_configuration(int device_index, int capability, void *value)
464 struct sr_device_instance *sdi;
467 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
470 switch (capability) {
471 case SR_HWCAP_SAMPLERATE:
473 return set_configuration_samplerate(*tmp_u64);
474 case SR_HWCAP_PROBECONFIG:
475 return configure_probes((GSList *) value);
476 case SR_HWCAP_LIMIT_SAMPLES:
478 limit_samples = *tmp_u64;
485 static int hw_start_acquisition(int device_index, gpointer session_device_id)
487 struct sr_device_instance *sdi;
488 struct sr_datafeed_packet packet;
489 struct sr_datafeed_header header;
491 unsigned int packet_num;
494 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
497 /* push configured settings to device */
498 analyzer_configure(sdi->usb->devhdl);
500 analyzer_start(sdi->usb->devhdl);
501 g_message("Waiting for data");
502 analyzer_wait_data(sdi->usb->devhdl);
504 g_message("Stop address = 0x%x",
505 analyzer_get_stop_address(sdi->usb->devhdl));
506 g_message("Now address = 0x%x",
507 analyzer_get_now_address(sdi->usb->devhdl));
508 g_message("Trigger address = 0x%x",
509 analyzer_get_trigger_address(sdi->usb->devhdl));
511 packet.type = SR_DF_HEADER;
512 packet.length = sizeof(struct sr_datafeed_header);
513 packet.payload = (unsigned char *)&header;
514 header.feed_version = 1;
515 gettimeofday(&header.starttime, NULL);
516 header.samplerate = cur_samplerate;
517 header.protocol_id = SR_PROTO_RAW;
518 header.num_logic_probes = num_channels;
519 header.num_analog_probes = 0;
520 sr_session_bus(session_device_id, &packet);
522 buf = g_malloc(PACKET_SIZE);
525 analyzer_read_start(sdi->usb->devhdl);
526 /* Send the incoming transfer to the session bus. */
527 for (packet_num = 0; packet_num < (memory_size * 4 / PACKET_SIZE);
529 res = analyzer_read_data(sdi->usb->devhdl, buf, PACKET_SIZE);
531 g_message("Tried to read %llx bytes, actually read %x bytes",
535 packet.type = SR_DF_LOGIC;
536 packet.length = PACKET_SIZE;
538 packet.payload = buf;
539 sr_session_bus(session_device_id, &packet);
541 analyzer_read_stop(sdi->usb->devhdl);
544 packet.type = SR_DF_END;
545 sr_session_bus(session_device_id, &packet);
550 /* This stops acquisition on ALL devices, ignoring device_index. */
551 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
553 struct sr_datafeed_packet packet;
554 struct sr_device_instance *sdi;
556 packet.type = SR_DF_END;
557 sr_session_bus(session_device_id, &packet);
559 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
560 return; /* TODO: Cry? */
562 analyzer_reset(sdi->usb->devhdl);
563 /* TODO: Need to cancel and free any queued up transfers. */
566 struct sr_device_plugin zeroplus_logic_cube_plugin_info = {
567 "zeroplus-logic-cube",
568 "Zeroplus Logic Cube LAP-C series",
577 hw_set_configuration,
578 hw_start_acquisition,