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/>.
30 #define USB_VENDOR 0x0c12
31 #define USB_VENDOR_NAME "Zeroplus"
32 #define USB_MODEL_NAME "Logic Cube"
33 #define USB_MODEL_VERSION ""
35 #define USB_INTERFACE 0
36 #define USB_CONFIGURATION 1
37 #define NUM_TRIGGER_STAGES 4
38 #define TRIGGER_TYPES "01"
40 #define PACKET_SIZE 2048 // ??
45 unsigned int channels;
46 unsigned int sample_depth; // in Ksamples/channel
47 unsigned int max_sampling_freq;
50 /* Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the same 128K sample depth */
51 model_t zeroplus_models[] = {
52 {0x7009, "LAP-C(16064)", 16, 64, 100},
53 {0x700A, "LAP-C(16128)", 16, 128, 200},
54 {0x700B, "LAP-C(32128)", 32, 128, 200},
55 {0x700C, "LAP-C(321000)", 32, 1024, 200},
56 {0x700D, "LAP-C(322000)", 32, 2048, 200},
57 {0x700E, "LAP-C(16032)", 16, 32, 100},
58 {0x7016, "LAP-C(162000)", 16, 2048, 200},
61 static int capabilities[] = {
66 /* these are really implemented in the driver, not the hardware */
72 /* list of struct sigrok_device_instance, maintained by opendev() and closedev() */
73 static GSList *device_instances = NULL;
75 static libusb_context *usb_context = NULL;
77 /* The hardware supports more samplerates than these, but these are the options
78 hardcoded into the vendor's Windows GUI */
80 // XXX we shouldn't support 150MHz and 200MHz on devices that don't go up that high
81 static uint64_t supported_samplerates[] = {
103 static struct samplerates samplerates = {
105 supported_samplerates
108 /* TODO: all of these should go in a device-specific struct */
109 static uint64_t cur_samplerate = 0;
110 static uint64_t limit_samples = 0;
111 int num_channels = 32; // XXX this is not getting initialized before it is needed :(
112 uint64_t memory_size = 0;
113 static uint8_t probe_mask = 0;
114 static uint8_t trigger_mask[NUM_TRIGGER_STAGES] = {0};
115 static uint8_t trigger_value[NUM_TRIGGER_STAGES] = {0};
116 // static uint8_t trigger_buffer[NUM_TRIGGER_STAGES] = {0};
119 static int hw_set_configuration(int device_index, int capability, void *value);
121 static unsigned int get_memory_size(int type)
123 if (type == MEMORY_SIZE_8K)
125 else if (type == MEMORY_SIZE_64K)
127 else if (type == MEMORY_SIZE_128K)
129 else if (type == MEMORY_SIZE_512K)
135 struct sigrok_device_instance *zp_open_device(int device_index)
137 struct sigrok_device_instance *sdi;
138 libusb_device **devlist;
139 struct libusb_device_descriptor des;
142 if(!(sdi = get_sigrok_device_instance(device_instances, device_index)))
145 libusb_get_device_list(usb_context, &devlist);
146 if(sdi->status == ST_INACTIVE) {
147 /* find the device by vendor, product, bus and address */
148 libusb_get_device_list(usb_context, &devlist);
149 for(i = 0; devlist[i]; i++) {
150 if( (err = libusb_get_device_descriptor(devlist[i], &des)) ) {
151 g_warning("failed to get device descriptor: %d", err);
155 if(des.idVendor == USB_VENDOR) {
156 if(libusb_get_bus_number(devlist[i]) == sdi->usb->bus &&
157 libusb_get_device_address(devlist[i]) == sdi->usb->address) {
158 for (j = 0; j < sizeof(zeroplus_models) / sizeof(zeroplus_models[0]); j++) {
159 if (des.idProduct == zeroplus_models[j].pid) {
160 g_message("Found PID=%04X (%s)", des.idProduct, zeroplus_models[j].model_name);
161 num_channels = zeroplus_models[j].channels;
162 memory_size = zeroplus_models[j].sample_depth * 1024;
166 if (num_channels == 0) {
167 g_warning("Unknown ZeroPlus device %04X", des.idProduct);
171 if( !(err = libusb_open(devlist[i], &(sdi->usb->devhdl))) ) {
172 sdi->status = ST_ACTIVE;
173 g_message("opened device %d on %d.%d interface %d", sdi->index,
174 sdi->usb->bus, sdi->usb->address, USB_INTERFACE);
177 g_warning("failed to open device: %d", err);
185 /* status must be ST_ACTIVE, i.e. already in use... */
188 libusb_free_device_list(devlist, 1);
190 if(sdi && sdi->status != ST_ACTIVE)
197 static void close_device(struct sigrok_device_instance *sdi)
202 g_message("closing device %d on %d.%d interface %d", sdi->index, sdi->usb->bus,
203 sdi->usb->address, USB_INTERFACE);
204 libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE);
205 libusb_close(sdi->usb->devhdl);
206 sdi->usb->devhdl = NULL;
207 sdi->status = ST_INACTIVE;
213 static int configure_probes(GSList *probes)
217 int probe_bit, stage, i;
221 for(i = 0; i < NUM_TRIGGER_STAGES; i++)
224 trigger_value[i] = 0;
228 for(l = probes; l; l = l->next)
230 probe = (struct probe *) l->data;
231 if(probe->enabled == FALSE)
233 probe_bit = 1 << (probe->index - 1);
234 probe_mask |= probe_bit;
238 for(tc = probe->trigger; *tc; tc++)
240 trigger_mask[stage] |= probe_bit;
242 trigger_value[stage] |= probe_bit;
244 if(stage > NUM_TRIGGER_STAGES)
259 static int hw_init(char *deviceinfo)
261 struct sigrok_device_instance *sdi;
262 struct libusb_device_descriptor des;
263 libusb_device **devlist;
266 if(libusb_init(&usb_context) != 0) {
267 g_warning("Failed to initialize USB.");
271 /* find all ZeroPlus analyzers and add them to device list */
273 libusb_get_device_list(usb_context, &devlist);
274 for(i = 0; devlist[i]; i++) {
275 err = libusb_get_device_descriptor(devlist[i], &des);
277 g_warning("failed to get device descriptor: %d", err);
281 if(des.idVendor == USB_VENDOR) {
282 /* definitely a Zeroplus */
283 /* TODO: any way to detect specific model/version in the zeroplus range? */
284 sdi = sigrok_device_instance_new(devcnt, ST_INACTIVE,
285 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
288 device_instances = g_slist_append(device_instances, sdi);
289 sdi->usb = usb_device_instance_new(libusb_get_bus_number(devlist[i]),
290 libusb_get_device_address(devlist[i]), NULL);
294 libusb_free_device_list(devlist, 1);
300 static int hw_opendev(int device_index)
302 struct sigrok_device_instance *sdi;
305 if( !(sdi = zp_open_device(device_index)) ) {
306 g_warning("unable to open device");
310 err = libusb_claim_interface(sdi->usb->devhdl, USB_INTERFACE);
312 g_warning("Unable to claim interface: %d", err);
315 analyzer_reset(sdi->usb->devhdl);
316 analyzer_initialize(sdi->usb->devhdl);
317 analyzer_configure(sdi->usb->devhdl);
319 analyzer_set_memory_size(MEMORY_SIZE_512K);
320 // analyzer_set_freq(g_freq, g_freq_scale);
321 analyzer_set_trigger_count(1);
322 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger) * get_memory_size(g_memory_size)) / 100) >> 2);
323 analyzer_set_ramsize_trigger_address((100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
325 /* if (g_double_mode == 1)
326 analyzer_set_compression(COMPRESSION_DOUBLE);
327 else if (g_compression == 1)
328 analyzer_set_compression(COMPRESSION_ENABLE);
330 analyzer_set_compression(COMPRESSION_NONE);
332 if(cur_samplerate == 0) {
333 /* sample rate hasn't been set; default to the slowest it has */
334 if(hw_set_configuration(device_index, HWCAP_SAMPLERATE, &samplerates.low) == SIGROK_ERR)
342 static void hw_closedev(int device_index)
344 struct sigrok_device_instance *sdi;
346 if( (sdi = get_sigrok_device_instance(device_instances, device_index)) )
352 static void hw_cleanup(void)
356 /* properly close all devices */
357 for(l = device_instances; l; l = l->next)
358 close_device( (struct sigrok_device_instance *) l->data);
360 /* and free all their memory */
361 for(l = device_instances; l; l = l->next)
363 g_slist_free(device_instances);
364 device_instances = NULL;
367 libusb_exit(usb_context);
373 static void *hw_get_device_info(int device_index, int device_info_id)
375 struct sigrok_device_instance *sdi;
378 if( !(sdi = get_sigrok_device_instance(device_instances, device_index)) )
382 switch(device_info_id)
388 info = GINT_TO_POINTER(num_channels);
393 case DI_TRIGGER_TYPES:
394 info = TRIGGER_TYPES;
396 case DI_CUR_SAMPLERATE:
397 info = &cur_samplerate;
405 static int hw_get_status(int device_index)
407 struct sigrok_device_instance *sdi;
409 sdi = get_sigrok_device_instance(device_instances, device_index);
417 static int *hw_get_capabilities(void)
423 // XXX this will set the same samplerate for all devices
424 int set_configuration_samplerate(struct sigrok_device_instance *sdi, uint64_t samplerate)
426 g_message("%s(%llu)", __FUNCTION__, samplerate);
427 if (samplerate > MHZ(1))
428 analyzer_set_freq(samplerate / MHZ(1), FREQ_SCALE_MHZ);
429 else if (samplerate > KHZ(1))
430 analyzer_set_freq(samplerate / KHZ(1), FREQ_SCALE_KHZ);
432 analyzer_set_freq(samplerate , FREQ_SCALE_HZ);
434 cur_samplerate = samplerate;
439 static int hw_set_configuration(int device_index, int capability, void *value)
441 struct sigrok_device_instance *sdi;
444 if( !(sdi = get_sigrok_device_instance(device_instances, device_index)) )
447 switch (capability) {
448 case HWCAP_SAMPLERATE:
450 return set_configuration_samplerate(sdi, *tmp_u64);
452 case HWCAP_PROBECONFIG:
453 return configure_probes( (GSList *) value);
455 case HWCAP_LIMIT_SAMPLES:
456 limit_samples = strtoull(value, NULL, 10);
464 static int hw_start_acquisition(int device_index, gpointer session_device_id)
466 struct sigrok_device_instance *sdi;
467 struct datafeed_packet packet;
468 struct datafeed_header header;
473 if( !(sdi = get_sigrok_device_instance(device_instances, device_index)))
476 analyzer_start(sdi->usb->devhdl);
477 g_message("Waiting for data");
478 analyzer_wait_data(sdi->usb->devhdl);
480 g_message("Stop address = 0x%x", analyzer_get_stop_address(sdi->usb->devhdl));
481 g_message("Now address = 0x%x", analyzer_get_now_address(sdi->usb->devhdl));
482 g_message("Trigger address = 0x%x", analyzer_get_trigger_address(sdi->usb->devhdl));
484 packet.type = DF_HEADER;
485 packet.length = sizeof(struct datafeed_header);
486 packet.payload = (unsigned char *) &header;
487 header.feed_version = 1;
488 gettimeofday(&header.starttime, NULL);
489 header.samplerate = cur_samplerate;
490 header.protocol_id = PROTO_RAW;
491 header.num_probes = num_channels;
492 session_bus(session_device_id, &packet);
494 buf = g_malloc(PACKET_SIZE);
497 analyzer_read_start(sdi->usb->devhdl);
498 /* send the incoming transfer to the session bus */
499 for(packet_num = 0; packet_num < (memory_size * 4 / PACKET_SIZE); packet_num++) {
500 res = analyzer_read_data(sdi->usb->devhdl, buf, PACKET_SIZE);
501 // g_message("Tried to read %llx bytes, actually read %x bytes", PACKET_SIZE, res);
503 packet.type = DF_LOGIC32;
504 packet.length = PACKET_SIZE;
505 packet.payload = buf;
506 session_bus(session_device_id, &packet);
508 analyzer_read_stop(sdi->usb->devhdl);
511 packet.type = DF_END;
512 session_bus(session_device_id, &packet);
518 /* this stops acquisition on ALL devices, ignoring device_index */
519 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
521 struct datafeed_packet packet;
522 struct sigrok_device_instance *sdi;
524 packet.type = DF_END;
525 session_bus(session_device_id, &packet);
527 if( !(sdi = get_sigrok_device_instance(device_instances, device_index)))
530 analyzer_reset(sdi->usb->devhdl);
531 /* TODO: need to cancel and free any queued up transfers */
536 struct device_plugin zeroplus_logic_cube_plugin_info = {
537 "zeroplus-logic-cube",
547 hw_set_configuration,
548 hw_start_acquisition,