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