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