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