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