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