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