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