]> sigrok.org Git - libsigrok.git/blame - hardware/zeroplus-logic-cube/api.c
zeroplus-logic-cube: Publish SR_CONF_MAX_UNCOMPRESSED_SAMPLES.
[libsigrok.git] / hardware / zeroplus-logic-cube / api.c
CommitLineData
a1bb33af 1/*
50985c20 2 * This file is part of the libsigrok project.
a1bb33af 3 *
c73d2ea4 4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
a1bb33af
UH
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
6d116114 20#include "protocol.h"
a1bb33af 21
8fdecced 22#define VENDOR_NAME "ZEROPLUS"
a1bb33af
UH
23#define USB_INTERFACE 0
24#define USB_CONFIGURATION 1
25#define NUM_TRIGGER_STAGES 4
c50277a6 26#define TRIGGER_TYPE "01"
fed16f06 27#define PACKET_SIZE 2048 /* ?? */
a1bb33af 28
0ab0cb94
TY
29//#define ZP_EXPERIMENTAL
30
e495a676
UH
31struct zp_model {
32 uint16_t vid;
33 uint16_t pid;
428edbe1 34 char *model_name;
a1bb33af 35 unsigned int channels;
fed16f06 36 unsigned int sample_depth; /* In Ksamples/channel */
a1bb33af 37 unsigned int max_sampling_freq;
e495a676 38};
a1bb33af 39
fed16f06
UH
40/*
41 * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the
42 * same 128K sample depth.
43 */
e495a676 44static const struct zp_model zeroplus_models[] = {
9e5670d0 45 {0x0c12, 0x7002, "LAP-16128U", 16, 128, 200},
428edbe1 46 {0x0c12, 0x7009, "LAP-C(16064)", 16, 64, 100},
e495a676 47 {0x0c12, 0x700a, "LAP-C(16128)", 16, 128, 200},
5db0c668
RD
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},
e495a676 51 {0x0c12, 0x700e, "LAP-C(16032)", 16, 32, 100},
428edbe1
BV
52 {0x0c12, 0x7016, "LAP-C(162000)", 16, 2048, 200},
53 { 0, 0, 0, 0, 0, 0 }
a1bb33af
UH
54};
55
8386096f 56static const int32_t hwcaps[] = {
1953564a
BV
57 SR_CONF_LOGIC_ANALYZER,
58 SR_CONF_SAMPLERATE,
59 SR_CONF_CAPTURE_RATIO,
7142d6b9 60 SR_CONF_VOLTAGE_THRESHOLD,
1953564a 61 SR_CONF_LIMIT_SAMPLES,
a1bb33af
UH
62};
63
d261dbbf
UH
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 */
e495a676 68static const char *probe_names[] = {
78693401
UH
69 "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7",
70 "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7",
5db0c668
RD
71 "C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7",
72 "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
464d12c7
KS
73 NULL,
74};
75
32756547 76SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
a873c594 77static struct sr_dev_driver *di = &zeroplus_logic_cube_driver_info;
a1bb33af 78
fed16f06
UH
79/*
80 * The hardware supports more samplerates than these, but these are the
81 * options hardcoded into the vendor's Windows GUI.
82 */
a1bb33af 83
8386096f 84static const uint64_t samplerates_100[] = {
17548571
UH
85 SR_HZ(100),
86 SR_HZ(500),
87 SR_KHZ(1),
88 SR_KHZ(5),
89 SR_KHZ(25),
90 SR_KHZ(50),
91 SR_KHZ(100),
92 SR_KHZ(200),
93 SR_KHZ(400),
94 SR_KHZ(800),
95 SR_MHZ(1),
96 SR_MHZ(10),
97 SR_MHZ(25),
98 SR_MHZ(50),
99 SR_MHZ(80),
100 SR_MHZ(100),
17548571
UH
101};
102
8386096f 103const uint64_t samplerates_200[] = {
c9140419
UH
104 SR_HZ(100),
105 SR_HZ(500),
59df0c77
UH
106 SR_KHZ(1),
107 SR_KHZ(5),
108 SR_KHZ(25),
109 SR_KHZ(50),
110 SR_KHZ(100),
111 SR_KHZ(200),
112 SR_KHZ(400),
113 SR_KHZ(800),
114 SR_MHZ(1),
115 SR_MHZ(10),
116 SR_MHZ(25),
117 SR_MHZ(50),
118 SR_MHZ(80),
119 SR_MHZ(100),
120 SR_MHZ(150),
121 SR_MHZ(200),
bf43ea23 122};
a1bb33af 123
6078d2c9 124static int dev_close(struct sr_dev_inst *sdi);
a1bb33af 125
0ab0cb94 126#if 0
014359e3 127static int configure_probes(const struct sr_dev_inst *sdi)
a1bb33af 128{
310e9e9b 129 struct dev_context *devc;
1b79df2f
JH
130 const struct sr_probe *probe;
131 const GSList *l;
a1bb33af
UH
132 int probe_bit, stage, i;
133 char *tc;
134
bf43ea23 135 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
310e9e9b 136 devc = sdi->priv;
bf43ea23 137
310e9e9b 138 devc->probe_mask = 0;
fed16f06 139 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
310e9e9b
BV
140 devc->trigger_mask[i] = 0;
141 devc->trigger_value[i] = 0;
a1bb33af
UH
142 }
143
144 stage = -1;
014359e3 145 for (l = sdi->probes; l; l = l->next) {
1afe8989 146 probe = (struct sr_probe *)l->data;
fed16f06 147 if (probe->enabled == FALSE)
a1bb33af 148 continue;
b35c8293 149 probe_bit = 1 << (probe->index);
310e9e9b 150 devc->probe_mask |= probe_bit;
fed16f06
UH
151
152 if (probe->trigger) {
a1bb33af 153 stage = 0;
fed16f06 154 for (tc = probe->trigger; *tc; tc++) {
310e9e9b 155 devc->trigger_mask[stage] |= probe_bit;
fed16f06 156 if (*tc == '1')
310e9e9b 157 devc->trigger_value[stage] |= probe_bit;
a1bb33af 158 stage++;
fed16f06 159 if (stage > NUM_TRIGGER_STAGES)
e46b8fb1 160 return SR_ERR;
a1bb33af
UH
161 }
162 }
163 }
164
e46b8fb1 165 return SR_OK;
a1bb33af 166}
0ab0cb94
TY
167#endif
168
169static int configure_probes(const struct sr_dev_inst *sdi)
170{
171 struct dev_context *devc;
172 const GSList *l;
173 const struct sr_probe *probe;
174 char *tc;
175 int type;
176
177 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
178 devc = sdi->priv;
179
180 for (l = sdi->probes; l; l = l->next) {
181 probe = (struct sr_probe *)l->data;
182 if (probe->enabled == FALSE)
183 continue;
184
185 if ((tc = probe->trigger)) {
186 switch (*tc) {
187 case '1':
188 type = TRIGGER_HIGH;
189 break;
190 case '0':
191 type = TRIGGER_LOW;
192 break;
193#if 0
194 case 'r':
195 type = TRIGGER_POSEDGE;
196 break;
197 case 'f':
198 type = TRIGGER_NEGEDGE;
199 break;
200 case 'c':
201 type = TRIGGER_ANYEDGE;
202 break;
203#endif
204 default:
205 return SR_ERR;
206 }
207 analyzer_add_trigger(probe->index, type);
208 devc->trigger = 1;
209 }
210 }
211
212 return SR_OK;
213}
a1bb33af 214
3316e149
BV
215SR_PRIV int zp_set_samplerate(struct dev_context *devc, uint64_t samplerate)
216{
217 int i;
218
219 for (i = 0; ARRAY_SIZE(samplerates_200); i++)
220 if (samplerate == samplerates_200[i])
221 break;
222
223 if (i == ARRAY_SIZE(samplerates_200) || samplerate > devc->max_samplerate) {
224 sr_err("Unsupported samplerate: %" PRIu64 "Hz.", samplerate);
225 return SR_ERR_ARG;
226 }
227
228 sr_info("Setting samplerate to %" PRIu64 "Hz.", samplerate);
229
230 if (samplerate >= SR_MHZ(1))
231 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
232 else if (samplerate >= SR_KHZ(1))
233 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
234 else
235 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
236
237 devc->cur_samplerate = samplerate;
238
239 return SR_OK;
240}
241
3b412e3a 242static int dev_clear(void)
7da6f9d5 243{
d6445cb0 244 return std_dev_clear(di, NULL);
7da6f9d5
BV
245}
246
6078d2c9 247static int init(struct sr_context *sr_ctx)
61136ea6 248{
f6beaac5 249 return std_init(sr_ctx, di, LOG_PREFIX);
61136ea6
BV
250}
251
6078d2c9 252static GSList *scan(GSList *options)
a1bb33af 253{
d68e2d1a 254 struct sr_dev_inst *sdi;
428edbe1 255 struct sr_probe *probe;
310e9e9b
BV
256 struct drv_context *drvc;
257 struct dev_context *devc;
e495a676 258 const struct zp_model *prof;
a1bb33af
UH
259 struct libusb_device_descriptor des;
260 libusb_device **devlist;
428edbe1
BV
261 GSList *devices;
262 int ret, devcnt, i, j;
a1bb33af 263
4ca38984 264 (void)options;
64d33dc2 265
a873c594 266 drvc = di->priv;
4b97c74e 267
4ca38984
BV
268 devices = NULL;
269
8fdecced 270 /* Find all ZEROPLUS analyzers and add them to device list. */
a1bb33af 271 devcnt = 0;
d4abb463 272 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); /* TODO: Errors. */
fed16f06
UH
273
274 for (i = 0; devlist[i]; i++) {
ebc34738
UH
275 ret = libusb_get_device_descriptor(devlist[i], &des);
276 if (ret != 0) {
6d116114 277 sr_err("Failed to get device descriptor: %s.",
d4928d71 278 libusb_error_name(ret));
a1bb33af
UH
279 continue;
280 }
281
428edbe1
BV
282 prof = NULL;
283 for (j = 0; j < zeroplus_models[j].vid; j++) {
284 if (des.idVendor == zeroplus_models[j].vid &&
285 des.idProduct == zeroplus_models[j].pid) {
286 prof = &zeroplus_models[j];
bf43ea23 287 }
a1bb33af 288 }
e495a676 289 /* Skip if the device was not found. */
428edbe1
BV
290 if (!prof)
291 continue;
e495a676 292 sr_info("Found ZEROPLUS %s.", prof->model_name);
428edbe1
BV
293
294 /* Register the device with libsigrok. */
295 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
296 VENDOR_NAME, prof->model_name, NULL))) {
6d116114 297 sr_err("%s: sr_dev_inst_new failed", __func__);
428edbe1
BV
298 return NULL;
299 }
a873c594 300 sdi->driver = di;
428edbe1
BV
301
302 /* Allocate memory for our private driver context. */
310e9e9b 303 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
6d116114 304 sr_err("Device context malloc failed.");
886a52b6 305 return NULL;
428edbe1 306 }
e495a676 307
310e9e9b 308 sdi->priv = devc;
17548571 309 devc->prof = prof;
310e9e9b 310 devc->num_channels = prof->channels;
0ab0cb94 311#ifdef ZP_EXPERIMENTAL
e93fb98b 312 devc->max_sample_depth = 128 * 1024;
0ab0cb94
TY
313 devc->max_samplerate = 200;
314#else
e93fb98b 315 devc->max_sample_depth = prof->sample_depth * 1024;
0ab0cb94
TY
316 devc->max_samplerate = prof->max_sampling_freq;
317#endif
318 devc->max_samplerate *= SR_MHZ(1);
319 devc->memory_size = MEMORY_SIZE_8K;
310e9e9b 320 // memset(devc->trigger_buffer, 0, NUM_TRIGGER_STAGES);
428edbe1
BV
321
322 /* Fill in probelist according to this device's profile. */
310e9e9b 323 for (j = 0; j < devc->num_channels; j++) {
428edbe1
BV
324 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
325 probe_names[j])))
326 return NULL;
327 sdi->probes = g_slist_append(sdi->probes, probe);
328 }
329
330 devices = g_slist_append(devices, sdi);
310e9e9b 331 drvc->instances = g_slist_append(drvc->instances, sdi);
8111446a 332 sdi->inst_type = SR_INST_USB;
609bfd75 333 sdi->conn = sr_usb_dev_inst_new(
428edbe1
BV
334 libusb_get_bus_number(devlist[i]),
335 libusb_get_device_address(devlist[i]), NULL);
336 devcnt++;
337
a1bb33af
UH
338 }
339 libusb_free_device_list(devlist, 1);
340
4ca38984 341 return devices;
a1bb33af
UH
342}
343
6078d2c9 344static GSList *dev_list(void)
811deee4 345{
0e94d524 346 return ((struct drv_context *)(di->priv))->instances;
811deee4
BV
347}
348
6078d2c9 349static int dev_open(struct sr_dev_inst *sdi)
a1bb33af 350{
310e9e9b 351 struct dev_context *devc;
e495a676 352 struct drv_context *drvc;
609bfd75 353 struct sr_usb_dev_inst *usb;
428edbe1
BV
354 libusb_device **devlist, *dev;
355 struct libusb_device_descriptor des;
356 int device_count, ret, i;
a1bb33af 357
e495a676 358 drvc = di->priv;
609bfd75 359 usb = sdi->conn;
e495a676 360
310e9e9b 361 if (!(devc = sdi->priv)) {
6d116114 362 sr_err("%s: sdi->priv was NULL", __func__);
bf43ea23
UH
363 return SR_ERR_ARG;
364 }
365
d4abb463
PS
366 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx,
367 &devlist);
428edbe1 368 if (device_count < 0) {
6d116114 369 sr_err("Failed to retrieve device list.");
428edbe1
BV
370 return SR_ERR;
371 }
372
373 dev = NULL;
374 for (i = 0; i < device_count; i++) {
375 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
6d116114 376 sr_err("Failed to get device descriptor: %s.",
d4928d71 377 libusb_error_name(ret));
428edbe1
BV
378 continue;
379 }
609bfd75
BV
380 if (libusb_get_bus_number(devlist[i]) == usb->bus
381 && libusb_get_device_address(devlist[i]) == usb->address) {
428edbe1
BV
382 dev = devlist[i];
383 break;
384 }
385 }
386 if (!dev) {
6d116114 387 sr_err("Device on bus %d address %d disappeared!",
609bfd75 388 usb->bus, usb->address);
428edbe1
BV
389 return SR_ERR;
390 }
391
609bfd75 392 if (!(ret = libusb_open(dev, &(usb->devhdl)))) {
428edbe1 393 sdi->status = SR_ST_ACTIVE;
6d116114 394 sr_info("Opened device %d on %d.%d interface %d.",
609bfd75 395 sdi->index, usb->bus, usb->address, USB_INTERFACE);
428edbe1 396 } else {
6d116114 397 sr_err("Failed to open device: %s.", libusb_error_name(ret));
428edbe1
BV
398 return SR_ERR;
399 }
400
609bfd75 401 ret = libusb_set_configuration(usb->devhdl, USB_CONFIGURATION);
ebc34738 402 if (ret < 0) {
6d116114 403 sr_err("Unable to set USB configuration %d: %s.",
d4928d71 404 USB_CONFIGURATION, libusb_error_name(ret));
185ae2c5
UH
405 return SR_ERR;
406 }
407
609bfd75 408 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
ebc34738 409 if (ret != 0) {
6d116114 410 sr_err("Unable to claim interface: %s.",
d4928d71 411 libusb_error_name(ret));
e46b8fb1 412 return SR_ERR;
a1bb33af 413 }
185ae2c5 414
e495a676 415 /* Set default configuration after power on. */
609bfd75
BV
416 if (analyzer_read_status(usb->devhdl) == 0)
417 analyzer_configure(usb->devhdl);
0ab0cb94 418
609bfd75
BV
419 analyzer_reset(usb->devhdl);
420 analyzer_initialize(usb->devhdl);
a1bb33af 421
0ab0cb94 422 //analyzer_set_memory_size(MEMORY_SIZE_512K);
fed16f06 423 // analyzer_set_freq(g_freq, g_freq_scale);
a1bb33af 424 analyzer_set_trigger_count(1);
408e7199
UH
425 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
426 // * get_memory_size(g_memory_size)) / 100) >> 2);
a1bb33af 427
fed16f06
UH
428#if 0
429 if (g_double_mode == 1)
a1bb33af
UH
430 analyzer_set_compression(COMPRESSION_DOUBLE);
431 else if (g_compression == 1)
432 analyzer_set_compression(COMPRESSION_ENABLE);
fed16f06
UH
433 else
434#endif
435 analyzer_set_compression(COMPRESSION_NONE);
a1bb33af 436
310e9e9b 437 if (devc->cur_samplerate == 0) {
0ab0cb94
TY
438 /* Samplerate hasn't been set. Default to 1MHz. */
439 analyzer_set_freq(1, FREQ_SCALE_MHZ);
440 devc->cur_samplerate = SR_MHZ(1);
a1bb33af
UH
441 }
442
7142d6b9
RD
443 if (devc->cur_threshold == 0)
444 set_voltage_threshold(devc, 1.5);
445
e46b8fb1 446 return SR_OK;
a1bb33af
UH
447}
448
6078d2c9 449static int dev_close(struct sr_dev_inst *sdi)
a1bb33af 450{
609bfd75 451 struct sr_usb_dev_inst *usb;
a1bb33af 452
609bfd75 453 usb = sdi->conn;
697785d1 454
609bfd75 455 if (!usb->devhdl)
25a0f108
BV
456 return SR_ERR;
457
6d116114 458 sr_info("Closing device %d on %d.%d interface %d.", sdi->index,
609bfd75
BV
459 usb->bus, usb->address, USB_INTERFACE);
460 libusb_release_interface(usb->devhdl, USB_INTERFACE);
461 libusb_reset_device(usb->devhdl);
462 libusb_close(usb->devhdl);
463 usb->devhdl = NULL;
25a0f108 464 sdi->status = SR_ST_INACTIVE;
697785d1
UH
465
466 return SR_OK;
a1bb33af
UH
467}
468
6078d2c9 469static int cleanup(void)
a1bb33af 470{
3b412e3a 471 return dev_clear();
a1bb33af
UH
472}
473
8f996b89
ML
474static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
475 const struct sr_probe_group *probe_group)
a1bb33af 476{
310e9e9b 477 struct dev_context *devc;
a1bb33af 478
8f996b89
ML
479 (void)probe_group;
480
035a1078 481 switch (id) {
123e1313 482 case SR_CONF_SAMPLERATE:
626409ab 483 if (sdi) {
310e9e9b 484 devc = sdi->priv;
8386096f 485 *data = g_variant_new_uint64(devc->cur_samplerate);
6d116114
UH
486 sr_spew("Returning samplerate: %" PRIu64 "Hz.",
487 devc->cur_samplerate);
626409ab 488 } else
67055d4c 489 return SR_ERR_ARG;
bf43ea23 490 break;
05f853b5
RD
491 case SR_CONF_CAPTURE_RATIO:
492 if (sdi) {
493 devc = sdi->priv;
494 *data = g_variant_new_uint64(devc->capture_ratio);
495 } else
67055d4c 496 return SR_ERR_ARG;
05f853b5 497 break;
7142d6b9
RD
498 case SR_CONF_VOLTAGE_THRESHOLD:
499 if (sdi) {
500 GVariant *range[2];
501 devc = sdi->priv;
502 range[0] = g_variant_new_double(devc->cur_threshold);
503 range[1] = g_variant_new_double(devc->cur_threshold);
504 *data = g_variant_new_tuple(range, 2);
67055d4c
BV
505 } else
506 return SR_ERR_ARG;
507 break;
508 case SR_CONF_MAX_UNCOMPRESSED_SAMPLES:
509 if (sdi) {
510 /* As long as this driver doesn't support compression,
511 * this is ok. When compression is enabled, this should
512 * return SR_ERR_NA instead. */
513 devc = sdi->priv;
514 *data = g_variant_new_uint64(devc->max_sample_depth);
7142d6b9
RD
515 } else
516 return SR_ERR;
517 break;
bf43ea23 518 default:
bd6fbf62 519 return SR_ERR_NA;
a1bb33af
UH
520 }
521
626409ab 522 return SR_OK;
a1bb33af
UH
523}
524
8f996b89
ML
525static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
526 const struct sr_probe_group *probe_group)
a1bb33af 527{
310e9e9b 528 struct dev_context *devc;
7142d6b9 529 gdouble low, high;
a1bb33af 530
8f996b89
ML
531 (void)probe_group;
532
e73ffd42
BV
533 if (sdi->status != SR_ST_ACTIVE)
534 return SR_ERR_DEV_CLOSED;
0ab0cb94 535
310e9e9b 536 if (!(devc = sdi->priv)) {
6d116114 537 sr_err("%s: sdi->priv was NULL", __func__);
bf43ea23
UH
538 return SR_ERR_ARG;
539 }
a1bb33af 540
035a1078 541 switch (id) {
1953564a 542 case SR_CONF_SAMPLERATE:
8386096f 543 return zp_set_samplerate(devc, g_variant_get_uint64(data));
1953564a 544 case SR_CONF_LIMIT_SAMPLES:
8386096f 545 return set_limit_samples(devc, g_variant_get_uint64(data));
1953564a 546 case SR_CONF_CAPTURE_RATIO:
8386096f 547 return set_capture_ratio(devc, g_variant_get_uint64(data));
7142d6b9
RD
548 case SR_CONF_VOLTAGE_THRESHOLD:
549 g_variant_get(data, "(dd)", &low, &high);
550 return set_voltage_threshold(devc, (low + high) / 2.0);
fed16f06 551 default:
bd6fbf62 552 return SR_ERR_NA;
a1bb33af 553 }
e495a676
UH
554
555 return SR_OK;
a1bb33af
UH
556}
557
8f996b89
ML
558static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
559 const struct sr_probe_group *probe_group)
a1c743fc 560{
17548571 561 struct dev_context *devc;
8386096f
BV
562 GVariant *gvar;
563 GVariantBuilder gvb;
7142d6b9
RD
564 double v;
565 GVariant *range[2];
a1c743fc 566
8f996b89
ML
567 (void)probe_group;
568
a1c743fc 569 switch (key) {
9a6517d1 570 case SR_CONF_DEVICE_OPTIONS:
8386096f
BV
571 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
572 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
9a6517d1 573 break;
a1c743fc 574 case SR_CONF_SAMPLERATE:
17548571 575 devc = sdi->priv;
8386096f 576 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
17548571 577 if (devc->prof->max_sampling_freq == 100) {
8386096f
BV
578 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
579 samplerates_100, ARRAY_SIZE(samplerates_100),
580 sizeof(uint64_t));
17548571 581 } else if (devc->prof->max_sampling_freq == 200) {
8386096f
BV
582 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
583 samplerates_200, ARRAY_SIZE(samplerates_200),
584 sizeof(uint64_t));
17548571
UH
585 } else {
586 sr_err("Internal error: Unknown max. samplerate: %d.",
587 devc->prof->max_sampling_freq);
588 return SR_ERR_ARG;
589 }
8386096f
BV
590 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
591 *data = g_variant_builder_end(&gvb);
a1c743fc 592 break;
c50277a6 593 case SR_CONF_TRIGGER_TYPE:
8386096f 594 *data = g_variant_new_string(TRIGGER_TYPE);
c50277a6 595 break;
7142d6b9
RD
596 case SR_CONF_VOLTAGE_THRESHOLD:
597 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
598 for (v = -6.0; v <= 6.0; v += 0.1) {
599 range[0] = g_variant_new_double(v);
600 range[1] = g_variant_new_double(v);
601 gvar = g_variant_new_tuple(range, 2);
602 g_variant_builder_add_value(&gvb, gvar);
603 }
604 *data = g_variant_builder_end(&gvb);
605 break;
a1c743fc 606 default:
bd6fbf62 607 return SR_ERR_NA;
a1c743fc
BV
608 }
609
610 return SR_OK;
611}
612
6078d2c9 613static int dev_acquisition_start(const struct sr_dev_inst *sdi,
3ffb6964 614 void *cb_data)
a1bb33af 615{
609bfd75
BV
616 struct dev_context *devc;
617 struct sr_usb_dev_inst *usb;
b9c735a2 618 struct sr_datafeed_packet packet;
9c939c51 619 struct sr_datafeed_logic logic;
42ceb777 620 unsigned int samples_read;
a1bb33af 621 int res;
e495a676 622 unsigned int packet_num, n;
a1bb33af 623 unsigned char *buf;
42ceb777
RD
624 unsigned int status;
625 unsigned int stop_address;
626 unsigned int now_address;
627 unsigned int trigger_address;
628 unsigned int trigger_offset;
629 unsigned int triggerbar;
630 unsigned int ramsize_trigger;
631 unsigned int memory_size;
632 unsigned int valid_samples;
633 unsigned int discard;
634 int trigger_now;
a1bb33af 635
e73ffd42
BV
636 if (sdi->status != SR_ST_ACTIVE)
637 return SR_ERR_DEV_CLOSED;
638
310e9e9b 639 if (!(devc = sdi->priv)) {
6d116114 640 sr_err("%s: sdi->priv was NULL", __func__);
bf43ea23
UH
641 return SR_ERR_ARG;
642 }
a1bb33af 643
014359e3 644 if (configure_probes(sdi) != SR_OK) {
6d116114 645 sr_err("Failed to configure probes.");
014359e3
BV
646 return SR_ERR;
647 }
648
609bfd75
BV
649 usb = sdi->conn;
650
0ab0cb94
TY
651 set_triggerbar(devc);
652
e495a676 653 /* Push configured settings to device. */
609bfd75 654 analyzer_configure(usb->devhdl);
a143e4e5 655
609bfd75 656 analyzer_start(usb->devhdl);
6d116114 657 sr_info("Waiting for data.");
609bfd75 658 analyzer_wait_data(usb->devhdl);
a1bb33af 659
42ceb777
RD
660 status = analyzer_read_status(usb->devhdl);
661 stop_address = analyzer_get_stop_address(usb->devhdl);
662 now_address = analyzer_get_now_address(usb->devhdl);
663 trigger_address = analyzer_get_trigger_address(usb->devhdl);
664
665 triggerbar = analyzer_get_triggerbar_address();
666 ramsize_trigger = analyzer_get_ramsize_trigger_address();
667
668 n = get_memory_size(devc->memory_size);
669 memory_size = n / 4;
670
671 sr_info("Status = 0x%x.", status);
672 sr_info("Stop address = 0x%x.", stop_address);
673 sr_info("Now address = 0x%x.", now_address);
674 sr_info("Trigger address = 0x%x.", trigger_address);
675 sr_info("Triggerbar address = 0x%x.", triggerbar);
676 sr_info("Ramsize trigger = 0x%x.", ramsize_trigger);
677 sr_info("Memory size = 0x%x.", memory_size);
a1bb33af 678
4afdfd46 679 /* Send header packet to the session bus. */
29a27196 680 std_session_send_df_header(cb_data, LOG_PREFIX);
f366e86c 681
42ceb777
RD
682 /* Check for empty capture */
683 if ((status & STATUS_READY) && !stop_address) {
684 packet.type = SR_DF_END;
685 sr_session_send(cb_data, &packet);
686 return SR_OK;
687 }
688
b53738ba 689 if (!(buf = g_try_malloc(PACKET_SIZE))) {
6d116114 690 sr_err("Packet buffer malloc failed.");
b53738ba
UH
691 return SR_ERR_MALLOC;
692 }
693
42ceb777
RD
694 /* Check if the trigger is in the samples we are throwing away */
695 trigger_now = now_address == trigger_address ||
696 ((now_address + 1) % memory_size) == trigger_address;
697
698 /*
699 * STATUS_READY doesn't clear until now_address advances past
700 * addr 0, but for our logic, clear it in that case
701 */
702 if (!now_address)
703 status &= ~STATUS_READY;
704
609bfd75 705 analyzer_read_start(usb->devhdl);
42ceb777
RD
706
707 /* Calculate how much data to discard */
708 discard = 0;
709 if (status & STATUS_READY) {
710 /*
711 * We haven't wrapped around, we need to throw away data from
712 * our current position to the end of the buffer.
713 * Additionally, the first two samples captured are always
714 * bogus.
715 */
716 discard += memory_size - now_address + 2;
717 now_address = 2;
718 }
719
720 /* If we have more samples than we need, discard them */
721 valid_samples = (stop_address - now_address) % memory_size;
722 if (valid_samples > ramsize_trigger + triggerbar) {
723 discard += valid_samples - (ramsize_trigger + triggerbar);
724 now_address += valid_samples - (ramsize_trigger + triggerbar);
725 }
726
727 sr_info("Need to discard %d samples.", discard);
728
729 /* Calculate how far in the trigger is */
730 if (trigger_now)
731 trigger_offset = 0;
732 else
733 trigger_offset = (trigger_address - now_address) % memory_size;
734
735 /* Recalculate the number of samples available */
736 valid_samples = (stop_address - now_address) % memory_size;
737
fed16f06 738 /* Send the incoming transfer to the session bus. */
42ceb777 739 samples_read = 0;
0ab0cb94 740 for (packet_num = 0; packet_num < n / PACKET_SIZE; packet_num++) {
42ceb777
RD
741 unsigned int len;
742 unsigned int buf_offset;
743
609bfd75 744 res = analyzer_read_data(usb->devhdl, buf, PACKET_SIZE);
6d116114 745 sr_info("Tried to read %d bytes, actually read %d bytes.",
b08024a8 746 PACKET_SIZE, res);
a1bb33af 747
42ceb777
RD
748 if (discard >= PACKET_SIZE / 4) {
749 discard -= PACKET_SIZE / 4;
750 continue;
751 }
752
753 len = PACKET_SIZE - discard * 4;
754 buf_offset = discard * 4;
755 discard = 0;
756
757 /* Check if we've read all the samples */
758 if (samples_read + len / 4 >= valid_samples)
759 len = (valid_samples - samples_read) * 4;
760 if (!len)
761 break;
762
763 if (samples_read < trigger_offset &&
764 samples_read + len / 4 > trigger_offset) {
765 /* Send out samples remaining before trigger */
766 packet.type = SR_DF_LOGIC;
767 packet.payload = &logic;
768 logic.length = (trigger_offset - samples_read) * 4;
769 logic.unitsize = 4;
770 logic.data = buf + buf_offset;
771 sr_session_send(cb_data, &packet);
772 len -= logic.length;
773 samples_read += logic.length / 4;
774 buf_offset += logic.length;
775 }
776
777 if (samples_read == trigger_offset) {
778 /* Send out trigger */
779 packet.type = SR_DF_TRIGGER;
780 packet.payload = NULL;
781 sr_session_send(cb_data, &packet);
782 }
783
784 /* Send out data (or data after trigger) */
5a2326a7 785 packet.type = SR_DF_LOGIC;
9c939c51 786 packet.payload = &logic;
42ceb777 787 logic.length = len;
9c939c51 788 logic.unitsize = 4;
42ceb777 789 logic.data = buf + buf_offset;
3cd3a20b 790 sr_session_send(cb_data, &packet);
42ceb777 791 samples_read += len / 4;
a1bb33af 792 }
609bfd75 793 analyzer_read_stop(usb->devhdl);
a1bb33af
UH
794 g_free(buf);
795
5a2326a7 796 packet.type = SR_DF_END;
3cd3a20b 797 sr_session_send(cb_data, &packet);
a1bb33af 798
e46b8fb1 799 return SR_OK;
a1bb33af
UH
800}
801
3cd3a20b 802/* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
6078d2c9 803static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
a1bb33af 804{
310e9e9b 805 struct dev_context *devc;
609bfd75
BV
806 struct sr_usb_dev_inst *usb;
807 struct sr_datafeed_packet packet;
a1bb33af 808
5a2326a7 809 packet.type = SR_DF_END;
3cd3a20b 810 sr_session_send(cb_data, &packet);
a1bb33af 811
310e9e9b 812 if (!(devc = sdi->priv)) {
6d116114 813 sr_err("%s: sdi->priv was NULL", __func__);
3010f21c 814 return SR_ERR_BUG;
69890f73 815 }
a1bb33af 816
609bfd75
BV
817 usb = sdi->conn;
818 analyzer_reset(usb->devhdl);
fed16f06 819 /* TODO: Need to cancel and free any queued up transfers. */
3010f21c
UH
820
821 return SR_OK;
a1bb33af
UH
822}
823
c09f0b57 824SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
e519ba86 825 .name = "zeroplus-logic-cube",
8fdecced 826 .longname = "ZEROPLUS Logic Cube LAP-C series",
e519ba86 827 .api_version = 1,
6078d2c9
UH
828 .init = init,
829 .cleanup = cleanup,
830 .scan = scan,
831 .dev_list = dev_list,
3b412e3a 832 .dev_clear = dev_clear,
035a1078
BV
833 .config_get = config_get,
834 .config_set = config_set,
a1c743fc 835 .config_list = config_list,
6078d2c9
UH
836 .dev_open = dev_open,
837 .dev_close = dev_close,
838 .dev_acquisition_start = dev_acquisition_start,
839 .dev_acquisition_stop = dev_acquisition_stop,
310e9e9b 840 .priv = NULL,
a1bb33af 841};