]> sigrok.org Git - libsigrok.git/blob - hardware/hantek-dso/dso.c
sr: initial support for Hantek 2xxx/5200 USB oscilloscopes
[libsigrok.git] / hardware / hantek-dso / dso.c
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
30 extern libusb_context *usb_context;
31 extern GSList *dev_insts;
32
33
34 static 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
52 static 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
67 SR_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
100 err:
101         if (conf_dsc)
102                 libusb_free_config_descriptor(conf_dsc);
103
104         return mps;
105 }
106
107 SR_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
183 SR_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
201 static 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
220 SR_PRIV int dso_set_trigger_samplerate(struct context *ctx)
221 {
222         int ret, tmp;
223         uint8_t cmdstring[12];
224         uint8_t timebasefast_small[] = {1, 2, 3, 4};
225         uint8_t timebasefast_large[] = {0, 0, 2, 3};
226         uint16_t timebase_small[] = { 0xffff, 0xfffc, 0xfff7, 0xffe8, 0xffce,
227                         0xff9c, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79, 0xd8f1 };
228         uint16_t timebase_large[] = { 0xffff, 0x0000, 0xfffc, 0xfff7, 0xffe8,
229                         0xffce, 0xff9d, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79 };
230
231         sr_dbg("hantek-dso: sending CMD_SET_TRIGGER_SAMPLERATE");
232
233         memset(cmdstring, 0, sizeof(cmdstring));
234         /* Command */
235         cmdstring[0] = CMD_SET_TRIGGER_SAMPLERATE;
236
237         /* Trigger source */
238         cmdstring[2] = (ctx->triggersource & 0x03) << 6;
239
240         /* Frame size */
241         cmdstring[2] |= (ctx->framesize == FRAMESIZE_SMALL ? 0x01 : 0x02) << 3;
242
243         /* Timebase fast (no idea what this means) */
244         if (ctx->timebase < TIME_20us)
245                 tmp = 0;
246         else if (ctx->timebase > TIME_200us)
247                 tmp = 4;
248         else {
249                 if (ctx->framesize == FRAMESIZE_SMALL)
250                         tmp = timebasefast_small[ctx->timebase - 1];
251                 else
252                         tmp = timebasefast_large[ctx->timebase - 1];
253         }
254         cmdstring[2] |= tmp & 0x07;
255 cmdstring[2] = 0x45;
256         /* Selected channel */
257         cmdstring[3] = (ctx->selected_channel & 0x03) << 6;
258 cmdstring[3] = 0x02;
259
260         /* TODO: Fast rates channel */
261         cmdstring[3] |= 0 << 5;
262
263         /* Trigger slope */
264         cmdstring[3] |= (ctx->triggerslope ? 1 : 0) << 4;
265
266         /* Timebase */
267         if (ctx->timebase < TIME_100us)
268                 tmp = 0;
269         else if (ctx->timebase > TIME_400ms)
270                 tmp = 0xffed;
271         else {
272                 if (ctx->framesize == FRAMESIZE_SMALL)
273                         tmp = timebase_small[ctx->timebase - 3];
274                 else
275                         tmp = timebase_large[ctx->timebase - 3];
276         }
277 tmp = 0xebff;
278         cmdstring[6] = tmp & 0xff;
279         cmdstring[7] = (tmp >> 8) & 0xff;
280
281         /* Trigger position (time) */
282         tmp = 0x77660 + ctx->triggerposition;
283 tmp = 0x7006f;
284         cmdstring[8] = tmp & 0xff;
285         cmdstring[9] = (tmp >> 8) & 0xff;
286         cmdstring[10] = (tmp >> 16) & 0xff;
287
288         if (send_begin(ctx) != SR_OK)
289                 return SR_ERR;
290
291         if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
292                         DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
293                         cmdstring, sizeof(cmdstring),
294                         &tmp, 100)) != 0) {
295                 sr_err("Failed to set trigger/samplerate: %d", ret);
296                 return SR_ERR;
297         }
298
299         return SR_OK;
300 }
301
302 SR_PRIV int dso_set_filters(struct context *ctx)
303 {
304         int ret, tmp;
305         uint8_t cmdstring[8];
306
307         sr_dbg("hantek-dso: sending CMD_SET_FILTERS");
308
309         memset(cmdstring, 0, sizeof(cmdstring));
310         cmdstring[0] = CMD_SET_FILTERS;
311         cmdstring[1] = 0x0f;
312         if (ctx->filter_ch1)
313                 cmdstring[2] |= 0x80;
314         if (ctx->filter_ch2)
315                 cmdstring[2] |= 0x40;
316         if (ctx->filter_trigger)
317                 cmdstring[2] |= 0x20;
318
319         if (send_begin(ctx) != SR_OK)
320                 return SR_ERR;
321
322         if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
323                         DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
324                         cmdstring, sizeof(cmdstring),
325                         &tmp, 100)) != 0) {
326                 sr_err("Failed to set filters: %d", ret);
327                 return SR_ERR;
328         }
329
330         return SR_OK;
331 }
332
333 SR_PRIV int dso_set_voltage(struct context *ctx)
334 {
335         int ret, tmp;
336         uint8_t cmdstring[8];
337
338         sr_dbg("hantek-dso: sending CMD_SET_VOLTAGE");
339
340         memset(cmdstring, 0, sizeof(cmdstring));
341         cmdstring[0] = CMD_SET_VOLTAGE;
342         cmdstring[1] = 0x0f;
343         cmdstring[2] = 0x03;
344         cmdstring[2] |= ((2 - ctx->voltage_ch1 % 3) << 6);
345         cmdstring[2] |= ((2 - ctx->voltage_ch2 % 3) << 4);
346 cmdstring[2] = 0x30;
347
348         if (send_begin(ctx) != SR_OK)
349                 return SR_ERR;
350
351         if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
352                         DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
353                         cmdstring, sizeof(cmdstring),
354                         &tmp, 100)) != 0) {
355                 sr_err("Failed to set voltage: %d", ret);
356                 return SR_ERR;
357         }
358
359         return SR_OK;
360 }
361
362 SR_PRIV int dso_set_relays(struct context *ctx)
363 {
364         int ret, cv1, cv2;
365         uint8_t relays[] = { 0x00, 0x04, 0x08, 0x02, 0x20, 0x40, 0x10, 0x01,
366                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
367
368         sr_dbg("hantek-dso: sending CTRL_SETRELAYS");
369
370         cv1 = ctx->voltage_ch1 / 3;
371         cv2 = ctx->voltage_ch2 / 3;
372 relays[0] = 0x01;
373         if (cv1 > 0)
374                 relays[1] = ~relays[1];
375
376         if (cv1 > 1)
377                 relays[2] = ~relays[2];
378
379         if (ctx->coupling_ch1 != COUPLING_AC)
380                 relays[3] = ~relays[3];
381
382         if (cv2 > 0)
383                 relays[4] = ~relays[4];
384
385         if (cv2 > 1)
386                 relays[5] = ~relays[5];
387
388         if (ctx->coupling_ch2 != COUPLING_AC)
389                 relays[6] = ~relays[6];
390
391         if (ctx->triggersource == TRIGGER_EXT || ctx->triggersource == TRIGGER_EXT10)
392                 relays[7] = ~relays[7];
393
394         if ((ret = libusb_control_transfer(ctx->usb->devhdl,
395                         LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETRELAYS,
396                         0, 0, relays, sizeof(relays), 100)) != sizeof(relays)) {
397                 sr_err("failed to set relays: %d", ret);
398                 return SR_ERR;
399         }
400
401         return SR_OK;
402 }
403
404 SR_PRIV int dso_set_voffsets(struct context *ctx)
405 {
406         int offset, ret;
407         uint16_t *ch_levels;
408 //      uint8_t offsets[17];
409 uint8_t offsets[] = {0x20,0x75,0x30,0x3f,0x20,0xbd,0x3f,0x02,0x20,0x00,0x71,0x01,0x2e,0x0b,0x3f,0x02,0x50};
410 //uint8_t offsets[] = {0xff, 0x75, 0x30, 0x3f, 0x20, 0xbd,
411 //              0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
412 //};
413
414         sr_dbg("hantek-dso: sending CTRL_SETOFFSET");
415
416 //      memset(offsets, 0, sizeof(offsets));
417 //      /* Channel 1 */
418 //      ch_levels = ctx->channel_levels[0][VOLTAGE_10mV - ctx->voltage_ch1];
419 //      offset = (ch_levels[1] - ch_levels[0]) * ctx->voffset_ch1 + ch_levels[0];
420 //      offsets[0] = (offset >> 8) | 0x20;
421 //      offsets[1] = offset & 0xff;
422 //
423 //      /* Channel 2 */
424 //      ch_levels = ctx->channel_levels[1][VOLTAGE_10mV - ctx->voltage_ch2];
425 //      offset = (ch_levels[1] - ch_levels[0]) * ctx->voffset_ch2 + ch_levels[0];
426 //      offsets[2] = (offset >> 8) | 0x20;
427 //      offsets[3] = offset & 0xff;
428 //
429 //      /* Trigger */
430 //      offset = MAX_VERT_TRIGGER * ctx->voffset_trigger;
431 //      offsets[4] = (offset >> 8) | 0x20;
432 //      offsets[5] = offset & 0xff;
433
434         if ((ret = libusb_control_transfer(ctx->usb->devhdl,
435                         LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETOFFSET,
436                         0, 0, offsets, sizeof(offsets), 100)) != sizeof(offsets)) {
437                 sr_err("failed to set offsets: %d", ret);
438                 return SR_ERR;
439         }
440
441         return SR_OK;
442 }
443
444 SR_PRIV int dso_enable_trigger(struct context *ctx)
445 {
446         int ret, tmp;
447         uint8_t cmdstring[2];
448
449         sr_dbg("hantek-dso: sending CMD_ENABLE_TRIGGER");
450
451         memset(cmdstring, 0, sizeof(cmdstring));
452         cmdstring[0] = CMD_ENABLE_TRIGGER;
453         cmdstring[1] = 0x00;
454
455         if (send_begin(ctx) != SR_OK)
456                 return SR_ERR;
457
458         if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
459                         DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
460                         cmdstring, sizeof(cmdstring),
461                         &tmp, 100)) != 0) {
462                 sr_err("Failed to enable trigger: %d", ret);
463                 return SR_ERR;
464         }
465
466         return SR_OK;
467 }
468
469 SR_PRIV int dso_force_trigger(struct context *ctx)
470 {
471         int ret, tmp;
472         uint8_t cmdstring[2];
473
474         sr_dbg("hantek-dso: sending CMD_FORCE_TRIGGER");
475
476         memset(cmdstring, 0, sizeof(cmdstring));
477         cmdstring[0] = CMD_FORCE_TRIGGER;
478         cmdstring[1] = 0x00;
479
480         if (send_begin(ctx) != SR_OK)
481                 return SR_ERR;
482
483         if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
484                         DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
485                         cmdstring, sizeof(cmdstring),
486                         &tmp, 100)) != 0) {
487                 sr_err("Failed to force trigger: %d", ret);
488                 return SR_ERR;
489         }
490
491         return SR_OK;
492 }
493
494 SR_PRIV int dso_init(struct context *ctx)
495 {
496
497         sr_dbg("hantek-dso: initializing dso");
498
499         if (get_channel_offsets(ctx) != SR_OK)
500                 return SR_ERR;
501
502         if (dso_set_trigger_samplerate(ctx) != SR_OK)
503                 return SR_ERR;
504
505         if (dso_set_filters(ctx) != SR_OK)
506                 return SR_ERR;
507
508         if (dso_set_voltage(ctx) != SR_OK)
509                 return SR_ERR;
510
511         if (dso_set_relays(ctx) != SR_OK)
512                 return SR_ERR;
513
514         if (dso_set_voffsets(ctx) != SR_OK)
515                 return SR_ERR;
516
517         if (dso_enable_trigger(ctx) != SR_OK)
518                 return SR_ERR;
519
520         if (dso_force_trigger(ctx) != SR_OK)
521                 return SR_ERR;
522
523         return SR_OK;
524 }
525
526 SR_PRIV uint8_t dso_get_capturestate(struct context *ctx)
527 {
528         int ret, tmp;
529         uint8_t cmdstring[2], inbuf[512];
530
531         sr_dbg("hantek-dso: sending CMD_GET_CAPTURESTATE");
532
533         cmdstring[0] = CMD_GET_CAPTURESTATE;
534         cmdstring[1] = 0;
535
536         if ((ret = send_bulkcmd(ctx, cmdstring, sizeof(cmdstring))) != SR_OK) {
537                 sr_dbg("Failed to send get_capturestate command: %d", ret);
538                 return CAPTURE_UNKNOWN;
539         }
540
541         if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
542                         DSO_EP_IN | LIBUSB_ENDPOINT_IN,
543                         inbuf, 512, &tmp, 100)) != 0) {
544                 sr_dbg("Failed to get capturestate: %d", ret);
545                 return CAPTURE_UNKNOWN;
546         }
547
548         return inbuf[0];
549 }
550
551 SR_PRIV uint8_t dso_capture_start(struct context *ctx)
552 {
553         int ret;
554         uint8_t cmdstring[2];
555
556         sr_dbg("hantek-dso: sending CMD_CAPTURE_START");
557
558         cmdstring[0] = CMD_CAPTURE_START;
559         cmdstring[1] = 0;
560
561         if ((ret = send_bulkcmd(ctx, cmdstring, sizeof(cmdstring))) != SR_OK) {
562                 sr_err("Failed to send capture_start command: %d", ret);
563                 return SR_ERR;
564         }
565
566         return SR_OK;
567 }
568
569 SR_PRIV int dso_get_channeldata(struct context *ctx, libusb_transfer_cb_fn cb)
570 {
571         struct libusb_transfer *transfer;
572         int num_transfers, ret, i;
573         uint8_t cmdstring[2];
574         unsigned char *buf;
575
576         sr_dbg("hantek-dso: sending CMD_GET_CHANNELDATA");
577
578         cmdstring[0] = CMD_GET_CHANNELDATA;
579         cmdstring[1] = 0;
580
581         if ((ret = send_bulkcmd(ctx, cmdstring, sizeof(cmdstring))) != SR_OK) {
582                 sr_err("Failed to get channel data: %d", ret);
583                 return SR_ERR;
584         }
585
586         /* TODO: dso-2xxx only */
587         num_transfers = ctx->framesize * sizeof(unsigned short) / ctx->epin_maxpacketsize;
588         sr_dbg("hantek-dso: queueing up %d transfers", num_transfers);
589         for (i = 0; i < num_transfers; i++) {
590                 if (!(buf = g_try_malloc(ctx->epin_maxpacketsize))) {
591                         sr_err("hantek-dso: %s: buf malloc failed", __func__);
592                         return SR_ERR_MALLOC;
593                 }
594                 transfer = libusb_alloc_transfer(0);
595                 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
596                                 DSO_EP_IN | LIBUSB_ENDPOINT_IN, buf,
597                                 ctx->epin_maxpacketsize, cb, ctx, 40);
598                 if ((ret = libusb_submit_transfer(transfer)) != 0) {
599                         sr_err("failed to submit transfer: %d", ret);
600                         /* TODO: Free them all. */
601                         libusb_free_transfer(transfer);
602                         g_free(buf);
603                         return SR_ERR;
604                 }
605         }
606
607         return SR_OK;
608 }
609