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