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