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