]> sigrok.org Git - libsigrok.git/blob - src/hardware/hantek-6xxx/protocol.h
af66c70ef1b4a82092bd692f687d4d78af24e748
[libsigrok.git] / src / hardware / hantek-6xxx / protocol.h
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2015 Christer Ekholm <christerekholm@gmail.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef LIBSIGROK_HARDWARE_HANTEK_6XXX_PROTOCOL_H
21 #define LIBSIGROK_HARDWARE_HANTEK_6XXX_PROTOCOL_H
22
23 #include <stdint.h>
24 #include <string.h>
25 #include <glib.h>
26 #include <libsigrok/libsigrok.h>
27 #include "libsigrok-internal.h"
28
29 #define LOG_PREFIX "hantek-6xxx"
30
31 #define MAX_RENUM_DELAY_MS      3000
32
33 #define DEFAULT_VOLTAGE         2
34 #define DEFAULT_COUPLING        COUPLING_DC
35 #define DEFAULT_SAMPLERATE      SR_MHZ(8)
36
37 #define NUM_CHANNELS            2
38
39 #define SAMPLERATE_VALUES \
40         SR_MHZ(48), SR_MHZ(30), SR_MHZ(24), \
41         SR_MHZ(16), SR_MHZ(8), SR_MHZ(4), \
42         SR_MHZ(1), SR_KHZ(500), SR_KHZ(200), \
43         SR_KHZ(100),
44
45 #define SAMPLERATE_REGS \
46         48, 30, 24, 16, 8, 4, 1, 50, 20, 10,
47
48 #define VDIV_VALUES \
49         { 100, 1000 }, \
50         { 250, 1000 }, \
51         { 500, 1000 }, \
52         { 1, 1 },
53
54 #define VDIV_REG \
55         10, 5, 2, 1,
56
57 #define VDIV_MULTIPLIER         10
58
59 /* Weird flushing needed for filtering glitch away. */
60 #define FLUSH_PACKET_SIZE       1024
61
62 #define MIN_PACKET_SIZE         512
63 #ifdef _WIN32
64 #define MAX_PACKET_SIZE         (2 * 1024 * 1024)
65 #else
66 #define MAX_PACKET_SIZE         (12 * 1024 * 1024)
67 #endif
68
69 #define HANTEK_EP_IN            0x86
70 #define USB_INTERFACE           0
71 #define USB_CONFIGURATION       1
72
73 enum control_requests {
74         VDIV_CH1_REG   = 0xe0,
75         VDIV_CH2_REG   = 0xe1,
76         SAMPLERATE_REG = 0xe2,
77         TRIGGER_REG    = 0xe3,
78         CHANNELS_REG   = 0xe4,
79         COUPLING_REG   = 0xe5,
80 };
81
82 enum states {
83         IDLE,
84         FLUSH,
85         CAPTURE,
86         STOPPING,
87 };
88
89 enum couplings {
90         COUPLING_AC = 0,
91         COUPLING_DC,
92 };
93
94 struct hantek_6xxx_profile {
95         /* VID/PID after cold boot */
96         uint16_t orig_vid;
97         uint16_t orig_pid;
98         /* VID/PID after firmware upload */
99         uint16_t fw_vid;
100         uint16_t fw_pid;
101         uint16_t fw_prod_ver;
102         const char *vendor;
103         const char *model;
104         const char *firmware;
105         const char **coupling_vals;
106         uint8_t coupling_tab_size;
107         gboolean has_coupling;
108 };
109
110 struct dev_context {
111         const struct hantek_6xxx_profile *profile;
112         GSList *enabled_channels;
113         /*
114          * We can't keep track of an FX2-based device after upgrading
115          * the firmware (it re-enumerates into a different device address
116          * after the upgrade) this is like a global lock. No device will open
117          * until a proper delay after the last device was upgraded.
118          */
119         int64_t fw_updated;
120         int dev_state;
121         uint64_t samp_received;
122         uint64_t aq_started;
123
124         uint64_t read_start_ts;
125
126         gboolean ch_enabled[NUM_CHANNELS];
127         int voltage[NUM_CHANNELS];
128         int coupling[NUM_CHANNELS];
129         const char **coupling_vals;
130         uint8_t coupling_tab_size;
131         gboolean has_coupling;
132         uint64_t samplerate;
133
134         uint64_t limit_msec;
135         uint64_t limit_samples;
136 };
137
138 SR_PRIV int hantek_6xxx_open(struct sr_dev_inst *sdi);
139 SR_PRIV void hantek_6xxx_close(struct sr_dev_inst *sdi);
140 SR_PRIV int hantek_6xxx_get_channeldata(const struct sr_dev_inst *sdi,
141                 libusb_transfer_cb_fn cb, uint32_t data_amount);
142
143 SR_PRIV int hantek_6xxx_start_data_collecting(const struct sr_dev_inst *sdi);
144 SR_PRIV int hantek_6xxx_stop_data_collecting(const struct sr_dev_inst *sdi);
145
146 SR_PRIV int hantek_6xxx_update_coupling(const struct sr_dev_inst *sdi);
147 SR_PRIV int hantek_6xxx_update_samplerate(const struct sr_dev_inst *sdi);
148 SR_PRIV int hantek_6xxx_update_vdiv(const struct sr_dev_inst *sdi);
149 SR_PRIV int hantek_6xxx_update_channels(const struct sr_dev_inst *sdi);
150 SR_PRIV int hantek_6xxx_init(const struct sr_dev_inst *sdi);
151
152 #endif