]> sigrok.org Git - libsigrok.git/blame - src/hardware/hantek-6xxx/api.c
zketech-ebd-usb: Don't ignore errors
[libsigrok.git] / src / hardware / hantek-6xxx / api.c
CommitLineData
6c6bc80a
C
1/*
2 * This file is part of the libsigrok project.
3 *
f2a66a8e 4 * Copyright (C) 2015 Christer Ekholm <christerekholm@gmail.com>
6c6bc80a
C
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
20#include <config.h>
1e1fdbc9 21#include <math.h>
6c6bc80a
C
22#include "protocol.h"
23
f2a66a8e
C
24/* Max time in ms before we want to check on USB events */
25#define TICK 200
26
27#define RANGE(ch) (((float)vdivs[devc->voltage[ch]][0] / vdivs[devc->voltage[ch]][1]) * VDIV_MULTIPLIER)
28
29static const uint32_t scanopts[] = {
30 SR_CONF_CONN,
31};
32
33static const uint32_t drvopts[] = {
34 SR_CONF_OSCILLOSCOPE,
35};
36
37static const uint32_t devopts[] = {
38 SR_CONF_CONN | SR_CONF_GET,
f2a66a8e
C
39 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
40 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
86621306
UH
41 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
42 SR_CONF_NUM_VDIV | SR_CONF_GET,
f2a66a8e
C
43};
44
45static const uint32_t devopts_cg[] = {
46 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
cc5ebc8a 47 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
f2a66a8e
C
48};
49
50static const char *channel_names[] = {
51 "CH1", "CH2",
52};
53
5eb4ba29 54static const char *dc_coupling[] = {
64f628bf 55 "DC",
5eb4ba29
BL
56};
57
58static const char *acdc_coupling[] = {
cc5ebc8a
BL
59 "AC", "DC",
60};
61
f2a66a8e
C
62static const struct hantek_6xxx_profile dev_profiles[] = {
63 {
2efb3cd7 64 /* Windows: "Hantek6022BE DRIVER 1": 04b4:6022 */
459324ac 65 0x04b4, 0x6022, 0x1d50, 0x608e, 0x0001,
7dc72c37 66 "Hantek", "6022BE", "fx2lafw-hantek-6022be.fw",
53012da6 67 ARRAY_AND_SIZE(dc_coupling), FALSE,
f2a66a8e 68 },
2efb3cd7
UH
69 {
70 /* Windows: "Hantek6022BE DRIVER 2": 04b5:6022 */
71 0x04b5, 0x6022, 0x1d50, 0x608e, 0x0001,
72 "Hantek", "6022BE", "fx2lafw-hantek-6022be.fw",
73 ARRAY_AND_SIZE(dc_coupling), FALSE,
74 },
692c3b22 75 {
459324ac 76 0x8102, 0x8102, 0x1d50, 0x608e, 0x0002,
7dc72c37 77 "Sainsmart", "DDS120", "fx2lafw-sainsmart-dds120.fw",
53012da6 78 ARRAY_AND_SIZE(acdc_coupling), TRUE,
692c3b22 79 },
759f2ef7 80 {
822a9c0e 81 /* Windows: "Hantek6022BL DRIVER 1": 04b4:602a */
759f2ef7
STA
82 0x04b4, 0x602a, 0x1d50, 0x608e, 0x0003,
83 "Hantek", "6022BL", "fx2lafw-hantek-6022bl.fw",
53012da6 84 ARRAY_AND_SIZE(dc_coupling), FALSE,
759f2ef7 85 },
822a9c0e
UH
86 {
87 /* Windows: "Hantek6022BL DRIVER 2": 04b5:602a */
88 0x04b5, 0x602a, 0x1d50, 0x608e, 0x0003,
89 "Hantek", "6022BL", "fx2lafw-hantek-6022bl.fw",
90 ARRAY_AND_SIZE(dc_coupling), FALSE,
91 },
e150d550 92 {
93 0xd4a2, 0x5660, 0x1d50, 0x608e, 0x0004,
94 "YiXingDianZi", "MDSO", "fx2lafw-yixingdianzi-mdso.fw",
95 ARRAY_AND_SIZE(dc_coupling), FALSE,
96 },
f2a66a8e
C
97 ALL_ZERO
98};
99
100static const uint64_t samplerates[] = {
101 SAMPLERATE_VALUES
102};
103
104static const uint64_t vdivs[][2] = {
105 VDIV_VALUES
106};
107
f2a66a8e
C
108static int read_channel(const struct sr_dev_inst *sdi, uint32_t amount);
109
15a5bfe4 110static struct sr_dev_inst *hantek_6xxx_dev_new(const struct hantek_6xxx_profile *prof)
f2a66a8e
C
111{
112 struct sr_dev_inst *sdi;
113 struct sr_channel *ch;
114 struct sr_channel_group *cg;
f2a66a8e
C
115 struct dev_context *devc;
116 unsigned int i;
117
118 sdi = g_malloc0(sizeof(struct sr_dev_inst));
119 sdi->status = SR_ST_INITIALIZING;
120 sdi->vendor = g_strdup(prof->vendor);
121 sdi->model = g_strdup(prof->model);
f2a66a8e
C
122
123 for (i = 0; i < ARRAY_SIZE(channel_names); i++) {
124 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, channel_names[i]);
125 cg = g_malloc0(sizeof(struct sr_channel_group));
126 cg->name = g_strdup(channel_names[i]);
127 cg->channels = g_slist_append(cg->channels, ch);
128 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
129 }
130
131 devc = g_malloc0(sizeof(struct dev_context));
132
133 for (i = 0; i < NUM_CHANNELS; i++) {
134 devc->ch_enabled[i] = TRUE;
135 devc->voltage[i] = DEFAULT_VOLTAGE;
cc5ebc8a 136 devc->coupling[i] = DEFAULT_COUPLING;
f2a66a8e 137 }
5eb4ba29
BL
138 devc->coupling_vals = prof->coupling_vals;
139 devc->coupling_tab_size = prof->coupling_tab_size;
364b09c2 140 devc->has_coupling = prof->has_coupling;
f2a66a8e 141
f2a66a8e
C
142 devc->profile = prof;
143 devc->dev_state = IDLE;
144 devc->samplerate = DEFAULT_SAMPLERATE;
145
146 sdi->priv = devc;
f2a66a8e
C
147
148 return sdi;
149}
150
151static int configure_channels(const struct sr_dev_inst *sdi)
152{
153 struct dev_context *devc;
154 const GSList *l;
155 int p;
156 struct sr_channel *ch;
157 devc = sdi->priv;
158
159 g_slist_free(devc->enabled_channels);
160 devc->enabled_channels = NULL;
161 memset(devc->ch_enabled, 0, sizeof(devc->ch_enabled));
162
163 for (l = sdi->channels, p = 0; l; l = l->next, p++) {
164 ch = l->data;
165 if (p < NUM_CHANNELS) {
166 devc->ch_enabled[p] = ch->enabled;
167 devc->enabled_channels = g_slist_append(devc->enabled_channels, ch);
168 }
169 }
170
171 return SR_OK;
172}
173
3553451f 174static void clear_helper(struct dev_context *devc)
f2a66a8e 175{
f2a66a8e
C
176 g_slist_free(devc->enabled_channels);
177}
178
179static int dev_clear(const struct sr_dev_driver *di)
180{
3553451f 181 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
f2a66a8e
C
182}
183
6c6bc80a
C
184static GSList *scan(struct sr_dev_driver *di, GSList *options)
185{
186 struct drv_context *drvc;
f2a66a8e
C
187 struct dev_context *devc;
188 struct sr_dev_inst *sdi;
189 struct sr_usb_dev_inst *usb;
190 struct sr_config *src;
191 const struct hantek_6xxx_profile *prof;
192 GSList *l, *devices, *conn_devices;
193 struct libusb_device_descriptor des;
194 libusb_device **devlist;
195 int i, j;
196 const char *conn;
197 char connection_id[64];
6c6bc80a 198
6c6bc80a 199 drvc = di->context;
6c6bc80a 200
f2a66a8e
C
201 devices = 0;
202
203 conn = NULL;
204 for (l = options; l; l = l->next) {
205 src = l->data;
206 if (src->key == SR_CONF_CONN) {
207 conn = g_variant_get_string(src->data, NULL);
208 break;
209 }
210 }
211 if (conn)
212 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
213 else
214 conn_devices = NULL;
215
216 /* Find all Hantek 60xx devices and upload firmware to all of them. */
217 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
218 for (i = 0; devlist[i]; i++) {
219 if (conn) {
220 usb = NULL;
221 for (l = conn_devices; l; l = l->next) {
222 usb = l->data;
223 if (usb->bus == libusb_get_bus_number(devlist[i])
224 && usb->address == libusb_get_device_address(devlist[i]))
225 break;
226 }
227 if (!l)
228 /* This device matched none of the ones that
229 * matched the conn specification. */
230 continue;
231 }
232
c940b7a3 233 libusb_get_device_descriptor(devlist[i], &des);
f2a66a8e 234
6c1a76d1
RT
235 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
236 continue;
f2a66a8e
C
237
238 prof = NULL;
0d8a3063 239 for (j = 0; dev_profiles[j].orig_vid; j++) {
f2a66a8e
C
240 if (des.idVendor == dev_profiles[j].orig_vid
241 && des.idProduct == dev_profiles[j].orig_pid) {
242 /* Device matches the pre-firmware profile. */
243 prof = &dev_profiles[j];
244 sr_dbg("Found a %s %s.", prof->vendor, prof->model);
15a5bfe4 245 sdi = hantek_6xxx_dev_new(prof);
f2a66a8e
C
246 sdi->connection_id = g_strdup(connection_id);
247 devices = g_slist_append(devices, sdi);
248 devc = sdi->priv;
249 if (ezusb_upload_firmware(drvc->sr_ctx, devlist[i],
1372bdcd 250 USB_CONFIGURATION, prof->firmware) == SR_OK) {
f2a66a8e
C
251 /* Remember when the firmware on this device was updated. */
252 devc->fw_updated = g_get_monotonic_time();
1372bdcd
GS
253 } else {
254 sr_err("Firmware upload failed, name %s.", prof->firmware);
255 }
f2a66a8e
C
256 /* Dummy USB address of 0xff will get overwritten later. */
257 sdi->conn = sr_usb_dev_inst_new(
258 libusb_get_bus_number(devlist[i]), 0xff, NULL);
259 break;
260 } else if (des.idVendor == dev_profiles[j].fw_vid
459324ac
UH
261 && des.idProduct == dev_profiles[j].fw_pid
262 && des.bcdDevice == dev_profiles[j].fw_prod_ver) {
f2a66a8e
C
263 /* Device matches the post-firmware profile. */
264 prof = &dev_profiles[j];
265 sr_dbg("Found a %s %s.", prof->vendor, prof->model);
15a5bfe4 266 sdi = hantek_6xxx_dev_new(prof);
f2a66a8e
C
267 sdi->connection_id = g_strdup(connection_id);
268 sdi->status = SR_ST_INACTIVE;
269 devices = g_slist_append(devices, sdi);
270 sdi->inst_type = SR_INST_USB;
271 sdi->conn = sr_usb_dev_inst_new(
272 libusb_get_bus_number(devlist[i]),
273 libusb_get_device_address(devlist[i]), NULL);
274 break;
275 }
276 }
277 if (!prof)
278 /* Not a supported VID/PID. */
279 continue;
280 }
281 libusb_free_device_list(devlist, 1);
6c6bc80a 282
15a5bfe4 283 return std_scan_complete(di, devices);
6c6bc80a
C
284}
285
6c6bc80a
C
286static int dev_open(struct sr_dev_inst *sdi)
287{
f2a66a8e
C
288 struct dev_context *devc;
289 struct sr_usb_dev_inst *usb;
290 int64_t timediff_us, timediff_ms;
291 int err;
292
293 devc = sdi->priv;
294 usb = sdi->conn;
295
296 /*
297 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
298 * for the FX2 to renumerate.
299 */
300 err = SR_ERR;
301 if (devc->fw_updated > 0) {
302 sr_info("Waiting for device to reset.");
303 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
304 g_usleep(300 * 1000);
305 timediff_ms = 0;
306 while (timediff_ms < MAX_RENUM_DELAY_MS) {
307 if ((err = hantek_6xxx_open(sdi)) == SR_OK)
308 break;
309 g_usleep(100 * 1000);
310 timediff_us = g_get_monotonic_time() - devc->fw_updated;
311 timediff_ms = timediff_us / 1000;
312 sr_spew("Waited %" PRIi64 " ms.", timediff_ms);
313 }
314 if (timediff_ms < MAX_RENUM_DELAY_MS)
315 sr_info("Device came back after %"PRIu64" ms.", timediff_ms);
316 } else {
317 err = hantek_6xxx_open(sdi);
318 }
6c6bc80a 319
f2a66a8e
C
320 if (err != SR_OK) {
321 sr_err("Unable to open device.");
322 return SR_ERR;
323 }
6c6bc80a 324
f2a66a8e
C
325 err = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
326 if (err != 0) {
327 sr_err("Unable to claim interface: %s.",
d9251a2c 328 libusb_error_name(err));
f2a66a8e
C
329 return SR_ERR;
330 }
6c6bc80a
C
331
332 return SR_OK;
333}
334
335static int dev_close(struct sr_dev_inst *sdi)
336{
f2a66a8e 337 hantek_6xxx_close(sdi);
6c6bc80a
C
338
339 return SR_OK;
340}
341
dd7a72ea
UH
342static int config_get(uint32_t key, GVariant **data,
343 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
6c6bc80a 344{
f2a66a8e
C
345 struct dev_context *devc;
346 struct sr_usb_dev_inst *usb;
f2a66a8e
C
347 const uint64_t *vdiv;
348 int ch_idx;
6c6bc80a 349
6c6bc80a 350 switch (key) {
f2a66a8e
C
351 case SR_CONF_NUM_VDIV:
352 *data = g_variant_new_int32(ARRAY_SIZE(vdivs));
353 break;
6c6bc80a
C
354 }
355
f2a66a8e
C
356 if (!sdi)
357 return SR_ERR_ARG;
358
359 devc = sdi->priv;
360 if (!cg) {
361 switch (key) {
362 case SR_CONF_SAMPLERATE:
363 *data = g_variant_new_uint64(devc->samplerate);
364 break;
365 case SR_CONF_LIMIT_MSEC:
366 *data = g_variant_new_uint64(devc->limit_msec);
367 break;
368 case SR_CONF_LIMIT_SAMPLES:
369 *data = g_variant_new_uint64(devc->limit_samples);
370 break;
371 case SR_CONF_CONN:
372 if (!sdi->conn)
373 return SR_ERR_ARG;
374 usb = sdi->conn;
375 if (usb->address == 255)
376 /* Device still needs to re-enumerate after firmware
377 * upload, so we don't know its (future) address. */
378 return SR_ERR;
95c1fe62 379 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
f2a66a8e
C
380 break;
381 default:
382 return SR_ERR_NA;
383 }
384 } else {
385 if (sdi->channel_groups->data == cg)
386 ch_idx = 0;
387 else if (sdi->channel_groups->next->data == cg)
388 ch_idx = 1;
389 else
390 return SR_ERR_ARG;
391 switch (key) {
392 case SR_CONF_VDIV:
393 vdiv = vdivs[devc->voltage[ch_idx]];
394 *data = g_variant_new("(tt)", vdiv[0], vdiv[1]);
395 break;
cc5ebc8a 396 case SR_CONF_COUPLING:
64f628bf
UH
397 *data = g_variant_new_string((devc->coupling[ch_idx] \
398 == COUPLING_DC) ? "DC" : "AC");
cc5ebc8a 399 break;
f2a66a8e
C
400 }
401 }
402
403 return SR_OK;
6c6bc80a
C
404}
405
dd7a72ea
UH
406static int config_set(uint32_t key, GVariant *data,
407 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
6c6bc80a 408{
f2a66a8e 409 struct dev_context *devc;
697fb6dd 410 int ch_idx, idx;
6c6bc80a 411
f2a66a8e
C
412 devc = sdi->priv;
413 if (!cg) {
414 switch (key) {
415 case SR_CONF_SAMPLERATE:
416 devc->samplerate = g_variant_get_uint64(data);
417 hantek_6xxx_update_samplerate(sdi);
418 break;
419 case SR_CONF_LIMIT_MSEC:
420 devc->limit_msec = g_variant_get_uint64(data);
421 break;
422 case SR_CONF_LIMIT_SAMPLES:
423 devc->limit_samples = g_variant_get_uint64(data);
424 break;
425 default:
a9010323 426 return SR_ERR_NA;
f2a66a8e
C
427 }
428 } else {
429 if (sdi->channel_groups->data == cg)
430 ch_idx = 0;
431 else if (sdi->channel_groups->next->data == cg)
432 ch_idx = 1;
433 else
434 return SR_ERR_ARG;
435 switch (key) {
436 case SR_CONF_VDIV:
697fb6dd 437 if ((idx = std_u64_tuple_idx(data, ARRAY_AND_SIZE(vdivs))) < 0)
a9010323 438 return SR_ERR_ARG;
697fb6dd
UH
439 devc->voltage[ch_idx] = idx;
440 hantek_6xxx_update_vdiv(sdi);
f2a66a8e 441 break;
cc5ebc8a 442 case SR_CONF_COUPLING:
697fb6dd
UH
443 if ((idx = std_str_idx(data, devc->coupling_vals,
444 devc->coupling_tab_size)) < 0)
a9010323 445 return SR_ERR_ARG;
697fb6dd 446 devc->coupling[ch_idx] = idx;
cc5ebc8a 447 break;
f2a66a8e 448 default:
a9010323 449 return SR_ERR_NA;
f2a66a8e 450 }
6c6bc80a
C
451 }
452
a9010323 453 return SR_OK;
6c6bc80a
C
454}
455
dd7a72ea
UH
456static int config_list(uint32_t key, GVariant **data,
457 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
6c6bc80a 458{
e66d1892 459 struct dev_context *devc;
6c6bc80a 460
e66d1892 461 devc = (sdi) ? sdi->priv : NULL;
7f46b27e 462
f2a66a8e
C
463 if (!cg) {
464 switch (key) {
e66d1892 465 case SR_CONF_SCAN_OPTIONS:
f2a66a8e 466 case SR_CONF_DEVICE_OPTIONS:
e66d1892 467 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
f2a66a8e 468 case SR_CONF_SAMPLERATE:
53012da6 469 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates));
f2a66a8e
C
470 break;
471 default:
472 return SR_ERR_NA;
473 }
474 } else {
475 switch (key) {
476 case SR_CONF_DEVICE_OPTIONS:
53012da6 477 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg));
f2a66a8e 478 break;
cc5ebc8a 479 case SR_CONF_COUPLING:
e93ca8a4
GS
480 if (!devc)
481 return SR_ERR_ARG;
5eb4ba29 482 *data = g_variant_new_strv(devc->coupling_vals, devc->coupling_tab_size);
cc5ebc8a 483 break;
f2a66a8e 484 case SR_CONF_VDIV:
58ffcf97 485 *data = std_gvar_tuple_array(ARRAY_AND_SIZE(vdivs));
f2a66a8e
C
486 break;
487 default:
488 return SR_ERR_NA;
489 }
490 }
6c6bc80a 491
f2a66a8e
C
492 return SR_OK;
493}
494
495/* Minimise data amount for limit_samples and limit_msec limits. */
496static uint32_t data_amount(const struct sr_dev_inst *sdi)
497{
498 struct dev_context *devc = sdi->priv;
af7f8824 499 uint32_t data_left, data_left_2, i;
f2a66a8e
C
500 int32_t time_left;
501
502 if (devc->limit_msec) {
503 time_left = devc->limit_msec - (g_get_monotonic_time() - devc->aq_started) / 1000;
504 data_left = devc->samplerate * MAX(time_left, 0) * NUM_CHANNELS / 1000;
505 } else if (devc->limit_samples) {
506 data_left = (devc->limit_samples - devc->samp_received) * NUM_CHANNELS;
507 } else {
508 data_left = devc->samplerate * NUM_CHANNELS;
509 }
510
af7f8824
UH
511 /* Round up to nearest power of two. */
512 for (i = MIN_PACKET_SIZE; i < data_left; i *= 2)
513 ;
514 data_left_2 = i;
f2a66a8e 515
af7f8824 516 sr_spew("data_amount: %u (rounded to power of 2: %u)", data_left, data_left_2);
f2a66a8e 517
af7f8824 518 return data_left_2;
f2a66a8e
C
519}
520
521static void send_chunk(struct sr_dev_inst *sdi, unsigned char *buf,
522 int num_samples)
523{
524 struct sr_datafeed_packet packet;
2938c9d1
UH
525 struct sr_datafeed_analog analog;
526 struct sr_analog_encoding encoding;
527 struct sr_analog_meaning meaning;
528 struct sr_analog_spec spec;
f2a66a8e 529 struct dev_context *devc = sdi->priv;
1e1fdbc9 530 GSList *channels = devc->enabled_channels;
f2a66a8e 531
1e1fdbc9
AJ
532 const float ch_bit[] = { RANGE(0) / 255, RANGE(1) / 255 };
533 const float ch_center[] = { RANGE(0) / 2, RANGE(1) / 2 };
f2a66a8e 534
2938c9d1
UH
535 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
536
2938c9d1 537 packet.type = SR_DF_ANALOG;
f2a66a8e
C
538 packet.payload = &analog;
539
f2a66a8e 540 analog.num_samples = num_samples;
2938c9d1
UH
541 analog.meaning->mq = SR_MQ_VOLTAGE;
542 analog.meaning->unit = SR_UNIT_VOLT;
543 analog.meaning->mqflags = 0;
f2a66a8e 544
1e1fdbc9 545 analog.data = g_try_malloc(num_samples * sizeof(float));
f2a66a8e
C
546 if (!analog.data) {
547 sr_err("Analog data buffer malloc failed.");
548 devc->dev_state = STOPPING;
549 return;
550 }
551
b3fd0993 552 for (int ch = 0; ch < NUM_CHANNELS; ch++) {
1e1fdbc9
AJ
553 if (!devc->ch_enabled[ch])
554 continue;
f2a66a8e 555
1e1fdbc9
AJ
556 float vdivlog = log10f(ch_bit[ch]);
557 int digits = -(int)vdivlog + (vdivlog < 0.0);
558 analog.encoding->digits = digits;
559 analog.spec->spec_digits = digits;
560 analog.meaning->channels = g_slist_append(NULL, channels->data);
561
562 for (int i = 0; i < num_samples; i++) {
563 /*
564 * The device always sends data for both channels. If a channel
565 * is disabled, it contains a copy of the enabled channel's
566 * data. However, we only send the requested channels to
567 * the bus.
568 *
569 * Voltage values are encoded as a value 0-255, where the
570 * value is a point in the range represented by the vdiv
571 * setting. There are 10 vertical divs, so e.g. 500mV/div
572 * represents 5V peak-to-peak where 0 = -2.5V and 255 = +2.5V.
573 */
574 ((float *)analog.data)[i] = ch_bit[ch] * *(buf + i * 2 + ch) - ch_center[ch];
575 }
576
577 sr_session_send(sdi, &packet);
578 g_slist_free(analog.meaning->channels);
579
580 channels = channels->next;
581 }
f2a66a8e
C
582 g_free(analog.data);
583}
584
f2a66a8e
C
585/*
586 * Called by libusb (as triggered by handle_event()) when a transfer comes in.
587 * Only channel data comes in asynchronously, and all transfers for this are
588 * queued up beforehand, so this just needs to chuck the incoming data onto
589 * the libsigrok session bus.
590 */
591static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
592{
593 struct sr_dev_inst *sdi;
594 struct dev_context *devc;
595
596 sdi = transfer->user_data;
597 devc = sdi->priv;
598
599 if (devc->dev_state == FLUSH) {
5954e716
BL
600 g_free(transfer->buffer);
601 libusb_free_transfer(transfer);
f2a66a8e
C
602 devc->dev_state = CAPTURE;
603 devc->aq_started = g_get_monotonic_time();
604 read_channel(sdi, data_amount(sdi));
605 return;
606 }
607
608 if (devc->dev_state != CAPTURE)
609 return;
610
f2a66a8e
C
611 sr_spew("receive_transfer(): calculated samplerate == %" PRIu64 "ks/s",
612 (uint64_t)(transfer->actual_length * 1000 /
613 (g_get_monotonic_time() - devc->read_start_ts + 1) /
614 NUM_CHANNELS));
615
616 sr_spew("receive_transfer(): status %s received %d bytes.",
617 libusb_error_name(transfer->status), transfer->actual_length);
618
619 if (transfer->actual_length == 0)
620 /* Nothing to send to the bus. */
621 return;
622
4299fcc0
SA
623 unsigned samples_received = transfer->actual_length / NUM_CHANNELS;
624 send_chunk(sdi, transfer->buffer, samples_received);
625 devc->samp_received += samples_received;
626
627 g_free(transfer->buffer);
628 libusb_free_transfer(transfer);
629
f2a66a8e
C
630 if (devc->limit_samples && devc->samp_received >= devc->limit_samples) {
631 sr_info("Requested number of samples reached, stopping. %"
632 PRIu64 " <= %" PRIu64, devc->limit_samples,
633 devc->samp_received);
d2f7c417 634 sr_dev_acquisition_stop(sdi);
f2a66a8e
C
635 } else if (devc->limit_msec && (g_get_monotonic_time() -
636 devc->aq_started) / 1000 >= devc->limit_msec) {
637 sr_info("Requested time limit reached, stopping. %d <= %d",
638 (uint32_t)devc->limit_msec,
639 (uint32_t)(g_get_monotonic_time() - devc->aq_started) / 1000);
d2f7c417 640 sr_dev_acquisition_stop(sdi);
f2a66a8e
C
641 } else {
642 read_channel(sdi, data_amount(sdi));
643 }
644}
645
646static int read_channel(const struct sr_dev_inst *sdi, uint32_t amount)
647{
648 int ret;
649 struct dev_context *devc;
650
651 devc = sdi->priv;
652
653 amount = MIN(amount, MAX_PACKET_SIZE);
654 ret = hantek_6xxx_get_channeldata(sdi, receive_transfer, amount);
655 devc->read_start_ts = g_get_monotonic_time();
f2a66a8e 656
6c6bc80a
C
657 return ret;
658}
659
f2a66a8e 660static int handle_event(int fd, int revents, void *cb_data)
6c6bc80a 661{
f2a66a8e 662 const struct sr_dev_inst *sdi;
f2a66a8e
C
663 struct timeval tv;
664 struct sr_dev_driver *di;
665 struct dev_context *devc;
666 struct drv_context *drvc;
667
668 (void)fd;
669 (void)revents;
670
671 sdi = cb_data;
672 di = sdi->driver;
673 drvc = di->context;
674 devc = sdi->priv;
675
676 /* Always handle pending libusb events. */
677 tv.tv_sec = tv.tv_usec = 0;
678 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
679
680 if (devc->dev_state == STOPPING) {
681 /* We've been told to wind up the acquisition. */
682 sr_dbg("Stopping acquisition.");
683
684 hantek_6xxx_stop_data_collecting(sdi);
685 /*
686 * TODO: Doesn't really cancel pending transfers so they might
687 * come in after SR_DF_END is sent.
688 */
689 usb_source_remove(sdi->session, drvc->sr_ctx);
690
bee2b016 691 std_session_send_df_end(sdi);
f2a66a8e
C
692
693 devc->dev_state = IDLE;
694
695 return TRUE;
696 }
697
698 return TRUE;
699}
700
695dc859 701static int dev_acquisition_start(const struct sr_dev_inst *sdi)
f2a66a8e
C
702{
703 struct dev_context *devc;
704 struct sr_dev_driver *di = sdi->driver;
705 struct drv_context *drvc = di->context;
6c6bc80a 706
f2a66a8e 707 devc = sdi->priv;
f2a66a8e
C
708
709 if (configure_channels(sdi) != SR_OK) {
710 sr_err("Failed to configure channels.");
711 return SR_ERR;
712 }
713
714 if (hantek_6xxx_init(sdi) != SR_OK)
715 return SR_ERR;
716
bee2b016 717 std_session_send_df_header(sdi);
f2a66a8e
C
718
719 devc->samp_received = 0;
720 devc->dev_state = FLUSH;
721
722 usb_source_add(sdi->session, drvc->sr_ctx, TICK,
723 handle_event, (void *)sdi);
724
725 hantek_6xxx_start_data_collecting(sdi);
726
727 read_channel(sdi, FLUSH_PACKET_SIZE);
6c6bc80a
C
728
729 return SR_OK;
730}
731
695dc859 732static int dev_acquisition_stop(struct sr_dev_inst *sdi)
6c6bc80a 733{
f2a66a8e
C
734 struct dev_context *devc;
735
f2a66a8e
C
736 devc = sdi->priv;
737 devc->dev_state = STOPPING;
6c6bc80a 738
6c6bc80a
C
739 return SR_OK;
740}
741
dd5c48a6 742static struct sr_dev_driver hantek_6xxx_driver_info = {
6c6bc80a
C
743 .name = "hantek-6xxx",
744 .longname = "Hantek 6xxx",
745 .api_version = 1,
c2fdcc25 746 .init = std_init,
700d6b64 747 .cleanup = std_cleanup,
6c6bc80a 748 .scan = scan,
c01bf34c 749 .dev_list = std_dev_list,
6c6bc80a
C
750 .dev_clear = dev_clear,
751 .config_get = config_get,
752 .config_set = config_set,
753 .config_list = config_list,
754 .dev_open = dev_open,
755 .dev_close = dev_close,
756 .dev_acquisition_start = dev_acquisition_start,
757 .dev_acquisition_stop = dev_acquisition_stop,
758 .context = NULL,
759};
dd5c48a6 760SR_REGISTER_DEV_DRIVER(hantek_6xxx_driver_info);