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