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