]>
Commit | Line | Data |
---|---|---|
6a25fa42 AZ |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
5089a143 AZ |
4 | * Copyright (C) 2016 Andreas Zschunke <andreas.zschunke@gmx.net> |
5 | * Copyright (C) 2017 Andrej Valek <andy@skyrain.eu> | |
6 | * Copyright (C) 2017 Uwe Hermann <uwe@hermann-uwe.de> | |
6a25fa42 AZ |
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 <config.h> | |
23 | #include "protocol.h" | |
24 | ||
5089a143 AZ |
25 | #define USB_INTERFACE 0 |
26 | #define NUM_CHANNELS 32 | |
6a25fa42 | 27 | |
5089a143 AZ |
28 | static const uint32_t scanopts[] = { |
29 | SR_CONF_CONN, | |
30 | }; | |
31 | ||
32 | static const uint32_t drvopts[] = { | |
33 | SR_CONF_LOGIC_ANALYZER, | |
34 | }; | |
35 | ||
36 | static const uint32_t devopts[] = { | |
37 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
3dc976fe | 38 | SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, |
4b75f84c | 39 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, |
5089a143 AZ |
40 | SR_CONF_TRIGGER_MATCH | SR_CONF_LIST, |
41 | SR_CONF_CONN | SR_CONF_GET, | |
f49065c6 AV |
42 | SR_CONF_EXTERNAL_CLOCK | SR_CONF_GET | SR_CONF_SET, |
43 | SR_CONF_EXTERNAL_CLOCK_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
44 | SR_CONF_CLOCK_EDGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
45 | }; | |
46 | ||
47 | static const uint32_t devopts_fpga_zero[] = { | |
48 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
49 | SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
50 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
51 | SR_CONF_TRIGGER_MATCH | SR_CONF_LIST, | |
52 | SR_CONF_CONN | SR_CONF_GET, | |
2a801861 AV |
53 | }; |
54 | ||
55 | static const uint32_t devopts_cg[] = { | |
caad0024 | 56 | SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, |
5089a143 AZ |
57 | }; |
58 | ||
cbc656e4 UH |
59 | static const char *cg_names[] = { |
60 | "A", "B", | |
61 | }; | |
62 | ||
f49065c6 AV |
63 | static const char *signal_edges[] = { |
64 | [H4032L_CLOCK_EDGE_TYPE_RISE] = "rising", | |
65 | [H4032L_CLOCK_EDGE_TYPE_FALL] = "falling", | |
66 | [H4032L_CLOCK_EDGE_TYPE_BOTH] = "both", | |
67 | }; | |
68 | ||
69 | static const char *ext_clock_sources[] = { | |
70 | [H4032L_EXT_CLOCK_SOURCE_CHANNEL_A] = "ACLK", | |
71 | [H4032L_EXT_CLOCK_SOURCE_CHANNEL_B] = "BCLK" | |
72 | }; | |
73 | ||
74 | static const uint8_t ext_clock_edges[2][3] = { | |
75 | { | |
76 | H4032L_CLOCK_EDGE_TYPE_RISE_A, | |
77 | H4032L_CLOCK_EDGE_TYPE_FALL_A, | |
78 | H4032L_CLOCK_EDGE_TYPE_BOTH_A | |
79 | }, | |
80 | { | |
81 | H4032L_CLOCK_EDGE_TYPE_RISE_B, | |
82 | H4032L_CLOCK_EDGE_TYPE_FALL_B, | |
83 | H4032L_CLOCK_EDGE_TYPE_BOTH_B | |
84 | } | |
85 | }; | |
86 | ||
5089a143 AZ |
87 | static const int32_t trigger_matches[] = { |
88 | SR_TRIGGER_ZERO, | |
89 | SR_TRIGGER_ONE, | |
90 | SR_TRIGGER_RISING, | |
91 | SR_TRIGGER_FALLING, | |
92 | SR_TRIGGER_EDGE, | |
93 | }; | |
94 | ||
95 | static const uint64_t samplerates[] = { | |
96 | SR_KHZ(1), | |
97 | SR_KHZ(2), | |
98 | SR_KHZ(4), | |
99 | SR_KHZ(8), | |
100 | SR_KHZ(16), | |
101 | SR_HZ(31250), | |
102 | SR_HZ(62500), | |
103 | SR_KHZ(125), | |
104 | SR_KHZ(250), | |
105 | SR_KHZ(500), | |
106 | SR_KHZ(625), | |
107 | SR_HZ(781250), | |
108 | SR_MHZ(1), | |
109 | SR_KHZ(1250), | |
110 | SR_HZ(1562500), | |
111 | SR_MHZ(2), | |
112 | SR_KHZ(2500), | |
113 | SR_KHZ(3125), | |
114 | SR_MHZ(4), | |
115 | SR_MHZ(5), | |
116 | SR_KHZ(6250), | |
117 | SR_MHZ(10), | |
118 | SR_KHZ(12500), | |
119 | SR_MHZ(20), | |
120 | SR_MHZ(25), | |
121 | SR_MHZ(40), | |
122 | SR_MHZ(50), | |
123 | SR_MHZ(80), | |
124 | SR_MHZ(100), | |
125 | SR_MHZ(160), | |
126 | SR_MHZ(200), | |
127 | SR_MHZ(320), | |
128 | SR_MHZ(400), | |
129 | }; | |
130 | ||
131 | static const uint64_t samplerates_hw[] = { | |
132 | SR_MHZ(100), | |
133 | SR_MHZ(50), | |
134 | SR_MHZ(25), | |
135 | SR_KHZ(12500), | |
136 | SR_KHZ(6250), | |
137 | SR_KHZ(3125), | |
138 | SR_HZ(1562500), | |
139 | SR_HZ(781250), | |
140 | SR_MHZ(80), | |
141 | SR_MHZ(40), | |
142 | SR_MHZ(20), | |
143 | SR_MHZ(10), | |
144 | SR_MHZ(5), | |
145 | SR_KHZ(2500), | |
146 | SR_KHZ(1250), | |
147 | SR_KHZ(625), | |
148 | SR_MHZ(4), | |
149 | SR_MHZ(2), | |
150 | SR_MHZ(1), | |
151 | SR_KHZ(500), | |
152 | SR_KHZ(250), | |
153 | SR_KHZ(125), | |
154 | SR_HZ(62500), | |
155 | SR_HZ(31250), | |
156 | SR_KHZ(16), | |
157 | SR_KHZ(8), | |
158 | SR_KHZ(4), | |
159 | SR_KHZ(2), | |
160 | SR_KHZ(1), | |
161 | 0, | |
162 | 0, | |
163 | 0, | |
164 | SR_MHZ(200), | |
165 | SR_MHZ(160), | |
166 | SR_MHZ(400), | |
167 | SR_MHZ(320), | |
168 | }; | |
169 | ||
3f34a402 | 170 | static struct sr_dev_driver hantek_4032l_driver_info; |
6a25fa42 AZ |
171 | |
172 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
173 | { | |
5089a143 AZ |
174 | struct drv_context *drvc = di->context; |
175 | GSList *l, *devices, *conn_devices; | |
176 | libusb_device **devlist; | |
177 | struct libusb_device_descriptor des; | |
178 | const char *conn; | |
179 | int i; | |
180 | char connection_id[64]; | |
181 | struct sr_channel_group *cg; | |
182 | struct sr_dev_inst *sdi; | |
183 | struct sr_channel *ch; | |
6a25fa42 AZ |
184 | |
185 | devices = NULL; | |
5089a143 | 186 | conn_devices = NULL; |
6a25fa42 | 187 | drvc->instances = NULL; |
5089a143 AZ |
188 | conn = NULL; |
189 | ||
190 | for (l = options; l; l = l->next) { | |
191 | struct sr_config *src = l->data; | |
192 | if (src->key == SR_CONF_CONN) { | |
193 | conn = g_variant_get_string(src->data, NULL); | |
194 | break; | |
195 | } | |
196 | } | |
6a25fa42 | 197 | |
5089a143 AZ |
198 | if (conn) |
199 | conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn); | |
200 | else | |
201 | conn_devices = NULL; | |
202 | ||
203 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
204 | for (i = 0; devlist[i]; i++) { | |
205 | if (conn) { | |
206 | struct sr_usb_dev_inst *usb = NULL; | |
207 | for (l = conn_devices; l; l = l->next) { | |
208 | usb = l->data; | |
28f2d07f AV |
209 | if (usb->bus == libusb_get_bus_number(devlist[i]) && |
210 | usb->address == libusb_get_device_address(devlist[i])) | |
5089a143 AZ |
211 | break; |
212 | } | |
213 | if (!l) | |
214 | /* This device matched none of the ones that | |
215 | * matched the conn specification. */ | |
216 | continue; | |
217 | } | |
218 | ||
219 | libusb_get_device_descriptor(devlist[i], &des); | |
220 | ||
221 | if (des.idVendor != H4032L_USB_VENDOR || | |
222 | des.idProduct != H4032L_USB_PRODUCT) | |
223 | continue; | |
224 | ||
6c1a76d1 RT |
225 | if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0) |
226 | continue; | |
5089a143 AZ |
227 | |
228 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
229 | sdi->driver = &hantek_4032l_driver_info; | |
230 | sdi->vendor = g_strdup("Hantek"); | |
231 | sdi->model = g_strdup("4032L"); | |
232 | sdi->connection_id = g_strdup(connection_id); | |
233 | ||
234 | struct sr_channel_group *channel_groups[2]; | |
235 | for (int j = 0; j < 2; j++) { | |
d810901a | 236 | cg = sr_channel_group_new(sdi, NULL, NULL); |
5089a143 AZ |
237 | cg->name = g_strdup_printf("%c", 'A' + j); |
238 | channel_groups[j] = cg; | |
5089a143 AZ |
239 | } |
240 | ||
241 | /* Assemble channel list and add channel to channel groups. */ | |
242 | for (int j = 0; j < NUM_CHANNELS; j++) { | |
243 | char channel_name[4]; | |
244 | sprintf(channel_name, "%c%d", 'A' + (j & 1), j / 2); | |
245 | ch = sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE, channel_name); | |
246 | cg = channel_groups[j & 1]; | |
247 | cg->channels = g_slist_append(cg->channels, ch); | |
248 | } | |
249 | ||
250 | struct dev_context *devc = g_malloc0(sizeof(struct dev_context)); | |
251 | ||
252 | /* Initialize command packet. */ | |
253 | devc->cmd_pkt.magic = H4032L_CMD_PKT_MAGIC; | |
bf2a6eea | 254 | devc->cmd_pkt.sample_size = 16 * 1024; |
f49065c6 | 255 | devc->sample_rate = 0; |
5089a143 AZ |
256 | |
257 | devc->status = H4032L_STATUS_IDLE; | |
258 | ||
259 | devc->capture_ratio = 5; | |
f49065c6 AV |
260 | devc->external_clock = FALSE; |
261 | devc->clock_edge = H4032L_CLOCK_EDGE_TYPE_RISE; | |
262 | ||
ee1a7d2f AV |
263 | /* Create array of thresholds from min to max. */ |
264 | GVariant *thresholds = std_gvar_min_max_step_thresholds( | |
265 | H4032L_THR_VOLTAGE_MIN, H4032L_THR_VOLTAGE_MAX, | |
266 | H4032L_THR_VOLTAGE_STEP); | |
267 | /* Take default threshold value from array (FP workaround). */ | |
268 | g_variant_get_child(thresholds, H4032L_THR_VOLTAGE_DEFAULT, | |
269 | "(dd)", &devc->cur_threshold[0], &devc->cur_threshold[1]); | |
5089a143 | 270 | |
5089a143 AZ |
271 | sdi->priv = devc; |
272 | devices = g_slist_append(devices, sdi); | |
273 | ||
274 | sdi->status = SR_ST_INACTIVE; | |
275 | sdi->inst_type = SR_INST_USB; | |
276 | sdi->conn = sr_usb_dev_inst_new( | |
277 | libusb_get_bus_number(devlist[i]), | |
278 | libusb_get_device_address(devlist[i]), NULL); | |
279 | } | |
6a25fa42 | 280 | |
5089a143 AZ |
281 | g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free); |
282 | libusb_free_device_list(devlist, 1); | |
6a25fa42 | 283 | |
5089a143 | 284 | return std_scan_complete(di, devices); |
6a25fa42 AZ |
285 | } |
286 | ||
287 | static int dev_open(struct sr_dev_inst *sdi) | |
288 | { | |
5089a143 AZ |
289 | struct sr_usb_dev_inst *usb = sdi->conn; |
290 | int ret; | |
6a25fa42 | 291 | |
5089a143 AZ |
292 | ret = h4032l_dev_open(sdi); |
293 | if (ret != SR_OK) { | |
294 | sr_err("Unable to open device."); | |
295 | return SR_ERR; | |
296 | } | |
6a25fa42 | 297 | |
5089a143 AZ |
298 | ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE); |
299 | if (ret != 0) { | |
300 | switch (ret) { | |
301 | case LIBUSB_ERROR_BUSY: | |
302 | sr_err("Unable to claim USB interface. Another " | |
303 | "program or driver has already claimed it."); | |
304 | break; | |
305 | case LIBUSB_ERROR_NO_DEVICE: | |
306 | sr_err("Device has been disconnected."); | |
307 | break; | |
308 | default: | |
309 | sr_err("Unable to claim interface: %s.", | |
310 | libusb_error_name(ret)); | |
311 | break; | |
312 | } | |
313 | ||
314 | return SR_ERR; | |
315 | } | |
6a25fa42 | 316 | |
7a7afc00 AV |
317 | /* Get FPGA version. */ |
318 | if ((ret = h4032l_get_fpga_version(sdi)) != SR_OK) | |
319 | return ret; | |
320 | ||
6a25fa42 AZ |
321 | return SR_OK; |
322 | } | |
323 | ||
324 | static int dev_close(struct sr_dev_inst *sdi) | |
325 | { | |
5089a143 | 326 | struct sr_usb_dev_inst *usb; |
6a25fa42 | 327 | |
5089a143 | 328 | usb = sdi->conn; |
6a25fa42 | 329 | |
5089a143 AZ |
330 | if (!usb->devhdl) |
331 | return SR_ERR_BUG; | |
6a25fa42 | 332 | |
5089a143 AZ |
333 | sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.", |
334 | usb->bus, usb->address, sdi->connection_id, USB_INTERFACE); | |
335 | libusb_release_interface(usb->devhdl, USB_INTERFACE); | |
336 | libusb_close(usb->devhdl); | |
337 | usb->devhdl = NULL; | |
6a25fa42 AZ |
338 | |
339 | return SR_OK; | |
340 | } | |
341 | ||
342 | static int config_get(uint32_t key, GVariant **data, | |
343 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
344 | { | |
5089a143 AZ |
345 | struct dev_context *devc = sdi->priv; |
346 | struct sr_usb_dev_inst *usb; | |
cbc656e4 | 347 | int idx; |
6a25fa42 | 348 | |
6a25fa42 | 349 | switch (key) { |
caad0024 | 350 | case SR_CONF_VOLTAGE_THRESHOLD: |
61803a29 UH |
351 | if (!cg) |
352 | return SR_ERR_CHANNEL_GROUP; | |
cbc656e4 | 353 | if ((idx = std_str_idx_s(cg->name, ARRAY_AND_SIZE(cg_names))) < 0) |
61803a29 | 354 | return SR_ERR_CHANNEL_GROUP; |
cbc656e4 UH |
355 | *data = std_gvar_tuple_double( |
356 | devc->cur_threshold[idx], devc->cur_threshold[idx]); | |
caad0024 | 357 | break; |
5089a143 | 358 | case SR_CONF_SAMPLERATE: |
f49065c6 | 359 | *data = g_variant_new_uint64(samplerates_hw[devc->sample_rate]); |
5089a143 AZ |
360 | break; |
361 | case SR_CONF_CAPTURE_RATIO: | |
362 | *data = g_variant_new_uint64(devc->capture_ratio); | |
363 | break; | |
364 | case SR_CONF_LIMIT_SAMPLES: | |
365 | *data = g_variant_new_uint64(devc->cmd_pkt.sample_size); | |
366 | break; | |
f49065c6 AV |
367 | case SR_CONF_EXTERNAL_CLOCK: |
368 | *data = g_variant_new_boolean(devc->external_clock); | |
369 | break; | |
370 | case SR_CONF_EXTERNAL_CLOCK_SOURCE: | |
cbc656e4 | 371 | *data = g_variant_new_string(ext_clock_sources[devc->external_clock_source]); |
f49065c6 | 372 | break; |
5089a143 AZ |
373 | case SR_CONF_CONN: |
374 | if (!sdi || !(usb = sdi->conn)) | |
375 | return SR_ERR_ARG; | |
376 | *data = g_variant_new_printf("%d.%d", usb->bus, usb->address); | |
377 | break; | |
f49065c6 | 378 | case SR_CONF_CLOCK_EDGE: |
cbc656e4 | 379 | *data = g_variant_new_string(signal_edges[devc->clock_edge]); |
f49065c6 | 380 | break; |
6a25fa42 AZ |
381 | default: |
382 | return SR_ERR_NA; | |
383 | } | |
384 | ||
5089a143 | 385 | return SR_OK; |
6a25fa42 AZ |
386 | } |
387 | ||
388 | static int config_set(uint32_t key, GVariant *data, | |
389 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
390 | { | |
61803a29 | 391 | int idx; |
5089a143 AZ |
392 | struct dev_context *devc = sdi->priv; |
393 | struct h4032l_cmd_pkt *cmd_pkt = &devc->cmd_pkt; | |
7b9387b8 | 394 | uint64_t sample_rate, num_samples; |
cbc656e4 | 395 | double low, high; |
6a25fa42 | 396 | |
6a25fa42 | 397 | switch (key) { |
61803a29 UH |
398 | case SR_CONF_SAMPLERATE: |
399 | idx = 0; | |
400 | sample_rate = g_variant_get_uint64(data); | |
cbc656e4 | 401 | while (idx < (int)ARRAY_SIZE(samplerates_hw) && samplerates_hw[idx] != sample_rate) |
61803a29 UH |
402 | idx++; |
403 | if (idx == ARRAY_SIZE(samplerates_hw) || sample_rate == 0) { | |
404 | sr_err("Invalid sample rate."); | |
405 | return SR_ERR_SAMPLERATE; | |
3dc976fe | 406 | } |
61803a29 UH |
407 | devc->sample_rate = idx; |
408 | break; | |
409 | case SR_CONF_CAPTURE_RATIO: | |
583fc126 | 410 | devc->capture_ratio = g_variant_get_uint64(data); |
61803a29 UH |
411 | break; |
412 | case SR_CONF_LIMIT_SAMPLES: | |
7b9387b8 UH |
413 | num_samples = g_variant_get_uint64(data); |
414 | num_samples += 511; | |
415 | num_samples &= 0xfffffe00; | |
416 | if (num_samples < H4043L_NUM_SAMPLES_MIN || | |
417 | num_samples > H4032L_NUM_SAMPLES_MAX) { | |
61803a29 | 418 | sr_err("Invalid sample range 2k...64M: %" |
7b9387b8 | 419 | PRIu64 ".", num_samples); |
61803a29 | 420 | return SR_ERR; |
5089a143 | 421 | } |
7b9387b8 | 422 | cmd_pkt->sample_size = num_samples; |
61803a29 UH |
423 | break; |
424 | case SR_CONF_VOLTAGE_THRESHOLD: | |
425 | if (!cg) | |
426 | return SR_ERR_CHANNEL_GROUP; | |
cbc656e4 | 427 | if ((idx = std_str_idx_s(cg->name, ARRAY_AND_SIZE(cg_names))) < 0) |
61803a29 | 428 | return SR_ERR_CHANNEL_GROUP; |
cbc656e4 UH |
429 | g_variant_get(data, "(dd)", &low, &high); |
430 | devc->cur_threshold[idx] = (low + high) / 2.0; | |
61803a29 | 431 | break; |
f49065c6 AV |
432 | case SR_CONF_EXTERNAL_CLOCK: |
433 | devc->external_clock = g_variant_get_boolean(data); | |
434 | break; | |
435 | case SR_CONF_EXTERNAL_CLOCK_SOURCE: | |
436 | if ((idx = std_str_idx(data, ARRAY_AND_SIZE(ext_clock_sources))) < 0) | |
437 | return SR_ERR_ARG; | |
438 | devc->external_clock_source = idx; | |
439 | break; | |
440 | case SR_CONF_CLOCK_EDGE: | |
441 | if ((idx = std_str_idx(data, ARRAY_AND_SIZE(signal_edges))) < 0) | |
442 | return SR_ERR_ARG; | |
443 | devc->clock_edge = idx; | |
444 | break; | |
28f2d07f AV |
445 | default: |
446 | return SR_ERR_NA; | |
6a25fa42 AZ |
447 | } |
448 | ||
28f2d07f | 449 | return SR_OK; |
6a25fa42 AZ |
450 | } |
451 | ||
452 | static int config_list(uint32_t key, GVariant **data, | |
453 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
454 | { | |
f49065c6 AV |
455 | struct dev_context *devc = (sdi) ? sdi->priv : NULL; |
456 | ||
6a25fa42 | 457 | switch (key) { |
5089a143 AZ |
458 | case SR_CONF_SCAN_OPTIONS: |
459 | case SR_CONF_DEVICE_OPTIONS: | |
2a801861 AV |
460 | if (cg) { |
461 | *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg)); | |
462 | break; | |
463 | } | |
f49065c6 AV |
464 | /* Disable external clock and edges for FPGA version 0. */ |
465 | if (devc && (!devc->fpga_version)) | |
466 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts_fpga_zero); | |
5089a143 AZ |
467 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); |
468 | case SR_CONF_SAMPLERATE: | |
469 | *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates)); | |
470 | break; | |
471 | case SR_CONF_TRIGGER_MATCH: | |
472 | *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches)); | |
473 | break; | |
474 | case SR_CONF_VOLTAGE_THRESHOLD: | |
ee1a7d2f AV |
475 | *data = std_gvar_min_max_step_thresholds(H4032L_THR_VOLTAGE_MIN, |
476 | H4032L_THR_VOLTAGE_MAX, H4032L_THR_VOLTAGE_STEP); | |
5089a143 | 477 | break; |
4b75f84c AV |
478 | case SR_CONF_LIMIT_SAMPLES: |
479 | *data = std_gvar_tuple_u64(H4043L_NUM_SAMPLES_MIN, H4032L_NUM_SAMPLES_MAX); | |
480 | break; | |
f49065c6 AV |
481 | case SR_CONF_CLOCK_EDGE: |
482 | *data = g_variant_new_strv(ARRAY_AND_SIZE(signal_edges)); | |
483 | break; | |
484 | case SR_CONF_EXTERNAL_CLOCK_SOURCE: | |
485 | *data = g_variant_new_strv(ARRAY_AND_SIZE(ext_clock_sources)); | |
486 | break; | |
6a25fa42 AZ |
487 | default: |
488 | return SR_ERR_NA; | |
489 | } | |
490 | ||
5089a143 | 491 | return SR_OK; |
6a25fa42 AZ |
492 | } |
493 | ||
5089a143 | 494 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) |
6a25fa42 | 495 | { |
5089a143 AZ |
496 | struct sr_dev_driver *di = sdi->driver; |
497 | struct drv_context *drvc = di->context; | |
498 | struct dev_context *devc = sdi->priv; | |
499 | struct sr_trigger *trigger = sr_session_trigger_get(sdi->session); | |
500 | struct h4032l_cmd_pkt *cmd_pkt = &devc->cmd_pkt; | |
501 | ||
28f2d07f | 502 | /* Initialize variables. */ |
a5b9880e | 503 | devc->acq_aborted = FALSE; |
2958315d | 504 | devc->submitted_transfers = 0; |
3dc976fe | 505 | devc->sent_samples = 0; |
a5b9880e | 506 | |
5089a143 AZ |
507 | /* Calculate packet ratio. */ |
508 | cmd_pkt->pre_trigger_size = (cmd_pkt->sample_size * devc->capture_ratio) / 100; | |
3dc976fe | 509 | devc->trigger_pos = cmd_pkt->pre_trigger_size; |
5089a143 | 510 | |
f49065c6 AV |
511 | /* Set clock edge, when external clock is enabled. */ |
512 | if (devc->external_clock) | |
513 | cmd_pkt->sample_rate = ext_clock_edges[devc->external_clock_source][devc->clock_edge]; | |
514 | else | |
515 | cmd_pkt->sample_rate = devc->sample_rate; | |
516 | ||
e6bb2984 | 517 | /* Set PWM channel values. */ |
2a801861 AV |
518 | devc->cmd_pkt.pwm_a = h4032l_voltage2pwm(devc->cur_threshold[0]); |
519 | devc->cmd_pkt.pwm_b = h4032l_voltage2pwm(devc->cur_threshold[1]); | |
caad0024 | 520 | |
5089a143 AZ |
521 | cmd_pkt->trig_flags.enable_trigger1 = 0; |
522 | cmd_pkt->trig_flags.enable_trigger2 = 0; | |
523 | cmd_pkt->trig_flags.trigger_and_logic = 0; | |
524 | ||
525 | if (trigger && trigger->stages) { | |
526 | GSList *stages = trigger->stages; | |
527 | struct sr_trigger_stage *stage1 = stages->data; | |
528 | if (stages->next) { | |
4868f15a | 529 | sr_err("Only one trigger stage supported for now."); |
5089a143 AZ |
530 | return SR_ERR; |
531 | } | |
532 | cmd_pkt->trig_flags.enable_trigger1 = 1; | |
533 | cmd_pkt->trigger[0].flags.edge_type = H4032L_TRIGGER_EDGE_TYPE_DISABLED; | |
534 | cmd_pkt->trigger[0].flags.data_range_enabled = 0; | |
535 | cmd_pkt->trigger[0].flags.time_range_enabled = 0; | |
536 | cmd_pkt->trigger[0].flags.combined_enabled = 0; | |
537 | cmd_pkt->trigger[0].flags.data_range_type = H4032L_TRIGGER_DATA_RANGE_TYPE_MAX; | |
538 | cmd_pkt->trigger[0].data_range_mask = 0; | |
539 | cmd_pkt->trigger[0].data_range_max = 0; | |
540 | ||
541 | /* Initialize range mask values. */ | |
542 | uint32_t range_mask = 0; | |
543 | uint32_t range_value = 0; | |
544 | ||
545 | GSList *channel = stage1->matches; | |
546 | while (channel) { | |
547 | struct sr_trigger_match *match = channel->data; | |
548 | ||
549 | switch (match->match) { | |
550 | case SR_TRIGGER_ZERO: | |
551 | range_mask |= (1 << match->channel->index); | |
552 | break; | |
553 | case SR_TRIGGER_ONE: | |
554 | range_mask |= (1 << match->channel->index); | |
555 | range_value |= (1 << match->channel->index); | |
556 | break; | |
557 | case SR_TRIGGER_RISING: | |
558 | if (cmd_pkt->trigger[0].flags.edge_type != H4032L_TRIGGER_EDGE_TYPE_DISABLED) { | |
4868f15a | 559 | sr_err("Only one trigger signal with fall/rising/edge allowed."); |
5089a143 AZ |
560 | return SR_ERR; |
561 | } | |
562 | cmd_pkt->trigger[0].flags.edge_type = H4032L_TRIGGER_EDGE_TYPE_RISE; | |
563 | cmd_pkt->trigger[0].flags.edge_signal = match->channel->index; | |
564 | break; | |
565 | case SR_TRIGGER_FALLING: | |
566 | if (cmd_pkt->trigger[0].flags.edge_type != H4032L_TRIGGER_EDGE_TYPE_DISABLED) { | |
4868f15a | 567 | sr_err("Only one trigger signal with fall/rising/edge allowed."); |
5089a143 AZ |
568 | return SR_ERR; |
569 | } | |
570 | cmd_pkt->trigger[0].flags.edge_type = H4032L_TRIGGER_EDGE_TYPE_FALL; | |
571 | cmd_pkt->trigger[0].flags.edge_signal = match->channel->index; | |
572 | break; | |
573 | case SR_TRIGGER_EDGE: | |
574 | if (cmd_pkt->trigger[0].flags.edge_type != H4032L_TRIGGER_EDGE_TYPE_DISABLED) { | |
4868f15a | 575 | sr_err("Only one trigger signal with fall/rising/edge allowed."); |
5089a143 AZ |
576 | return SR_ERR; |
577 | } | |
578 | cmd_pkt->trigger[0].flags.edge_type = H4032L_TRIGGER_EDGE_TYPE_TOGGLE; | |
579 | cmd_pkt->trigger[0].flags.edge_signal = match->channel->index; | |
580 | break; | |
581 | default: | |
4868f15a | 582 | sr_err("Unknown trigger value."); |
5089a143 AZ |
583 | return SR_ERR; |
584 | } | |
585 | ||
586 | channel = channel->next; | |
587 | } | |
588 | ||
80d34975 UH |
589 | cmd_pkt->trigger[0].flags.data_range_enabled = 1; |
590 | cmd_pkt->trigger[0].data_range_mask |= range_mask; | |
591 | cmd_pkt->trigger[0].data_range_max = range_value; | |
5089a143 | 592 | } |
6a25fa42 | 593 | |
74c4c174 | 594 | usb_source_add(sdi->session, drvc->sr_ctx, 1000, |
5089a143 | 595 | h4032l_receive_data, sdi->driver->context); |
6a25fa42 | 596 | |
5089a143 AZ |
597 | /* Start capturing. */ |
598 | return h4032l_start(sdi); | |
6a25fa42 AZ |
599 | } |
600 | ||
5089a143 | 601 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) |
6a25fa42 | 602 | { |
2958315d AV |
603 | /* Stop capturing. */ |
604 | return h4032l_stop(sdi); | |
6a25fa42 AZ |
605 | } |
606 | ||
3f34a402 | 607 | static struct sr_dev_driver hantek_4032l_driver_info = { |
6a25fa42 | 608 | .name = "hantek-4032l", |
5089a143 | 609 | .longname = "Hantek 4032L", |
6a25fa42 | 610 | .api_version = 1, |
5089a143 AZ |
611 | .init = std_init, |
612 | .cleanup = std_cleanup, | |
6a25fa42 | 613 | .scan = scan, |
5089a143 AZ |
614 | .dev_list = std_dev_list, |
615 | .dev_clear = std_dev_clear, | |
6a25fa42 AZ |
616 | .config_get = config_get, |
617 | .config_set = config_set, | |
618 | .config_list = config_list, | |
619 | .dev_open = dev_open, | |
620 | .dev_close = dev_close, | |
621 | .dev_acquisition_start = dev_acquisition_start, | |
622 | .dev_acquisition_stop = dev_acquisition_stop, | |
623 | .context = NULL, | |
624 | }; | |
5089a143 | 625 | SR_REGISTER_DEV_DRIVER(hantek_4032l_driver_info); |