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