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