]>
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 | 30 | |
269971dd | 31 | static int send_begin(struct dev_context *devc) |
3b533202 BV |
32 | { |
33 | int ret; | |
34 | unsigned char buffer[] = {0x0f, 0x03, 0x03, 0x03, 0x68, 0xac, 0xfe, | |
35 | 0x00, 0x01, 0x00}; | |
36 | ||
e98b7f1b | 37 | sr_dbg("Sending CTRL_BEGINCOMMAND."); |
3b533202 | 38 | |
269971dd | 39 | if ((ret = libusb_control_transfer(devc->usb->devhdl, |
3b533202 BV |
40 | LIBUSB_REQUEST_TYPE_VENDOR, CTRL_BEGINCOMMAND, |
41 | 0, 0, buffer, sizeof(buffer), 200)) != sizeof(buffer)) { | |
e98b7f1b | 42 | sr_err("Failed to send begincommand: %d.", ret); |
3b533202 BV |
43 | return SR_ERR; |
44 | } | |
45 | ||
46 | return SR_OK; | |
47 | } | |
48 | ||
269971dd | 49 | static int send_bulkcmd(struct dev_context *devc, uint8_t *cmdstring, int cmdlen) |
3b533202 BV |
50 | { |
51 | int ret, tmp; | |
52 | ||
269971dd | 53 | if (send_begin(devc) != SR_OK) |
3b533202 BV |
54 | return SR_ERR; |
55 | ||
269971dd | 56 | if ((ret = libusb_bulk_transfer(devc->usb->devhdl, |
3b533202 BV |
57 | DSO_EP_OUT | LIBUSB_ENDPOINT_OUT, |
58 | cmdstring, cmdlen, &tmp, 200)) != 0) | |
59 | return SR_ERR; | |
60 | ||
61 | return SR_OK; | |
62 | } | |
63 | ||
64 | SR_PRIV int dso_getmps(libusb_device *dev) | |
65 | { | |
66 | struct libusb_device_descriptor des; | |
67 | struct libusb_config_descriptor *conf_dsc; | |
68 | const struct libusb_interface_descriptor *intf_dsc; | |
69 | int mps; | |
70 | ||
71 | if (libusb_get_device_descriptor(dev, &des) != 0) | |
72 | return 0; | |
73 | ||
74 | if (des.bNumConfigurations != 1) | |
75 | return 0; | |
76 | ||
77 | if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0) | |
78 | return 0; | |
79 | ||
80 | mps = 0; | |
81 | intf_dsc = &(conf_dsc->interface[0].altsetting[0]); | |
82 | if (intf_dsc->bNumEndpoints != 2) | |
83 | goto err; | |
84 | ||
85 | if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) != | |
86 | (2 | LIBUSB_ENDPOINT_OUT)) | |
87 | /* The first endpoint should be 2 (outbound). */ | |
88 | goto err; | |
89 | ||
90 | if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) != | |
91 | (6 | LIBUSB_ENDPOINT_IN)) | |
92 | /* The second endpoint should be 6 (inbound). */ | |
93 | goto err; | |
94 | ||
95 | mps = intf_dsc->endpoint[1].wMaxPacketSize; | |
96 | ||
97 | err: | |
98 | if (conf_dsc) | |
99 | libusb_free_config_descriptor(conf_dsc); | |
100 | ||
101 | return mps; | |
102 | } | |
103 | ||
25a0f108 | 104 | SR_PRIV int dso_open(struct sr_dev_inst *sdi) |
3b533202 BV |
105 | { |
106 | libusb_device **devlist; | |
107 | struct libusb_device_descriptor des; | |
269971dd | 108 | struct dev_context *devc; |
d4abb463 | 109 | struct drv_context *drvc = hantek_dso_driver_info.priv; |
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; | |
d4abb463 | 119 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); |
3b533202 BV |
120 | for (i = 0; devlist[i]; i++) { |
121 | if ((err = libusb_get_device_descriptor(devlist[i], &des))) { | |
e98b7f1b | 122 | sr_err("Failed to get device descriptor: %d.", err); |
3b533202 BV |
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 | |
e98b7f1b UH |
155 | if (!(devc->epin_maxpacketsize = dso_getmps(devlist[i]))) |
156 | sr_err("Wrong endpoint profile."); | |
3b533202 BV |
157 | else { |
158 | sdi->status = SR_ST_ACTIVE; | |
e98b7f1b | 159 | sr_info("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 { | |
e98b7f1b | 164 | sr_err("Failed to open device: %d.", err); |
3b533202 BV |
165 | } |
166 | ||
e98b7f1b | 167 | /* If we made it here, we handled the device (somehow). */ |
3b533202 BV |
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 | ||
e98b7f1b | 187 | sr_info("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 | 200 | |
e98b7f1b | 201 | sr_dbg("Getting channel offsets."); |
3b533202 | 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)) { | |
e98b7f1b | 209 | sr_err("Failed to get channel offsets: %d.", ret); |
3b533202 BV |
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++) { | |
e98b7f1b UH |
220 | devc->channel_levels[chan][v][0] = |
221 | g_ntohs(devc->channel_levels[chan][v][0]); | |
222 | devc->channel_levels[chan][v][1] = | |
223 | g_ntohs(devc->channel_levels[chan][v][1]); | |
2715c0b8 BV |
224 | } |
225 | } | |
226 | ||
227 | if (sr_log_loglevel_get() >= SR_LOG_DBG) { | |
228 | gs = g_string_sized_new(128); | |
229 | for (chan = 0; chan < 2; chan++) { | |
e98b7f1b | 230 | g_string_printf(gs, "CH%d:", chan + 1); |
2715c0b8 BV |
231 | for (v = 0; v < 9; v++) { |
232 | g_string_append_printf(gs, " %.4x-%.4x", | |
e98b7f1b UH |
233 | devc->channel_levels[chan][v][0], |
234 | devc->channel_levels[chan][v][1]); | |
2715c0b8 BV |
235 | } |
236 | sr_dbg(gs->str); | |
237 | } | |
238 | g_string_free(gs, TRUE); | |
239 | } | |
240 | ||
3b533202 BV |
241 | return SR_OK; |
242 | } | |
243 | ||
269971dd | 244 | SR_PRIV int dso_set_trigger_samplerate(struct dev_context *devc) |
3b533202 BV |
245 | { |
246 | int ret, tmp; | |
247 | uint8_t cmdstring[12]; | |
3b533202 | 248 | uint16_t timebase_small[] = { 0xffff, 0xfffc, 0xfff7, 0xffe8, 0xffce, |
e98b7f1b | 249 | 0xff9c, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79, 0xd8f1 }; |
3b533202 | 250 | uint16_t timebase_large[] = { 0xffff, 0x0000, 0xfffc, 0xfff7, 0xffe8, |
e98b7f1b | 251 | 0xffce, 0xff9d, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79 }; |
3b533202 | 252 | |
e98b7f1b | 253 | sr_dbg("Preparing CMD_SET_TRIGGER_SAMPLERATE."); |
3b533202 BV |
254 | |
255 | memset(cmdstring, 0, sizeof(cmdstring)); | |
256 | /* Command */ | |
257 | cmdstring[0] = CMD_SET_TRIGGER_SAMPLERATE; | |
258 | ||
259 | /* Trigger source */ | |
e98b7f1b | 260 | sr_dbg("Trigger source %s.", devc->triggersource); |
269971dd | 261 | if (!strcmp("CH2", devc->triggersource)) |
a370ef19 | 262 | tmp = 0; |
269971dd | 263 | else if (!strcmp("CH1", devc->triggersource)) |
a370ef19 | 264 | tmp = 1; |
269971dd | 265 | else if (!strcmp("EXT", devc->triggersource)) |
a370ef19 BV |
266 | tmp = 2; |
267 | else { | |
e98b7f1b | 268 | sr_err("Invalid trigger source: '%s'.", devc->triggersource); |
a370ef19 BV |
269 | return SR_ERR_ARG; |
270 | } | |
271 | cmdstring[2] = tmp; | |
3b533202 BV |
272 | |
273 | /* Frame size */ | |
e98b7f1b | 274 | sr_dbg("Frame size: %d.", devc->framesize); |
269971dd | 275 | cmdstring[2] |= (devc->framesize == FRAMESIZE_SMALL ? 0x01 : 0x02) << 2; |
bc79e906 BV |
276 | |
277 | /* Timebase fast */ | |
e98b7f1b | 278 | sr_dbg("Time base index: %d.", devc->timebase); |
269971dd | 279 | switch (devc->framesize) { |
bc79e906 | 280 | case FRAMESIZE_SMALL: |
269971dd | 281 | if (devc->timebase < TIME_20us) |
bc79e906 | 282 | tmp = 0; |
269971dd | 283 | else if (devc->timebase == TIME_20us) |
bc79e906 | 284 | tmp = 1; |
269971dd | 285 | else if (devc->timebase == TIME_40us) |
bc79e906 | 286 | tmp = 2; |
269971dd | 287 | else if (devc->timebase == TIME_100us) |
bc79e906 | 288 | tmp = 3; |
269971dd | 289 | else if (devc->timebase >= TIME_200us) |
bc79e906 BV |
290 | tmp = 4; |
291 | break; | |
292 | case FRAMESIZE_LARGE: | |
269971dd | 293 | if (devc->timebase < TIME_40us) { |
e98b7f1b | 294 | sr_err("Timebase < 40us only supported with 10K buffer."); |
bc79e906 BV |
295 | return SR_ERR_ARG; |
296 | } | |
269971dd | 297 | else if (devc->timebase == TIME_40us) |
bc79e906 | 298 | tmp = 0; |
269971dd | 299 | else if (devc->timebase == TIME_100us) |
bc79e906 | 300 | tmp = 2; |
269971dd | 301 | else if (devc->timebase == TIME_200us) |
bc79e906 | 302 | tmp = 3; |
269971dd | 303 | else if (devc->timebase >= TIME_400us) |
bc79e906 BV |
304 | tmp = 4; |
305 | break; | |
3b533202 | 306 | } |
bc79e906 BV |
307 | cmdstring[2] |= (tmp & 0x07) << 5; |
308 | ||
309 | /* Enabled channels: 00=CH1 01=CH2 10=both */ | |
e98b7f1b | 310 | sr_dbg("Channels CH1=%d CH2=%d", devc->ch1_enabled, devc->ch2_enabled); |
269971dd | 311 | tmp = (((devc->ch2_enabled ? 1 : 0) << 1) + (devc->ch1_enabled ? 1 : 0)) - 1; |
6e71ef3b | 312 | cmdstring[3] = tmp; |
3b533202 | 313 | |
bc79e906 | 314 | /* Fast rates channel */ |
e98b7f1b | 315 | /* TODO: Is this right? */ |
269971dd | 316 | tmp = devc->timebase < TIME_10us ? 1 : 0; |
bc79e906 | 317 | cmdstring[3] |= tmp << 2; |
3b533202 | 318 | |
bc79e906 | 319 | /* Trigger slope: 0=positive 1=negative */ |
e98b7f1b UH |
320 | /* TODO: Does this work? */ |
321 | sr_dbg("Trigger slope: %d.", devc->triggerslope); | |
269971dd | 322 | cmdstring[3] |= (devc->triggerslope == SLOPE_NEGATIVE ? 1 : 0) << 3; |
3b533202 | 323 | |
a370ef19 | 324 | /* Timebase slow */ |
269971dd | 325 | if (devc->timebase < TIME_100us) |
3b533202 | 326 | tmp = 0; |
269971dd | 327 | else if (devc->timebase > TIME_400ms) |
3b533202 BV |
328 | tmp = 0xffed; |
329 | else { | |
269971dd BV |
330 | if (devc->framesize == FRAMESIZE_SMALL) |
331 | tmp = timebase_small[devc->timebase - 3]; | |
3b533202 | 332 | else |
269971dd | 333 | tmp = timebase_large[devc->timebase - 3]; |
3b533202 | 334 | } |
bc79e906 BV |
335 | cmdstring[4] = tmp & 0xff; |
336 | cmdstring[5] = (tmp >> 8) & 0xff; | |
337 | ||
338 | /* Horizontal trigger position */ | |
e98b7f1b | 339 | sr_dbg("Trigger position: %3.2f.", devc->triggerposition); |
269971dd | 340 | tmp = 0x77fff + 0x8000 * devc->triggerposition; |
3b533202 BV |
341 | cmdstring[6] = tmp & 0xff; |
342 | cmdstring[7] = (tmp >> 8) & 0xff; | |
3b533202 BV |
343 | cmdstring[10] = (tmp >> 16) & 0xff; |
344 | ||
269971dd | 345 | if (send_begin(devc) != SR_OK) |
3b533202 BV |
346 | return SR_ERR; |
347 | ||
269971dd | 348 | if ((ret = libusb_bulk_transfer(devc->usb->devhdl, |
3b533202 BV |
349 | DSO_EP_OUT | LIBUSB_ENDPOINT_OUT, |
350 | cmdstring, sizeof(cmdstring), | |
351 | &tmp, 100)) != 0) { | |
e98b7f1b | 352 | sr_err("Failed to set trigger/samplerate: %d.", ret); |
3b533202 BV |
353 | return SR_ERR; |
354 | } | |
e98b7f1b | 355 | sr_dbg("Sent CMD_SET_TRIGGER_SAMPLERATE."); |
3b533202 BV |
356 | |
357 | return SR_OK; | |
358 | } | |
359 | ||
269971dd | 360 | SR_PRIV int dso_set_filters(struct dev_context *devc) |
3b533202 BV |
361 | { |
362 | int ret, tmp; | |
363 | uint8_t cmdstring[8]; | |
364 | ||
e98b7f1b | 365 | sr_dbg("Preparing CMD_SET_FILTERS."); |
3b533202 BV |
366 | |
367 | memset(cmdstring, 0, sizeof(cmdstring)); | |
368 | cmdstring[0] = CMD_SET_FILTERS; | |
369 | cmdstring[1] = 0x0f; | |
269971dd | 370 | if (devc->filter_ch1) { |
e98b7f1b | 371 | sr_dbg("Turning on CH1 filter."); |
3b533202 | 372 | cmdstring[2] |= 0x80; |
ebb781a6 | 373 | } |
269971dd | 374 | if (devc->filter_ch2) { |
e98b7f1b | 375 | sr_dbg("Turning on CH2 filter."); |
3b533202 | 376 | cmdstring[2] |= 0x40; |
ebb781a6 | 377 | } |
269971dd | 378 | if (devc->filter_trigger) { |
ebb781a6 | 379 | /* TODO: supported on the DSO-2090? */ |
e98b7f1b | 380 | sr_dbg("Turning on trigger filter."); |
3b533202 | 381 | cmdstring[2] |= 0x20; |
ebb781a6 | 382 | } |
3b533202 | 383 | |
269971dd | 384 | if (send_begin(devc) != SR_OK) |
3b533202 BV |
385 | return SR_ERR; |
386 | ||
269971dd | 387 | if ((ret = libusb_bulk_transfer(devc->usb->devhdl, |
3b533202 BV |
388 | DSO_EP_OUT | LIBUSB_ENDPOINT_OUT, |
389 | cmdstring, sizeof(cmdstring), | |
390 | &tmp, 100)) != 0) { | |
391 | sr_err("Failed to set filters: %d", ret); | |
392 | return SR_ERR; | |
393 | } | |
e98b7f1b | 394 | sr_dbg("Sent CMD_SET_FILTERS."); |
3b533202 BV |
395 | |
396 | return SR_OK; | |
397 | } | |
398 | ||
269971dd | 399 | SR_PRIV int dso_set_voltage(struct dev_context *devc) |
3b533202 BV |
400 | { |
401 | int ret, tmp; | |
402 | uint8_t cmdstring[8]; | |
403 | ||
e98b7f1b | 404 | sr_dbg("Preparing CMD_SET_VOLTAGE."); |
3b533202 BV |
405 | |
406 | memset(cmdstring, 0, sizeof(cmdstring)); | |
407 | cmdstring[0] = CMD_SET_VOLTAGE; | |
408 | cmdstring[1] = 0x0f; | |
313deed2 BV |
409 | cmdstring[2] = 0x30; |
410 | ||
411 | /* CH1 volts/div is encoded in bits 0-1 */ | |
e98b7f1b | 412 | sr_dbg("CH1 vdiv index: %d.", devc->voltage_ch1); |
269971dd | 413 | switch (devc->voltage_ch1) { |
313deed2 BV |
414 | case VDIV_1V: |
415 | case VDIV_100MV: | |
416 | case VDIV_10MV: | |
417 | cmdstring[2] |= 0x00; | |
418 | break; | |
419 | case VDIV_2V: | |
420 | case VDIV_200MV: | |
421 | case VDIV_20MV: | |
422 | cmdstring[2] |= 0x01; | |
423 | break; | |
424 | case VDIV_5V: | |
425 | case VDIV_500MV: | |
426 | case VDIV_50MV: | |
427 | cmdstring[2] |= 0x02; | |
428 | break; | |
429 | } | |
430 | ||
431 | /* CH2 volts/div is encoded in bits 2-3 */ | |
e98b7f1b | 432 | sr_dbg("CH2 vdiv index: %d.", devc->voltage_ch2); |
269971dd | 433 | switch (devc->voltage_ch2) { |
313deed2 BV |
434 | case VDIV_1V: |
435 | case VDIV_100MV: | |
436 | case VDIV_10MV: | |
437 | cmdstring[2] |= 0x00; | |
438 | break; | |
439 | case VDIV_2V: | |
440 | case VDIV_200MV: | |
441 | case VDIV_20MV: | |
1d97091e | 442 | cmdstring[2] |= 0x04; |
313deed2 BV |
443 | break; |
444 | case VDIV_5V: | |
445 | case VDIV_500MV: | |
446 | case VDIV_50MV: | |
1d97091e | 447 | cmdstring[2] |= 0x08; |
313deed2 BV |
448 | break; |
449 | } | |
3b533202 | 450 | |
269971dd | 451 | if (send_begin(devc) != SR_OK) |
3b533202 BV |
452 | return SR_ERR; |
453 | ||
269971dd | 454 | if ((ret = libusb_bulk_transfer(devc->usb->devhdl, |
3b533202 BV |
455 | DSO_EP_OUT | LIBUSB_ENDPOINT_OUT, |
456 | cmdstring, sizeof(cmdstring), | |
457 | &tmp, 100)) != 0) { | |
e98b7f1b | 458 | sr_err("Failed to set voltage: %d.", ret); |
3b533202 BV |
459 | return SR_ERR; |
460 | } | |
e98b7f1b | 461 | sr_dbg("Sent CMD_SET_VOLTAGE."); |
3b533202 BV |
462 | |
463 | return SR_OK; | |
464 | } | |
465 | ||
269971dd | 466 | SR_PRIV int dso_set_relays(struct dev_context *devc) |
3b533202 | 467 | { |
a217bcdf BV |
468 | GString *gs; |
469 | int ret, i; | |
470 | uint8_t relays[17] = { 0x00, 0x04, 0x08, 0x02, 0x20, 0x40, 0x10, 0x01, | |
3b533202 BV |
471 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
472 | ||
e98b7f1b | 473 | sr_dbg("Preparing CTRL_SETRELAYS."); |
3b533202 | 474 | |
269971dd | 475 | if (devc->voltage_ch1 < VDIV_1V) |
3b533202 BV |
476 | relays[1] = ~relays[1]; |
477 | ||
269971dd | 478 | if (devc->voltage_ch1 < VDIV_100MV) |
3b533202 BV |
479 | relays[2] = ~relays[2]; |
480 | ||
e98b7f1b | 481 | sr_dbg("CH1 coupling: %d.", devc->coupling_ch1); |
269971dd | 482 | if (devc->coupling_ch1 != COUPLING_AC) |
3b533202 BV |
483 | relays[3] = ~relays[3]; |
484 | ||
269971dd | 485 | if (devc->voltage_ch2 < VDIV_1V) |
3b533202 BV |
486 | relays[4] = ~relays[4]; |
487 | ||
269971dd | 488 | if (devc->voltage_ch2 < VDIV_100MV) |
3b533202 BV |
489 | relays[5] = ~relays[5]; |
490 | ||
e98b7f1b | 491 | sr_dbg("CH2 coupling: %d.", devc->coupling_ch1); |
269971dd | 492 | if (devc->coupling_ch2 != COUPLING_AC) |
3b533202 BV |
493 | relays[6] = ~relays[6]; |
494 | ||
269971dd | 495 | if (!strcmp(devc->triggersource, "EXT")) |
3b533202 BV |
496 | relays[7] = ~relays[7]; |
497 | ||
a217bcdf BV |
498 | if (sr_log_loglevel_get() >= SR_LOG_DBG) { |
499 | gs = g_string_sized_new(128); | |
e98b7f1b | 500 | g_string_printf(gs, "Relays:"); |
a217bcdf BV |
501 | for (i = 0; i < 17; i++) |
502 | g_string_append_printf(gs, " %.2x", relays[i]); | |
503 | sr_dbg(gs->str); | |
504 | g_string_free(gs, TRUE); | |
505 | } | |
506 | ||
269971dd | 507 | if ((ret = libusb_control_transfer(devc->usb->devhdl, |
3b533202 | 508 | LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETRELAYS, |
a217bcdf | 509 | 0, 0, relays, 17, 100)) != sizeof(relays)) { |
e98b7f1b | 510 | sr_err("Failed to set relays: %d.", ret); |
3b533202 BV |
511 | return SR_ERR; |
512 | } | |
e98b7f1b | 513 | sr_dbg("Sent CTRL_SETRELAYS."); |
3b533202 BV |
514 | |
515 | return SR_OK; | |
516 | } | |
517 | ||
269971dd | 518 | SR_PRIV int dso_set_voffsets(struct dev_context *devc) |
3b533202 BV |
519 | { |
520 | int offset, ret; | |
521 | uint16_t *ch_levels; | |
2715c0b8 BV |
522 | uint8_t offsets[17]; |
523 | ||
e98b7f1b | 524 | sr_dbg("Preparing CTRL_SETOFFSET."); |
2715c0b8 BV |
525 | |
526 | memset(offsets, 0, sizeof(offsets)); | |
527 | /* Channel 1 */ | |
269971dd BV |
528 | ch_levels = devc->channel_levels[0][devc->voltage_ch1]; |
529 | offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch1 + ch_levels[0]; | |
2715c0b8 BV |
530 | offsets[0] = (offset >> 8) | 0x20; |
531 | offsets[1] = offset & 0xff; | |
e98b7f1b UH |
532 | sr_dbg("CH1 offset: %3.2f (%.2x%.2x).", devc->voffset_ch1, |
533 | offsets[0], offsets[1]); | |
2715c0b8 BV |
534 | |
535 | /* Channel 2 */ | |
269971dd BV |
536 | ch_levels = devc->channel_levels[1][devc->voltage_ch2]; |
537 | offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch2 + ch_levels[0]; | |
2715c0b8 BV |
538 | offsets[2] = (offset >> 8) | 0x20; |
539 | offsets[3] = offset & 0xff; | |
e98b7f1b UH |
540 | sr_dbg("CH2 offset: %3.2f (%.2x%.2x).", devc->voffset_ch2, |
541 | offsets[2], offsets[3]); | |
2715c0b8 BV |
542 | |
543 | /* Trigger */ | |
269971dd | 544 | offset = MAX_VERT_TRIGGER * devc->voffset_trigger; |
2715c0b8 BV |
545 | offsets[4] = (offset >> 8) | 0x20; |
546 | offsets[5] = offset & 0xff; | |
e98b7f1b | 547 | sr_dbg("Trigger offset: %3.2f (%.2x%.2x).", devc->voffset_trigger, |
2715c0b8 | 548 | offsets[4], offsets[5]); |
3b533202 | 549 | |
269971dd | 550 | if ((ret = libusb_control_transfer(devc->usb->devhdl, |
3b533202 BV |
551 | LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETOFFSET, |
552 | 0, 0, offsets, sizeof(offsets), 100)) != sizeof(offsets)) { | |
e98b7f1b | 553 | sr_err("Failed to set offsets: %d.", ret); |
3b533202 BV |
554 | return SR_ERR; |
555 | } | |
e98b7f1b | 556 | sr_dbg("Sent CTRL_SETOFFSET."); |
3b533202 BV |
557 | |
558 | return SR_OK; | |
559 | } | |
560 | ||
269971dd | 561 | SR_PRIV int dso_enable_trigger(struct dev_context *devc) |
3b533202 BV |
562 | { |
563 | int ret, tmp; | |
564 | uint8_t cmdstring[2]; | |
565 | ||
e98b7f1b | 566 | sr_dbg("Sending CMD_ENABLE_TRIGGER."); |
3b533202 BV |
567 | |
568 | memset(cmdstring, 0, sizeof(cmdstring)); | |
569 | cmdstring[0] = CMD_ENABLE_TRIGGER; | |
570 | cmdstring[1] = 0x00; | |
571 | ||
269971dd | 572 | if (send_begin(devc) != SR_OK) |
3b533202 BV |
573 | return SR_ERR; |
574 | ||
269971dd | 575 | if ((ret = libusb_bulk_transfer(devc->usb->devhdl, |
3b533202 BV |
576 | DSO_EP_OUT | LIBUSB_ENDPOINT_OUT, |
577 | cmdstring, sizeof(cmdstring), | |
578 | &tmp, 100)) != 0) { | |
e98b7f1b | 579 | sr_err("Failed to enable trigger: %d.", ret); |
3b533202 BV |
580 | return SR_ERR; |
581 | } | |
582 | ||
583 | return SR_OK; | |
584 | } | |
585 | ||
269971dd | 586 | SR_PRIV int dso_force_trigger(struct dev_context *devc) |
3b533202 BV |
587 | { |
588 | int ret, tmp; | |
589 | uint8_t cmdstring[2]; | |
590 | ||
e98b7f1b | 591 | sr_dbg("Sending CMD_FORCE_TRIGGER."); |
3b533202 BV |
592 | |
593 | memset(cmdstring, 0, sizeof(cmdstring)); | |
594 | cmdstring[0] = CMD_FORCE_TRIGGER; | |
595 | cmdstring[1] = 0x00; | |
596 | ||
269971dd | 597 | if (send_begin(devc) != SR_OK) |
3b533202 BV |
598 | return SR_ERR; |
599 | ||
269971dd | 600 | if ((ret = libusb_bulk_transfer(devc->usb->devhdl, |
3b533202 BV |
601 | DSO_EP_OUT | LIBUSB_ENDPOINT_OUT, |
602 | cmdstring, sizeof(cmdstring), | |
603 | &tmp, 100)) != 0) { | |
e98b7f1b | 604 | sr_err("Failed to force trigger: %d.", ret); |
3b533202 BV |
605 | return SR_ERR; |
606 | } | |
607 | ||
608 | return SR_OK; | |
609 | } | |
610 | ||
269971dd | 611 | SR_PRIV int dso_init(struct dev_context *devc) |
3b533202 BV |
612 | { |
613 | ||
e98b7f1b | 614 | sr_dbg("Initializing DSO."); |
3b533202 | 615 | |
269971dd | 616 | if (get_channel_offsets(devc) != SR_OK) |
3b533202 BV |
617 | return SR_ERR; |
618 | ||
269971dd | 619 | if (dso_set_trigger_samplerate(devc) != SR_OK) |
3b533202 BV |
620 | return SR_ERR; |
621 | ||
269971dd | 622 | if (dso_set_filters(devc) != SR_OK) |
3b533202 BV |
623 | return SR_ERR; |
624 | ||
269971dd | 625 | if (dso_set_voltage(devc) != SR_OK) |
3b533202 BV |
626 | return SR_ERR; |
627 | ||
269971dd | 628 | if (dso_set_relays(devc) != SR_OK) |
3b533202 BV |
629 | return SR_ERR; |
630 | ||
269971dd | 631 | if (dso_set_voffsets(devc) != SR_OK) |
3b533202 BV |
632 | return SR_ERR; |
633 | ||
269971dd | 634 | if (dso_enable_trigger(devc) != SR_OK) |
3b533202 BV |
635 | return SR_ERR; |
636 | ||
3b533202 BV |
637 | return SR_OK; |
638 | } | |
639 | ||
e98b7f1b UH |
640 | SR_PRIV int dso_get_capturestate(struct dev_context *devc, |
641 | uint8_t *capturestate, | |
642 | uint32_t *trigger_offset) | |
3b533202 | 643 | { |
e05a174b BV |
644 | int ret, tmp, i; |
645 | unsigned int bitvalue, toff; | |
3b533202 BV |
646 | uint8_t cmdstring[2], inbuf[512]; |
647 | ||
e98b7f1b | 648 | sr_dbg("Sending CMD_GET_CAPTURESTATE."); |
3b533202 BV |
649 | |
650 | cmdstring[0] = CMD_GET_CAPTURESTATE; | |
651 | cmdstring[1] = 0; | |
652 | ||
269971dd | 653 | if ((ret = send_bulkcmd(devc, cmdstring, sizeof(cmdstring))) != SR_OK) { |
e98b7f1b | 654 | sr_dbg("Failed to send get_capturestate command: %d.", ret); |
6e6eeff4 | 655 | return SR_ERR; |
3b533202 BV |
656 | } |
657 | ||
269971dd | 658 | if ((ret = libusb_bulk_transfer(devc->usb->devhdl, |
3b533202 BV |
659 | DSO_EP_IN | LIBUSB_ENDPOINT_IN, |
660 | inbuf, 512, &tmp, 100)) != 0) { | |
e98b7f1b | 661 | sr_dbg("Failed to get capturestate: %d.", ret); |
6e6eeff4 | 662 | return SR_ERR; |
3b533202 | 663 | } |
6e6eeff4 | 664 | *capturestate = inbuf[0]; |
e05a174b BV |
665 | toff = (inbuf[1] << 16) | (inbuf[3] << 8) | inbuf[2]; |
666 | ||
e98b7f1b UH |
667 | /* |
668 | * This conversion comes from the openhantek project. | |
e05a174b BV |
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 | ||
e98b7f1b | 689 | sr_dbg("Sending CMD_CAPTURE_START."); |
3b533202 BV |
690 | |
691 | cmdstring[0] = CMD_CAPTURE_START; | |
692 | cmdstring[1] = 0; | |
693 | ||
269971dd | 694 | if ((ret = send_bulkcmd(devc, cmdstring, sizeof(cmdstring))) != SR_OK) { |
e98b7f1b | 695 | sr_err("Failed to send capture_start command: %d.", ret); |
3b533202 BV |
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 | ||
e98b7f1b | 709 | sr_dbg("Sending CMD_GET_CHANNELDATA."); |
3b533202 BV |
710 | |
711 | cmdstring[0] = CMD_GET_CHANNELDATA; | |
712 | cmdstring[1] = 0; | |
713 | ||
269971dd | 714 | if ((ret = send_bulkcmd(devc, cmdstring, sizeof(cmdstring))) != SR_OK) { |
e98b7f1b | 715 | sr_err("Failed to get channel data: %d.", ret); |
3b533202 BV |
716 | return SR_ERR; |
717 | } | |
718 | ||
e98b7f1b UH |
719 | /* TODO: DSO-2xxx only. */ |
720 | num_transfers = devc->framesize * | |
721 | sizeof(unsigned short) / devc->epin_maxpacketsize; | |
722 | sr_dbg("Queueing up %d transfers.", num_transfers); | |
3b533202 | 723 | for (i = 0; i < num_transfers; i++) { |
269971dd | 724 | if (!(buf = g_try_malloc(devc->epin_maxpacketsize))) { |
e98b7f1b | 725 | sr_err("Failed to malloc USB endpoint buffer."); |
3b533202 BV |
726 | return SR_ERR_MALLOC; |
727 | } | |
728 | transfer = libusb_alloc_transfer(0); | |
269971dd | 729 | libusb_fill_bulk_transfer(transfer, devc->usb->devhdl, |
3b533202 | 730 | DSO_EP_IN | LIBUSB_ENDPOINT_IN, buf, |
269971dd | 731 | devc->epin_maxpacketsize, cb, devc, 40); |
3b533202 | 732 | if ((ret = libusb_submit_transfer(transfer)) != 0) { |
e98b7f1b | 733 | sr_err("Failed to submit transfer: %d.", ret); |
3b533202 BV |
734 | /* TODO: Free them all. */ |
735 | libusb_free_transfer(transfer); | |
736 | g_free(buf); | |
737 | return SR_ERR; | |
738 | } | |
739 | } | |
740 | ||
741 | return SR_OK; | |
742 | } |