]> sigrok.org Git - libsigrok.git/blob - hardware/hantek-dso/dso.h
hantek-dso: remove sources from session when done
[libsigrok.git] / hardware / hantek-dso / dso.h
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 #ifndef LIBSIGROK_HARDWARE_HANTEK_DSO_H
23 #define LIBSIGROK_HARDWARE_HANTEK_DSO_H
24
25 #define USB_INTERFACE          0
26 #define USB_CONFIGURATION      1
27 #define DSO_EP_IN              0x86
28 #define DSO_EP_OUT             0x02
29 #define NUM_PROBES             2
30
31 /* FX2 renumeration delay in ms */
32 #define MAX_RENUM_DELAY_MS     3000
33
34 #define MAX_CAPTURE_EMPTY      3
35
36 #define DEFAULT_VOLTAGE            VDIV_500MV
37 #define DEFAULT_FRAMESIZE          FRAMESIZE_SMALL
38 #define DEFAULT_TIMEBASE           TIME_100us
39 #define DEFAULT_TRIGGER_SOURCE     "CH1"
40 #define DEFAULT_COUPLING           COUPLING_DC
41 #define DEFAULT_HORIZ_TRIGGERPOS   0.5
42 #define DEFAULT_VERT_OFFSET        0.5
43 #define DEFAULT_VERT_TRIGGERPOS    0.5
44
45 #define MAX_VERT_TRIGGER           0xfe
46
47 /* Hantek DSO-specific protocol values */
48 #define EEPROM_CHANNEL_OFFSETS     0x08
49
50 #define FRAMESIZE_SMALL        10240
51 #define FRAMESIZE_LARGE        32768
52
53
54 enum control_requests {
55         CTRL_READ_EEPROM = 0xa2,
56         CTRL_GETSPEED = 0xb2,
57         CTRL_BEGINCOMMAND = 0xb3,
58         CTRL_SETOFFSET = 0xb4,
59         CTRL_SETRELAYS = 0xb5
60 };
61
62 enum dso_commands {
63         CMD_SET_FILTERS = 0,
64         CMD_SET_TRIGGER_SAMPLERATE,
65         CMD_FORCE_TRIGGER,
66         CMD_CAPTURE_START,
67         CMD_ENABLE_TRIGGER,
68         CMD_GET_CHANNELDATA,
69         CMD_GET_CAPTURESTATE,
70         CMD_SET_VOLTAGE,
71         /* unused */
72         cmdSetLogicalData,
73         cmdGetLogicalData
74 };
75
76 /* Must match the coupling table. */
77 enum couplings {
78         COUPLING_AC = 0,
79         COUPLING_DC,
80         /* TODO not used, how to enable? */
81         COUPLING_GND
82 };
83
84 /* Must match the timebases table. */
85 enum time_bases {
86         TIME_10us = 0,
87         TIME_20us,
88         TIME_40us,
89         TIME_100us,
90         TIME_200us,
91         TIME_400us,
92         TIME_1ms,
93         TIME_2ms,
94         TIME_4ms,
95         TIME_10ms,
96         TIME_20ms,
97         TIME_40ms,
98         TIME_100ms,
99         TIME_200ms,
100         TIME_400ms
101 };
102
103 /* Must match the vdivs table. */
104 enum {
105         VDIV_10MV,
106         VDIV_20MV,
107         VDIV_50MV,
108         VDIV_100MV,
109         VDIV_200MV,
110         VDIV_500MV,
111         VDIV_1V,
112         VDIV_2V,
113         VDIV_5V,
114 };
115
116 enum trigger_slopes {
117         SLOPE_POSITIVE = 0,
118         SLOPE_NEGATIVE
119 };
120
121 enum trigger_sources {
122         TRIGGER_CH2 = 0,
123         TRIGGER_CH1,
124         TRIGGER_EXT,
125 };
126
127 enum capturestates {
128         CAPTURE_EMPTY = 0,
129         CAPTURE_FILLING = 1,
130         CAPTURE_READY_8BIT = 2,
131         CAPTURE_READY_9BIT = 7,
132         CAPTURE_TIMEOUT = 127,
133         CAPTURE_UNKNOWN = 255
134 };
135
136 enum triggermodes {
137         TRIGGERMODE_AUTO,
138         TRIGGERMODE_NORMAL,
139         TRIGGERMODE_SINGLE
140 };
141
142 enum states {
143         IDLE,
144         NEW_CAPTURE,
145         CAPTURE,
146         FETCH_DATA,
147         STOPPING,
148 };
149
150 struct dso_profile {
151         /* VID/PID after cold boot */
152         uint16_t orig_vid;
153         uint16_t orig_pid;
154         /* VID/PID after firmware upload */
155         uint16_t fw_vid;
156         uint16_t fw_pid;
157         char *vendor;
158         char *model;
159         char *firmware;
160 };
161
162 struct drv_context {
163         libusb_context *usb_context;
164         GSList *instances;
165 };
166
167 struct dev_context {
168         const struct dso_profile *profile;
169         struct sr_usb_dev_inst *usb;
170         void *cb_data;
171         uint64_t limit_frames;
172         uint64_t num_frames;
173         /* We can't keep track of an FX2-based device after upgrading
174          * the firmware (it re-enumerates into a different device address
175          * after the upgrade) this is like a global lock. No device will open
176          * until a proper delay after the last device was upgraded.
177          */
178         int64_t fw_updated;
179         int epin_maxpacketsize;
180         int capture_empty_count;
181         int dev_state;
182
183         /* Oscilloscope settings. */
184         int timebase;
185         gboolean ch1_enabled;
186         gboolean ch2_enabled;
187         int voltage_ch1;
188         int voltage_ch2;
189         int coupling_ch1;
190         int coupling_ch2;
191         // voltage offset (vertical position)
192         float voffset_ch1;
193         float voffset_ch2;
194         float voffset_trigger;
195         uint16_t channel_levels[2][9][2];
196         unsigned int framesize;
197         gboolean filter_ch1;
198         gboolean filter_ch2;
199         gboolean filter_trigger;
200         int triggerslope;
201         char *triggersource;
202         float triggerposition;
203         int triggermode;
204
205         /* Frame transfer */
206         unsigned int samp_received;
207         unsigned int samp_buffered;
208         unsigned int trigger_offset;
209         unsigned char *framebuf;
210 };
211
212 SR_PRIV int dso_open(struct sr_dev_inst *sdi);
213 SR_PRIV void dso_close(struct sr_dev_inst *sdi);
214 SR_PRIV int dso_enable_trigger(struct dev_context *devc);
215 SR_PRIV int dso_force_trigger(struct dev_context *devc);
216 SR_PRIV int dso_init(struct dev_context *devc);
217 SR_PRIV int dso_get_capturestate(struct dev_context *devc, uint8_t *capturestate,
218                 uint32_t *trigger_offset);
219 SR_PRIV int dso_capture_start(struct dev_context *devc);
220 SR_PRIV int dso_get_channeldata(struct dev_context *devc, libusb_transfer_cb_fn cb);
221
222 #endif