]> sigrok.org Git - libsigrok.git/blame_incremental - hardware/zeroplus-logic-cube/zeroplus.c
sr: zp: Add missing <string.h> #include.
[libsigrok.git] / hardware / zeroplus-logic-cube / zeroplus.c
... / ...
CommitLineData
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/time.h>
24#include <inttypes.h>
25#include <glib.h>
26#include <libusb.h>
27#include "config.h"
28#include "sigrok.h"
29#include "sigrok-internal.h"
30#include "analyzer.h"
31
32#define USB_VENDOR 0x0c12
33#define USB_VENDOR_NAME "Zeroplus"
34#define USB_MODEL_NAME "Logic Cube"
35#define USB_MODEL_VERSION ""
36
37#define USB_INTERFACE 0
38#define USB_CONFIGURATION 1
39#define NUM_TRIGGER_STAGES 4
40#define TRIGGER_TYPES "01"
41
42#define PACKET_SIZE 2048 /* ?? */
43
44typedef struct {
45 unsigned short pid;
46 char model_name[64];
47 unsigned int channels;
48 unsigned int sample_depth; /* In Ksamples/channel */
49 unsigned int max_sampling_freq;
50} model_t;
51
52/*
53 * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the
54 * same 128K sample depth.
55 */
56static model_t zeroplus_models[] = {
57 {0x7009, "LAP-C(16064)", 16, 64, 100},
58 {0x700A, "LAP-C(16128)", 16, 128, 200},
59 {0x700B, "LAP-C(32128)", 32, 128, 200},
60 {0x700C, "LAP-C(321000)", 32, 1024, 200},
61 {0x700D, "LAP-C(322000)", 32, 2048, 200},
62 {0x700E, "LAP-C(16032)", 16, 32, 100},
63 {0x7016, "LAP-C(162000)", 16, 2048, 200},
64};
65
66static int capabilities[] = {
67 SR_HWCAP_LOGIC_ANALYZER,
68 SR_HWCAP_SAMPLERATE,
69 SR_HWCAP_PROBECONFIG,
70 SR_HWCAP_CAPTURE_RATIO,
71
72 /* These are really implemented in the driver, not the hardware. */
73 SR_HWCAP_LIMIT_SAMPLES,
74 0,
75};
76
77static const char *probe_names[] = {
78 "0",
79 "1",
80 "2",
81 "3",
82 "4",
83 "5",
84 "6",
85 "7",
86 "8",
87 "9",
88 "10",
89 "11",
90 "12",
91 "13",
92 "14",
93 "15",
94 "16",
95 "17",
96 "18",
97 "19",
98 "20",
99 "21",
100 "22",
101 "23",
102 "24",
103 "25",
104 "26",
105 "27",
106 "28",
107 "29",
108 "30",
109 "31",
110 NULL,
111};
112
113/* List of struct sr_device_instance, maintained by opendev()/closedev(). */
114static GSList *device_instances = NULL;
115
116static libusb_context *usb_context = NULL;
117
118/*
119 * The hardware supports more samplerates than these, but these are the
120 * options hardcoded into the vendor's Windows GUI.
121 */
122
123/*
124 * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up
125 * that high.
126 */
127static uint64_t supported_samplerates[] = {
128 SR_HZ(100),
129 SR_HZ(500),
130 SR_KHZ(1),
131 SR_KHZ(5),
132 SR_KHZ(25),
133 SR_KHZ(50),
134 SR_KHZ(100),
135 SR_KHZ(200),
136 SR_KHZ(400),
137 SR_KHZ(800),
138 SR_MHZ(1),
139 SR_MHZ(10),
140 SR_MHZ(25),
141 SR_MHZ(50),
142 SR_MHZ(80),
143 SR_MHZ(100),
144 SR_MHZ(150),
145 SR_MHZ(200),
146 0,
147};
148
149static struct sr_samplerates samplerates = {
150 SR_HZ(0),
151 SR_HZ(0),
152 SR_HZ(0),
153 supported_samplerates,
154};
155
156struct zp {
157 uint64_t cur_samplerate;
158 uint64_t period_ps;
159 uint64_t limit_samples;
160 int num_channels; /* TODO: This isn't initialized before it's needed :( */
161 uint64_t memory_size;
162 uint8_t probe_mask;
163 uint8_t trigger_mask[NUM_TRIGGER_STAGES];
164 uint8_t trigger_value[NUM_TRIGGER_STAGES];
165 // uint8_t trigger_buffer[NUM_TRIGGER_STAGES];
166};
167
168static int hw_set_configuration(int device_index, int capability, void *value);
169
170static unsigned int get_memory_size(int type)
171{
172 if (type == MEMORY_SIZE_8K)
173 return 8 * 1024;
174 else if (type == MEMORY_SIZE_64K)
175 return 64 * 1024;
176 else if (type == MEMORY_SIZE_128K)
177 return 128 * 1024;
178 else if (type == MEMORY_SIZE_512K)
179 return 512 * 1024;
180 else
181 return 0;
182}
183
184static int opendev4(struct sr_device_instance **sdi, libusb_device *dev,
185 struct libusb_device_descriptor *des)
186{
187 struct zp *zp;
188 unsigned int i;
189 int err;
190
191 /* Note: sdi is non-NULL, the caller already checked this. */
192
193 if (!(zp = (*sdi)->priv)) {
194 sr_err("zp: %s: (*sdi)->priv was NULL", __func__);
195 return -1;
196 }
197
198 if ((err = libusb_get_device_descriptor(dev, des))) {
199 sr_warn("failed to get device descriptor: %d", err);
200 return -1;
201 }
202
203 if (des->idVendor != USB_VENDOR)
204 return 0;
205
206 if (libusb_get_bus_number(dev) == (*sdi)->usb->bus
207 && libusb_get_device_address(dev) == (*sdi)->usb->address) {
208
209 for (i = 0; i < ARRAY_SIZE(zeroplus_models); i++) {
210 if (!(des->idProduct == zeroplus_models[i].pid))
211 continue;
212
213 sr_info("Found PID=%04X (%s)", des->idProduct,
214 zeroplus_models[i].model_name);
215 zp->num_channels = zeroplus_models[i].channels;
216 zp->memory_size = zeroplus_models[i].sample_depth * 1024;
217 break;
218 }
219
220 if (zp->num_channels == 0) {
221 sr_warn("Unknown ZeroPlus device %04X", des->idProduct);
222 return -2;
223 }
224
225 /* Found it. */
226 if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) {
227 (*sdi)->status = SR_ST_ACTIVE;
228 sr_info("opened device %d on %d.%d interface %d",
229 (*sdi)->index, (*sdi)->usb->bus,
230 (*sdi)->usb->address, USB_INTERFACE);
231 } else {
232 sr_warn("failed to open device: %d", err);
233 *sdi = NULL;
234 }
235 }
236
237 return 0;
238}
239
240static struct sr_device_instance *zp_open_device(int device_index)
241{
242 struct sr_device_instance *sdi;
243 libusb_device **devlist;
244 struct libusb_device_descriptor des;
245 int err, i;
246
247 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
248 return NULL;
249
250 libusb_get_device_list(usb_context, &devlist);
251 if (sdi->status == SR_ST_INACTIVE) {
252 /* Find the device by vendor, product, bus and address. */
253 libusb_get_device_list(usb_context, &devlist);
254 for (i = 0; devlist[i]; i++) {
255 /* TODO: Error handling. */
256 err = opendev4(&sdi, devlist[i], &des);
257 }
258 } else {
259 /* Status must be SR_ST_ACTIVE, i.e. already in use... */
260 sdi = NULL;
261 }
262 libusb_free_device_list(devlist, 1);
263
264 if (sdi && sdi->status != SR_ST_ACTIVE)
265 sdi = NULL;
266
267 return sdi;
268}
269
270static void close_device(struct sr_device_instance *sdi)
271{
272 if (!sdi->usb->devhdl)
273 return;
274
275 sr_info("closing device %d on %d.%d interface %d", sdi->index,
276 sdi->usb->bus, sdi->usb->address, USB_INTERFACE);
277 libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE);
278 libusb_close(sdi->usb->devhdl);
279 sdi->usb->devhdl = NULL;
280 sdi->status = SR_ST_INACTIVE;
281}
282
283static int configure_probes(struct sr_device_instance *sdi, GSList *probes)
284{
285 struct zp *zp;
286 struct sr_probe *probe;
287 GSList *l;
288 int probe_bit, stage, i;
289 char *tc;
290
291 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
292 zp = sdi->priv;
293
294 zp->probe_mask = 0;
295 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
296 zp->trigger_mask[i] = 0;
297 zp->trigger_value[i] = 0;
298 }
299
300 stage = -1;
301 for (l = probes; l; l = l->next) {
302 probe = (struct sr_probe *)l->data;
303 if (probe->enabled == FALSE)
304 continue;
305 probe_bit = 1 << (probe->index - 1);
306 zp->probe_mask |= probe_bit;
307
308 if (probe->trigger) {
309 stage = 0;
310 for (tc = probe->trigger; *tc; tc++) {
311 zp->trigger_mask[stage] |= probe_bit;
312 if (*tc == '1')
313 zp->trigger_value[stage] |= probe_bit;
314 stage++;
315 if (stage > NUM_TRIGGER_STAGES)
316 return SR_ERR;
317 }
318 }
319 }
320
321 return SR_OK;
322}
323
324/*
325 * API callbacks
326 */
327
328static int hw_init(const char *deviceinfo)
329{
330 struct sr_device_instance *sdi;
331 struct libusb_device_descriptor des;
332 libusb_device **devlist;
333 int err, devcnt, i;
334 struct zp *zp;
335
336 /* Avoid compiler warnings. */
337 (void)deviceinfo;
338
339 /* Allocate memory for our private driver context. */
340 if (!(zp = g_try_malloc(sizeof(struct zp)))) {
341 sr_err("zp: %s: struct zp malloc failed", __func__);
342 return 0;
343 }
344
345 /* Set some sane defaults. */
346 zp->cur_samplerate = 0;
347 zp->period_ps = 0;
348 zp->limit_samples = 0;
349 zp->num_channels = 32; /* TODO: This isn't initialized before it's needed :( */
350 zp->memory_size = 0;
351 zp->probe_mask = 0;
352 memset(zp->trigger_mask, 0, NUM_TRIGGER_STAGES);
353 memset(zp->trigger_value, 0, NUM_TRIGGER_STAGES);
354 // memset(zp->trigger_buffer, 0, NUM_TRIGGER_STAGES);
355
356 if (libusb_init(&usb_context) != 0) {
357 sr_warn("Failed to initialize USB.");
358 return 0;
359 }
360
361 /* Find all ZeroPlus analyzers and add them to device list. */
362 devcnt = 0;
363 libusb_get_device_list(usb_context, &devlist);
364
365 for (i = 0; devlist[i]; i++) {
366 err = libusb_get_device_descriptor(devlist[i], &des);
367 if (err != 0) {
368 sr_warn("failed to get device descriptor: %d", err);
369 continue;
370 }
371
372 if (des.idVendor == USB_VENDOR) {
373 /*
374 * Definitely a Zeroplus.
375 * TODO: Any way to detect specific model/version in
376 * the zeroplus range?
377 */
378 /* Register the device with libsigrok. */
379 sdi = sr_device_instance_new(devcnt,
380 SR_ST_INACTIVE, USB_VENDOR_NAME,
381 USB_MODEL_NAME, USB_MODEL_VERSION);
382 if (!sdi) {
383 sr_err("zp: %s: sr_device_instance_new failed",
384 __func__);
385 return 0;
386 }
387
388 sdi->priv = zp;
389
390 device_instances =
391 g_slist_append(device_instances, sdi);
392 sdi->usb = sr_usb_device_instance_new(
393 libusb_get_bus_number(devlist[i]),
394 libusb_get_device_address(devlist[i]), NULL);
395 devcnt++;
396 }
397 }
398 libusb_free_device_list(devlist, 1);
399
400 return devcnt;
401}
402
403static int hw_opendev(int device_index)
404{
405 struct sr_device_instance *sdi;
406 struct zp *zp;
407 int err;
408
409 if (!(sdi = zp_open_device(device_index))) {
410 sr_warn("unable to open device");
411 return SR_ERR;
412 }
413
414 /* TODO: Note: sdi is retrieved in zp_open_device(). */
415
416 if (!(zp = sdi->priv)) {
417 sr_err("zp: %s: sdi->priv was NULL", __func__);
418 return SR_ERR_ARG;
419 }
420
421 err = libusb_claim_interface(sdi->usb->devhdl, USB_INTERFACE);
422 if (err != 0) {
423 sr_warn("Unable to claim interface: %d", err);
424 return SR_ERR;
425 }
426 analyzer_reset(sdi->usb->devhdl);
427 analyzer_initialize(sdi->usb->devhdl);
428
429 analyzer_set_memory_size(MEMORY_SIZE_512K);
430 // analyzer_set_freq(g_freq, g_freq_scale);
431 analyzer_set_trigger_count(1);
432 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
433 // * get_memory_size(g_memory_size)) / 100) >> 2);
434 analyzer_set_ramsize_trigger_address(
435 (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
436
437#if 0
438 if (g_double_mode == 1)
439 analyzer_set_compression(COMPRESSION_DOUBLE);
440 else if (g_compression == 1)
441 analyzer_set_compression(COMPRESSION_ENABLE);
442 else
443#endif
444 analyzer_set_compression(COMPRESSION_NONE);
445
446 if (zp->cur_samplerate == 0) {
447 /* Samplerate hasn't been set. Default to the slowest one. */
448 if (hw_set_configuration(device_index, SR_HWCAP_SAMPLERATE,
449 &samplerates.list[0]) == SR_ERR)
450 return SR_ERR;
451 }
452
453 return SR_OK;
454}
455
456static int hw_closedev(int device_index)
457{
458 struct sr_device_instance *sdi;
459
460 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
461 sr_err("zp: %s: sdi was NULL", __func__);
462 return SR_ERR; /* TODO: SR_ERR_ARG? */
463 }
464
465 /* TODO */
466 close_device(sdi);
467
468 return SR_OK;
469}
470
471static void hw_cleanup(void)
472{
473 GSList *l;
474
475 /* Properly close all devices... */
476 for (l = device_instances; l; l = l->next)
477 close_device((struct sr_device_instance *)l->data);
478
479 /* ...and free all their memory. */
480 for (l = device_instances; l; l = l->next)
481 g_free(l->data);
482 g_slist_free(device_instances);
483 device_instances = NULL;
484
485 if (usb_context)
486 libusb_exit(usb_context);
487 usb_context = NULL;
488}
489
490static void *hw_get_device_info(int device_index, int device_info_id)
491{
492 struct sr_device_instance *sdi;
493 struct zp *zp;
494 void *info;
495
496 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
497 sr_err("zp: %s: sdi was NULL", __func__);
498 return NULL;
499 }
500
501 if (!(zp = sdi->priv)) {
502 sr_err("zp: %s: sdi->priv was NULL", __func__);
503 return NULL;
504 }
505
506 switch (device_info_id) {
507 case SR_DI_INSTANCE:
508 info = sdi;
509 break;
510 case SR_DI_NUM_PROBES:
511 info = GINT_TO_POINTER(zp->num_channels);
512 break;
513 case SR_DI_PROBE_NAMES:
514 info = probe_names;
515 break;
516 case SR_DI_SAMPLERATES:
517 info = &samplerates;
518 break;
519 case SR_DI_TRIGGER_TYPES:
520 info = TRIGGER_TYPES;
521 break;
522 case SR_DI_CUR_SAMPLERATE:
523 info = &zp->cur_samplerate;
524 break;
525 default:
526 /* Unknown device info ID, return NULL. */
527 sr_err("zp: %s: Unknown device info ID", __func__);
528 info = NULL;
529 break;
530 }
531
532 return info;
533}
534
535static int hw_get_status(int device_index)
536{
537 struct sr_device_instance *sdi;
538
539 sdi = sr_get_device_instance(device_instances, device_index);
540 if (sdi)
541 return sdi->status;
542 else
543 return SR_ST_NOT_FOUND;
544}
545
546static int *hw_get_capabilities(void)
547{
548 return capabilities;
549}
550
551static int set_configuration_samplerate(struct sr_device_instance *sdi,
552 uint64_t samplerate)
553{
554 struct zp *zp;
555
556 if (!sdi) {
557 sr_err("zp: %s: sdi was NULL", __func__);
558 return SR_ERR_ARG;
559 }
560
561 if (!(zp = sdi->priv)) {
562 sr_err("zp: %s: sdi->priv was NULL", __func__);
563 return SR_ERR_ARG;
564 }
565
566 sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate);
567
568 if (samplerate > SR_MHZ(1))
569 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
570 else if (samplerate > SR_KHZ(1))
571 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
572 else
573 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
574
575 zp->cur_samplerate = samplerate;
576 zp->period_ps = 1000000000000 / samplerate;
577
578 return SR_OK;
579}
580
581static int hw_set_configuration(int device_index, int capability, void *value)
582{
583 struct sr_device_instance *sdi;
584 uint64_t *tmp_u64;
585 struct zp *zp;
586
587 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
588 sr_err("zp: %s: sdi was NULL", __func__);
589 return SR_ERR;
590 }
591
592 if (!(zp = sdi->priv)) {
593 sr_err("zp: %s: sdi->priv was NULL", __func__);
594 return SR_ERR_ARG;
595 }
596
597 switch (capability) {
598 case SR_HWCAP_SAMPLERATE:
599 tmp_u64 = value;
600 return set_configuration_samplerate(sdi, *tmp_u64);
601 case SR_HWCAP_PROBECONFIG:
602 return configure_probes(sdi, (GSList *)value);
603 case SR_HWCAP_LIMIT_SAMPLES:
604 tmp_u64 = value;
605 zp->limit_samples = *tmp_u64;
606 return SR_OK;
607 default:
608 return SR_ERR;
609 }
610}
611
612static int hw_start_acquisition(int device_index, gpointer session_data)
613{
614 struct sr_device_instance *sdi;
615 struct sr_datafeed_packet packet;
616 struct sr_datafeed_logic logic;
617 struct sr_datafeed_header header;
618 uint64_t samples_read;
619 int res;
620 unsigned int packet_num;
621 unsigned char *buf;
622 struct zp *zp;
623
624 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
625 sr_err("zp: %s: sdi was NULL", __func__);
626 return SR_ERR;
627 }
628
629 if (!(zp = sdi->priv)) {
630 sr_err("zp: %s: sdi->priv was NULL", __func__);
631 return SR_ERR_ARG;
632 }
633
634 /* push configured settings to device */
635 analyzer_configure(sdi->usb->devhdl);
636
637 analyzer_start(sdi->usb->devhdl);
638 sr_info("Waiting for data");
639 analyzer_wait_data(sdi->usb->devhdl);
640
641 sr_info("Stop address = 0x%x", analyzer_get_stop_address(sdi->usb->devhdl));
642 sr_info("Now address = 0x%x", analyzer_get_now_address(sdi->usb->devhdl));
643 sr_info("Trigger address = 0x%x", analyzer_get_trigger_address(sdi->usb->devhdl));
644
645 packet.type = SR_DF_HEADER;
646 packet.payload = &header;
647 header.feed_version = 1;
648 gettimeofday(&header.starttime, NULL);
649 header.samplerate = zp->cur_samplerate;
650 header.num_logic_probes = zp->num_channels;
651 header.num_analog_probes = 0;
652 sr_session_bus(session_data, &packet);
653
654 if (!(buf = g_try_malloc(PACKET_SIZE))) {
655 sr_err("zp: %s: buf malloc failed", __func__);
656 return SR_ERR_MALLOC;
657 }
658
659 samples_read = 0;
660 analyzer_read_start(sdi->usb->devhdl);
661 /* Send the incoming transfer to the session bus. */
662 for (packet_num = 0; packet_num < (zp->memory_size * 4 / PACKET_SIZE);
663 packet_num++) {
664 res = analyzer_read_data(sdi->usb->devhdl, buf, PACKET_SIZE);
665 sr_info("Tried to read %llx bytes, actually read %x bytes",
666 PACKET_SIZE, res);
667
668 packet.type = SR_DF_LOGIC;
669 packet.timeoffset = samples_read * zp->period_ps;
670 packet.duration = res / 4 * zp->period_ps;
671 packet.payload = &logic;
672 logic.length = PACKET_SIZE;
673 logic.unitsize = 4;
674 logic.data = buf;
675 sr_session_bus(session_data, &packet);
676 samples_read += res / 4;
677 }
678 analyzer_read_stop(sdi->usb->devhdl);
679 g_free(buf);
680
681 packet.type = SR_DF_END;
682 sr_session_bus(session_data, &packet);
683
684 return SR_OK;
685}
686
687/* This stops acquisition on ALL devices, ignoring device_index. */
688static void hw_stop_acquisition(int device_index, gpointer session_device_id)
689{
690 struct sr_datafeed_packet packet;
691 struct sr_device_instance *sdi;
692
693 packet.type = SR_DF_END;
694 sr_session_bus(session_device_id, &packet);
695
696 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
697 return; /* TODO: Cry? */
698
699 analyzer_reset(sdi->usb->devhdl);
700 /* TODO: Need to cancel and free any queued up transfers. */
701}
702
703struct sr_device_plugin zeroplus_logic_cube_plugin_info = {
704 .name = "zeroplus-logic-cube",
705 .longname = "Zeroplus Logic Cube LAP-C series",
706 .api_version = 1,
707 .init = hw_init,
708 .cleanup = hw_cleanup,
709 .opendev = hw_opendev,
710 .closedev = hw_closedev,
711 .get_device_info = hw_get_device_info,
712 .get_status = hw_get_status,
713 .get_capabilities = hw_get_capabilities,
714 .set_configuration = hw_set_configuration,
715 .start_acquisition = hw_start_acquisition,
716 .stop_acquisition = hw_stop_acquisition,
717};