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