]> sigrok.org Git - libsigrok.git/blob - hardware/zeroplus-logic-cube/zeroplus.c
zeroplus-logic-cube: fix scan to correctly find model
[libsigrok.git] / hardware / zeroplus-logic-cube / zeroplus.c
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 "config.h"
28 #include "libsigrok.h"
29 #include "libsigrok-internal.h"
30 #include "analyzer.h"
31
32 #define USB_VENDOR                      0x0c12
33
34 #define VENDOR_NAME                     "ZEROPLUS"
35 #define MODEL_NAME                      "Logic Cube LAP-C"
36 #define MODEL_VERSION                   NULL
37
38 #define NUM_PROBES                      16
39 #define USB_INTERFACE                   0
40 #define USB_CONFIGURATION               1
41 #define NUM_TRIGGER_STAGES              4
42 #define TRIGGER_TYPES                   "01"
43
44 #define PACKET_SIZE                     2048    /* ?? */
45
46 typedef struct {
47         unsigned short vid;
48         unsigned short pid;
49         char *model_name;
50         unsigned int channels;
51         unsigned int sample_depth;      /* In Ksamples/channel */
52         unsigned int max_sampling_freq;
53 } model_t;
54
55 /*
56  * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the
57  * same 128K sample depth.
58  */
59 static model_t zeroplus_models[] = {
60         {0x0c12, 0x7009, "LAP-C(16064)",  16, 64,   100},
61         {0x0c12, 0x700A, "LAP-C(16128)",  16, 128,  200},
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         {0x0c12, 0x700E, "LAP-C(16032)",  16, 32,   100},
66         {0x0c12, 0x7016, "LAP-C(162000)", 16, 2048, 200},
67         { 0, 0, 0, 0, 0, 0 }
68 };
69
70 static const int hwcaps[] = {
71         SR_HWCAP_LOGIC_ANALYZER,
72         SR_HWCAP_SAMPLERATE,
73         SR_HWCAP_PROBECONFIG,
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  */
85 static 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(). */
106 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
107 static struct sr_dev_driver *zdi = &zeroplus_logic_cube_driver_info;
108
109 static 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  */
120 static 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
142 static const struct sr_samplerates samplerates = {
143         0,
144         0,
145         0,
146         supported_samplerates,
147 };
148
149 /* Private, per-device-instance driver context. */
150 struct 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
164 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
165                 const void *value);
166 static int hw_dev_close(struct sr_dev_inst *sdi);
167
168 static 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
182 static int configure_probes(const struct sr_dev_inst *sdi, const GSList *probes)
183 {
184         struct context *ctx;
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         ctx = sdi->priv;
192
193         ctx->probe_mask = 0;
194         for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
195                 ctx->trigger_mask[i] = 0;
196                 ctx->trigger_value[i] = 0;
197         }
198
199         stage = -1;
200         for (l = 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 - 1);
205                 ctx->probe_mask |= probe_bit;
206
207                 if (probe->trigger) {
208                         stage = 0;
209                         for (tc = probe->trigger; *tc; tc++) {
210                                 ctx->trigger_mask[stage] |= probe_bit;
211                                 if (*tc == '1')
212                                         ctx->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
223 static void clear_instances(void)
224 {
225         GSList *l;
226         struct sr_dev_inst *sdi;
227
228         for (l = zdi->instances; l; l = l->next) {
229                 sdi = l->data;
230                 /* Properly close all devices... */
231                 hw_dev_close(sdi);
232                 /* ...and free all their memory. */
233                 sr_dev_inst_free(sdi);
234         }
235         g_slist_free(zdi->instances);
236         zdi->instances = NULL;
237
238 }
239
240 /*
241  * API callbacks
242  */
243
244 static int hw_init(void)
245 {
246
247         if (libusb_init(&usb_context) != 0) {
248                 sr_err("zp: Failed to initialize USB.");
249                 return 0;
250         }
251
252         return SR_OK;
253 }
254
255 static GSList *hw_scan(GSList *options)
256 {
257         struct sr_dev_inst *sdi;
258         struct sr_probe *probe;
259         struct context *ctx;
260         model_t *prof;
261         struct libusb_device_descriptor des;
262         libusb_device **devlist;
263         GSList *devices;
264         int ret, devcnt, i, j;
265
266         (void)options;
267         devices = NULL;
268
269         clear_instances();
270
271         /* Find all ZEROPLUS analyzers and add them to device list. */
272         devcnt = 0;
273         libusb_get_device_list(usb_context, &devlist); /* TODO: Errors. */
274
275         for (i = 0; devlist[i]; i++) {
276                 ret = libusb_get_device_descriptor(devlist[i], &des);
277                 if (ret != 0) {
278                         sr_err("zp: failed to get device descriptor: %d", ret);
279                         continue;
280                 }
281
282                 prof = NULL;
283                 for (j = 0; j < zeroplus_models[j].vid; j++) {
284                         if (des.idVendor == zeroplus_models[j].vid &&
285                                 des.idProduct == zeroplus_models[j].pid) {
286                                 prof = &zeroplus_models[j];
287                         }
288                 }
289                 /* Skip if the device was not found */
290                 if (!prof)
291                         continue;
292                 sr_info("zp: Found ZEROPLUS model %s", prof->model_name);
293
294                 /* Register the device with libsigrok. */
295                 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
296                                 VENDOR_NAME, prof->model_name, NULL))) {
297                         sr_err("zp: %s: sr_dev_inst_new failed", __func__);
298                         return NULL;
299                 }
300                 sdi->driver = zdi;
301
302                 /* Allocate memory for our private driver context. */
303                 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
304                         sr_err("zp: %s: ctx malloc failed", __func__);
305                         return 0;
306                 }
307                 sdi->priv = ctx;
308                 ctx->num_channels = prof->channels;
309                 ctx->memory_size = prof->sample_depth * 1024;
310                 // memset(ctx->trigger_buffer, 0, NUM_TRIGGER_STAGES);
311
312                 /* Fill in probelist according to this device's profile. */
313                 for (j = 0; j < ctx->num_channels; j++) {
314                         if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
315                                         probe_names[j])))
316                                 return NULL;
317                         sdi->probes = g_slist_append(sdi->probes, probe);
318                 }
319
320                 devices = g_slist_append(devices, sdi);
321                 zdi->instances = g_slist_append(zdi->instances, sdi);
322                 ctx->usb = sr_usb_dev_inst_new(
323                         libusb_get_bus_number(devlist[i]),
324                         libusb_get_device_address(devlist[i]), NULL);
325                 devcnt++;
326
327         }
328         libusb_free_device_list(devlist, 1);
329
330         return devices;
331 }
332
333 static int hw_dev_open(struct sr_dev_inst *sdi)
334 {
335         struct context *ctx;
336         libusb_device **devlist, *dev;
337         struct libusb_device_descriptor des;
338         int device_count, ret, i;
339
340         if (!(ctx = sdi->priv)) {
341                 sr_err("zp: %s: sdi->priv was NULL", __func__);
342                 return SR_ERR_ARG;
343         }
344
345         device_count = libusb_get_device_list(usb_context, &devlist);
346         if (device_count < 0) {
347                 sr_err("zp: Failed to retrieve device list");
348                 return SR_ERR;
349         }
350
351         dev = NULL;
352         for (i = 0; i < device_count; i++) {
353                 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
354                         sr_err("fx2lafw: Failed to get device descriptor: %d.",
355                                ret);
356                         continue;
357                 }
358                 if (libusb_get_bus_number(devlist[i]) == ctx->usb->bus
359                     && libusb_get_device_address(devlist[i]) == ctx->usb->address) {
360                         dev = devlist[i];
361                         break;
362                 }
363         }
364         if (!dev) {
365                 sr_err("device on bus %d address %d disappeared!",
366                                 ctx->usb->bus, ctx->usb->address);
367                 return SR_ERR;
368         }
369
370         if (!(ret = libusb_open(dev, &(ctx->usb->devhdl)))) {
371                 sdi->status = SR_ST_ACTIVE;
372                 sr_info("zp: opened device %d on %d.%d interface %d",
373                         sdi->index, ctx->usb->bus,
374                         ctx->usb->address, USB_INTERFACE);
375         } else {
376                 sr_err("zp: failed to open device: %d", ret);
377                 return SR_ERR;
378         }
379
380         ret = libusb_set_configuration(ctx->usb->devhdl, USB_CONFIGURATION);
381         if (ret < 0) {
382                 sr_err("zp: Unable to set USB configuration %d: %d",
383                        USB_CONFIGURATION, ret);
384                 return SR_ERR;
385         }
386
387         ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
388         if (ret != 0) {
389                 sr_err("zp: Unable to claim interface: %d", ret);
390                 return SR_ERR;
391         }
392
393         analyzer_reset(ctx->usb->devhdl);
394         analyzer_initialize(ctx->usb->devhdl);
395
396         analyzer_set_memory_size(MEMORY_SIZE_512K);
397         // analyzer_set_freq(g_freq, g_freq_scale);
398         analyzer_set_trigger_count(1);
399         // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
400         // * get_memory_size(g_memory_size)) / 100) >> 2);
401         analyzer_set_ramsize_trigger_address(
402                 (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
403
404 #if 0
405         if (g_double_mode == 1)
406                 analyzer_set_compression(COMPRESSION_DOUBLE);
407         else if (g_compression == 1)
408                 analyzer_set_compression(COMPRESSION_ENABLE);
409         else
410 #endif
411         analyzer_set_compression(COMPRESSION_NONE);
412
413         if (ctx->cur_samplerate == 0) {
414                 /* Samplerate hasn't been set. Default to the slowest one. */
415                 if (hw_dev_config_set(sdi, SR_HWCAP_SAMPLERATE,
416                      &samplerates.list[0]) == SR_ERR)
417                         return SR_ERR;
418         }
419
420         return SR_OK;
421 }
422
423 static int hw_dev_close(struct sr_dev_inst *sdi)
424 {
425         struct context *ctx;
426
427         if (!(ctx = sdi->priv)) {
428                 sr_err("zp: %s: sdi->priv was NULL", __func__);
429                 return SR_ERR;
430         }
431
432         if (!ctx->usb->devhdl)
433                 return SR_ERR;
434
435         sr_info("zp: closing device %d on %d.%d interface %d", sdi->index,
436                 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
437         libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
438         libusb_reset_device(ctx->usb->devhdl);
439         libusb_close(ctx->usb->devhdl);
440         ctx->usb->devhdl = NULL;
441         sdi->status = SR_ST_INACTIVE;
442
443         return SR_OK;
444 }
445
446 static int hw_cleanup(void)
447 {
448
449         clear_instances();
450
451         if (usb_context)
452                 libusb_exit(usb_context);
453         usb_context = NULL;
454
455         return SR_OK;
456 }
457
458 static int hw_info_get(int info_id, const void **data,
459        const struct sr_dev_inst *sdi)
460 {
461         struct context *ctx;
462
463         switch (info_id) {
464         case SR_DI_INST:
465                 *data = sdi;
466                 sr_spew("zp: %s: Returning sdi.", __func__);
467                 break;
468         case SR_DI_HWCAPS:
469                 *data = hwcaps;
470                 break;
471         case SR_DI_NUM_PROBES:
472                 if (sdi) {
473                         ctx = sdi->priv;
474                         *data = GINT_TO_POINTER(ctx->num_channels);
475                         sr_spew("zp: %s: Returning number of channels: %d.",
476                                         __func__, ctx->num_channels);
477                 } else
478                         return SR_ERR;
479                 break;
480         case SR_DI_PROBE_NAMES:
481                 *data = probe_names;
482                 sr_spew("zp: %s: Returning probenames.", __func__);
483                 break;
484         case SR_DI_SAMPLERATES:
485                 *data = &samplerates;
486                 sr_spew("zp: %s: Returning samplerates.", __func__);
487                 break;
488         case SR_DI_TRIGGER_TYPES:
489                 *data = TRIGGER_TYPES;
490                 sr_spew("zp: %s: Returning triggertypes: %s.", __func__, TRIGGER_TYPES);
491                 break;
492         case SR_DI_CUR_SAMPLERATE:
493                 if (sdi) {
494                         ctx = sdi->priv;
495                         *data = &ctx->cur_samplerate;
496                         sr_spew("zp: %s: Returning samplerate: %" PRIu64 "Hz.",
497                                 __func__, ctx->cur_samplerate);
498                 } else
499                         return SR_ERR;
500                 break;
501         default:
502                 return SR_ERR_ARG;
503         }
504
505         return SR_OK;
506 }
507
508 static int hw_dev_status_get(int dev_index)
509 {
510         struct sr_dev_inst *sdi;
511
512         sdi = sr_dev_inst_get(zdi->instances, dev_index);
513         if (sdi)
514                 return sdi->status;
515         else
516                 return SR_ST_NOT_FOUND;
517 }
518
519 static int set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
520 {
521         struct context *ctx;
522
523         if (!sdi) {
524                 sr_err("zp: %s: sdi was NULL", __func__);
525                 return SR_ERR_ARG;
526         }
527
528         if (!(ctx = sdi->priv)) {
529                 sr_err("zp: %s: sdi->priv was NULL", __func__);
530                 return SR_ERR_ARG;
531         }
532
533         sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate);
534
535         if (samplerate > SR_MHZ(1))
536                 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
537         else if (samplerate > SR_KHZ(1))
538                 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
539         else
540                 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
541
542         ctx->cur_samplerate = samplerate;
543
544         return SR_OK;
545 }
546
547 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
548                 const void *value)
549 {
550         struct context *ctx;
551
552         if (!(ctx = sdi->priv)) {
553                 sr_err("zp: %s: sdi->priv was NULL", __func__);
554                 return SR_ERR_ARG;
555         }
556
557         switch (hwcap) {
558         case SR_HWCAP_SAMPLERATE:
559                 return set_samplerate(sdi, *(const uint64_t *)value);
560         case SR_HWCAP_PROBECONFIG:
561                 return configure_probes(sdi, (const GSList *)value);
562         case SR_HWCAP_LIMIT_SAMPLES:
563                 ctx->limit_samples = *(const uint64_t *)value;
564                 return SR_OK;
565         default:
566                 return SR_ERR;
567         }
568 }
569
570 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
571                 void *cb_data)
572 {
573         struct sr_datafeed_packet packet;
574         struct sr_datafeed_logic logic;
575         struct sr_datafeed_header header;
576         struct sr_datafeed_meta_logic meta;
577         uint64_t samples_read;
578         int res;
579         unsigned int packet_num;
580         unsigned char *buf;
581         struct context *ctx;
582
583         if (!(ctx = sdi->priv)) {
584                 sr_err("zp: %s: sdi->priv was NULL", __func__);
585                 return SR_ERR_ARG;
586         }
587
588         /* push configured settings to device */
589         analyzer_configure(ctx->usb->devhdl);
590
591         analyzer_start(ctx->usb->devhdl);
592         sr_info("zp: Waiting for data");
593         analyzer_wait_data(ctx->usb->devhdl);
594
595         sr_info("zp: Stop address    = 0x%x",
596                 analyzer_get_stop_address(ctx->usb->devhdl));
597         sr_info("zp: Now address     = 0x%x",
598                 analyzer_get_now_address(ctx->usb->devhdl));
599         sr_info("zp: Trigger address = 0x%x",
600                 analyzer_get_trigger_address(ctx->usb->devhdl));
601
602         packet.type = SR_DF_HEADER;
603         packet.payload = &header;
604         header.feed_version = 1;
605         gettimeofday(&header.starttime, NULL);
606         sr_session_send(cb_data, &packet);
607
608         /* Send metadata about the SR_DF_LOGIC packets to come. */
609         packet.type = SR_DF_META_LOGIC;
610         packet.payload = &meta;
611         meta.samplerate = ctx->cur_samplerate;
612         meta.num_probes = ctx->num_channels;
613         sr_session_send(cb_data, &packet);
614
615         if (!(buf = g_try_malloc(PACKET_SIZE))) {
616                 sr_err("zp: %s: buf malloc failed", __func__);
617                 return SR_ERR_MALLOC;
618         }
619
620         samples_read = 0;
621         analyzer_read_start(ctx->usb->devhdl);
622         /* Send the incoming transfer to the session bus. */
623         for (packet_num = 0; packet_num < (ctx->memory_size * 4 / PACKET_SIZE);
624              packet_num++) {
625                 res = analyzer_read_data(ctx->usb->devhdl, buf, PACKET_SIZE);
626                 sr_info("zp: Tried to read %llx bytes, actually read %x bytes",
627                         PACKET_SIZE, res);
628
629                 packet.type = SR_DF_LOGIC;
630                 packet.payload = &logic;
631                 logic.length = PACKET_SIZE;
632                 logic.unitsize = 4;
633                 logic.data = buf;
634                 sr_session_send(cb_data, &packet);
635                 samples_read += res / 4;
636         }
637         analyzer_read_stop(ctx->usb->devhdl);
638         g_free(buf);
639
640         packet.type = SR_DF_END;
641         sr_session_send(cb_data, &packet);
642
643         return SR_OK;
644 }
645
646 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
647 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
648                 void *cb_data)
649 {
650         struct sr_datafeed_packet packet;
651         struct context *ctx;
652
653         packet.type = SR_DF_END;
654         sr_session_send(cb_data, &packet);
655
656         if (!(ctx = sdi->priv)) {
657                 sr_err("zp: %s: sdi->priv was NULL", __func__);
658                 return SR_ERR_BUG;
659         }
660
661         analyzer_reset(ctx->usb->devhdl);
662         /* TODO: Need to cancel and free any queued up transfers. */
663
664         return SR_OK;
665 }
666
667 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
668         .name = "zeroplus-logic-cube",
669         .longname = "ZEROPLUS Logic Cube LAP-C series",
670         .api_version = 1,
671         .init = hw_init,
672         .cleanup = hw_cleanup,
673         .scan = hw_scan,
674         .dev_open = hw_dev_open,
675         .dev_close = hw_dev_close,
676         .info_get = hw_info_get,
677         .dev_status_get = hw_dev_status_get,
678         .dev_config_set = hw_dev_config_set,
679         .dev_acquisition_start = hw_dev_acquisition_start,
680         .dev_acquisition_stop = hw_dev_acquisition_stop,
681         .instances = NULL,
682 };