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