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