]> sigrok.org Git - libsigrok.git/blame - hardware/zeroplus-logic-cube/api.c
zeroplus: Just wait until not busy on data capture
[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},
60cc6f85
RD
48 /* We don't yet support 32 probes */
49 {0x0c12, 0x700b, "LAP-C(32128)", 16, 128, 200},
50 {0x0c12, 0x700c, "LAP-C(321000)", 16, 1024, 200},
51 {0x0c12, 0x700d, "LAP-C(322000)", 16, 2048, 200},
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
6078d2c9 122static int 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
3b412e3a 240static int dev_clear(void)
7da6f9d5 241{
d6445cb0 242 return std_dev_clear(di, NULL);
7da6f9d5
BV
243}
244
6078d2c9 245static int init(struct sr_context *sr_ctx)
61136ea6 246{
f6beaac5 247 return std_init(sr_ctx, di, LOG_PREFIX);
61136ea6
BV
248}
249
6078d2c9 250static GSList *scan(GSList *options)
a1bb33af 251{
d68e2d1a 252 struct sr_dev_inst *sdi;
428edbe1 253 struct sr_probe *probe;
310e9e9b
BV
254 struct drv_context *drvc;
255 struct dev_context *devc;
e495a676 256 const struct zp_model *prof;
a1bb33af
UH
257 struct libusb_device_descriptor des;
258 libusb_device **devlist;
428edbe1
BV
259 GSList *devices;
260 int ret, devcnt, i, j;
a1bb33af 261
4ca38984 262 (void)options;
64d33dc2 263
a873c594 264 drvc = di->priv;
4b97c74e 265
4ca38984
BV
266 devices = NULL;
267
8fdecced 268 /* Find all ZEROPLUS analyzers and add them to device list. */
a1bb33af 269 devcnt = 0;
d4abb463 270 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); /* TODO: Errors. */
fed16f06
UH
271
272 for (i = 0; devlist[i]; i++) {
ebc34738
UH
273 ret = libusb_get_device_descriptor(devlist[i], &des);
274 if (ret != 0) {
6d116114 275 sr_err("Failed to get device descriptor: %s.",
d4928d71 276 libusb_error_name(ret));
a1bb33af
UH
277 continue;
278 }
279
428edbe1
BV
280 prof = NULL;
281 for (j = 0; j < zeroplus_models[j].vid; j++) {
282 if (des.idVendor == zeroplus_models[j].vid &&
283 des.idProduct == zeroplus_models[j].pid) {
284 prof = &zeroplus_models[j];
bf43ea23 285 }
a1bb33af 286 }
e495a676 287 /* Skip if the device was not found. */
428edbe1
BV
288 if (!prof)
289 continue;
e495a676 290 sr_info("Found ZEROPLUS %s.", prof->model_name);
428edbe1
BV
291
292 /* Register the device with libsigrok. */
293 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
294 VENDOR_NAME, prof->model_name, NULL))) {
6d116114 295 sr_err("%s: sr_dev_inst_new failed", __func__);
428edbe1
BV
296 return NULL;
297 }
a873c594 298 sdi->driver = di;
428edbe1
BV
299
300 /* Allocate memory for our private driver context. */
310e9e9b 301 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
6d116114 302 sr_err("Device context malloc failed.");
886a52b6 303 return NULL;
428edbe1 304 }
e495a676 305
310e9e9b 306 sdi->priv = devc;
17548571 307 devc->prof = prof;
310e9e9b 308 devc->num_channels = prof->channels;
0ab0cb94
TY
309#ifdef ZP_EXPERIMENTAL
310 devc->max_memory_size = 128 * 1024;
311 devc->max_samplerate = 200;
312#else
313 devc->max_memory_size = prof->sample_depth * 1024;
314 devc->max_samplerate = prof->max_sampling_freq;
315#endif
316 devc->max_samplerate *= SR_MHZ(1);
317 devc->memory_size = MEMORY_SIZE_8K;
310e9e9b 318 // memset(devc->trigger_buffer, 0, NUM_TRIGGER_STAGES);
428edbe1
BV
319
320 /* Fill in probelist according to this device's profile. */
310e9e9b 321 for (j = 0; j < devc->num_channels; j++) {
428edbe1
BV
322 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
323 probe_names[j])))
324 return NULL;
325 sdi->probes = g_slist_append(sdi->probes, probe);
326 }
327
328 devices = g_slist_append(devices, sdi);
310e9e9b 329 drvc->instances = g_slist_append(drvc->instances, sdi);
8111446a 330 sdi->inst_type = SR_INST_USB;
609bfd75 331 sdi->conn = sr_usb_dev_inst_new(
428edbe1
BV
332 libusb_get_bus_number(devlist[i]),
333 libusb_get_device_address(devlist[i]), NULL);
334 devcnt++;
335
a1bb33af
UH
336 }
337 libusb_free_device_list(devlist, 1);
338
4ca38984 339 return devices;
a1bb33af
UH
340}
341
6078d2c9 342static GSList *dev_list(void)
811deee4 343{
0e94d524 344 return ((struct drv_context *)(di->priv))->instances;
811deee4
BV
345}
346
6078d2c9 347static int dev_open(struct sr_dev_inst *sdi)
a1bb33af 348{
310e9e9b 349 struct dev_context *devc;
e495a676 350 struct drv_context *drvc;
609bfd75 351 struct sr_usb_dev_inst *usb;
428edbe1
BV
352 libusb_device **devlist, *dev;
353 struct libusb_device_descriptor des;
354 int device_count, ret, i;
a1bb33af 355
e495a676 356 drvc = di->priv;
609bfd75 357 usb = sdi->conn;
e495a676 358
310e9e9b 359 if (!(devc = sdi->priv)) {
6d116114 360 sr_err("%s: sdi->priv was NULL", __func__);
bf43ea23
UH
361 return SR_ERR_ARG;
362 }
363
d4abb463
PS
364 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx,
365 &devlist);
428edbe1 366 if (device_count < 0) {
6d116114 367 sr_err("Failed to retrieve device list.");
428edbe1
BV
368 return SR_ERR;
369 }
370
371 dev = NULL;
372 for (i = 0; i < device_count; i++) {
373 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
6d116114 374 sr_err("Failed to get device descriptor: %s.",
d4928d71 375 libusb_error_name(ret));
428edbe1
BV
376 continue;
377 }
609bfd75
BV
378 if (libusb_get_bus_number(devlist[i]) == usb->bus
379 && libusb_get_device_address(devlist[i]) == usb->address) {
428edbe1
BV
380 dev = devlist[i];
381 break;
382 }
383 }
384 if (!dev) {
6d116114 385 sr_err("Device on bus %d address %d disappeared!",
609bfd75 386 usb->bus, usb->address);
428edbe1
BV
387 return SR_ERR;
388 }
389
609bfd75 390 if (!(ret = libusb_open(dev, &(usb->devhdl)))) {
428edbe1 391 sdi->status = SR_ST_ACTIVE;
6d116114 392 sr_info("Opened device %d on %d.%d interface %d.",
609bfd75 393 sdi->index, usb->bus, usb->address, USB_INTERFACE);
428edbe1 394 } else {
6d116114 395 sr_err("Failed to open device: %s.", libusb_error_name(ret));
428edbe1
BV
396 return SR_ERR;
397 }
398
609bfd75 399 ret = libusb_set_configuration(usb->devhdl, USB_CONFIGURATION);
ebc34738 400 if (ret < 0) {
6d116114 401 sr_err("Unable to set USB configuration %d: %s.",
d4928d71 402 USB_CONFIGURATION, libusb_error_name(ret));
185ae2c5
UH
403 return SR_ERR;
404 }
405
609bfd75 406 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
ebc34738 407 if (ret != 0) {
6d116114 408 sr_err("Unable to claim interface: %s.",
d4928d71 409 libusb_error_name(ret));
e46b8fb1 410 return SR_ERR;
a1bb33af 411 }
185ae2c5 412
e495a676 413 /* Set default configuration after power on. */
609bfd75
BV
414 if (analyzer_read_status(usb->devhdl) == 0)
415 analyzer_configure(usb->devhdl);
0ab0cb94 416
609bfd75
BV
417 analyzer_reset(usb->devhdl);
418 analyzer_initialize(usb->devhdl);
a1bb33af 419
0ab0cb94 420 //analyzer_set_memory_size(MEMORY_SIZE_512K);
fed16f06 421 // analyzer_set_freq(g_freq, g_freq_scale);
a1bb33af 422 analyzer_set_trigger_count(1);
408e7199
UH
423 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
424 // * get_memory_size(g_memory_size)) / 100) >> 2);
a1bb33af 425
fed16f06
UH
426#if 0
427 if (g_double_mode == 1)
a1bb33af
UH
428 analyzer_set_compression(COMPRESSION_DOUBLE);
429 else if (g_compression == 1)
430 analyzer_set_compression(COMPRESSION_ENABLE);
fed16f06
UH
431 else
432#endif
433 analyzer_set_compression(COMPRESSION_NONE);
a1bb33af 434
310e9e9b 435 if (devc->cur_samplerate == 0) {
0ab0cb94
TY
436 /* Samplerate hasn't been set. Default to 1MHz. */
437 analyzer_set_freq(1, FREQ_SCALE_MHZ);
438 devc->cur_samplerate = SR_MHZ(1);
a1bb33af
UH
439 }
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{
3b412e3a 466 return dev_clear();
a1bb33af
UH
467}
468
8f996b89
ML
469static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
470 const struct sr_probe_group *probe_group)
a1bb33af 471{
310e9e9b 472 struct dev_context *devc;
a1bb33af 473
8f996b89
ML
474 (void)probe_group;
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
BV
483 } else
484 return SR_ERR;
bf43ea23
UH
485 break;
486 default:
bd6fbf62 487 return SR_ERR_NA;
a1bb33af
UH
488 }
489
626409ab 490 return SR_OK;
a1bb33af
UH
491}
492
8f996b89
ML
493static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
494 const struct sr_probe_group *probe_group)
a1bb33af 495{
310e9e9b 496 struct dev_context *devc;
a1bb33af 497
8f996b89
ML
498 (void)probe_group;
499
e73ffd42
BV
500 if (sdi->status != SR_ST_ACTIVE)
501 return SR_ERR_DEV_CLOSED;
0ab0cb94 502
310e9e9b 503 if (!(devc = sdi->priv)) {
6d116114 504 sr_err("%s: sdi->priv was NULL", __func__);
bf43ea23
UH
505 return SR_ERR_ARG;
506 }
a1bb33af 507
035a1078 508 switch (id) {
1953564a 509 case SR_CONF_SAMPLERATE:
8386096f 510 return zp_set_samplerate(devc, g_variant_get_uint64(data));
1953564a 511 case SR_CONF_LIMIT_SAMPLES:
8386096f 512 return set_limit_samples(devc, g_variant_get_uint64(data));
1953564a 513 case SR_CONF_CAPTURE_RATIO:
8386096f 514 return set_capture_ratio(devc, g_variant_get_uint64(data));
fed16f06 515 default:
bd6fbf62 516 return SR_ERR_NA;
a1bb33af 517 }
e495a676
UH
518
519 return SR_OK;
a1bb33af
UH
520}
521
8f996b89
ML
522static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
523 const struct sr_probe_group *probe_group)
a1c743fc 524{
17548571 525 struct dev_context *devc;
8386096f
BV
526 GVariant *gvar;
527 GVariantBuilder gvb;
a1c743fc 528
8f996b89
ML
529 (void)probe_group;
530
a1c743fc 531 switch (key) {
9a6517d1 532 case SR_CONF_DEVICE_OPTIONS:
8386096f
BV
533 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
534 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
9a6517d1 535 break;
a1c743fc 536 case SR_CONF_SAMPLERATE:
17548571 537 devc = sdi->priv;
8386096f 538 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
17548571 539 if (devc->prof->max_sampling_freq == 100) {
8386096f
BV
540 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
541 samplerates_100, ARRAY_SIZE(samplerates_100),
542 sizeof(uint64_t));
17548571 543 } else if (devc->prof->max_sampling_freq == 200) {
8386096f
BV
544 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
545 samplerates_200, ARRAY_SIZE(samplerates_200),
546 sizeof(uint64_t));
17548571
UH
547 } else {
548 sr_err("Internal error: Unknown max. samplerate: %d.",
549 devc->prof->max_sampling_freq);
550 return SR_ERR_ARG;
551 }
8386096f
BV
552 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
553 *data = g_variant_builder_end(&gvb);
a1c743fc 554 break;
c50277a6 555 case SR_CONF_TRIGGER_TYPE:
8386096f 556 *data = g_variant_new_string(TRIGGER_TYPE);
c50277a6 557 break;
a1c743fc 558 default:
bd6fbf62 559 return SR_ERR_NA;
a1c743fc
BV
560 }
561
562 return SR_OK;
563}
564
6078d2c9 565static int dev_acquisition_start(const struct sr_dev_inst *sdi,
3ffb6964 566 void *cb_data)
a1bb33af 567{
609bfd75
BV
568 struct dev_context *devc;
569 struct sr_usb_dev_inst *usb;
b9c735a2 570 struct sr_datafeed_packet packet;
9c939c51 571 struct sr_datafeed_logic logic;
0ab0cb94 572 //uint64_t samples_read;
a1bb33af 573 int res;
e495a676 574 unsigned int packet_num, n;
a1bb33af
UH
575 unsigned char *buf;
576
e73ffd42
BV
577 if (sdi->status != SR_ST_ACTIVE)
578 return SR_ERR_DEV_CLOSED;
579
310e9e9b 580 if (!(devc = sdi->priv)) {
6d116114 581 sr_err("%s: sdi->priv was NULL", __func__);
bf43ea23
UH
582 return SR_ERR_ARG;
583 }
a1bb33af 584
014359e3 585 if (configure_probes(sdi) != SR_OK) {
6d116114 586 sr_err("Failed to configure probes.");
014359e3
BV
587 return SR_ERR;
588 }
589
609bfd75
BV
590 usb = sdi->conn;
591
0ab0cb94
TY
592 set_triggerbar(devc);
593
e495a676 594 /* Push configured settings to device. */
609bfd75 595 analyzer_configure(usb->devhdl);
a143e4e5 596
609bfd75 597 analyzer_start(usb->devhdl);
6d116114 598 sr_info("Waiting for data.");
609bfd75 599 analyzer_wait_data(usb->devhdl);
a1bb33af 600
6d116114 601 sr_info("Stop address = 0x%x.",
609bfd75 602 analyzer_get_stop_address(usb->devhdl));
6d116114 603 sr_info("Now address = 0x%x.",
609bfd75 604 analyzer_get_now_address(usb->devhdl));
6d116114 605 sr_info("Trigger address = 0x%x.",
609bfd75 606 analyzer_get_trigger_address(usb->devhdl));
a1bb33af 607
4afdfd46 608 /* Send header packet to the session bus. */
29a27196 609 std_session_send_df_header(cb_data, LOG_PREFIX);
f366e86c 610
b53738ba 611 if (!(buf = g_try_malloc(PACKET_SIZE))) {
6d116114 612 sr_err("Packet buffer malloc failed.");
b53738ba
UH
613 return SR_ERR_MALLOC;
614 }
615
0ab0cb94 616 //samples_read = 0;
609bfd75 617 analyzer_read_start(usb->devhdl);
fed16f06 618 /* Send the incoming transfer to the session bus. */
0ab0cb94
TY
619 n = get_memory_size(devc->memory_size);
620 if (devc->max_memory_size * 4 < n)
621 n = devc->max_memory_size * 4;
622 for (packet_num = 0; packet_num < n / PACKET_SIZE; packet_num++) {
609bfd75 623 res = analyzer_read_data(usb->devhdl, buf, PACKET_SIZE);
6d116114 624 sr_info("Tried to read %d bytes, actually read %d bytes.",
b08024a8 625 PACKET_SIZE, res);
a1bb33af 626
5a2326a7 627 packet.type = SR_DF_LOGIC;
9c939c51
BV
628 packet.payload = &logic;
629 logic.length = PACKET_SIZE;
630 logic.unitsize = 4;
631 logic.data = buf;
3cd3a20b 632 sr_session_send(cb_data, &packet);
0ab0cb94 633 //samples_read += res / 4;
a1bb33af 634 }
609bfd75 635 analyzer_read_stop(usb->devhdl);
a1bb33af
UH
636 g_free(buf);
637
5a2326a7 638 packet.type = SR_DF_END;
3cd3a20b 639 sr_session_send(cb_data, &packet);
a1bb33af 640
e46b8fb1 641 return SR_OK;
a1bb33af
UH
642}
643
3cd3a20b 644/* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
6078d2c9 645static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
a1bb33af 646{
310e9e9b 647 struct dev_context *devc;
609bfd75
BV
648 struct sr_usb_dev_inst *usb;
649 struct sr_datafeed_packet packet;
a1bb33af 650
5a2326a7 651 packet.type = SR_DF_END;
3cd3a20b 652 sr_session_send(cb_data, &packet);
a1bb33af 653
310e9e9b 654 if (!(devc = sdi->priv)) {
6d116114 655 sr_err("%s: sdi->priv was NULL", __func__);
3010f21c 656 return SR_ERR_BUG;
69890f73 657 }
a1bb33af 658
609bfd75
BV
659 usb = sdi->conn;
660 analyzer_reset(usb->devhdl);
fed16f06 661 /* TODO: Need to cancel and free any queued up transfers. */
3010f21c
UH
662
663 return SR_OK;
a1bb33af
UH
664}
665
c09f0b57 666SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
e519ba86 667 .name = "zeroplus-logic-cube",
8fdecced 668 .longname = "ZEROPLUS Logic Cube LAP-C series",
e519ba86 669 .api_version = 1,
6078d2c9
UH
670 .init = init,
671 .cleanup = cleanup,
672 .scan = scan,
673 .dev_list = dev_list,
3b412e3a 674 .dev_clear = dev_clear,
035a1078
BV
675 .config_get = config_get,
676 .config_set = config_set,
a1c743fc 677 .config_list = config_list,
6078d2c9
UH
678 .dev_open = dev_open,
679 .dev_close = dev_close,
680 .dev_acquisition_start = dev_acquisition_start,
681 .dev_acquisition_stop = dev_acquisition_stop,
310e9e9b 682 .priv = NULL,
a1bb33af 683};