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