]> sigrok.org Git - libsigrok.git/blame_incremental - hardware/zeroplus-logic-cube/api.c
Enforce open device before config_set()/dev_acquisition_start()
[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 /* 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 struct sr_usb_dev_inst *usb;
248
249 drvc = di->priv;
250 for (l = drvc->instances; l; l = l->next) {
251 sdi = l->data;
252 if (!(devc = sdi->priv)) {
253 /* Log error, but continue cleaning up the rest. */
254 sr_err("%s: sdi->priv was NULL, continuing", __func__);
255 continue;
256 }
257 usb = sdi->conn;
258 sr_usb_dev_inst_free(usb);
259 /* Properly close all devices... */
260 hw_dev_close(sdi);
261 /* ...and free all their memory. */
262 sr_dev_inst_free(sdi);
263 }
264 g_slist_free(drvc->instances);
265 drvc->instances = NULL;
266
267 return SR_OK;
268}
269
270static int hw_init(struct sr_context *sr_ctx)
271{
272 return std_hw_init(sr_ctx, di, "zeroplus: ");
273}
274
275static GSList *hw_scan(GSList *options)
276{
277 struct sr_dev_inst *sdi;
278 struct sr_probe *probe;
279 struct drv_context *drvc;
280 struct dev_context *devc;
281 const struct zp_model *prof;
282 struct libusb_device_descriptor des;
283 libusb_device **devlist;
284 GSList *devices;
285 int ret, devcnt, i, j;
286
287 (void)options;
288
289 drvc = di->priv;
290
291 devices = NULL;
292
293 clear_instances();
294
295 /* Find all ZEROPLUS analyzers and add them to device list. */
296 devcnt = 0;
297 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); /* TODO: Errors. */
298
299 for (i = 0; devlist[i]; i++) {
300 ret = libusb_get_device_descriptor(devlist[i], &des);
301 if (ret != 0) {
302 sr_err("Failed to get device descriptor: %s.",
303 libusb_error_name(ret));
304 continue;
305 }
306
307 prof = NULL;
308 for (j = 0; j < zeroplus_models[j].vid; j++) {
309 if (des.idVendor == zeroplus_models[j].vid &&
310 des.idProduct == zeroplus_models[j].pid) {
311 prof = &zeroplus_models[j];
312 }
313 }
314 /* Skip if the device was not found. */
315 if (!prof)
316 continue;
317 sr_info("Found ZEROPLUS %s.", prof->model_name);
318
319 /* Register the device with libsigrok. */
320 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
321 VENDOR_NAME, prof->model_name, NULL))) {
322 sr_err("%s: sr_dev_inst_new failed", __func__);
323 return NULL;
324 }
325 sdi->driver = di;
326
327 /* Allocate memory for our private driver context. */
328 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
329 sr_err("Device context malloc failed.");
330 return NULL;
331 }
332
333 sdi->priv = devc;
334 devc->prof = prof;
335 devc->num_channels = prof->channels;
336#ifdef ZP_EXPERIMENTAL
337 devc->max_memory_size = 128 * 1024;
338 devc->max_samplerate = 200;
339#else
340 devc->max_memory_size = prof->sample_depth * 1024;
341 devc->max_samplerate = prof->max_sampling_freq;
342#endif
343 devc->max_samplerate *= SR_MHZ(1);
344 devc->memory_size = MEMORY_SIZE_8K;
345 // memset(devc->trigger_buffer, 0, NUM_TRIGGER_STAGES);
346
347 /* Fill in probelist according to this device's profile. */
348 for (j = 0; j < devc->num_channels; j++) {
349 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
350 probe_names[j])))
351 return NULL;
352 sdi->probes = g_slist_append(sdi->probes, probe);
353 }
354
355 devices = g_slist_append(devices, sdi);
356 drvc->instances = g_slist_append(drvc->instances, sdi);
357 sdi->conn = sr_usb_dev_inst_new(
358 libusb_get_bus_number(devlist[i]),
359 libusb_get_device_address(devlist[i]), NULL);
360 devcnt++;
361
362 }
363 libusb_free_device_list(devlist, 1);
364
365 return devices;
366}
367
368static GSList *hw_dev_list(void)
369{
370 return ((struct drv_context *)(di->priv))->instances;
371}
372
373static int hw_dev_open(struct sr_dev_inst *sdi)
374{
375 struct dev_context *devc;
376 struct drv_context *drvc;
377 struct sr_usb_dev_inst *usb;
378 libusb_device **devlist, *dev;
379 struct libusb_device_descriptor des;
380 int device_count, ret, i;
381
382 drvc = di->priv;
383 usb = sdi->conn;
384
385 if (!(devc = sdi->priv)) {
386 sr_err("%s: sdi->priv was NULL", __func__);
387 return SR_ERR_ARG;
388 }
389
390 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx,
391 &devlist);
392 if (device_count < 0) {
393 sr_err("Failed to retrieve device list.");
394 return SR_ERR;
395 }
396
397 dev = NULL;
398 for (i = 0; i < device_count; i++) {
399 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
400 sr_err("Failed to get device descriptor: %s.",
401 libusb_error_name(ret));
402 continue;
403 }
404 if (libusb_get_bus_number(devlist[i]) == usb->bus
405 && libusb_get_device_address(devlist[i]) == usb->address) {
406 dev = devlist[i];
407 break;
408 }
409 }
410 if (!dev) {
411 sr_err("Device on bus %d address %d disappeared!",
412 usb->bus, usb->address);
413 return SR_ERR;
414 }
415
416 if (!(ret = libusb_open(dev, &(usb->devhdl)))) {
417 sdi->status = SR_ST_ACTIVE;
418 sr_info("Opened device %d on %d.%d interface %d.",
419 sdi->index, usb->bus, usb->address, USB_INTERFACE);
420 } else {
421 sr_err("Failed to open device: %s.", libusb_error_name(ret));
422 return SR_ERR;
423 }
424
425 ret = libusb_set_configuration(usb->devhdl, USB_CONFIGURATION);
426 if (ret < 0) {
427 sr_err("Unable to set USB configuration %d: %s.",
428 USB_CONFIGURATION, libusb_error_name(ret));
429 return SR_ERR;
430 }
431
432 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
433 if (ret != 0) {
434 sr_err("Unable to claim interface: %s.",
435 libusb_error_name(ret));
436 return SR_ERR;
437 }
438
439 /* Set default configuration after power on. */
440 if (analyzer_read_status(usb->devhdl) == 0)
441 analyzer_configure(usb->devhdl);
442
443 analyzer_reset(usb->devhdl);
444 analyzer_initialize(usb->devhdl);
445
446 //analyzer_set_memory_size(MEMORY_SIZE_512K);
447 // analyzer_set_freq(g_freq, g_freq_scale);
448 analyzer_set_trigger_count(1);
449 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
450 // * get_memory_size(g_memory_size)) / 100) >> 2);
451
452#if 0
453 if (g_double_mode == 1)
454 analyzer_set_compression(COMPRESSION_DOUBLE);
455 else if (g_compression == 1)
456 analyzer_set_compression(COMPRESSION_ENABLE);
457 else
458#endif
459 analyzer_set_compression(COMPRESSION_NONE);
460
461 if (devc->cur_samplerate == 0) {
462 /* Samplerate hasn't been set. Default to 1MHz. */
463 analyzer_set_freq(1, FREQ_SCALE_MHZ);
464 devc->cur_samplerate = SR_MHZ(1);
465 }
466
467 return SR_OK;
468}
469
470static int hw_dev_close(struct sr_dev_inst *sdi)
471{
472 struct sr_usb_dev_inst *usb;
473
474 usb = sdi->conn;
475
476 if (!usb->devhdl)
477 return SR_ERR;
478
479 sr_info("Closing device %d on %d.%d interface %d.", sdi->index,
480 usb->bus, usb->address, USB_INTERFACE);
481 libusb_release_interface(usb->devhdl, USB_INTERFACE);
482 libusb_reset_device(usb->devhdl);
483 libusb_close(usb->devhdl);
484 usb->devhdl = NULL;
485 sdi->status = SR_ST_INACTIVE;
486
487 return SR_OK;
488}
489
490static int hw_cleanup(void)
491{
492 struct drv_context *drvc;
493
494 if (!(drvc = di->priv))
495 return SR_OK;
496
497 clear_instances();
498
499 return SR_OK;
500}
501
502static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi)
503{
504 struct dev_context *devc;
505
506 switch (id) {
507 case SR_CONF_SAMPLERATE:
508 if (sdi) {
509 devc = sdi->priv;
510 *data = g_variant_new_uint64(devc->cur_samplerate);
511 sr_spew("Returning samplerate: %" PRIu64 "Hz.",
512 devc->cur_samplerate);
513 } else
514 return SR_ERR;
515 break;
516 default:
517 return SR_ERR_NA;
518 }
519
520 return SR_OK;
521}
522
523static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
524{
525 struct dev_context *devc;
526
527 if (sdi->status != SR_ST_ACTIVE)
528 return SR_ERR_DEV_CLOSED;
529
530 if (!(devc = sdi->priv)) {
531 sr_err("%s: sdi->priv was NULL", __func__);
532 return SR_ERR_ARG;
533 }
534
535 switch (id) {
536 case SR_CONF_SAMPLERATE:
537 return zp_set_samplerate(devc, g_variant_get_uint64(data));
538 case SR_CONF_LIMIT_SAMPLES:
539 return set_limit_samples(devc, g_variant_get_uint64(data));
540 case SR_CONF_CAPTURE_RATIO:
541 return set_capture_ratio(devc, g_variant_get_uint64(data));
542 default:
543 return SR_ERR_NA;
544 }
545
546 return SR_OK;
547}
548
549static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
550{
551 struct dev_context *devc;
552 GVariant *gvar;
553 GVariantBuilder gvb;
554
555 switch (key) {
556 case SR_CONF_DEVICE_OPTIONS:
557 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
558 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
559 break;
560 case SR_CONF_SAMPLERATE:
561 devc = sdi->priv;
562 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
563 if (devc->prof->max_sampling_freq == 100) {
564 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
565 samplerates_100, ARRAY_SIZE(samplerates_100),
566 sizeof(uint64_t));
567 } else if (devc->prof->max_sampling_freq == 200) {
568 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
569 samplerates_200, ARRAY_SIZE(samplerates_200),
570 sizeof(uint64_t));
571 } else {
572 sr_err("Internal error: Unknown max. samplerate: %d.",
573 devc->prof->max_sampling_freq);
574 return SR_ERR_ARG;
575 }
576 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
577 *data = g_variant_builder_end(&gvb);
578 break;
579 case SR_CONF_TRIGGER_TYPE:
580 *data = g_variant_new_string(TRIGGER_TYPE);
581 break;
582 default:
583 return SR_ERR_NA;
584 }
585
586 return SR_OK;
587}
588
589static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
590 void *cb_data)
591{
592 struct dev_context *devc;
593 struct sr_usb_dev_inst *usb;
594 struct sr_datafeed_packet packet;
595 struct sr_datafeed_logic logic;
596 //uint64_t samples_read;
597 int res;
598 unsigned int packet_num, n;
599 unsigned char *buf;
600
601 if (sdi->status != SR_ST_ACTIVE)
602 return SR_ERR_DEV_CLOSED;
603
604 if (!(devc = sdi->priv)) {
605 sr_err("%s: sdi->priv was NULL", __func__);
606 return SR_ERR_ARG;
607 }
608
609 if (configure_probes(sdi) != SR_OK) {
610 sr_err("Failed to configure probes.");
611 return SR_ERR;
612 }
613
614 usb = sdi->conn;
615
616 set_triggerbar(devc);
617
618 /* Push configured settings to device. */
619 analyzer_configure(usb->devhdl);
620
621 analyzer_start(usb->devhdl);
622 sr_info("Waiting for data.");
623 analyzer_wait_data(usb->devhdl);
624
625 sr_info("Stop address = 0x%x.",
626 analyzer_get_stop_address(usb->devhdl));
627 sr_info("Now address = 0x%x.",
628 analyzer_get_now_address(usb->devhdl));
629 sr_info("Trigger address = 0x%x.",
630 analyzer_get_trigger_address(usb->devhdl));
631
632 /* Send header packet to the session bus. */
633 std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
634
635 if (!(buf = g_try_malloc(PACKET_SIZE))) {
636 sr_err("Packet buffer malloc failed.");
637 return SR_ERR_MALLOC;
638 }
639
640 //samples_read = 0;
641 analyzer_read_start(usb->devhdl);
642 /* Send the incoming transfer to the session bus. */
643 n = get_memory_size(devc->memory_size);
644 if (devc->max_memory_size * 4 < n)
645 n = devc->max_memory_size * 4;
646 for (packet_num = 0; packet_num < n / PACKET_SIZE; packet_num++) {
647 res = analyzer_read_data(usb->devhdl, buf, PACKET_SIZE);
648 sr_info("Tried to read %d bytes, actually read %d bytes.",
649 PACKET_SIZE, res);
650
651 packet.type = SR_DF_LOGIC;
652 packet.payload = &logic;
653 logic.length = PACKET_SIZE;
654 logic.unitsize = 4;
655 logic.data = buf;
656 sr_session_send(cb_data, &packet);
657 //samples_read += res / 4;
658 }
659 analyzer_read_stop(usb->devhdl);
660 g_free(buf);
661
662 packet.type = SR_DF_END;
663 sr_session_send(cb_data, &packet);
664
665 return SR_OK;
666}
667
668/* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
669static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
670{
671 struct dev_context *devc;
672 struct sr_usb_dev_inst *usb;
673 struct sr_datafeed_packet packet;
674
675 packet.type = SR_DF_END;
676 sr_session_send(cb_data, &packet);
677
678 if (!(devc = sdi->priv)) {
679 sr_err("%s: sdi->priv was NULL", __func__);
680 return SR_ERR_BUG;
681 }
682
683 usb = sdi->conn;
684 analyzer_reset(usb->devhdl);
685 /* TODO: Need to cancel and free any queued up transfers. */
686
687 return SR_OK;
688}
689
690SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
691 .name = "zeroplus-logic-cube",
692 .longname = "ZEROPLUS Logic Cube LAP-C series",
693 .api_version = 1,
694 .init = hw_init,
695 .cleanup = hw_cleanup,
696 .scan = hw_scan,
697 .dev_list = hw_dev_list,
698 .dev_clear = hw_cleanup,
699 .config_get = config_get,
700 .config_set = config_set,
701 .config_list = config_list,
702 .dev_open = hw_dev_open,
703 .dev_close = hw_dev_close,
704 .dev_acquisition_start = hw_dev_acquisition_start,
705 .dev_acquisition_stop = hw_dev_acquisition_stop,
706 .priv = NULL,
707};