]> sigrok.org Git - libsigrok.git/blob - hardware/zeroplus-logic-cube/zeroplus.c
probe names: Fix cosmetics, add docs, fix off-by-one.
[libsigrok.git] / hardware / zeroplus-logic-cube / zeroplus.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010 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 "config.h"
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <sys/time.h>
24 #include <inttypes.h>
25 #include <glib.h>
26 #include <libusb.h>
27 #include "sigrok.h"
28 #include "sigrok-internal.h"
29 #include "analyzer.h"
30
31 #define USB_VENDOR                      0x0c12
32 #define USB_VENDOR_NAME                 "Zeroplus"
33 #define USB_MODEL_NAME                  "Logic Cube"
34 #define USB_MODEL_VERSION               ""
35
36 #define USB_INTERFACE                   0
37 #define USB_CONFIGURATION               1
38 #define NUM_TRIGGER_STAGES              4
39 #define TRIGGER_TYPES                   "01"
40
41 #define PACKET_SIZE                     2048    /* ?? */
42
43 typedef struct {
44         unsigned short pid;
45         char model_name[64];
46         unsigned int channels;
47         unsigned int sample_depth;      /* In Ksamples/channel */
48         unsigned int max_sampling_freq;
49 } model_t;
50
51 /*
52  * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the
53  * same 128K sample depth.
54  */
55 static model_t zeroplus_models[] = {
56         {0x7009, "LAP-C(16064)",  16, 64,   100},
57         {0x700A, "LAP-C(16128)",  16, 128,  200},
58         {0x700B, "LAP-C(32128)",  32, 128,  200},
59         {0x700C, "LAP-C(321000)", 32, 1024, 200},
60         {0x700D, "LAP-C(322000)", 32, 2048, 200},
61         {0x700E, "LAP-C(16032)",  16, 32,   100},
62         {0x7016, "LAP-C(162000)", 16, 2048, 200},
63 };
64
65 static int capabilities[] = {
66         SR_HWCAP_LOGIC_ANALYZER,
67         SR_HWCAP_SAMPLERATE,
68         SR_HWCAP_PROBECONFIG,
69         SR_HWCAP_CAPTURE_RATIO,
70
71         /* These are really implemented in the driver, not the hardware. */
72         SR_HWCAP_LIMIT_SAMPLES,
73         0,
74 };
75
76 static const char *probe_names[] = {
77         "0",
78         "1",
79         "2",
80         "3",
81         "4",
82         "5",
83         "6",
84         "7",
85         "8",
86         "9",
87         "10",
88         "11",
89         "12",
90         "13",
91         "14",
92         "15",
93         "16",
94         "17",
95         "18",
96         "19",
97         "20",
98         "21",
99         "22",
100         "23",
101         "24",
102         "25",
103         "26",
104         "27",
105         "28",
106         "29",
107         "30",
108         "31",
109         NULL,
110 };
111
112 /* List of struct sr_device_instance, maintained by opendev()/closedev(). */
113 static GSList *device_instances = NULL;
114
115 static libusb_context *usb_context = NULL;
116
117 /*
118  * The hardware supports more samplerates than these, but these are the
119  * options hardcoded into the vendor's Windows GUI.
120  */
121
122 /*
123  * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up
124  * that high.
125  */
126 static uint64_t supported_samplerates[] = {
127         SR_HZ(100),
128         SR_HZ(500),
129         SR_KHZ(1),
130         SR_KHZ(5),
131         SR_KHZ(25),
132         SR_KHZ(50),
133         SR_KHZ(100),
134         SR_KHZ(200),
135         SR_KHZ(400),
136         SR_KHZ(800),
137         SR_MHZ(1),
138         SR_MHZ(10),
139         SR_MHZ(25),
140         SR_MHZ(50),
141         SR_MHZ(80),
142         SR_MHZ(100),
143         SR_MHZ(150),
144         SR_MHZ(200),
145         0,
146 };
147
148 static struct sr_samplerates samplerates = {
149         SR_HZ(0),
150         SR_HZ(0),
151         SR_HZ(0),
152         supported_samplerates,
153 };
154
155 /* TODO: All of these should go in a device-specific struct. */
156 static uint64_t cur_samplerate = 0;
157 static uint64_t period_ps = 0;
158 static uint64_t limit_samples = 0;
159 static int num_channels = 32; /* TODO: This isn't initialized before it's needed :( */
160 static uint64_t memory_size = 0;
161 static uint8_t probe_mask = 0;
162 static uint8_t trigger_mask[NUM_TRIGGER_STAGES] = { 0 };
163 static uint8_t trigger_value[NUM_TRIGGER_STAGES] = { 0 };
164
165 // static uint8_t trigger_buffer[NUM_TRIGGER_STAGES] = { 0 };
166
167 static int hw_set_configuration(int device_index, int capability, void *value);
168
169 static unsigned int get_memory_size(int type)
170 {
171         if (type == MEMORY_SIZE_8K)
172                 return 8 * 1024;
173         else if (type == MEMORY_SIZE_64K)
174                 return 64 * 1024;
175         else if (type == MEMORY_SIZE_128K)
176                 return 128 * 1024;
177         else if (type == MEMORY_SIZE_512K)
178                 return 512 * 1024;
179         else
180                 return 0;
181 }
182
183 static int opendev4(struct sr_device_instance **sdi, libusb_device *dev,
184                     struct libusb_device_descriptor *des)
185 {
186         unsigned int i;
187         int err;
188
189         if ((err = libusb_get_device_descriptor(dev, des))) {
190                 sr_warn("failed to get device descriptor: %d", err);
191                 return -1;
192         }
193
194         if (des->idVendor != USB_VENDOR)
195                 return 0;
196
197         if (libusb_get_bus_number(dev) == (*sdi)->usb->bus
198             && libusb_get_device_address(dev) == (*sdi)->usb->address) {
199
200                 for (i = 0; i < ARRAY_SIZE(zeroplus_models); i++) {
201                         if (!(des->idProduct == zeroplus_models[i].pid))
202                                 continue;
203
204                         sr_info("Found PID=%04X (%s)", des->idProduct,
205                                 zeroplus_models[i].model_name);
206                         num_channels = zeroplus_models[i].channels;
207                         memory_size = zeroplus_models[i].sample_depth * 1024;
208                         break;
209                 }
210
211                 if (num_channels == 0) {
212                         sr_warn("Unknown ZeroPlus device %04X", des->idProduct);
213                         return -2;
214                 }
215
216                 /* Found it. */
217                 if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) {
218                         (*sdi)->status = SR_ST_ACTIVE;
219                         sr_info("opened device %d on %d.%d interface %d",
220                                 (*sdi)->index, (*sdi)->usb->bus,
221                                 (*sdi)->usb->address, USB_INTERFACE);
222                 } else {
223                         sr_warn("failed to open device: %d", err);
224                         *sdi = NULL;
225                 }
226         }
227
228         return 0;
229 }
230
231 static struct sr_device_instance *zp_open_device(int device_index)
232 {
233         struct sr_device_instance *sdi;
234         libusb_device **devlist;
235         struct libusb_device_descriptor des;
236         int err, i;
237
238         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
239                 return NULL;
240
241         libusb_get_device_list(usb_context, &devlist);
242         if (sdi->status == SR_ST_INACTIVE) {
243                 /* Find the device by vendor, product, bus and address. */
244                 libusb_get_device_list(usb_context, &devlist);
245                 for (i = 0; devlist[i]; i++) {
246                         /* TODO: Error handling. */
247                         err = opendev4(&sdi, devlist[i], &des);
248                 }
249         } else {
250                 /* Status must be SR_ST_ACTIVE, i.e. already in use... */
251                 sdi = NULL;
252         }
253         libusb_free_device_list(devlist, 1);
254
255         if (sdi && sdi->status != SR_ST_ACTIVE)
256                 sdi = NULL;
257
258         return sdi;
259 }
260
261 static void close_device(struct sr_device_instance *sdi)
262 {
263         if (!sdi->usb->devhdl)
264                 return;
265
266         sr_info("closing device %d on %d.%d interface %d", sdi->index,
267                 sdi->usb->bus, sdi->usb->address, USB_INTERFACE);
268         libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE);
269         libusb_close(sdi->usb->devhdl);
270         sdi->usb->devhdl = NULL;
271         sdi->status = SR_ST_INACTIVE;
272 }
273
274 static int configure_probes(GSList *probes)
275 {
276         struct sr_probe *probe;
277         GSList *l;
278         int probe_bit, stage, i;
279         char *tc;
280
281         probe_mask = 0;
282         for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
283                 trigger_mask[i] = 0;
284                 trigger_value[i] = 0;
285         }
286
287         stage = -1;
288         for (l = probes; l; l = l->next) {
289                 probe = (struct sr_probe *)l->data;
290                 if (probe->enabled == FALSE)
291                         continue;
292                 probe_bit = 1 << (probe->index - 1);
293                 probe_mask |= probe_bit;
294
295                 if (probe->trigger) {
296                         stage = 0;
297                         for (tc = probe->trigger; *tc; tc++) {
298                                 trigger_mask[stage] |= probe_bit;
299                                 if (*tc == '1')
300                                         trigger_value[stage] |= probe_bit;
301                                 stage++;
302                                 if (stage > NUM_TRIGGER_STAGES)
303                                         return SR_ERR;
304                         }
305                 }
306         }
307
308         return SR_OK;
309 }
310
311 /*
312  * API callbacks
313  */
314
315 static int hw_init(const char *deviceinfo)
316 {
317         struct sr_device_instance *sdi;
318         struct libusb_device_descriptor des;
319         libusb_device **devlist;
320         int err, devcnt, i;
321
322         /* Avoid compiler warnings. */
323         (void)deviceinfo;
324
325         if (libusb_init(&usb_context) != 0) {
326                 sr_warn("Failed to initialize USB.");
327                 return 0;
328         }
329
330         /* Find all ZeroPlus analyzers and add them to device list. */
331         devcnt = 0;
332         libusb_get_device_list(usb_context, &devlist);
333
334         for (i = 0; devlist[i]; i++) {
335                 err = libusb_get_device_descriptor(devlist[i], &des);
336                 if (err != 0) {
337                         sr_warn("failed to get device descriptor: %d", err);
338                         continue;
339                 }
340
341                 if (des.idVendor == USB_VENDOR) {
342                         /*
343                          * Definitely a Zeroplus.
344                          * TODO: Any way to detect specific model/version in
345                          * the zeroplus range?
346                          */
347                         sdi = sr_device_instance_new(devcnt,
348                                         SR_ST_INACTIVE, USB_VENDOR_NAME,
349                                         USB_MODEL_NAME, USB_MODEL_VERSION);
350                         if (!sdi)
351                                 return 0;
352                         device_instances =
353                             g_slist_append(device_instances, sdi);
354                         sdi->usb = sr_usb_device_instance_new(
355                                 libusb_get_bus_number(devlist[i]),
356                                 libusb_get_device_address(devlist[i]), NULL);
357                         devcnt++;
358                 }
359         }
360         libusb_free_device_list(devlist, 1);
361
362         return devcnt;
363 }
364
365 static int hw_opendev(int device_index)
366 {
367         struct sr_device_instance *sdi;
368         int err;
369
370         if (!(sdi = zp_open_device(device_index))) {
371                 sr_warn("unable to open device");
372                 return SR_ERR;
373         }
374
375         err = libusb_claim_interface(sdi->usb->devhdl, USB_INTERFACE);
376         if (err != 0) {
377                 sr_warn("Unable to claim interface: %d", err);
378                 return SR_ERR;
379         }
380         analyzer_reset(sdi->usb->devhdl);
381         analyzer_initialize(sdi->usb->devhdl);
382
383         analyzer_set_memory_size(MEMORY_SIZE_512K);
384         // analyzer_set_freq(g_freq, g_freq_scale);
385         analyzer_set_trigger_count(1);
386         // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
387         // * get_memory_size(g_memory_size)) / 100) >> 2);
388         analyzer_set_ramsize_trigger_address(
389                 (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
390
391 #if 0
392         if (g_double_mode == 1)
393                 analyzer_set_compression(COMPRESSION_DOUBLE);
394         else if (g_compression == 1)
395                 analyzer_set_compression(COMPRESSION_ENABLE);
396         else
397 #endif
398         analyzer_set_compression(COMPRESSION_NONE);
399
400         if (cur_samplerate == 0) {
401                 /* Samplerate hasn't been set. Default to the slowest one. */
402                 if (hw_set_configuration(device_index, SR_HWCAP_SAMPLERATE,
403                      &samplerates.low) == SR_ERR)
404                         return SR_ERR;
405         }
406
407         return SR_OK;
408 }
409
410 static int hw_closedev(int device_index)
411 {
412         struct sr_device_instance *sdi;
413
414         if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
415                 sr_err("lap-c: %s: sdi was NULL", __func__);
416                 return SR_ERR; /* TODO: SR_ERR_ARG? */
417         }
418
419         /* TODO */
420         close_device(sdi);
421
422         return SR_OK;
423 }
424
425 static void hw_cleanup(void)
426 {
427         GSList *l;
428
429         /* Properly close all devices... */
430         for (l = device_instances; l; l = l->next)
431                 close_device((struct sr_device_instance *)l->data);
432
433         /* ...and free all their memory. */
434         for (l = device_instances; l; l = l->next)
435                 g_free(l->data);
436         g_slist_free(device_instances);
437         device_instances = NULL;
438
439         if (usb_context)
440                 libusb_exit(usb_context);
441         usb_context = NULL;
442 }
443
444 static void *hw_get_device_info(int device_index, int device_info_id)
445 {
446         struct sr_device_instance *sdi;
447         void *info = NULL;
448
449         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
450                 return NULL;
451
452         switch (device_info_id) {
453         case SR_DI_INSTANCE:
454                 info = sdi;
455                 break;
456         case SR_DI_NUM_PROBES:
457                 info = GINT_TO_POINTER(num_channels);
458                 break;
459         case SR_DI_PROBE_NAMES:
460                 info = probe_names;
461                 break;
462         case SR_DI_SAMPLERATES:
463                 info = &samplerates;
464                 break;
465         case SR_DI_TRIGGER_TYPES:
466                 info = TRIGGER_TYPES;
467                 break;
468         case SR_DI_CUR_SAMPLERATE:
469                 info = &cur_samplerate;
470                 break;
471         }
472
473         return info;
474 }
475
476 static int hw_get_status(int device_index)
477 {
478         struct sr_device_instance *sdi;
479
480         sdi = sr_get_device_instance(device_instances, device_index);
481         if (sdi)
482                 return sdi->status;
483         else
484                 return SR_ST_NOT_FOUND;
485 }
486
487 static int *hw_get_capabilities(void)
488 {
489         return capabilities;
490 }
491
492 /* TODO: This will set the same samplerate for all devices. */
493 static int set_configuration_samplerate(uint64_t samplerate)
494 {
495         sr_info("%s(%" PRIu64 ")", __func__, samplerate);
496         if (samplerate > SR_MHZ(1))
497                 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
498         else if (samplerate > SR_KHZ(1))
499                 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
500         else
501                 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
502
503         cur_samplerate = samplerate;
504         period_ps = 1000000000000 / samplerate;
505
506         return SR_OK;
507 }
508
509 static int hw_set_configuration(int device_index, int capability, void *value)
510 {
511         struct sr_device_instance *sdi;
512         uint64_t *tmp_u64;
513
514         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
515                 return SR_ERR;
516
517         switch (capability) {
518         case SR_HWCAP_SAMPLERATE:
519                 tmp_u64 = value;
520                 return set_configuration_samplerate(*tmp_u64);
521         case SR_HWCAP_PROBECONFIG:
522                 return configure_probes((GSList *) value);
523         case SR_HWCAP_LIMIT_SAMPLES:
524                 tmp_u64 = value;
525                 limit_samples = *tmp_u64;
526                 return SR_OK;
527         default:
528                 return SR_ERR;
529         }
530 }
531
532 static int hw_start_acquisition(int device_index, gpointer session_data)
533 {
534         struct sr_device_instance *sdi;
535         struct sr_datafeed_packet packet;
536         struct sr_datafeed_logic logic;
537         struct sr_datafeed_header header;
538         uint64_t samples_read;
539         int res;
540         unsigned int packet_num;
541         unsigned char *buf;
542
543         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
544                 return SR_ERR;
545
546         /* push configured settings to device */
547         analyzer_configure(sdi->usb->devhdl);
548
549         analyzer_start(sdi->usb->devhdl);
550         sr_info("Waiting for data");
551         analyzer_wait_data(sdi->usb->devhdl);
552
553         sr_info("Stop address    = 0x%x", analyzer_get_stop_address(sdi->usb->devhdl));
554         sr_info("Now address     = 0x%x", analyzer_get_now_address(sdi->usb->devhdl));
555         sr_info("Trigger address = 0x%x", analyzer_get_trigger_address(sdi->usb->devhdl));
556
557         packet.type = SR_DF_HEADER;
558         packet.payload = &header;
559         header.feed_version = 1;
560         gettimeofday(&header.starttime, NULL);
561         header.samplerate = cur_samplerate;
562         header.num_logic_probes = num_channels;
563         header.num_analog_probes = 0;
564         sr_session_bus(session_data, &packet);
565
566         if (!(buf = g_try_malloc(PACKET_SIZE))) {
567                 sr_err("lap-c: %s: buf malloc failed", __func__);
568                 return SR_ERR_MALLOC;
569         }
570
571         samples_read = 0;
572         analyzer_read_start(sdi->usb->devhdl);
573         /* Send the incoming transfer to the session bus. */
574         for (packet_num = 0; packet_num < (memory_size * 4 / PACKET_SIZE);
575              packet_num++) {
576                 res = analyzer_read_data(sdi->usb->devhdl, buf, PACKET_SIZE);
577                 sr_info("Tried to read %llx bytes, actually read %x bytes",
578                         PACKET_SIZE, res);
579
580                 packet.type = SR_DF_LOGIC;
581                 packet.timeoffset = samples_read * period_ps;
582                 packet.duration = res / 4 * period_ps;
583                 packet.payload = &logic;
584                 logic.length = PACKET_SIZE;
585                 logic.unitsize = 4;
586                 logic.data = buf;
587                 sr_session_bus(session_data, &packet);
588                 samples_read += res / 4;
589         }
590         analyzer_read_stop(sdi->usb->devhdl);
591         g_free(buf);
592
593         packet.type = SR_DF_END;
594         sr_session_bus(session_data, &packet);
595
596         return SR_OK;
597 }
598
599 /* This stops acquisition on ALL devices, ignoring device_index. */
600 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
601 {
602         struct sr_datafeed_packet packet;
603         struct sr_device_instance *sdi;
604
605         packet.type = SR_DF_END;
606         sr_session_bus(session_device_id, &packet);
607
608         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
609                 return; /* TODO: Cry? */
610
611         analyzer_reset(sdi->usb->devhdl);
612         /* TODO: Need to cancel and free any queued up transfers. */
613 }
614
615 struct sr_device_plugin zeroplus_logic_cube_plugin_info = {
616         .name = "zeroplus-logic-cube",
617         .longname = "Zeroplus Logic Cube LAP-C series",
618         .api_version = 1,
619         .init = hw_init,
620         .cleanup = hw_cleanup,
621         .opendev = hw_opendev,
622         .closedev = hw_closedev,
623         .get_device_info = hw_get_device_info,
624         .get_status = hw_get_status,
625         .get_capabilities = hw_get_capabilities,
626         .set_configuration = hw_set_configuration,
627         .start_acquisition = hw_start_acquisition,
628         .stop_acquisition = hw_stop_acquisition,
629 };