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