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