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