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