]> sigrok.org Git - libsigrok.git/blame - hardware/hantek-dso/dso.c
sr: add period parser
[libsigrok.git] / hardware / hantek-dso / dso.c
CommitLineData
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
22#include "sigrok.h"
23#include "sigrok-internal.h"
24#include "config.h"
25#include "dso.h"
26#include <string.h>
27#include <glib.h>
28#include <libusb.h>
29
30extern libusb_context *usb_context;
31extern GSList *dev_insts;
32
33
34static int send_begin(struct context *ctx)
35{
36 int ret;
37 unsigned char buffer[] = {0x0f, 0x03, 0x03, 0x03, 0x68, 0xac, 0xfe,
38 0x00, 0x01, 0x00};
39
40 sr_dbg("hantek-dso: sending CTRL_BEGINCOMMAND");
41
42 if ((ret = libusb_control_transfer(ctx->usb->devhdl,
43 LIBUSB_REQUEST_TYPE_VENDOR, CTRL_BEGINCOMMAND,
44 0, 0, buffer, sizeof(buffer), 200)) != sizeof(buffer)) {
45 sr_err("failed to send begincommand: %d", ret);
46 return SR_ERR;
47 }
48
49 return SR_OK;
50}
51
52static int send_bulkcmd(struct context *ctx, uint8_t *cmdstring, int cmdlen)
53{
54 int ret, tmp;
55
56 if (send_begin(ctx) != SR_OK)
57 return SR_ERR;
58
59 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
60 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
61 cmdstring, cmdlen, &tmp, 200)) != 0)
62 return SR_ERR;
63
64 return SR_OK;
65}
66
67SR_PRIV int dso_getmps(libusb_device *dev)
68{
69 struct libusb_device_descriptor des;
70 struct libusb_config_descriptor *conf_dsc;
71 const struct libusb_interface_descriptor *intf_dsc;
72 int mps;
73
74 if (libusb_get_device_descriptor(dev, &des) != 0)
75 return 0;
76
77 if (des.bNumConfigurations != 1)
78 return 0;
79
80 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
81 return 0;
82
83 mps = 0;
84 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
85 if (intf_dsc->bNumEndpoints != 2)
86 goto err;
87
88 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
89 (2 | LIBUSB_ENDPOINT_OUT))
90 /* The first endpoint should be 2 (outbound). */
91 goto err;
92
93 if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
94 (6 | LIBUSB_ENDPOINT_IN))
95 /* The second endpoint should be 6 (inbound). */
96 goto err;
97
98 mps = intf_dsc->endpoint[1].wMaxPacketSize;
99
100err:
101 if (conf_dsc)
102 libusb_free_config_descriptor(conf_dsc);
103
104 return mps;
105}
106
107SR_PRIV int dso_open(int dev_index)
108{
109 libusb_device **devlist;
110 struct libusb_device_descriptor des;
111 struct sr_dev_inst *sdi;
112 struct context *ctx;
113 int err, skip, i;
114
115 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
116 return SR_ERR_ARG;
117 ctx = sdi->priv;
118
119 if (sdi->status == SR_ST_ACTIVE)
120 /* already in use */
121 return SR_ERR;
122
123 skip = 0;
124 libusb_get_device_list(usb_context, &devlist);
125 for (i = 0; devlist[i]; i++) {
126 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
127 sr_err("hantek-dso: failed to get device descriptor: %d", err);
128 continue;
129 }
130
131 if (des.idVendor != ctx->profile->fw_vid
132 || des.idProduct != ctx->profile->fw_pid)
133 continue;
134
135 if (sdi->status == SR_ST_INITIALIZING) {
136 if (skip != dev_index) {
137 /* Skip devices of this type that aren't the one we want. */
138 skip += 1;
139 continue;
140 }
141 } else if (sdi->status == SR_ST_INACTIVE) {
142 /*
143 * This device is fully enumerated, so we need to find
144 * this device by vendor, product, bus and address.
145 */
146 if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus
147 || libusb_get_device_address(devlist[i]) != ctx->usb->address)
148 /* this is not the one */
149 continue;
150 }
151
152 if (!(err = libusb_open(devlist[i], &ctx->usb->devhdl))) {
153 if (ctx->usb->address == 0xff)
154 /*
155 * first time we touch this device after firmware upload,
156 * so we don't know the address yet.
157 */
158 ctx->usb->address = libusb_get_device_address(devlist[i]);
159
160 if(!(ctx->epin_maxpacketsize = dso_getmps(devlist[i])))
161 sr_err("hantek-dso: wrong endpoint profile");
162 else {
163 sdi->status = SR_ST_ACTIVE;
164 sr_info("hantek-dso: opened device %d on %d.%d interface %d",
165 sdi->index, ctx->usb->bus,
166 ctx->usb->address, USB_INTERFACE);
167 }
168 } else {
169 sr_err("hantek-dso: failed to open device: %d", err);
170 }
171
172 /* if we made it here, we handled the device one way or another */
173 break;
174 }
175 libusb_free_device_list(devlist, 1);
176
177 if (sdi->status != SR_ST_ACTIVE)
178 return SR_ERR;
179
180 return SR_OK;
181}
182
183SR_PRIV void dso_close(struct sr_dev_inst *sdi)
184{
185 struct context *ctx;
186
187 ctx = sdi->priv;
188
189 if (ctx->usb->devhdl == NULL)
190 return;
191
192 sr_info("hantek-dso: closing device %d on %d.%d interface %d", sdi->index,
193 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
194 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
195 libusb_close(ctx->usb->devhdl);
196 ctx->usb->devhdl = NULL;
197 sdi->status = SR_ST_INACTIVE;
198
199}
200
201static int get_channel_offsets(struct context *ctx)
202{
203 int ret;
204
205 sr_dbg("hantek-dso: getting channel offsets");
206
207 ret = libusb_control_transfer(ctx->usb->devhdl,
208 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR,
209 CTRL_READ_EEPROM, EEPROM_CHANNEL_OFFSETS, 0,
210 (unsigned char *)&ctx->channel_levels,
211 sizeof(ctx->channel_levels), 200);
212 if (ret != sizeof(ctx->channel_levels)) {
213 sr_err("failed to get channel offsets: %d", ret);
214 return SR_ERR;
215 }
216
217 return SR_OK;
218}
219
220SR_PRIV int dso_set_trigger_samplerate(struct context *ctx)
221{
222 int ret, tmp;
223 uint8_t cmdstring[12];
3b533202
BV
224 uint16_t timebase_small[] = { 0xffff, 0xfffc, 0xfff7, 0xffe8, 0xffce,
225 0xff9c, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79, 0xd8f1 };
226 uint16_t timebase_large[] = { 0xffff, 0x0000, 0xfffc, 0xfff7, 0xffe8,
227 0xffce, 0xff9d, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79 };
228
229 sr_dbg("hantek-dso: sending CMD_SET_TRIGGER_SAMPLERATE");
230
231 memset(cmdstring, 0, sizeof(cmdstring));
232 /* Command */
233 cmdstring[0] = CMD_SET_TRIGGER_SAMPLERATE;
234
235 /* Trigger source */
bc79e906 236 cmdstring[2] = (ctx->triggersource & 0x03);
3b533202
BV
237
238 /* Frame size */
bc79e906
BV
239 cmdstring[2] |= (ctx->framesize == FRAMESIZE_SMALL ? 0x01 : 0x02) << 2;
240
241 /* Timebase fast */
242 switch (ctx->framesize) {
243 case FRAMESIZE_SMALL:
244 if (ctx->timebase < TIME_20us)
245 tmp = 0;
246 else if (ctx->timebase == TIME_20us)
247 tmp = 1;
248 else if (ctx->timebase == TIME_40us)
249 tmp = 2;
250 else if (ctx->timebase == TIME_100us)
251 tmp = 3;
252 else if (ctx->timebase >= TIME_200us)
253 tmp = 4;
254 break;
255 case FRAMESIZE_LARGE:
256 if (ctx->timebase < TIME_40us) {
257 sr_err("hantek-dso: timebase < 40us only supported with 10K buffer");
258 return SR_ERR_ARG;
259 }
260 else if (ctx->timebase == TIME_40us)
261 tmp = 0;
262 else if (ctx->timebase == TIME_100us)
263 tmp = 2;
264 else if (ctx->timebase == TIME_200us)
265 tmp = 3;
266 else if (ctx->timebase >= TIME_400us)
267 tmp = 4;
268 break;
3b533202 269 }
bc79e906
BV
270 cmdstring[2] |= (tmp & 0x07) << 5;
271
272 /* Enabled channels: 00=CH1 01=CH2 10=both */
6e71ef3b
BV
273 tmp = (((ctx->ch2_enabled ? 1 : 0) << 1) + (ctx->ch1_enabled ? 1 : 0)) - 1;
274 cmdstring[3] = tmp;
3b533202 275
bc79e906
BV
276 /* Fast rates channel */
277 /* TODO: is this right? */
278 tmp = ctx->timebase < TIME_10us ? 1 : 0;
279 cmdstring[3] |= tmp << 2;
3b533202 280
bc79e906
BV
281 /* Trigger slope: 0=positive 1=negative */
282 cmdstring[3] |= (ctx->triggerslope == SLOPE_NEGATIVE ? 1 : 0) << 3;
3b533202
BV
283
284 /* Timebase */
285 if (ctx->timebase < TIME_100us)
286 tmp = 0;
287 else if (ctx->timebase > TIME_400ms)
288 tmp = 0xffed;
289 else {
290 if (ctx->framesize == FRAMESIZE_SMALL)
291 tmp = timebase_small[ctx->timebase - 3];
292 else
293 tmp = timebase_large[ctx->timebase - 3];
294 }
bc79e906
BV
295 cmdstring[4] = tmp & 0xff;
296 cmdstring[5] = (tmp >> 8) & 0xff;
297
298 /* Horizontal trigger position */
299 tmp = 0x77fff + 0x8000 * ctx->triggerposition;
3b533202
BV
300 cmdstring[6] = tmp & 0xff;
301 cmdstring[7] = (tmp >> 8) & 0xff;
3b533202
BV
302 cmdstring[10] = (tmp >> 16) & 0xff;
303
304 if (send_begin(ctx) != SR_OK)
305 return SR_ERR;
306
307 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
308 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
309 cmdstring, sizeof(cmdstring),
310 &tmp, 100)) != 0) {
311 sr_err("Failed to set trigger/samplerate: %d", ret);
312 return SR_ERR;
313 }
314
315 return SR_OK;
316}
317
318SR_PRIV int dso_set_filters(struct context *ctx)
319{
320 int ret, tmp;
321 uint8_t cmdstring[8];
322
323 sr_dbg("hantek-dso: sending CMD_SET_FILTERS");
324
325 memset(cmdstring, 0, sizeof(cmdstring));
326 cmdstring[0] = CMD_SET_FILTERS;
327 cmdstring[1] = 0x0f;
328 if (ctx->filter_ch1)
329 cmdstring[2] |= 0x80;
330 if (ctx->filter_ch2)
331 cmdstring[2] |= 0x40;
332 if (ctx->filter_trigger)
333 cmdstring[2] |= 0x20;
334
335 if (send_begin(ctx) != SR_OK)
336 return SR_ERR;
337
338 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
339 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
340 cmdstring, sizeof(cmdstring),
341 &tmp, 100)) != 0) {
342 sr_err("Failed to set filters: %d", ret);
343 return SR_ERR;
344 }
345
346 return SR_OK;
347}
348
349SR_PRIV int dso_set_voltage(struct context *ctx)
350{
351 int ret, tmp;
352 uint8_t cmdstring[8];
353
354 sr_dbg("hantek-dso: sending CMD_SET_VOLTAGE");
355
356 memset(cmdstring, 0, sizeof(cmdstring));
357 cmdstring[0] = CMD_SET_VOLTAGE;
358 cmdstring[1] = 0x0f;
359 cmdstring[2] = 0x03;
360 cmdstring[2] |= ((2 - ctx->voltage_ch1 % 3) << 6);
361 cmdstring[2] |= ((2 - ctx->voltage_ch2 % 3) << 4);
362cmdstring[2] = 0x30;
363
364 if (send_begin(ctx) != SR_OK)
365 return SR_ERR;
366
367 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
368 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
369 cmdstring, sizeof(cmdstring),
370 &tmp, 100)) != 0) {
371 sr_err("Failed to set voltage: %d", ret);
372 return SR_ERR;
373 }
374
375 return SR_OK;
376}
377
378SR_PRIV int dso_set_relays(struct context *ctx)
379{
380 int ret, cv1, cv2;
381 uint8_t relays[] = { 0x00, 0x04, 0x08, 0x02, 0x20, 0x40, 0x10, 0x01,
382 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
383
384 sr_dbg("hantek-dso: sending CTRL_SETRELAYS");
385
386 cv1 = ctx->voltage_ch1 / 3;
387 cv2 = ctx->voltage_ch2 / 3;
388relays[0] = 0x01;
389 if (cv1 > 0)
390 relays[1] = ~relays[1];
391
392 if (cv1 > 1)
393 relays[2] = ~relays[2];
394
395 if (ctx->coupling_ch1 != COUPLING_AC)
396 relays[3] = ~relays[3];
397
398 if (cv2 > 0)
399 relays[4] = ~relays[4];
400
401 if (cv2 > 1)
402 relays[5] = ~relays[5];
403
404 if (ctx->coupling_ch2 != COUPLING_AC)
405 relays[6] = ~relays[6];
406
bc79e906 407 if (ctx->triggersource == TRIGGER_EXT)
3b533202
BV
408 relays[7] = ~relays[7];
409
410 if ((ret = libusb_control_transfer(ctx->usb->devhdl,
411 LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETRELAYS,
412 0, 0, relays, sizeof(relays), 100)) != sizeof(relays)) {
413 sr_err("failed to set relays: %d", ret);
414 return SR_ERR;
415 }
416
417 return SR_OK;
418}
419
420SR_PRIV int dso_set_voffsets(struct context *ctx)
421{
422 int offset, ret;
423 uint16_t *ch_levels;
424// uint8_t offsets[17];
425uint8_t offsets[] = {0x20,0x75,0x30,0x3f,0x20,0xbd,0x3f,0x02,0x20,0x00,0x71,0x01,0x2e,0x0b,0x3f,0x02,0x50};
426//uint8_t offsets[] = {0xff, 0x75, 0x30, 0x3f, 0x20, 0xbd,
427// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
428//};
429
430 sr_dbg("hantek-dso: sending CTRL_SETOFFSET");
431
432// memset(offsets, 0, sizeof(offsets));
433// /* Channel 1 */
434// ch_levels = ctx->channel_levels[0][VOLTAGE_10mV - ctx->voltage_ch1];
435// offset = (ch_levels[1] - ch_levels[0]) * ctx->voffset_ch1 + ch_levels[0];
436// offsets[0] = (offset >> 8) | 0x20;
437// offsets[1] = offset & 0xff;
438//
439// /* Channel 2 */
440// ch_levels = ctx->channel_levels[1][VOLTAGE_10mV - ctx->voltage_ch2];
441// offset = (ch_levels[1] - ch_levels[0]) * ctx->voffset_ch2 + ch_levels[0];
442// offsets[2] = (offset >> 8) | 0x20;
443// offsets[3] = offset & 0xff;
444//
445// /* Trigger */
446// offset = MAX_VERT_TRIGGER * ctx->voffset_trigger;
447// offsets[4] = (offset >> 8) | 0x20;
448// offsets[5] = offset & 0xff;
449
450 if ((ret = libusb_control_transfer(ctx->usb->devhdl,
451 LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETOFFSET,
452 0, 0, offsets, sizeof(offsets), 100)) != sizeof(offsets)) {
453 sr_err("failed to set offsets: %d", ret);
454 return SR_ERR;
455 }
456
457 return SR_OK;
458}
459
460SR_PRIV int dso_enable_trigger(struct context *ctx)
461{
462 int ret, tmp;
463 uint8_t cmdstring[2];
464
465 sr_dbg("hantek-dso: sending CMD_ENABLE_TRIGGER");
466
467 memset(cmdstring, 0, sizeof(cmdstring));
468 cmdstring[0] = CMD_ENABLE_TRIGGER;
469 cmdstring[1] = 0x00;
470
471 if (send_begin(ctx) != SR_OK)
472 return SR_ERR;
473
474 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
475 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
476 cmdstring, sizeof(cmdstring),
477 &tmp, 100)) != 0) {
478 sr_err("Failed to enable trigger: %d", ret);
479 return SR_ERR;
480 }
481
482 return SR_OK;
483}
484
485SR_PRIV int dso_force_trigger(struct context *ctx)
486{
487 int ret, tmp;
488 uint8_t cmdstring[2];
489
490 sr_dbg("hantek-dso: sending CMD_FORCE_TRIGGER");
491
492 memset(cmdstring, 0, sizeof(cmdstring));
493 cmdstring[0] = CMD_FORCE_TRIGGER;
494 cmdstring[1] = 0x00;
495
496 if (send_begin(ctx) != SR_OK)
497 return SR_ERR;
498
499 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
500 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
501 cmdstring, sizeof(cmdstring),
502 &tmp, 100)) != 0) {
503 sr_err("Failed to force trigger: %d", ret);
504 return SR_ERR;
505 }
506
507 return SR_OK;
508}
509
510SR_PRIV int dso_init(struct context *ctx)
511{
512
513 sr_dbg("hantek-dso: initializing dso");
514
515 if (get_channel_offsets(ctx) != SR_OK)
516 return SR_ERR;
517
518 if (dso_set_trigger_samplerate(ctx) != SR_OK)
519 return SR_ERR;
520
521 if (dso_set_filters(ctx) != SR_OK)
522 return SR_ERR;
523
524 if (dso_set_voltage(ctx) != SR_OK)
525 return SR_ERR;
526
527 if (dso_set_relays(ctx) != SR_OK)
528 return SR_ERR;
529
530 if (dso_set_voffsets(ctx) != SR_OK)
531 return SR_ERR;
532
533 if (dso_enable_trigger(ctx) != SR_OK)
534 return SR_ERR;
535
536 if (dso_force_trigger(ctx) != SR_OK)
537 return SR_ERR;
538
539 return SR_OK;
540}
541
542SR_PRIV uint8_t dso_get_capturestate(struct context *ctx)
543{
544 int ret, tmp;
545 uint8_t cmdstring[2], inbuf[512];
546
547 sr_dbg("hantek-dso: sending CMD_GET_CAPTURESTATE");
548
549 cmdstring[0] = CMD_GET_CAPTURESTATE;
550 cmdstring[1] = 0;
551
552 if ((ret = send_bulkcmd(ctx, cmdstring, sizeof(cmdstring))) != SR_OK) {
553 sr_dbg("Failed to send get_capturestate command: %d", ret);
554 return CAPTURE_UNKNOWN;
555 }
556
557 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
558 DSO_EP_IN | LIBUSB_ENDPOINT_IN,
559 inbuf, 512, &tmp, 100)) != 0) {
560 sr_dbg("Failed to get capturestate: %d", ret);
561 return CAPTURE_UNKNOWN;
562 }
563
564 return inbuf[0];
565}
566
567SR_PRIV uint8_t dso_capture_start(struct context *ctx)
568{
569 int ret;
570 uint8_t cmdstring[2];
571
572 sr_dbg("hantek-dso: sending CMD_CAPTURE_START");
573
574 cmdstring[0] = CMD_CAPTURE_START;
575 cmdstring[1] = 0;
576
577 if ((ret = send_bulkcmd(ctx, cmdstring, sizeof(cmdstring))) != SR_OK) {
578 sr_err("Failed to send capture_start command: %d", ret);
579 return SR_ERR;
580 }
581
582 return SR_OK;
583}
584
585SR_PRIV int dso_get_channeldata(struct context *ctx, libusb_transfer_cb_fn cb)
586{
587 struct libusb_transfer *transfer;
588 int num_transfers, ret, i;
589 uint8_t cmdstring[2];
590 unsigned char *buf;
591
592 sr_dbg("hantek-dso: sending CMD_GET_CHANNELDATA");
593
594 cmdstring[0] = CMD_GET_CHANNELDATA;
595 cmdstring[1] = 0;
596
597 if ((ret = send_bulkcmd(ctx, cmdstring, sizeof(cmdstring))) != SR_OK) {
598 sr_err("Failed to get channel data: %d", ret);
599 return SR_ERR;
600 }
601
602 /* TODO: dso-2xxx only */
603 num_transfers = ctx->framesize * sizeof(unsigned short) / ctx->epin_maxpacketsize;
604 sr_dbg("hantek-dso: queueing up %d transfers", num_transfers);
605 for (i = 0; i < num_transfers; i++) {
606 if (!(buf = g_try_malloc(ctx->epin_maxpacketsize))) {
607 sr_err("hantek-dso: %s: buf malloc failed", __func__);
608 return SR_ERR_MALLOC;
609 }
610 transfer = libusb_alloc_transfer(0);
611 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
612 DSO_EP_IN | LIBUSB_ENDPOINT_IN, buf,
613 ctx->epin_maxpacketsize, cb, ctx, 40);
614 if ((ret = libusb_submit_transfer(transfer)) != 0) {
615 sr_err("failed to submit transfer: %d", ret);
616 /* TODO: Free them all. */
617 libusb_free_transfer(transfer);
618 g_free(buf);
619 return SR_ERR;
620 }
621 }
622
623 return SR_OK;
624}
625