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