]> sigrok.org Git - libsigrok.git/blob - src/hardware/openbench-logic-sniffer/api.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / hardware / openbench-logic-sniffer / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21 #include "protocol.h"
22
23 #define SERIALCOMM "115200/8n1"
24
25 static const uint32_t scanopts[] = {
26         SR_CONF_CONN,
27         SR_CONF_SERIALCOMM,
28         SR_CONF_PROBE_NAMES,
29 };
30
31 static const uint32_t drvopts[] = {
32         SR_CONF_LOGIC_ANALYZER,
33 };
34
35 static const uint32_t devopts[] = {
36         SR_CONF_CONN | SR_CONF_GET,
37         SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
38         SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
39         SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
40         SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
41         SR_CONF_EXTERNAL_CLOCK | SR_CONF_GET | SR_CONF_SET,
42         SR_CONF_CLOCK_EDGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
43         SR_CONF_PATTERN_MODE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
44         SR_CONF_SWAP | SR_CONF_SET,
45         SR_CONF_RLE | SR_CONF_GET | SR_CONF_SET,
46 };
47
48 static const int32_t trigger_matches[] = {
49         SR_TRIGGER_ZERO,
50         SR_TRIGGER_ONE,
51 };
52
53 static const char *external_clock_edges[] = {
54         "rising", /* positive edge */
55         "falling" /* negative edge */
56 };
57
58 #define STR_PATTERN_NONE     "None"
59 #define STR_PATTERN_EXTERNAL "External"
60 #define STR_PATTERN_INTERNAL "Internal"
61
62 /* Supported methods of test pattern outputs */
63 enum {
64         /**
65          * Capture pins 31:16 (unbuffered wing) output a test pattern
66          * that can captured on pins 0:15.
67          */
68         PATTERN_EXTERNAL,
69
70         /** Route test pattern internally to capture buffer. */
71         PATTERN_INTERNAL,
72 };
73
74 static const char *patterns[] = {
75         STR_PATTERN_NONE,
76         STR_PATTERN_EXTERNAL,
77         STR_PATTERN_INTERNAL,
78 };
79
80 /* Channels are numbered 0-31 (on the PCB silkscreen). */
81 SR_PRIV const char *ols_channel_names[] = {
82         "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
83         "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23",
84         "24", "25", "26", "27", "28", "29", "30", "31",
85 };
86
87 /* Default supported samplerates, can be overridden by device metadata. */
88 static const uint64_t samplerates[] = {
89         SR_HZ(10),
90         SR_MHZ(200),
91         SR_HZ(1),
92 };
93
94 #define RESPONSE_DELAY_US (20 * 1000)
95
96 static GSList *scan(struct sr_dev_driver *di, GSList *options)
97 {
98         struct sr_config *src;
99         struct sr_dev_inst *sdi;
100         struct sr_serial_dev_inst *serial;
101         GSList *l;
102         int num_read;
103         unsigned int i;
104         const char *conn, *serialcomm, *probe_names;
105         char buf[4] = { 0, 0, 0, 0 };
106         struct dev_context *devc;
107         size_t ch_max;
108
109         conn = serialcomm = NULL;
110         probe_names = NULL;
111         for (l = options; l; l = l->next) {
112                 src = l->data;
113                 switch (src->key) {
114                 case SR_CONF_CONN:
115                         conn = g_variant_get_string(src->data, NULL);
116                         break;
117                 case SR_CONF_SERIALCOMM:
118                         serialcomm = g_variant_get_string(src->data, NULL);
119                         break;
120                 case SR_CONF_PROBE_NAMES:
121                         probe_names = g_variant_get_string(src->data, NULL);
122                         break;
123                 }
124         }
125         if (!conn)
126                 return NULL;
127
128         if (!serialcomm)
129                 serialcomm = SERIALCOMM;
130
131         serial = sr_serial_dev_inst_new(conn, serialcomm);
132
133         /* The discovery procedure is like this: first send the Reset
134          * command (0x00) 5 times, since the device could be anywhere
135          * in a 5-byte command. Then send the ID command (0x02).
136          * If the device responds with 4 bytes ("OLS1" or "SLA1"), we
137          * have a match.
138          */
139         sr_info("Probing %s.", conn);
140         if (serial_open(serial, SERIAL_RDWR) != SR_OK)
141                 return NULL;
142
143         if (ols_send_reset(serial) != SR_OK) {
144                 serial_close(serial);
145                 sr_err("Could not use port %s. Quitting.", conn);
146                 return NULL;
147         }
148         send_shortcommand(serial, CMD_ID);
149
150         g_usleep(RESPONSE_DELAY_US);
151
152         if (serial_has_receive_data(serial) == 0) {
153                 sr_dbg("Didn't get any ID reply.");
154                 return NULL;
155         }
156
157         num_read =
158                 serial_read_blocking(serial, buf, 4, serial_timeout(serial, 4));
159         if (num_read < 0) {
160                 sr_err("Getting ID reply failed (%d).", num_read);
161                 return NULL;
162         }
163
164         if (strncmp(buf, "1SLO", 4) && strncmp(buf, "1ALS", 4)) {
165                 GString *id = sr_hexdump_new((uint8_t *)buf, num_read);
166
167                 sr_err("Invalid ID reply (got %s).", id->str);
168
169                 sr_hexdump_free(id);
170                 return NULL;
171         } else {
172                 sr_dbg("Successful detection, got '%c%c%c%c' (0x%02x 0x%02x 0x%02x 0x%02x).",
173                        buf[0], buf[1], buf[2], buf[3],
174                        buf[0], buf[1], buf[2], buf[3]);
175         }
176
177         /*
178          * Create common data structures (sdi, devc) here in the common
179          * code path. These further get filled in either from metadata
180          * which is gathered from the device, or from open coded generic
181          * fallback data which is kept in the driver source code.
182          */
183         sdi = g_malloc0(sizeof(*sdi));
184         sdi->status = SR_ST_INACTIVE;
185         sdi->inst_type = SR_INST_SERIAL;
186         sdi->conn = serial;
187         sdi->connection_id = g_strdup(serial->port);
188         devc = g_malloc0(sizeof(*devc));
189         sdi->priv = devc;
190         devc->trigger_at_smpl = OLS_NO_TRIGGER;
191         devc->channel_names = sr_parse_probe_names(probe_names,
192                 ols_channel_names, ARRAY_SIZE(ols_channel_names),
193                 ARRAY_SIZE(ols_channel_names), &ch_max);
194
195         /*
196          * Definitely using the OLS protocol, check if it supports
197          * the metadata command. Otherwise assign generic values.
198          * Create as many sigrok channels as was determined when
199          * the device was probed.
200          */
201         send_shortcommand(serial, CMD_METADATA);
202         g_usleep(RESPONSE_DELAY_US);
203         if (serial_has_receive_data(serial) != 0) {
204                 /* Got metadata. */
205                 (void)ols_get_metadata(sdi);
206         } else {
207                 /* Not an OLS -- some other board using the SUMP protocol. */
208                 sr_info("Device does not support metadata.");
209                 sdi->vendor = g_strdup("Sump");
210                 sdi->model = g_strdup("Logic Analyzer");
211                 sdi->version = g_strdup("v1.0");
212                 devc->max_channels = ch_max;
213         }
214         if (devc->max_channels && ch_max > devc->max_channels)
215                 ch_max = devc->max_channels;
216         for (i = 0; i < ch_max; i++) {
217                 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
218                         devc->channel_names[i]);
219         }
220         /* Configure samplerate and divider. */
221         if (ols_set_samplerate(sdi, DEFAULT_SAMPLERATE) != SR_OK)
222                 sr_dbg("Failed to set default samplerate (%" PRIu64 ").",
223                        DEFAULT_SAMPLERATE);
224
225         serial_close(serial);
226
227         return std_scan_complete(di, g_slist_append(NULL, sdi));
228 }
229
230 static int config_get(uint32_t key, GVariant **data,
231                       const struct sr_dev_inst *sdi,
232                       const struct sr_channel_group *cg)
233 {
234         struct dev_context *devc;
235
236         (void)cg;
237
238         if (!sdi)
239                 return SR_ERR_ARG;
240
241         devc = sdi->priv;
242
243         switch (key) {
244         case SR_CONF_CONN:
245                 if (!sdi->conn || !sdi->connection_id)
246                         return SR_ERR_NA;
247                 *data = g_variant_new_string(sdi->connection_id);
248                 break;
249         case SR_CONF_SAMPLERATE:
250                 *data = g_variant_new_uint64(devc->cur_samplerate);
251                 break;
252         case SR_CONF_CAPTURE_RATIO:
253                 *data = g_variant_new_uint64(devc->capture_ratio);
254                 break;
255         case SR_CONF_LIMIT_SAMPLES:
256                 *data = g_variant_new_uint64(devc->limit_samples);
257                 break;
258         case SR_CONF_PATTERN_MODE:
259                 if (devc->capture_flags & CAPTURE_FLAG_EXTERNAL_TEST_MODE)
260                         *data = g_variant_new_string(STR_PATTERN_EXTERNAL);
261                 else if (devc->capture_flags & CAPTURE_FLAG_INTERNAL_TEST_MODE)
262                         *data = g_variant_new_string(STR_PATTERN_INTERNAL);
263                 else
264                         *data = g_variant_new_string(STR_PATTERN_NONE);
265                 break;
266         case SR_CONF_RLE:
267                 *data = g_variant_new_boolean(
268                         devc->capture_flags & CAPTURE_FLAG_RLE ? TRUE : FALSE);
269                 break;
270         case SR_CONF_EXTERNAL_CLOCK:
271                 *data = g_variant_new_boolean(
272                         devc->capture_flags & CAPTURE_FLAG_CLOCK_EXTERNAL
273                         ? TRUE : FALSE);
274                 break;
275         case SR_CONF_CLOCK_EDGE:
276                 *data = g_variant_new_string(external_clock_edges[
277                         devc->capture_flags & CAPTURE_FLAG_INVERT_EXT_CLOCK
278                         ? 1 : 0]);
279                 break;
280         default:
281                 return SR_ERR_NA;
282         }
283
284         return SR_OK;
285 }
286
287 static int config_set(uint32_t key, GVariant *data,
288                       const struct sr_dev_inst *sdi,
289                       const struct sr_channel_group *cg)
290 {
291         struct dev_context *devc;
292         uint16_t flag;
293         uint64_t tmp_u64;
294         const char *stropt;
295
296         (void)cg;
297
298         devc = sdi->priv;
299
300         switch (key) {
301         case SR_CONF_SAMPLERATE:
302                 tmp_u64 = g_variant_get_uint64(data);
303                 if (tmp_u64 < samplerates[0] || tmp_u64 > samplerates[1])
304                         return SR_ERR_SAMPLERATE;
305                 return ols_set_samplerate(sdi, g_variant_get_uint64(data));
306         case SR_CONF_LIMIT_SAMPLES:
307                 tmp_u64 = g_variant_get_uint64(data);
308                 if (tmp_u64 < MIN_NUM_SAMPLES)
309                         return SR_ERR;
310                 devc->limit_samples = tmp_u64;
311                 break;
312         case SR_CONF_CAPTURE_RATIO:
313                 devc->capture_ratio = g_variant_get_uint64(data);
314                 break;
315         case SR_CONF_EXTERNAL_CLOCK:
316                 if (g_variant_get_boolean(data)) {
317                         sr_info("Enabling external clock.");
318                         devc->capture_flags |= CAPTURE_FLAG_CLOCK_EXTERNAL;
319                 } else {
320                         sr_info("Disabled external clock.");
321                         devc->capture_flags &= ~CAPTURE_FLAG_CLOCK_EXTERNAL;
322                 }
323                 break;
324         case SR_CONF_CLOCK_EDGE:
325                 stropt = g_variant_get_string(data, NULL);
326                 if (!strcmp(stropt, external_clock_edges[1])) {
327                         sr_info("Triggering on falling edge of external clock.");
328                         devc->capture_flags |= CAPTURE_FLAG_INVERT_EXT_CLOCK;
329                 } else {
330                         sr_info("Triggering on rising edge of external clock.");
331                         devc->capture_flags &= ~CAPTURE_FLAG_INVERT_EXT_CLOCK;
332                 }
333                 break;
334         case SR_CONF_PATTERN_MODE:
335                 stropt = g_variant_get_string(data, NULL);
336                 if (!strcmp(stropt, STR_PATTERN_NONE)) {
337                         sr_info("Disabling test modes.");
338                         flag = 0x0000;
339                 } else if (!strcmp(stropt, STR_PATTERN_INTERNAL)) {
340                         sr_info("Enabling internal test mode.");
341                         flag = CAPTURE_FLAG_INTERNAL_TEST_MODE;
342                 } else if (!strcmp(stropt, STR_PATTERN_EXTERNAL)) {
343                         sr_info("Enabling external test mode.");
344                         flag = CAPTURE_FLAG_EXTERNAL_TEST_MODE;
345                 } else {
346                         return SR_ERR;
347                 }
348                 devc->capture_flags &= ~CAPTURE_FLAG_INTERNAL_TEST_MODE;
349                 devc->capture_flags &= ~CAPTURE_FLAG_EXTERNAL_TEST_MODE;
350                 devc->capture_flags |= flag;
351                 break;
352         case SR_CONF_SWAP:
353                 if (g_variant_get_boolean(data)) {
354                         sr_info("Enabling channel swapping.");
355                         devc->capture_flags |= CAPTURE_FLAG_SWAP_CHANNELS;
356                 } else {
357                         sr_info("Disabling channel swapping.");
358                         devc->capture_flags &= ~CAPTURE_FLAG_SWAP_CHANNELS;
359                 }
360                 break;
361         case SR_CONF_RLE:
362                 if (g_variant_get_boolean(data)) {
363                         sr_info("Enabling RLE.");
364                         devc->capture_flags |= CAPTURE_FLAG_RLE;
365                 } else {
366                         sr_info("Disabling RLE.");
367                         devc->capture_flags &= ~CAPTURE_FLAG_RLE;
368                 }
369                 break;
370         default:
371                 return SR_ERR_NA;
372         }
373
374         return SR_OK;
375 }
376
377 static int config_list(uint32_t key, GVariant **data,
378                        const struct sr_dev_inst *sdi,
379                        const struct sr_channel_group *cg)
380 {
381         struct dev_context *devc;
382         int num_ols_changrp, i;
383
384         switch (key) {
385         case SR_CONF_SCAN_OPTIONS:
386         case SR_CONF_DEVICE_OPTIONS:
387                 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts,
388                                        devopts);
389         case SR_CONF_SAMPLERATE:
390                 *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates));
391                 break;
392         case SR_CONF_TRIGGER_MATCH:
393                 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
394                 break;
395         case SR_CONF_CLOCK_EDGE:
396                 *data = std_gvar_array_str(ARRAY_AND_SIZE(external_clock_edges));
397                 break;
398         case SR_CONF_PATTERN_MODE:
399                 *data = g_variant_new_strv(ARRAY_AND_SIZE(patterns));
400                 break;
401         case SR_CONF_LIMIT_SAMPLES:
402                 if (!sdi)
403                         return SR_ERR_ARG;
404                 devc = sdi->priv;
405                 if (devc->max_samples == 0)
406                         /* Device didn't specify sample memory size in metadata. */
407                         return SR_ERR_NA;
408                 /*
409                  * Channel groups are turned off if no channels in that group are
410                  * enabled, making more room for samples for the enabled group.
411                 */
412                 uint32_t channel_mask = ols_channel_mask(sdi);
413                 num_ols_changrp = 0;
414                 for (i = 0; i < 4; i++) {
415                         if (channel_mask & (0xff << (i * 8)))
416                                 num_ols_changrp++;
417                 }
418
419                 *data = std_gvar_tuple_u64(MIN_NUM_SAMPLES, (num_ols_changrp)
420                         ? devc->max_samples / num_ols_changrp : MIN_NUM_SAMPLES);
421                 break;
422         default:
423                 return SR_ERR_NA;
424         }
425
426         return SR_OK;
427 }
428
429 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
430 {
431         int ret;
432         struct dev_context *devc;
433         struct sr_serial_dev_inst *serial;
434
435         devc = sdi->priv;
436         serial = sdi->conn;
437
438         ret = ols_prepare_acquisition(sdi);
439         if (ret != SR_OK)
440                 return ret;
441
442         /* Start acquisition on the device. */
443         if (send_shortcommand(serial, CMD_ARM_BASIC_TRIGGER) != SR_OK)
444                 return SR_ERR;
445
446         /* Reset all operational states. */
447         devc->rle_count = devc->num_transfers = 0;
448         devc->num_samples = devc->num_bytes = 0;
449         devc->cnt_bytes = devc->cnt_samples = devc->cnt_samples_rle = 0;
450         memset(devc->sample, 0, 4);
451
452         std_session_send_df_header(sdi);
453
454         /* If the device stops sending for longer than it takes to send a byte,
455          * that means it's finished. But wait at least 100 ms to be safe.
456          */
457         return serial_source_add(sdi->session, serial, G_IO_IN, 100,
458                                  ols_receive_data, (struct sr_dev_inst *)sdi);
459 }
460
461 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
462 {
463         abort_acquisition(sdi);
464
465         return SR_OK;
466 }
467
468 static struct sr_dev_driver ols_driver_info = {
469         .name = "ols",
470         .longname = "Openbench Logic Sniffer & SUMP compatibles",
471         .api_version = 1,
472         .init = std_init,
473         .cleanup = std_cleanup,
474         .scan = scan,
475         .dev_list = std_dev_list,
476         .dev_clear = std_dev_clear,
477         .config_get = config_get,
478         .config_set = config_set,
479         .config_list = config_list,
480         .dev_open = std_serial_dev_open,
481         .dev_close = std_serial_dev_close,
482         .dev_acquisition_start = dev_acquisition_start,
483         .dev_acquisition_stop = dev_acquisition_stop,
484         .context = NULL,
485 };
486 SR_REGISTER_DEV_DRIVER(ols_driver_info);