]> sigrok.org Git - libsigrok.git/blob - src/hardware/sysclk-sla5032/api.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / hardware / sysclk-sla5032 / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2014 Daniel Elstner <daniel.kitta@gmail.com>
5  * Copyright (C) 2019 Vitaliy Vorobyov
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22 #include <glib.h>
23 #include <libusb.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <libsigrok/libsigrok.h>
27 #include <libsigrok-internal.h>
28 #include "protocol.h"
29
30  /* Number of logic channels. */
31 #define NUM_CHANNELS    32
32
33 static const uint32_t scanopts[] = {
34         SR_CONF_CONN,
35 };
36
37 static const uint32_t drvopts[] = {
38         SR_CONF_LOGIC_ANALYZER,
39 };
40
41 static const int32_t trigger_matches[] = {
42         SR_TRIGGER_ZERO,
43         SR_TRIGGER_ONE,
44         SR_TRIGGER_RISING,
45         SR_TRIGGER_FALLING,
46 };
47
48 static const uint64_t capture_ratios[] = {
49         0, 10, 20, 30, 50, 70, 90, 100,
50 };
51
52 static const uint32_t devopts[] = {
53         SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
54         SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
55         SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
56         SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
57         SR_CONF_RLE | SR_CONF_GET,
58 };
59
60 static const uint64_t samplerates[] = {
61         SR_MHZ(500), SR_MHZ(400), SR_MHZ(250), SR_MHZ(200), SR_MHZ(100),
62         SR_MHZ(50), SR_MHZ(25), SR_MHZ(20), SR_MHZ(10), SR_MHZ(5), SR_MHZ(2),
63         SR_MHZ(1), SR_KHZ(500), SR_KHZ(200), SR_KHZ(100), SR_KHZ(50),
64         SR_KHZ(20), SR_KHZ(10), SR_KHZ(5), SR_KHZ(2),
65 };
66
67 static struct sr_dev_inst *dev_inst_new(void)
68 {
69         struct sr_dev_inst *sdi;
70         struct dev_context *devc;
71         int i;
72         char name[8];
73
74         devc = g_malloc0(sizeof(struct dev_context));
75         devc->active_fpga_config = FPGA_NOCONF;
76         devc->samplerate = samplerates[0];
77         devc->limit_samples = MAX_LIMIT_SAMPLES;
78         devc->capture_ratio = capture_ratios[4];
79         devc->channel_mask = (UINT64_C(1) << NUM_CHANNELS) - 1;
80
81         sdi = g_malloc0(sizeof(struct sr_dev_inst));
82         sdi->status = SR_ST_INACTIVE;
83         sdi->vendor = g_strdup("Sysclk");
84         sdi->model = g_strdup("SLA5032");
85         sdi->priv = devc;
86
87         for (i = 0; i < NUM_CHANNELS; i++) {
88                 g_snprintf(name, sizeof(name), "CH%d", i);
89                 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, name);
90         }
91
92         return sdi;
93 }
94
95 /* Create a new device instance for a libusb device if it is a Sysclk SLA5032
96  * device and also matches the connection specification.
97  */
98 static struct sr_dev_inst *dev_inst_new_matching(GSList *conn_matches,
99                                                  libusb_device *dev)
100 {
101         GSList *node;
102         struct sr_usb_dev_inst *usb;
103         struct sr_dev_inst *sdi;
104         struct libusb_device_descriptor des;
105         int bus, address, ret;
106         unsigned int vid, pid;
107
108         bus = libusb_get_bus_number(dev);
109         address = libusb_get_device_address(dev);
110
111         for (node = conn_matches; node != NULL; node = node->next) {
112                 usb = node->data;
113                 if (usb && usb->bus == bus && usb->address == address)
114                         break; /* found */
115         }
116         if (conn_matches && !node)
117                 return NULL; /* no match */
118
119         ret = libusb_get_device_descriptor(dev, &des);
120         if (ret != 0) {
121                 sr_err("Failed to get USB device descriptor: %s.",
122                         libusb_error_name(ret));
123                 return NULL;
124         }
125         vid = des.idVendor;
126         pid = des.idProduct;
127
128         /* Create sigrok device instance. */
129         if (vid == USB_VID_SYSCLK && pid == USB_PID_SLA5032) {
130         } else {
131                 if (conn_matches)
132                         sr_warn("USB device %d.%d (%04x:%04x) is not a"
133                                 " Sysclk SLA5032.", bus, address, vid, pid);
134                 return NULL;
135         }
136         sdi = dev_inst_new();
137
138         sdi->inst_type = SR_INST_USB;
139         sdi->conn = sr_usb_dev_inst_new(bus, address, NULL);
140
141         return sdi;
142 }
143
144 static GSList *scan(struct sr_dev_driver *di, GSList *options)
145 {
146         GSList *conn_devices, *devices, *node;
147         struct drv_context *drvc;
148         struct sr_dev_inst *sdi;
149         struct sr_config *src;
150         const char *conn;
151         libusb_device **devlist;
152         ssize_t num_devs, i;
153
154         drvc = di->context;
155         conn = NULL;
156         conn_devices = NULL;
157         devices = NULL;
158
159         for (node = options; node != NULL; node = node->next) {
160                 src = node->data;
161                 if (src->key == SR_CONF_CONN) {
162                         conn = g_variant_get_string(src->data, NULL);
163                         break;
164                 }
165         }
166         if (conn) {
167                 /* Find devices matching the connection specification. */
168                 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
169         }
170
171         /* List all libusb devices. */
172         num_devs = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
173         if (num_devs < 0) {
174                 sr_err("Failed to list USB devices: %s.",
175                         libusb_error_name(num_devs));
176                 g_slist_free_full(conn_devices,
177                         (GDestroyNotify)&sr_usb_dev_inst_free);
178                 return NULL;
179         }
180
181         /* Scan the USB device list for matching devices. */
182         for (i = 0; i < num_devs; i++) {
183                 sdi = dev_inst_new_matching(conn_devices, devlist[i]);
184                 if (!sdi)
185                         continue; /* no match */
186
187                 /* Register device instance with driver. */
188                 devices = g_slist_append(devices, sdi);
189         }
190
191         libusb_free_device_list(devlist, 1);
192         g_slist_free_full(conn_devices, (GDestroyNotify)&sr_usb_dev_inst_free);
193
194         return std_scan_complete(di, devices);
195 }
196
197 static int dev_open(struct sr_dev_inst *sdi)
198 {
199         struct drv_context *drvc;
200         struct dev_context *devc;
201         struct sr_usb_dev_inst *usb;
202         int ret;
203
204         drvc = sdi->driver->context;
205         devc = sdi->priv;
206         usb = sdi->conn;
207
208         ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
209         if (ret != SR_OK)
210                 return ret;
211
212         ret = libusb_set_configuration(usb->devhdl, USB_CONFIG);
213         if (ret != LIBUSB_SUCCESS) {
214                 sr_err("Failed to set USB configuration: %s.",
215                         libusb_error_name(ret));
216                 sr_usb_close(usb);
217                 return SR_ERR;
218         }
219
220         ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
221         if (ret != LIBUSB_SUCCESS) {
222                 sr_err("Failed to claim interface: %s.",
223                         libusb_error_name(ret));
224                 sr_usb_close(usb);
225                 return SR_ERR;
226         }
227
228         sdi->status = SR_ST_ACTIVE;
229
230         devc->active_fpga_config = FPGA_NOCONF;
231         devc->state = STATE_IDLE;
232
233         return sla5032_apply_fpga_config(sdi);
234 }
235
236 static int dev_close(struct sr_dev_inst *sdi)
237 {
238         struct sr_usb_dev_inst *usb;
239
240         usb = sdi->conn;
241
242         if (usb->devhdl)
243                 libusb_release_interface(usb->devhdl, USB_INTERFACE);
244
245         sr_usb_close(usb);
246
247         return SR_OK;
248 }
249
250 /* Check whether the device options contain a specific key.
251  * Also match against get/set/list bits if specified.
252  */
253 static int has_devopt(uint32_t key)
254 {
255         unsigned int i;
256
257         for (i = 0; i < ARRAY_SIZE(devopts); i++) {
258                 if ((devopts[i] & (SR_CONF_MASK | key)) == key)
259                         return TRUE;
260         }
261
262         return FALSE;
263 }
264
265 static int config_get(uint32_t key, GVariant **data,
266         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
267 {
268         struct dev_context *devc;
269
270         (void)cg;
271
272         if (!sdi)
273                 return SR_ERR_ARG;
274
275         devc = sdi->priv;
276
277         if (!has_devopt(key | SR_CONF_GET))
278                 return SR_ERR_NA;
279
280         switch (key) {
281         case SR_CONF_SAMPLERATE:
282                 *data = g_variant_new_uint64(devc->samplerate);
283                 break;
284         case SR_CONF_LIMIT_SAMPLES:
285                 *data = g_variant_new_uint64(devc->limit_samples);
286                 break;
287         case SR_CONF_CAPTURE_RATIO:
288                 *data = g_variant_new_uint64(devc->capture_ratio);
289                 break;
290         case SR_CONF_RLE:
291                 *data = g_variant_new_boolean(TRUE);
292                 break;
293         default:
294                 /* Must not happen for a key listed in devopts. */
295                 return SR_ERR_BUG;
296         }
297
298         return SR_OK;
299 }
300
301 static int config_set(uint32_t key, GVariant *data,
302         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
303 {
304         uint64_t value;
305         struct dev_context *devc;
306         int idx;
307
308         (void)cg;
309
310         if (!sdi)
311                 return SR_ERR_ARG;
312
313         devc = sdi->priv;
314
315         if (!has_devopt(key | SR_CONF_SET))
316                 return SR_ERR_NA;
317
318         switch (key) {
319         case SR_CONF_SAMPLERATE:
320                 value = g_variant_get_uint64(data);
321                 if ((idx = std_u64_idx(data, ARRAY_AND_SIZE(samplerates))) < 0)
322                         return SR_ERR_ARG;
323                 devc->samplerate = samplerates[idx];
324                 break;
325         case SR_CONF_LIMIT_SAMPLES:
326                 value = g_variant_get_uint64(data);
327                 if (value > MAX_LIMIT_SAMPLES || value < MIN_LIMIT_SAMPLES)
328                         return SR_ERR_ARG;
329                 devc->limit_samples = value;
330                 break;
331         case SR_CONF_CAPTURE_RATIO:
332                 devc->capture_ratio = g_variant_get_uint64(data);
333                 break;
334         default:
335                 /* Must not happen for a key listed in devopts. */
336                 return SR_ERR_BUG;
337         }
338
339         return SR_OK;
340 }
341
342 static int config_channel_set(const struct sr_dev_inst *sdi,
343         struct sr_channel *ch, unsigned int changes)
344 {
345         uint64_t channel_bit;
346         struct dev_context *devc;
347
348         if (!sdi)
349                 return SR_ERR_ARG;
350
351         devc = sdi->priv;
352
353         if (ch->index < 0 || ch->index >= NUM_CHANNELS) {
354                 sr_err("Channel index %d out of range.", ch->index);
355                 return SR_ERR_BUG;
356         }
357
358         if ((changes & SR_CHANNEL_SET_ENABLED) != 0) {
359                 channel_bit = UINT64_C(1) << ch->index;
360
361                 /* Enable or disable logic input for this channel. */
362                 if (ch->enabled)
363                         devc->channel_mask |= channel_bit;
364                 else
365                         devc->channel_mask &= ~channel_bit;
366         }
367
368         return SR_OK;
369 }
370
371 /* Derive trigger masks from the session's trigger configuration. */
372 static int prepare_trigger_masks(const struct sr_dev_inst *sdi)
373 {
374         uint32_t trigger_mask, trigger_values, trigger_edge_mask;
375         uint32_t level_bit, type_bit;
376         struct dev_context *devc;
377         struct sr_trigger *trigger;
378         struct sr_trigger_stage *stage;
379         struct sr_trigger_match *match;
380         const GSList *node;
381         int idx;
382         enum sr_trigger_matches trg;
383
384         devc = sdi->priv;
385
386         trigger_mask = 0;
387         trigger_values = 0;
388         trigger_edge_mask = 0;
389
390         trigger = sr_session_trigger_get(sdi->session);
391         if (!trigger || !trigger->stages) {
392                 goto no_triggers;
393         }
394
395         if (trigger->stages->next) {
396                 sr_err("This device only supports 1 trigger stage.");
397                 return SR_ERR_ARG;
398         }
399         stage = trigger->stages->data;
400
401         for (node = stage->matches; node; node = node->next) {
402                 match = node->data;
403
404                 if (!match->channel->enabled)
405                         continue; /* Ignore disabled channel. */
406
407                 idx = match->channel->index;
408                 trg = match->match;
409
410                 if (idx < 0 || idx >= NUM_CHANNELS) {
411                         sr_err("Channel index %d out of range.", idx);
412                         return SR_ERR_BUG; /* Should not happen. */
413                 }
414                 if (trg != SR_TRIGGER_ZERO
415                         && trg != SR_TRIGGER_ONE
416                         && trg != SR_TRIGGER_RISING
417                         && trg != SR_TRIGGER_FALLING) {
418                         sr_err("Unsupported trigger match for CH%d.", idx);
419                         return SR_ERR_ARG;
420                 }
421                 level_bit = (trg == SR_TRIGGER_ONE
422                         || trg == SR_TRIGGER_RISING) ? 1 : 0;
423                 type_bit = (trg == SR_TRIGGER_RISING
424                         || trg == SR_TRIGGER_FALLING) ? 1 : 0; /* 1 if edge triggered, 0 if level triggered */
425
426                 trigger_mask |= UINT32_C(1) << idx;
427                 trigger_values |= level_bit << idx;
428                 trigger_edge_mask |= type_bit << idx;
429         }
430
431 no_triggers:
432         devc->trigger_mask = trigger_mask;
433         devc->trigger_values = trigger_values;
434         devc->trigger_edge_mask = trigger_edge_mask;
435
436         return SR_OK;
437 }
438
439 static int config_commit(const struct sr_dev_inst *sdi)
440 {
441         int ret;
442
443         ret = prepare_trigger_masks(sdi);
444         if (ret != SR_OK)
445                 return ret;
446
447         ret = sla5032_apply_fpga_config(sdi);
448         if (ret != SR_OK) {
449                 sr_err("Failed to apply FPGA configuration.");
450                 return ret;
451         }
452
453         return SR_OK;
454 }
455
456 static int config_list(uint32_t key, GVariant **data,
457         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
458 {
459         struct dev_context *devc;
460
461         devc = (sdi) ? sdi->priv : NULL;
462
463         switch (key) {
464         case SR_CONF_SCAN_OPTIONS:
465         case SR_CONF_DEVICE_OPTIONS:
466                 return std_opts_config_list(key, data, sdi, cg,
467                         ARRAY_AND_SIZE(scanopts), ARRAY_AND_SIZE(drvopts),
468                         (devc) ? devopts : NULL,
469                         (devc) ? ARRAY_SIZE(devopts) : 0);
470         }
471
472         if (!devc)
473                 return SR_ERR_ARG;
474         if (!has_devopt(key | SR_CONF_LIST))
475                 return SR_ERR_NA;
476
477         switch (key) {
478         case SR_CONF_SAMPLERATE:
479                 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates));
480                 break;
481         case SR_CONF_LIMIT_SAMPLES:
482                 *data = std_gvar_tuple_u64(MIN_LIMIT_SAMPLES, MAX_LIMIT_SAMPLES);
483                 break;
484         case SR_CONF_CAPTURE_RATIO:
485                 *data = std_gvar_array_u64(ARRAY_AND_SIZE(capture_ratios));
486                 break;
487         case SR_CONF_TRIGGER_MATCH:
488                 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
489                 break;
490         default:
491                 /* Must not happen for a key listed in devopts. */
492                 return SR_ERR_BUG;
493         }
494
495         return SR_OK;
496 }
497
498 /* Set up the device hardware to begin capturing samples as soon as the
499  * configured trigger conditions are met, or immediately if no triggers
500  * are configured.
501  */
502 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
503 {
504         return sla5032_start_acquisition(sdi);
505 }
506
507 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
508 {
509         struct dev_context *devc;
510
511         devc = sdi->priv;
512
513         sr_session_source_remove(sdi->session, -1);
514
515         std_session_send_df_end(sdi);
516
517         devc->state = STATE_IDLE;
518
519         return SR_OK;
520 }
521
522 static struct sr_dev_driver sysclk_sla5032_driver_info = {
523         .name = "sysclk-sla5032",
524         .longname = "Sysclk SLA5032",
525         .api_version = 1,
526         .init = std_init,
527         .cleanup = std_cleanup,
528         .scan = scan,
529         .dev_list = std_dev_list,
530         .dev_clear = std_dev_clear,
531         .config_get = config_get,
532         .config_set = config_set,
533         .config_channel_set = config_channel_set,
534         .config_commit = config_commit,
535         .config_list = config_list,
536         .dev_open = dev_open,
537         .dev_close = dev_close,
538         .dev_acquisition_start = dev_acquisition_start,
539         .dev_acquisition_stop = dev_acquisition_stop,
540         .context = NULL,
541 };
542 SR_REGISTER_DEV_DRIVER(sysclk_sla5032_driver_info);