]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Bert Vermeulen <bert@biot.com> | |
5 | * With protocol information from the hantekdso project, | |
6 | * Copyright (C) 2008 Oleg Khudyakov <prcoder@gmail.com> | |
7 | * | |
8 | * This program is free software: you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation, either version 3 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
22 | #include "libsigrok.h" | |
23 | #include "libsigrok-internal.h" | |
24 | #include "dso.h" | |
25 | #include <string.h> | |
26 | #include <glib.h> | |
27 | #include <libusb.h> | |
28 | ||
29 | extern struct sr_dev_driver hantek_dso_driver_info; | |
30 | ||
31 | ||
32 | static int send_begin(struct dev_context *devc) | |
33 | { | |
34 | int ret; | |
35 | unsigned char buffer[] = {0x0f, 0x03, 0x03, 0x03, 0x68, 0xac, 0xfe, | |
36 | 0x00, 0x01, 0x00}; | |
37 | ||
38 | sr_dbg("hantek-dso: sending CTRL_BEGINCOMMAND"); | |
39 | ||
40 | if ((ret = libusb_control_transfer(devc->usb->devhdl, | |
41 | LIBUSB_REQUEST_TYPE_VENDOR, CTRL_BEGINCOMMAND, | |
42 | 0, 0, buffer, sizeof(buffer), 200)) != sizeof(buffer)) { | |
43 | sr_err("failed to send begincommand: %d", ret); | |
44 | return SR_ERR; | |
45 | } | |
46 | ||
47 | return SR_OK; | |
48 | } | |
49 | ||
50 | static int send_bulkcmd(struct dev_context *devc, uint8_t *cmdstring, int cmdlen) | |
51 | { | |
52 | int ret, tmp; | |
53 | ||
54 | if (send_begin(devc) != SR_OK) | |
55 | return SR_ERR; | |
56 | ||
57 | if ((ret = libusb_bulk_transfer(devc->usb->devhdl, | |
58 | DSO_EP_OUT | LIBUSB_ENDPOINT_OUT, | |
59 | cmdstring, cmdlen, &tmp, 200)) != 0) | |
60 | return SR_ERR; | |
61 | ||
62 | return SR_OK; | |
63 | } | |
64 | ||
65 | SR_PRIV int dso_getmps(libusb_device *dev) | |
66 | { | |
67 | struct libusb_device_descriptor des; | |
68 | struct libusb_config_descriptor *conf_dsc; | |
69 | const struct libusb_interface_descriptor *intf_dsc; | |
70 | int mps; | |
71 | ||
72 | if (libusb_get_device_descriptor(dev, &des) != 0) | |
73 | return 0; | |
74 | ||
75 | if (des.bNumConfigurations != 1) | |
76 | return 0; | |
77 | ||
78 | if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0) | |
79 | return 0; | |
80 | ||
81 | mps = 0; | |
82 | intf_dsc = &(conf_dsc->interface[0].altsetting[0]); | |
83 | if (intf_dsc->bNumEndpoints != 2) | |
84 | goto err; | |
85 | ||
86 | if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) != | |
87 | (2 | LIBUSB_ENDPOINT_OUT)) | |
88 | /* The first endpoint should be 2 (outbound). */ | |
89 | goto err; | |
90 | ||
91 | if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) != | |
92 | (6 | LIBUSB_ENDPOINT_IN)) | |
93 | /* The second endpoint should be 6 (inbound). */ | |
94 | goto err; | |
95 | ||
96 | mps = intf_dsc->endpoint[1].wMaxPacketSize; | |
97 | ||
98 | err: | |
99 | if (conf_dsc) | |
100 | libusb_free_config_descriptor(conf_dsc); | |
101 | ||
102 | return mps; | |
103 | } | |
104 | ||
105 | SR_PRIV int dso_open(struct sr_dev_inst *sdi) | |
106 | { | |
107 | libusb_device **devlist; | |
108 | struct libusb_device_descriptor des; | |
109 | struct dev_context *devc; | |
110 | int err, skip, i; | |
111 | ||
112 | devc = sdi->priv; | |
113 | ||
114 | if (sdi->status == SR_ST_ACTIVE) | |
115 | /* already in use */ | |
116 | return SR_ERR; | |
117 | ||
118 | skip = 0; | |
119 | libusb_get_device_list(NULL, &devlist); | |
120 | for (i = 0; devlist[i]; i++) { | |
121 | if ((err = libusb_get_device_descriptor(devlist[i], &des))) { | |
122 | sr_err("hantek-dso: failed to get device descriptor: %d", err); | |
123 | continue; | |
124 | } | |
125 | ||
126 | if (des.idVendor != devc->profile->fw_vid | |
127 | || des.idProduct != devc->profile->fw_pid) | |
128 | continue; | |
129 | ||
130 | if (sdi->status == SR_ST_INITIALIZING) { | |
131 | if (skip != sdi->index) { | |
132 | /* Skip devices of this type that aren't the one we want. */ | |
133 | skip += 1; | |
134 | continue; | |
135 | } | |
136 | } else if (sdi->status == SR_ST_INACTIVE) { | |
137 | /* | |
138 | * This device is fully enumerated, so we need to find | |
139 | * this device by vendor, product, bus and address. | |
140 | */ | |
141 | if (libusb_get_bus_number(devlist[i]) != devc->usb->bus | |
142 | || libusb_get_device_address(devlist[i]) != devc->usb->address) | |
143 | /* this is not the one */ | |
144 | continue; | |
145 | } | |
146 | ||
147 | if (!(err = libusb_open(devlist[i], &devc->usb->devhdl))) { | |
148 | if (devc->usb->address == 0xff) | |
149 | /* | |
150 | * first time we touch this device after firmware upload, | |
151 | * so we don't know the address yet. | |
152 | */ | |
153 | devc->usb->address = libusb_get_device_address(devlist[i]); | |
154 | ||
155 | if(!(devc->epin_maxpacketsize = dso_getmps(devlist[i]))) | |
156 | sr_err("hantek-dso: wrong endpoint profile"); | |
157 | else { | |
158 | sdi->status = SR_ST_ACTIVE; | |
159 | sr_info("hantek-dso: opened device %d on %d.%d interface %d", | |
160 | sdi->index, devc->usb->bus, | |
161 | devc->usb->address, USB_INTERFACE); | |
162 | } | |
163 | } else { | |
164 | sr_err("hantek-dso: failed to open device: %d", err); | |
165 | } | |
166 | ||
167 | /* if we made it here, we handled the device one way or another */ | |
168 | break; | |
169 | } | |
170 | libusb_free_device_list(devlist, 1); | |
171 | ||
172 | if (sdi->status != SR_ST_ACTIVE) | |
173 | return SR_ERR; | |
174 | ||
175 | return SR_OK; | |
176 | } | |
177 | ||
178 | SR_PRIV void dso_close(struct sr_dev_inst *sdi) | |
179 | { | |
180 | struct dev_context *devc; | |
181 | ||
182 | devc = sdi->priv; | |
183 | ||
184 | if (devc->usb->devhdl == NULL) | |
185 | return; | |
186 | ||
187 | sr_info("hantek-dso: closing device %d on %d.%d interface %d", sdi->index, | |
188 | devc->usb->bus, devc->usb->address, USB_INTERFACE); | |
189 | libusb_release_interface(devc->usb->devhdl, USB_INTERFACE); | |
190 | libusb_close(devc->usb->devhdl); | |
191 | devc->usb->devhdl = NULL; | |
192 | sdi->status = SR_ST_INACTIVE; | |
193 | ||
194 | } | |
195 | ||
196 | static int get_channel_offsets(struct dev_context *devc) | |
197 | { | |
198 | GString *gs; | |
199 | int chan, v, ret; | |
200 | ||
201 | sr_dbg("hantek-dso: getting channel offsets"); | |
202 | ||
203 | ret = libusb_control_transfer(devc->usb->devhdl, | |
204 | LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR, | |
205 | CTRL_READ_EEPROM, EEPROM_CHANNEL_OFFSETS, 0, | |
206 | (unsigned char *)&devc->channel_levels, | |
207 | sizeof(devc->channel_levels), 200); | |
208 | if (ret != sizeof(devc->channel_levels)) { | |
209 | sr_err("failed to get channel offsets: %d", ret); | |
210 | return SR_ERR; | |
211 | } | |
212 | ||
213 | /* Comes in as 16-bit numbers with the second byte always 0 on | |
214 | * the DSO-2090. Guessing this is supposed to be big-endian, | |
215 | * since that's how voltage offsets are submitted back to the DSO. | |
216 | * Convert to host order now, so we can use them natively. | |
217 | */ | |
218 | for (chan = 0; chan < 2; chan++) { | |
219 | for (v = 0; v < 9; v++) { | |
220 | devc->channel_levels[chan][v][0] = g_ntohs(devc->channel_levels[chan][v][0]); | |
221 | devc->channel_levels[chan][v][1] = g_ntohs(devc->channel_levels[chan][v][1]); | |
222 | } | |
223 | } | |
224 | ||
225 | if (sr_log_loglevel_get() >= SR_LOG_DBG) { | |
226 | gs = g_string_sized_new(128); | |
227 | for (chan = 0; chan < 2; chan++) { | |
228 | g_string_printf(gs, "hantek-dso: CH%d:", chan + 1); | |
229 | for (v = 0; v < 9; v++) { | |
230 | g_string_append_printf(gs, " %.4x-%.4x", | |
231 | devc->channel_levels[chan][v][0], | |
232 | devc->channel_levels[chan][v][1]); | |
233 | } | |
234 | sr_dbg(gs->str); | |
235 | } | |
236 | g_string_free(gs, TRUE); | |
237 | } | |
238 | ||
239 | return SR_OK; | |
240 | } | |
241 | ||
242 | SR_PRIV int dso_set_trigger_samplerate(struct dev_context *devc) | |
243 | { | |
244 | int ret, tmp; | |
245 | uint8_t cmdstring[12]; | |
246 | uint16_t timebase_small[] = { 0xffff, 0xfffc, 0xfff7, 0xffe8, 0xffce, | |
247 | 0xff9c, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79, 0xd8f1 }; | |
248 | uint16_t timebase_large[] = { 0xffff, 0x0000, 0xfffc, 0xfff7, 0xffe8, | |
249 | 0xffce, 0xff9d, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79 }; | |
250 | ||
251 | sr_dbg("hantek-dso: preparing CMD_SET_TRIGGER_SAMPLERATE"); | |
252 | ||
253 | memset(cmdstring, 0, sizeof(cmdstring)); | |
254 | /* Command */ | |
255 | cmdstring[0] = CMD_SET_TRIGGER_SAMPLERATE; | |
256 | ||
257 | /* Trigger source */ | |
258 | sr_dbg("hantek-dso: trigger source %s", devc->triggersource); | |
259 | if (!strcmp("CH2", devc->triggersource)) | |
260 | tmp = 0; | |
261 | else if (!strcmp("CH1", devc->triggersource)) | |
262 | tmp = 1; | |
263 | else if (!strcmp("EXT", devc->triggersource)) | |
264 | tmp = 2; | |
265 | else { | |
266 | sr_err("hantek-dso: invalid trigger source %s", devc->triggersource); | |
267 | return SR_ERR_ARG; | |
268 | } | |
269 | cmdstring[2] = tmp; | |
270 | ||
271 | /* Frame size */ | |
272 | sr_dbg("hantek-dso: frame size %d", devc->framesize); | |
273 | cmdstring[2] |= (devc->framesize == FRAMESIZE_SMALL ? 0x01 : 0x02) << 2; | |
274 | ||
275 | /* Timebase fast */ | |
276 | sr_dbg("hantek-dso: time base index %d", devc->timebase); | |
277 | switch (devc->framesize) { | |
278 | case FRAMESIZE_SMALL: | |
279 | if (devc->timebase < TIME_20us) | |
280 | tmp = 0; | |
281 | else if (devc->timebase == TIME_20us) | |
282 | tmp = 1; | |
283 | else if (devc->timebase == TIME_40us) | |
284 | tmp = 2; | |
285 | else if (devc->timebase == TIME_100us) | |
286 | tmp = 3; | |
287 | else if (devc->timebase >= TIME_200us) | |
288 | tmp = 4; | |
289 | break; | |
290 | case FRAMESIZE_LARGE: | |
291 | if (devc->timebase < TIME_40us) { | |
292 | sr_err("hantek-dso: timebase < 40us only supported with 10K buffer"); | |
293 | return SR_ERR_ARG; | |
294 | } | |
295 | else if (devc->timebase == TIME_40us) | |
296 | tmp = 0; | |
297 | else if (devc->timebase == TIME_100us) | |
298 | tmp = 2; | |
299 | else if (devc->timebase == TIME_200us) | |
300 | tmp = 3; | |
301 | else if (devc->timebase >= TIME_400us) | |
302 | tmp = 4; | |
303 | break; | |
304 | } | |
305 | cmdstring[2] |= (tmp & 0x07) << 5; | |
306 | ||
307 | /* Enabled channels: 00=CH1 01=CH2 10=both */ | |
308 | sr_dbg("hantek-dso: channels CH1=%d CH2=%d", devc->ch1_enabled, devc->ch2_enabled); | |
309 | tmp = (((devc->ch2_enabled ? 1 : 0) << 1) + (devc->ch1_enabled ? 1 : 0)) - 1; | |
310 | cmdstring[3] = tmp; | |
311 | ||
312 | /* Fast rates channel */ | |
313 | /* TODO: is this right? */ | |
314 | tmp = devc->timebase < TIME_10us ? 1 : 0; | |
315 | cmdstring[3] |= tmp << 2; | |
316 | ||
317 | /* Trigger slope: 0=positive 1=negative */ | |
318 | /* TODO: does this work? */ | |
319 | sr_dbg("hantek-dso: trigger slope %d", devc->triggerslope); | |
320 | cmdstring[3] |= (devc->triggerslope == SLOPE_NEGATIVE ? 1 : 0) << 3; | |
321 | ||
322 | /* Timebase slow */ | |
323 | if (devc->timebase < TIME_100us) | |
324 | tmp = 0; | |
325 | else if (devc->timebase > TIME_400ms) | |
326 | tmp = 0xffed; | |
327 | else { | |
328 | if (devc->framesize == FRAMESIZE_SMALL) | |
329 | tmp = timebase_small[devc->timebase - 3]; | |
330 | else | |
331 | tmp = timebase_large[devc->timebase - 3]; | |
332 | } | |
333 | cmdstring[4] = tmp & 0xff; | |
334 | cmdstring[5] = (tmp >> 8) & 0xff; | |
335 | ||
336 | /* Horizontal trigger position */ | |
337 | sr_dbg("hantek-dso: trigger position %3.2f", devc->triggerposition); | |
338 | tmp = 0x77fff + 0x8000 * devc->triggerposition; | |
339 | cmdstring[6] = tmp & 0xff; | |
340 | cmdstring[7] = (tmp >> 8) & 0xff; | |
341 | cmdstring[10] = (tmp >> 16) & 0xff; | |
342 | ||
343 | if (send_begin(devc) != SR_OK) | |
344 | return SR_ERR; | |
345 | ||
346 | if ((ret = libusb_bulk_transfer(devc->usb->devhdl, | |
347 | DSO_EP_OUT | LIBUSB_ENDPOINT_OUT, | |
348 | cmdstring, sizeof(cmdstring), | |
349 | &tmp, 100)) != 0) { | |
350 | sr_err("Failed to set trigger/samplerate: %d", ret); | |
351 | return SR_ERR; | |
352 | } | |
353 | sr_dbg("hantek-dso: sent CMD_SET_TRIGGER_SAMPLERATE"); | |
354 | ||
355 | return SR_OK; | |
356 | } | |
357 | ||
358 | SR_PRIV int dso_set_filters(struct dev_context *devc) | |
359 | { | |
360 | int ret, tmp; | |
361 | uint8_t cmdstring[8]; | |
362 | ||
363 | sr_dbg("hantek-dso: preparing CMD_SET_FILTERS"); | |
364 | ||
365 | memset(cmdstring, 0, sizeof(cmdstring)); | |
366 | cmdstring[0] = CMD_SET_FILTERS; | |
367 | cmdstring[1] = 0x0f; | |
368 | if (devc->filter_ch1) { | |
369 | sr_dbg("hantek-dso: turning on CH1 filter"); | |
370 | cmdstring[2] |= 0x80; | |
371 | } | |
372 | if (devc->filter_ch2) { | |
373 | sr_dbg("hantek-dso: turning on CH2 filter"); | |
374 | cmdstring[2] |= 0x40; | |
375 | } | |
376 | if (devc->filter_trigger) { | |
377 | /* TODO: supported on the DSO-2090? */ | |
378 | sr_dbg("hantek-dso: turning on trigger filter"); | |
379 | cmdstring[2] |= 0x20; | |
380 | } | |
381 | ||
382 | if (send_begin(devc) != SR_OK) | |
383 | return SR_ERR; | |
384 | ||
385 | if ((ret = libusb_bulk_transfer(devc->usb->devhdl, | |
386 | DSO_EP_OUT | LIBUSB_ENDPOINT_OUT, | |
387 | cmdstring, sizeof(cmdstring), | |
388 | &tmp, 100)) != 0) { | |
389 | sr_err("Failed to set filters: %d", ret); | |
390 | return SR_ERR; | |
391 | } | |
392 | sr_dbg("hantek-dso: sent CMD_SET_FILTERS"); | |
393 | ||
394 | return SR_OK; | |
395 | } | |
396 | ||
397 | SR_PRIV int dso_set_voltage(struct dev_context *devc) | |
398 | { | |
399 | int ret, tmp; | |
400 | uint8_t cmdstring[8]; | |
401 | ||
402 | sr_dbg("hantek-dso: preparing CMD_SET_VOLTAGE"); | |
403 | ||
404 | memset(cmdstring, 0, sizeof(cmdstring)); | |
405 | cmdstring[0] = CMD_SET_VOLTAGE; | |
406 | cmdstring[1] = 0x0f; | |
407 | cmdstring[2] = 0x30; | |
408 | ||
409 | /* CH1 volts/div is encoded in bits 0-1 */ | |
410 | sr_dbg("hantek-dso: CH1 vdiv index %d", devc->voltage_ch1); | |
411 | switch (devc->voltage_ch1) { | |
412 | case VDIV_1V: | |
413 | case VDIV_100MV: | |
414 | case VDIV_10MV: | |
415 | cmdstring[2] |= 0x00; | |
416 | break; | |
417 | case VDIV_2V: | |
418 | case VDIV_200MV: | |
419 | case VDIV_20MV: | |
420 | cmdstring[2] |= 0x01; | |
421 | break; | |
422 | case VDIV_5V: | |
423 | case VDIV_500MV: | |
424 | case VDIV_50MV: | |
425 | cmdstring[2] |= 0x02; | |
426 | break; | |
427 | } | |
428 | ||
429 | /* CH2 volts/div is encoded in bits 2-3 */ | |
430 | sr_dbg("hantek-dso: CH2 vdiv index %d", devc->voltage_ch2); | |
431 | switch (devc->voltage_ch2) { | |
432 | case VDIV_1V: | |
433 | case VDIV_100MV: | |
434 | case VDIV_10MV: | |
435 | cmdstring[2] |= 0x00; | |
436 | break; | |
437 | case VDIV_2V: | |
438 | case VDIV_200MV: | |
439 | case VDIV_20MV: | |
440 | cmdstring[2] |= 0x04; | |
441 | break; | |
442 | case VDIV_5V: | |
443 | case VDIV_500MV: | |
444 | case VDIV_50MV: | |
445 | cmdstring[2] |= 0x08; | |
446 | break; | |
447 | } | |
448 | ||
449 | if (send_begin(devc) != SR_OK) | |
450 | return SR_ERR; | |
451 | ||
452 | if ((ret = libusb_bulk_transfer(devc->usb->devhdl, | |
453 | DSO_EP_OUT | LIBUSB_ENDPOINT_OUT, | |
454 | cmdstring, sizeof(cmdstring), | |
455 | &tmp, 100)) != 0) { | |
456 | sr_err("Failed to set voltage: %d", ret); | |
457 | return SR_ERR; | |
458 | } | |
459 | sr_dbg("hantek-dso: sent CMD_SET_VOLTAGE"); | |
460 | ||
461 | return SR_OK; | |
462 | } | |
463 | ||
464 | SR_PRIV int dso_set_relays(struct dev_context *devc) | |
465 | { | |
466 | GString *gs; | |
467 | int ret, i; | |
468 | uint8_t relays[17] = { 0x00, 0x04, 0x08, 0x02, 0x20, 0x40, 0x10, 0x01, | |
469 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; | |
470 | ||
471 | sr_dbg("hantek-dso: preparing CTRL_SETRELAYS"); | |
472 | ||
473 | if (devc->voltage_ch1 < VDIV_1V) | |
474 | relays[1] = ~relays[1]; | |
475 | ||
476 | if (devc->voltage_ch1 < VDIV_100MV) | |
477 | relays[2] = ~relays[2]; | |
478 | ||
479 | sr_dbg("hantek-dso: CH1 coupling %d", devc->coupling_ch1); | |
480 | if (devc->coupling_ch1 != COUPLING_AC) | |
481 | relays[3] = ~relays[3]; | |
482 | ||
483 | if (devc->voltage_ch2 < VDIV_1V) | |
484 | relays[4] = ~relays[4]; | |
485 | ||
486 | if (devc->voltage_ch2 < VDIV_100MV) | |
487 | relays[5] = ~relays[5]; | |
488 | ||
489 | sr_dbg("hantek-dso: CH2 coupling %d", devc->coupling_ch1); | |
490 | if (devc->coupling_ch2 != COUPLING_AC) | |
491 | relays[6] = ~relays[6]; | |
492 | ||
493 | if (!strcmp(devc->triggersource, "EXT")) | |
494 | relays[7] = ~relays[7]; | |
495 | ||
496 | if (sr_log_loglevel_get() >= SR_LOG_DBG) { | |
497 | gs = g_string_sized_new(128); | |
498 | g_string_printf(gs, "hantek-dso: relays:"); | |
499 | for (i = 0; i < 17; i++) | |
500 | g_string_append_printf(gs, " %.2x", relays[i]); | |
501 | sr_dbg(gs->str); | |
502 | g_string_free(gs, TRUE); | |
503 | } | |
504 | ||
505 | if ((ret = libusb_control_transfer(devc->usb->devhdl, | |
506 | LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETRELAYS, | |
507 | 0, 0, relays, 17, 100)) != sizeof(relays)) { | |
508 | sr_err("failed to set relays: %d", ret); | |
509 | return SR_ERR; | |
510 | } | |
511 | sr_dbg("hantek-dso: sent CTRL_SETRELAYS"); | |
512 | ||
513 | return SR_OK; | |
514 | } | |
515 | ||
516 | SR_PRIV int dso_set_voffsets(struct dev_context *devc) | |
517 | { | |
518 | int offset, ret; | |
519 | uint16_t *ch_levels; | |
520 | uint8_t offsets[17]; | |
521 | ||
522 | sr_dbg("hantek-dso: preparing CTRL_SETOFFSET"); | |
523 | ||
524 | memset(offsets, 0, sizeof(offsets)); | |
525 | /* Channel 1 */ | |
526 | ch_levels = devc->channel_levels[0][devc->voltage_ch1]; | |
527 | offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch1 + ch_levels[0]; | |
528 | offsets[0] = (offset >> 8) | 0x20; | |
529 | offsets[1] = offset & 0xff; | |
530 | sr_dbg("hantek-dso: CH1 offset %3.2f (%.2x%.2x)", devc->voffset_ch1, | |
531 | offsets[0], offsets[1]); | |
532 | ||
533 | /* Channel 2 */ | |
534 | ch_levels = devc->channel_levels[1][devc->voltage_ch2]; | |
535 | offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch2 + ch_levels[0]; | |
536 | offsets[2] = (offset >> 8) | 0x20; | |
537 | offsets[3] = offset & 0xff; | |
538 | sr_dbg("hantek-dso: CH2 offset %3.2f (%.2x%.2x)", devc->voffset_ch2, | |
539 | offsets[2], offsets[3]); | |
540 | ||
541 | /* Trigger */ | |
542 | offset = MAX_VERT_TRIGGER * devc->voffset_trigger; | |
543 | offsets[4] = (offset >> 8) | 0x20; | |
544 | offsets[5] = offset & 0xff; | |
545 | sr_dbg("hantek-dso: trigger offset %3.2f (%.2x%.2x)", devc->voffset_trigger, | |
546 | offsets[4], offsets[5]); | |
547 | ||
548 | if ((ret = libusb_control_transfer(devc->usb->devhdl, | |
549 | LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETOFFSET, | |
550 | 0, 0, offsets, sizeof(offsets), 100)) != sizeof(offsets)) { | |
551 | sr_err("failed to set offsets: %d", ret); | |
552 | return SR_ERR; | |
553 | } | |
554 | sr_dbg("hantek-dso: sent CTRL_SETOFFSET"); | |
555 | ||
556 | return SR_OK; | |
557 | } | |
558 | ||
559 | SR_PRIV int dso_enable_trigger(struct dev_context *devc) | |
560 | { | |
561 | int ret, tmp; | |
562 | uint8_t cmdstring[2]; | |
563 | ||
564 | sr_dbg("hantek-dso: sending CMD_ENABLE_TRIGGER"); | |
565 | ||
566 | memset(cmdstring, 0, sizeof(cmdstring)); | |
567 | cmdstring[0] = CMD_ENABLE_TRIGGER; | |
568 | cmdstring[1] = 0x00; | |
569 | ||
570 | if (send_begin(devc) != SR_OK) | |
571 | return SR_ERR; | |
572 | ||
573 | if ((ret = libusb_bulk_transfer(devc->usb->devhdl, | |
574 | DSO_EP_OUT | LIBUSB_ENDPOINT_OUT, | |
575 | cmdstring, sizeof(cmdstring), | |
576 | &tmp, 100)) != 0) { | |
577 | sr_err("Failed to enable trigger: %d", ret); | |
578 | return SR_ERR; | |
579 | } | |
580 | ||
581 | return SR_OK; | |
582 | } | |
583 | ||
584 | SR_PRIV int dso_force_trigger(struct dev_context *devc) | |
585 | { | |
586 | int ret, tmp; | |
587 | uint8_t cmdstring[2]; | |
588 | ||
589 | sr_dbg("hantek-dso: sending CMD_FORCE_TRIGGER"); | |
590 | ||
591 | memset(cmdstring, 0, sizeof(cmdstring)); | |
592 | cmdstring[0] = CMD_FORCE_TRIGGER; | |
593 | cmdstring[1] = 0x00; | |
594 | ||
595 | if (send_begin(devc) != SR_OK) | |
596 | return SR_ERR; | |
597 | ||
598 | if ((ret = libusb_bulk_transfer(devc->usb->devhdl, | |
599 | DSO_EP_OUT | LIBUSB_ENDPOINT_OUT, | |
600 | cmdstring, sizeof(cmdstring), | |
601 | &tmp, 100)) != 0) { | |
602 | sr_err("Failed to force trigger: %d", ret); | |
603 | return SR_ERR; | |
604 | } | |
605 | ||
606 | return SR_OK; | |
607 | } | |
608 | ||
609 | SR_PRIV int dso_init(struct dev_context *devc) | |
610 | { | |
611 | ||
612 | sr_dbg("hantek-dso: initializing dso"); | |
613 | ||
614 | if (get_channel_offsets(devc) != SR_OK) | |
615 | return SR_ERR; | |
616 | ||
617 | if (dso_set_trigger_samplerate(devc) != SR_OK) | |
618 | return SR_ERR; | |
619 | ||
620 | if (dso_set_filters(devc) != SR_OK) | |
621 | return SR_ERR; | |
622 | ||
623 | if (dso_set_voltage(devc) != SR_OK) | |
624 | return SR_ERR; | |
625 | ||
626 | if (dso_set_relays(devc) != SR_OK) | |
627 | return SR_ERR; | |
628 | ||
629 | if (dso_set_voffsets(devc) != SR_OK) | |
630 | return SR_ERR; | |
631 | ||
632 | if (dso_enable_trigger(devc) != SR_OK) | |
633 | return SR_ERR; | |
634 | ||
635 | return SR_OK; | |
636 | } | |
637 | ||
638 | SR_PRIV int dso_get_capturestate(struct dev_context *devc, uint8_t *capturestate, | |
639 | uint32_t *trigger_offset) | |
640 | { | |
641 | int ret, tmp, i; | |
642 | unsigned int bitvalue, toff; | |
643 | uint8_t cmdstring[2], inbuf[512]; | |
644 | ||
645 | sr_dbg("hantek-dso: sending CMD_GET_CAPTURESTATE"); | |
646 | ||
647 | cmdstring[0] = CMD_GET_CAPTURESTATE; | |
648 | cmdstring[1] = 0; | |
649 | ||
650 | if ((ret = send_bulkcmd(devc, cmdstring, sizeof(cmdstring))) != SR_OK) { | |
651 | sr_dbg("Failed to send get_capturestate command: %d", ret); | |
652 | return SR_ERR; | |
653 | } | |
654 | ||
655 | if ((ret = libusb_bulk_transfer(devc->usb->devhdl, | |
656 | DSO_EP_IN | LIBUSB_ENDPOINT_IN, | |
657 | inbuf, 512, &tmp, 100)) != 0) { | |
658 | sr_dbg("Failed to get capturestate: %d", ret); | |
659 | return SR_ERR; | |
660 | } | |
661 | *capturestate = inbuf[0]; | |
662 | toff = (inbuf[1] << 16) | (inbuf[3] << 8) | inbuf[2]; | |
663 | ||
664 | /* This conversion comes from the openhantek project. | |
665 | * Each set bit in the 24-bit value inverts all bits with a lower | |
666 | * value. No idea why the device reports the trigger point this way. | |
667 | */ | |
668 | bitvalue = 1; | |
669 | for (i = 0; i < 24; i++) { | |
670 | /* Each set bit inverts all bits with a lower value. */ | |
671 | if(toff & bitvalue) | |
672 | toff ^= bitvalue - 1; | |
673 | bitvalue <<= 1; | |
674 | } | |
675 | *trigger_offset = toff; | |
676 | ||
677 | return SR_OK; | |
678 | } | |
679 | ||
680 | SR_PRIV int dso_capture_start(struct dev_context *devc) | |
681 | { | |
682 | int ret; | |
683 | uint8_t cmdstring[2]; | |
684 | ||
685 | sr_dbg("hantek-dso: sending CMD_CAPTURE_START"); | |
686 | ||
687 | cmdstring[0] = CMD_CAPTURE_START; | |
688 | cmdstring[1] = 0; | |
689 | ||
690 | if ((ret = send_bulkcmd(devc, cmdstring, sizeof(cmdstring))) != SR_OK) { | |
691 | sr_err("Failed to send capture_start command: %d", ret); | |
692 | return SR_ERR; | |
693 | } | |
694 | ||
695 | return SR_OK; | |
696 | } | |
697 | ||
698 | SR_PRIV int dso_get_channeldata(struct dev_context *devc, libusb_transfer_cb_fn cb) | |
699 | { | |
700 | struct libusb_transfer *transfer; | |
701 | int num_transfers, ret, i; | |
702 | uint8_t cmdstring[2]; | |
703 | unsigned char *buf; | |
704 | ||
705 | sr_dbg("hantek-dso: sending CMD_GET_CHANNELDATA"); | |
706 | ||
707 | cmdstring[0] = CMD_GET_CHANNELDATA; | |
708 | cmdstring[1] = 0; | |
709 | ||
710 | if ((ret = send_bulkcmd(devc, cmdstring, sizeof(cmdstring))) != SR_OK) { | |
711 | sr_err("Failed to get channel data: %d", ret); | |
712 | return SR_ERR; | |
713 | } | |
714 | ||
715 | /* TODO: dso-2xxx only */ | |
716 | num_transfers = devc->framesize * sizeof(unsigned short) / devc->epin_maxpacketsize; | |
717 | sr_dbg("hantek-dso: queueing up %d transfers", num_transfers); | |
718 | for (i = 0; i < num_transfers; i++) { | |
719 | if (!(buf = g_try_malloc(devc->epin_maxpacketsize))) { | |
720 | sr_err("hantek-dso: %s: buf malloc failed", __func__); | |
721 | return SR_ERR_MALLOC; | |
722 | } | |
723 | transfer = libusb_alloc_transfer(0); | |
724 | libusb_fill_bulk_transfer(transfer, devc->usb->devhdl, | |
725 | DSO_EP_IN | LIBUSB_ENDPOINT_IN, buf, | |
726 | devc->epin_maxpacketsize, cb, devc, 40); | |
727 | if ((ret = libusb_submit_transfer(transfer)) != 0) { | |
728 | sr_err("failed to submit transfer: %d", ret); | |
729 | /* TODO: Free them all. */ | |
730 | libusb_free_transfer(transfer); | |
731 | g_free(buf); | |
732 | return SR_ERR; | |
733 | } | |
734 | } | |
735 | ||
736 | return SR_OK; | |
737 | } | |
738 |