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