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