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