]>
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 | { | |
203 | int ret; | |
204 | ||
205 | sr_dbg("hantek-dso: getting channel offsets"); | |
206 | ||
207 | ret = libusb_control_transfer(ctx->usb->devhdl, | |
208 | LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR, | |
209 | CTRL_READ_EEPROM, EEPROM_CHANNEL_OFFSETS, 0, | |
210 | (unsigned char *)&ctx->channel_levels, | |
211 | sizeof(ctx->channel_levels), 200); | |
212 | if (ret != sizeof(ctx->channel_levels)) { | |
213 | sr_err("failed to get channel offsets: %d", ret); | |
214 | return SR_ERR; | |
215 | } | |
216 | ||
217 | return SR_OK; | |
218 | } | |
219 | ||
220 | SR_PRIV int dso_set_trigger_samplerate(struct context *ctx) | |
221 | { | |
222 | int ret, tmp; | |
223 | uint8_t cmdstring[12]; | |
3b533202 BV |
224 | uint16_t timebase_small[] = { 0xffff, 0xfffc, 0xfff7, 0xffe8, 0xffce, |
225 | 0xff9c, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79, 0xd8f1 }; | |
226 | uint16_t timebase_large[] = { 0xffff, 0x0000, 0xfffc, 0xfff7, 0xffe8, | |
227 | 0xffce, 0xff9d, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79 }; | |
228 | ||
a370ef19 | 229 | sr_dbg("hantek-dso: preparing CMD_SET_TRIGGER_SAMPLERATE"); |
3b533202 BV |
230 | |
231 | memset(cmdstring, 0, sizeof(cmdstring)); | |
232 | /* Command */ | |
233 | cmdstring[0] = CMD_SET_TRIGGER_SAMPLERATE; | |
234 | ||
235 | /* Trigger source */ | |
a370ef19 BV |
236 | sr_dbg("hantek-dso: trigger source %s", ctx->triggersource); |
237 | if (!strcmp("CH2", ctx->triggersource)) | |
238 | tmp = 0; | |
239 | else if (!strcmp("CH1", ctx->triggersource)) | |
240 | tmp = 1; | |
241 | else if (!strcmp("EXT", ctx->triggersource)) | |
242 | tmp = 2; | |
243 | else { | |
244 | sr_err("hantek-dso: invalid trigger source %s", ctx->triggersource); | |
245 | return SR_ERR_ARG; | |
246 | } | |
247 | cmdstring[2] = tmp; | |
3b533202 BV |
248 | |
249 | /* Frame size */ | |
a370ef19 | 250 | sr_dbg("hantek-dso: frame size %d", ctx->framesize); |
bc79e906 BV |
251 | cmdstring[2] |= (ctx->framesize == FRAMESIZE_SMALL ? 0x01 : 0x02) << 2; |
252 | ||
253 | /* Timebase fast */ | |
a370ef19 | 254 | sr_dbg("hantek-dso: time base index %d", ctx->timebase); |
bc79e906 BV |
255 | switch (ctx->framesize) { |
256 | case FRAMESIZE_SMALL: | |
257 | if (ctx->timebase < TIME_20us) | |
258 | tmp = 0; | |
259 | else if (ctx->timebase == TIME_20us) | |
260 | tmp = 1; | |
261 | else if (ctx->timebase == TIME_40us) | |
262 | tmp = 2; | |
263 | else if (ctx->timebase == TIME_100us) | |
264 | tmp = 3; | |
265 | else if (ctx->timebase >= TIME_200us) | |
266 | tmp = 4; | |
267 | break; | |
268 | case FRAMESIZE_LARGE: | |
269 | if (ctx->timebase < TIME_40us) { | |
270 | sr_err("hantek-dso: timebase < 40us only supported with 10K buffer"); | |
271 | return SR_ERR_ARG; | |
272 | } | |
273 | else if (ctx->timebase == TIME_40us) | |
274 | tmp = 0; | |
275 | else if (ctx->timebase == TIME_100us) | |
276 | tmp = 2; | |
277 | else if (ctx->timebase == TIME_200us) | |
278 | tmp = 3; | |
279 | else if (ctx->timebase >= TIME_400us) | |
280 | tmp = 4; | |
281 | break; | |
3b533202 | 282 | } |
bc79e906 BV |
283 | cmdstring[2] |= (tmp & 0x07) << 5; |
284 | ||
285 | /* Enabled channels: 00=CH1 01=CH2 10=both */ | |
a370ef19 | 286 | sr_dbg("hantek-dso: channels CH1=%d CH2=%d", ctx->ch1_enabled, ctx->ch2_enabled); |
6e71ef3b BV |
287 | tmp = (((ctx->ch2_enabled ? 1 : 0) << 1) + (ctx->ch1_enabled ? 1 : 0)) - 1; |
288 | cmdstring[3] = tmp; | |
3b533202 | 289 | |
bc79e906 BV |
290 | /* Fast rates channel */ |
291 | /* TODO: is this right? */ | |
292 | tmp = ctx->timebase < TIME_10us ? 1 : 0; | |
293 | cmdstring[3] |= tmp << 2; | |
3b533202 | 294 | |
bc79e906 | 295 | /* Trigger slope: 0=positive 1=negative */ |
a370ef19 BV |
296 | /* TODO: does this work? */ |
297 | sr_dbg("hantek-dso: trigger slope %d", ctx->triggerslope); | |
bc79e906 | 298 | cmdstring[3] |= (ctx->triggerslope == SLOPE_NEGATIVE ? 1 : 0) << 3; |
3b533202 | 299 | |
a370ef19 | 300 | /* Timebase slow */ |
3b533202 BV |
301 | if (ctx->timebase < TIME_100us) |
302 | tmp = 0; | |
303 | else if (ctx->timebase > TIME_400ms) | |
304 | tmp = 0xffed; | |
305 | else { | |
306 | if (ctx->framesize == FRAMESIZE_SMALL) | |
307 | tmp = timebase_small[ctx->timebase - 3]; | |
308 | else | |
309 | tmp = timebase_large[ctx->timebase - 3]; | |
310 | } | |
bc79e906 BV |
311 | cmdstring[4] = tmp & 0xff; |
312 | cmdstring[5] = (tmp >> 8) & 0xff; | |
313 | ||
314 | /* Horizontal trigger position */ | |
a370ef19 | 315 | sr_dbg("hantek-dso: trigger position %3.2f", ctx->triggerposition); |
bc79e906 | 316 | tmp = 0x77fff + 0x8000 * ctx->triggerposition; |
3b533202 BV |
317 | cmdstring[6] = tmp & 0xff; |
318 | cmdstring[7] = (tmp >> 8) & 0xff; | |
3b533202 BV |
319 | cmdstring[10] = (tmp >> 16) & 0xff; |
320 | ||
321 | if (send_begin(ctx) != SR_OK) | |
322 | return SR_ERR; | |
323 | ||
324 | if ((ret = libusb_bulk_transfer(ctx->usb->devhdl, | |
325 | DSO_EP_OUT | LIBUSB_ENDPOINT_OUT, | |
326 | cmdstring, sizeof(cmdstring), | |
327 | &tmp, 100)) != 0) { | |
328 | sr_err("Failed to set trigger/samplerate: %d", ret); | |
329 | return SR_ERR; | |
330 | } | |
a370ef19 | 331 | sr_dbg("hantek-dso: sent CMD_SET_TRIGGER_SAMPLERATE"); |
3b533202 BV |
332 | |
333 | return SR_OK; | |
334 | } | |
335 | ||
336 | SR_PRIV int dso_set_filters(struct context *ctx) | |
337 | { | |
338 | int ret, tmp; | |
339 | uint8_t cmdstring[8]; | |
340 | ||
ebb781a6 | 341 | sr_dbg("hantek-dso: preparing CMD_SET_FILTERS"); |
3b533202 BV |
342 | |
343 | memset(cmdstring, 0, sizeof(cmdstring)); | |
344 | cmdstring[0] = CMD_SET_FILTERS; | |
345 | cmdstring[1] = 0x0f; | |
ebb781a6 BV |
346 | if (ctx->filter_ch1) { |
347 | sr_dbg("hantek-dso: turning on CH1 filter"); | |
3b533202 | 348 | cmdstring[2] |= 0x80; |
ebb781a6 BV |
349 | } |
350 | if (ctx->filter_ch2) { | |
351 | sr_dbg("hantek-dso: turning on CH2 filter"); | |
3b533202 | 352 | cmdstring[2] |= 0x40; |
ebb781a6 BV |
353 | } |
354 | if (ctx->filter_trigger) { | |
355 | /* TODO: supported on the DSO-2090? */ | |
356 | sr_dbg("hantek-dso: turning on trigger filter"); | |
3b533202 | 357 | cmdstring[2] |= 0x20; |
ebb781a6 | 358 | } |
3b533202 BV |
359 | |
360 | if (send_begin(ctx) != SR_OK) | |
361 | return SR_ERR; | |
362 | ||
363 | if ((ret = libusb_bulk_transfer(ctx->usb->devhdl, | |
364 | DSO_EP_OUT | LIBUSB_ENDPOINT_OUT, | |
365 | cmdstring, sizeof(cmdstring), | |
366 | &tmp, 100)) != 0) { | |
367 | sr_err("Failed to set filters: %d", ret); | |
368 | return SR_ERR; | |
369 | } | |
ebb781a6 | 370 | sr_dbg("hantek-dso: sent CMD_SET_FILTERS"); |
3b533202 BV |
371 | |
372 | return SR_OK; | |
373 | } | |
374 | ||
375 | SR_PRIV int dso_set_voltage(struct context *ctx) | |
376 | { | |
377 | int ret, tmp; | |
378 | uint8_t cmdstring[8]; | |
379 | ||
313deed2 | 380 | sr_dbg("hantek-dso: preparing CMD_SET_VOLTAGE"); |
3b533202 BV |
381 | |
382 | memset(cmdstring, 0, sizeof(cmdstring)); | |
383 | cmdstring[0] = CMD_SET_VOLTAGE; | |
384 | cmdstring[1] = 0x0f; | |
313deed2 BV |
385 | cmdstring[2] = 0x30; |
386 | ||
387 | /* CH1 volts/div is encoded in bits 0-1 */ | |
388 | sr_dbg("hantek-dso: CH1 vdiv index %d", ctx->voltage_ch1); | |
389 | switch (ctx->voltage_ch1) { | |
390 | case VDIV_1V: | |
391 | case VDIV_100MV: | |
392 | case VDIV_10MV: | |
393 | cmdstring[2] |= 0x00; | |
394 | break; | |
395 | case VDIV_2V: | |
396 | case VDIV_200MV: | |
397 | case VDIV_20MV: | |
398 | cmdstring[2] |= 0x01; | |
399 | break; | |
400 | case VDIV_5V: | |
401 | case VDIV_500MV: | |
402 | case VDIV_50MV: | |
403 | cmdstring[2] |= 0x02; | |
404 | break; | |
405 | } | |
406 | ||
407 | /* CH2 volts/div is encoded in bits 2-3 */ | |
408 | sr_dbg("hantek-dso: CH2 vdiv index %d", ctx->voltage_ch2); | |
409 | switch (ctx->voltage_ch2) { | |
410 | case VDIV_1V: | |
411 | case VDIV_100MV: | |
412 | case VDIV_10MV: | |
413 | cmdstring[2] |= 0x00; | |
414 | break; | |
415 | case VDIV_2V: | |
416 | case VDIV_200MV: | |
417 | case VDIV_20MV: | |
418 | cmdstring[2] |= 0x08; | |
419 | break; | |
420 | case VDIV_5V: | |
421 | case VDIV_500MV: | |
422 | case VDIV_50MV: | |
423 | cmdstring[2] |= 0x04; | |
424 | break; | |
425 | } | |
3b533202 BV |
426 | |
427 | if (send_begin(ctx) != SR_OK) | |
428 | return SR_ERR; | |
429 | ||
430 | if ((ret = libusb_bulk_transfer(ctx->usb->devhdl, | |
431 | DSO_EP_OUT | LIBUSB_ENDPOINT_OUT, | |
432 | cmdstring, sizeof(cmdstring), | |
433 | &tmp, 100)) != 0) { | |
434 | sr_err("Failed to set voltage: %d", ret); | |
435 | return SR_ERR; | |
436 | } | |
313deed2 | 437 | sr_dbg("hantek-dso: sent CMD_SET_VOLTAGE"); |
3b533202 BV |
438 | |
439 | return SR_OK; | |
440 | } | |
441 | ||
442 | SR_PRIV int dso_set_relays(struct context *ctx) | |
443 | { | |
444 | int ret, cv1, cv2; | |
445 | uint8_t relays[] = { 0x00, 0x04, 0x08, 0x02, 0x20, 0x40, 0x10, 0x01, | |
446 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; | |
447 | ||
b58fbd99 | 448 | sr_dbg("hantek-dso: preparing CTRL_SETRELAYS"); |
3b533202 BV |
449 | |
450 | cv1 = ctx->voltage_ch1 / 3; | |
451 | cv2 = ctx->voltage_ch2 / 3; | |
b58fbd99 | 452 | |
3b533202 BV |
453 | if (cv1 > 0) |
454 | relays[1] = ~relays[1]; | |
455 | ||
456 | if (cv1 > 1) | |
457 | relays[2] = ~relays[2]; | |
458 | ||
b58fbd99 | 459 | sr_dbg("hantek-dso: CH1 coupling %d", ctx->coupling_ch1); |
3b533202 BV |
460 | if (ctx->coupling_ch1 != COUPLING_AC) |
461 | relays[3] = ~relays[3]; | |
462 | ||
463 | if (cv2 > 0) | |
464 | relays[4] = ~relays[4]; | |
465 | ||
466 | if (cv2 > 1) | |
467 | relays[5] = ~relays[5]; | |
468 | ||
b58fbd99 | 469 | sr_dbg("hantek-dso: CH2 coupling %d", ctx->coupling_ch1); |
3b533202 BV |
470 | if (ctx->coupling_ch2 != COUPLING_AC) |
471 | relays[6] = ~relays[6]; | |
472 | ||
a370ef19 | 473 | if (!strcmp(ctx->triggersource, "EXT")) |
3b533202 BV |
474 | relays[7] = ~relays[7]; |
475 | ||
476 | if ((ret = libusb_control_transfer(ctx->usb->devhdl, | |
477 | LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETRELAYS, | |
478 | 0, 0, relays, sizeof(relays), 100)) != sizeof(relays)) { | |
479 | sr_err("failed to set relays: %d", ret); | |
480 | return SR_ERR; | |
481 | } | |
b58fbd99 | 482 | sr_dbg("hantek-dso: sent CTRL_SETRELAYS"); |
3b533202 BV |
483 | |
484 | return SR_OK; | |
485 | } | |
486 | ||
487 | SR_PRIV int dso_set_voffsets(struct context *ctx) | |
488 | { | |
489 | int offset, ret; | |
490 | uint16_t *ch_levels; | |
491 | // uint8_t offsets[17]; | |
492 | uint8_t offsets[] = {0x20,0x75,0x30,0x3f,0x20,0xbd,0x3f,0x02,0x20,0x00,0x71,0x01,0x2e,0x0b,0x3f,0x02,0x50}; | |
493 | //uint8_t offsets[] = {0xff, 0x75, 0x30, 0x3f, 0x20, 0xbd, | |
494 | // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 | |
495 | //}; | |
496 | ||
497 | sr_dbg("hantek-dso: sending CTRL_SETOFFSET"); | |
498 | ||
499 | // memset(offsets, 0, sizeof(offsets)); | |
500 | // /* Channel 1 */ | |
501 | // ch_levels = ctx->channel_levels[0][VOLTAGE_10mV - ctx->voltage_ch1]; | |
502 | // offset = (ch_levels[1] - ch_levels[0]) * ctx->voffset_ch1 + ch_levels[0]; | |
503 | // offsets[0] = (offset >> 8) | 0x20; | |
504 | // offsets[1] = offset & 0xff; | |
505 | // | |
506 | // /* Channel 2 */ | |
507 | // ch_levels = ctx->channel_levels[1][VOLTAGE_10mV - ctx->voltage_ch2]; | |
508 | // offset = (ch_levels[1] - ch_levels[0]) * ctx->voffset_ch2 + ch_levels[0]; | |
509 | // offsets[2] = (offset >> 8) | 0x20; | |
510 | // offsets[3] = offset & 0xff; | |
511 | // | |
512 | // /* Trigger */ | |
513 | // offset = MAX_VERT_TRIGGER * ctx->voffset_trigger; | |
514 | // offsets[4] = (offset >> 8) | 0x20; | |
515 | // offsets[5] = offset & 0xff; | |
516 | ||
517 | if ((ret = libusb_control_transfer(ctx->usb->devhdl, | |
518 | LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETOFFSET, | |
519 | 0, 0, offsets, sizeof(offsets), 100)) != sizeof(offsets)) { | |
520 | sr_err("failed to set offsets: %d", ret); | |
521 | return SR_ERR; | |
522 | } | |
523 | ||
524 | return SR_OK; | |
525 | } | |
526 | ||
527 | SR_PRIV int dso_enable_trigger(struct context *ctx) | |
528 | { | |
529 | int ret, tmp; | |
530 | uint8_t cmdstring[2]; | |
531 | ||
532 | sr_dbg("hantek-dso: sending CMD_ENABLE_TRIGGER"); | |
533 | ||
534 | memset(cmdstring, 0, sizeof(cmdstring)); | |
535 | cmdstring[0] = CMD_ENABLE_TRIGGER; | |
536 | cmdstring[1] = 0x00; | |
537 | ||
538 | if (send_begin(ctx) != SR_OK) | |
539 | return SR_ERR; | |
540 | ||
541 | if ((ret = libusb_bulk_transfer(ctx->usb->devhdl, | |
542 | DSO_EP_OUT | LIBUSB_ENDPOINT_OUT, | |
543 | cmdstring, sizeof(cmdstring), | |
544 | &tmp, 100)) != 0) { | |
545 | sr_err("Failed to enable trigger: %d", ret); | |
546 | return SR_ERR; | |
547 | } | |
548 | ||
549 | return SR_OK; | |
550 | } | |
551 | ||
552 | SR_PRIV int dso_force_trigger(struct context *ctx) | |
553 | { | |
554 | int ret, tmp; | |
555 | uint8_t cmdstring[2]; | |
556 | ||
557 | sr_dbg("hantek-dso: sending CMD_FORCE_TRIGGER"); | |
558 | ||
559 | memset(cmdstring, 0, sizeof(cmdstring)); | |
560 | cmdstring[0] = CMD_FORCE_TRIGGER; | |
561 | cmdstring[1] = 0x00; | |
562 | ||
563 | if (send_begin(ctx) != SR_OK) | |
564 | return SR_ERR; | |
565 | ||
566 | if ((ret = libusb_bulk_transfer(ctx->usb->devhdl, | |
567 | DSO_EP_OUT | LIBUSB_ENDPOINT_OUT, | |
568 | cmdstring, sizeof(cmdstring), | |
569 | &tmp, 100)) != 0) { | |
570 | sr_err("Failed to force trigger: %d", ret); | |
571 | return SR_ERR; | |
572 | } | |
573 | ||
574 | return SR_OK; | |
575 | } | |
576 | ||
577 | SR_PRIV int dso_init(struct context *ctx) | |
578 | { | |
579 | ||
580 | sr_dbg("hantek-dso: initializing dso"); | |
581 | ||
582 | if (get_channel_offsets(ctx) != SR_OK) | |
583 | return SR_ERR; | |
584 | ||
585 | if (dso_set_trigger_samplerate(ctx) != SR_OK) | |
586 | return SR_ERR; | |
587 | ||
588 | if (dso_set_filters(ctx) != SR_OK) | |
589 | return SR_ERR; | |
590 | ||
591 | if (dso_set_voltage(ctx) != SR_OK) | |
592 | return SR_ERR; | |
593 | ||
594 | if (dso_set_relays(ctx) != SR_OK) | |
595 | return SR_ERR; | |
596 | ||
597 | if (dso_set_voffsets(ctx) != SR_OK) | |
598 | return SR_ERR; | |
599 | ||
600 | if (dso_enable_trigger(ctx) != SR_OK) | |
601 | return SR_ERR; | |
602 | ||
3b533202 BV |
603 | return SR_OK; |
604 | } | |
605 | ||
606 | SR_PRIV uint8_t dso_get_capturestate(struct context *ctx) | |
607 | { | |
608 | int ret, tmp; | |
609 | uint8_t cmdstring[2], inbuf[512]; | |
610 | ||
611 | sr_dbg("hantek-dso: sending CMD_GET_CAPTURESTATE"); | |
612 | ||
613 | cmdstring[0] = CMD_GET_CAPTURESTATE; | |
614 | cmdstring[1] = 0; | |
615 | ||
616 | if ((ret = send_bulkcmd(ctx, cmdstring, sizeof(cmdstring))) != SR_OK) { | |
617 | sr_dbg("Failed to send get_capturestate command: %d", ret); | |
618 | return CAPTURE_UNKNOWN; | |
619 | } | |
620 | ||
621 | if ((ret = libusb_bulk_transfer(ctx->usb->devhdl, | |
622 | DSO_EP_IN | LIBUSB_ENDPOINT_IN, | |
623 | inbuf, 512, &tmp, 100)) != 0) { | |
624 | sr_dbg("Failed to get capturestate: %d", ret); | |
625 | return CAPTURE_UNKNOWN; | |
626 | } | |
627 | ||
628 | return inbuf[0]; | |
629 | } | |
630 | ||
631 | SR_PRIV uint8_t dso_capture_start(struct context *ctx) | |
632 | { | |
633 | int ret; | |
634 | uint8_t cmdstring[2]; | |
635 | ||
636 | sr_dbg("hantek-dso: sending CMD_CAPTURE_START"); | |
637 | ||
638 | cmdstring[0] = CMD_CAPTURE_START; | |
639 | cmdstring[1] = 0; | |
640 | ||
641 | if ((ret = send_bulkcmd(ctx, cmdstring, sizeof(cmdstring))) != SR_OK) { | |
642 | sr_err("Failed to send capture_start command: %d", ret); | |
643 | return SR_ERR; | |
644 | } | |
645 | ||
646 | return SR_OK; | |
647 | } | |
648 | ||
649 | SR_PRIV int dso_get_channeldata(struct context *ctx, libusb_transfer_cb_fn cb) | |
650 | { | |
651 | struct libusb_transfer *transfer; | |
652 | int num_transfers, ret, i; | |
653 | uint8_t cmdstring[2]; | |
654 | unsigned char *buf; | |
655 | ||
656 | sr_dbg("hantek-dso: sending CMD_GET_CHANNELDATA"); | |
657 | ||
658 | cmdstring[0] = CMD_GET_CHANNELDATA; | |
659 | cmdstring[1] = 0; | |
660 | ||
661 | if ((ret = send_bulkcmd(ctx, cmdstring, sizeof(cmdstring))) != SR_OK) { | |
662 | sr_err("Failed to get channel data: %d", ret); | |
663 | return SR_ERR; | |
664 | } | |
665 | ||
666 | /* TODO: dso-2xxx only */ | |
667 | num_transfers = ctx->framesize * sizeof(unsigned short) / ctx->epin_maxpacketsize; | |
668 | sr_dbg("hantek-dso: queueing up %d transfers", num_transfers); | |
669 | for (i = 0; i < num_transfers; i++) { | |
670 | if (!(buf = g_try_malloc(ctx->epin_maxpacketsize))) { | |
671 | sr_err("hantek-dso: %s: buf malloc failed", __func__); | |
672 | return SR_ERR_MALLOC; | |
673 | } | |
674 | transfer = libusb_alloc_transfer(0); | |
675 | libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl, | |
676 | DSO_EP_IN | LIBUSB_ENDPOINT_IN, buf, | |
677 | ctx->epin_maxpacketsize, cb, ctx, 40); | |
678 | if ((ret = libusb_submit_transfer(transfer)) != 0) { | |
679 | sr_err("failed to submit transfer: %d", ret); | |
680 | /* TODO: Free them all. */ | |
681 | libusb_free_transfer(transfer); | |
682 | g_free(buf); | |
683 | return SR_ERR; | |
684 | } | |
685 | } | |
686 | ||
687 | return SR_OK; | |
688 | } | |
689 |