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