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