2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
23 #define SERIALCOMM "115200/8n1"
25 static const uint32_t scanopts[] = {
30 static const uint32_t drvopts[] = {
31 SR_CONF_LOGIC_ANALYZER,
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,
47 static const int32_t trigger_matches[] = {
52 static const char *external_clock_edges[] = {
53 "rising", /* positive edge */
54 "falling" /* negative edge */
57 #define STR_PATTERN_NONE "None"
58 #define STR_PATTERN_EXTERNAL "External"
59 #define STR_PATTERN_INTERNAL "Internal"
61 /* Supported methods of test pattern outputs */
64 * Capture pins 31:16 (unbuffered wing) output a test pattern
65 * that can captured on pins 0:15.
69 /** Route test pattern internally to capture buffer. */
73 static const char *patterns[] = {
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",
86 /* Default supported samplerates, can be overridden by device metadata. */
87 static const uint64_t samplerates[] = {
93 #define RESPONSE_DELAY_US (20 * 1000)
95 static GSList *scan(struct sr_dev_driver *di, GSList *options)
97 struct sr_config *src;
98 struct sr_dev_inst *sdi;
99 struct sr_serial_dev_inst *serial;
103 const char *conn, *serialcomm;
104 char buf[4] = { 0, 0, 0, 0 };
106 conn = serialcomm = NULL;
107 for (l = options; l; l = l->next) {
111 conn = g_variant_get_string(src->data, NULL);
113 case SR_CONF_SERIALCOMM:
114 serialcomm = g_variant_get_string(src->data, NULL);
122 serialcomm = SERIALCOMM;
124 serial = sr_serial_dev_inst_new(conn, serialcomm);
126 /* The discovery procedure is like this: first send the Reset
127 * command (0x00) 5 times, since the device could be anywhere
128 * in a 5-byte command. Then send the ID command (0x02).
129 * If the device responds with 4 bytes ("OLS1" or "SLA1"), we
132 sr_info("Probing %s.", conn);
133 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
136 if (ols_send_reset(serial) != SR_OK) {
137 serial_close(serial);
138 sr_err("Could not use port %s. Quitting.", conn);
141 send_shortcommand(serial, CMD_ID);
143 g_usleep(RESPONSE_DELAY_US);
145 if (serial_has_receive_data(serial) == 0) {
146 sr_dbg("Didn't get any ID reply.");
151 serial_read_blocking(serial, buf, 4, serial_timeout(serial, 4));
153 sr_err("Getting ID reply failed (%d).", num_read);
157 if (strncmp(buf, "1SLO", 4) && strncmp(buf, "1ALS", 4)) {
158 GString *id = sr_hexdump_new((uint8_t *)buf, num_read);
160 sr_err("Invalid ID reply (got %s).", id->str);
165 sr_dbg("Successful detection, got '%c%c%c%c' (0x%02x 0x%02x 0x%02x 0x%02x).",
166 buf[0], buf[1], buf[2], buf[3],
167 buf[0], buf[1], buf[2], buf[3]);
170 /* Definitely using the OLS protocol, check if it supports
171 * the metadata command.
173 send_shortcommand(serial, CMD_METADATA);
175 g_usleep(RESPONSE_DELAY_US);
177 if (serial_has_receive_data(serial) != 0) {
179 sdi = get_metadata(serial);
181 /* Not an OLS -- some other board that uses the sump protocol. */
182 sr_info("Device does not support metadata.");
183 sdi = g_malloc0(sizeof(struct sr_dev_inst));
184 sdi->status = SR_ST_INACTIVE;
185 sdi->vendor = g_strdup("Sump");
186 sdi->model = g_strdup("Logic Analyzer");
187 sdi->version = g_strdup("v1.0");
188 for (i = 0; i < ARRAY_SIZE(ols_channel_names); i++)
189 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
190 ols_channel_names[i]);
191 sdi->priv = ols_dev_new();
193 /* Configure samplerate and divider. */
194 if (ols_set_samplerate(sdi, DEFAULT_SAMPLERATE) != SR_OK)
195 sr_dbg("Failed to set default samplerate (%" PRIu64 ").",
197 sdi->inst_type = SR_INST_SERIAL;
199 sdi->connection_id = g_strdup(serial->port);
201 serial_close(serial);
203 return std_scan_complete(di, g_slist_append(NULL, sdi));
206 static int config_get(uint32_t key, GVariant **data,
207 const struct sr_dev_inst *sdi,
208 const struct sr_channel_group *cg)
210 struct dev_context *devc;
221 if (!sdi->conn || !sdi->connection_id)
223 *data = g_variant_new_string(sdi->connection_id);
225 case SR_CONF_SAMPLERATE:
226 *data = g_variant_new_uint64(devc->cur_samplerate);
228 case SR_CONF_CAPTURE_RATIO:
229 *data = g_variant_new_uint64(devc->capture_ratio);
231 case SR_CONF_LIMIT_SAMPLES:
232 *data = g_variant_new_uint64(devc->limit_samples);
234 case SR_CONF_PATTERN_MODE:
235 if (devc->capture_flags & CAPTURE_FLAG_EXTERNAL_TEST_MODE)
236 *data = g_variant_new_string(STR_PATTERN_EXTERNAL);
237 else if (devc->capture_flags & CAPTURE_FLAG_INTERNAL_TEST_MODE)
238 *data = g_variant_new_string(STR_PATTERN_INTERNAL);
240 *data = g_variant_new_string(STR_PATTERN_NONE);
243 *data = g_variant_new_boolean(
244 devc->capture_flags & CAPTURE_FLAG_RLE ? TRUE : FALSE);
246 case SR_CONF_EXTERNAL_CLOCK:
247 *data = g_variant_new_boolean(
248 devc->capture_flags & CAPTURE_FLAG_CLOCK_EXTERNAL
251 case SR_CONF_CLOCK_EDGE:
252 *data = g_variant_new_string(external_clock_edges[
253 devc->capture_flags & CAPTURE_FLAG_INVERT_EXT_CLOCK
263 static int config_set(uint32_t key, GVariant *data,
264 const struct sr_dev_inst *sdi,
265 const struct sr_channel_group *cg)
267 struct dev_context *devc;
277 case SR_CONF_SAMPLERATE:
278 tmp_u64 = g_variant_get_uint64(data);
279 if (tmp_u64 < samplerates[0] || tmp_u64 > samplerates[1])
280 return SR_ERR_SAMPLERATE;
281 return ols_set_samplerate(sdi, g_variant_get_uint64(data));
282 case SR_CONF_LIMIT_SAMPLES:
283 tmp_u64 = g_variant_get_uint64(data);
284 if (tmp_u64 < MIN_NUM_SAMPLES)
286 devc->limit_samples = tmp_u64;
288 case SR_CONF_CAPTURE_RATIO:
289 devc->capture_ratio = g_variant_get_uint64(data);
291 case SR_CONF_EXTERNAL_CLOCK:
292 if (g_variant_get_boolean(data)) {
293 sr_info("Enabling external clock.");
294 devc->capture_flags |= CAPTURE_FLAG_CLOCK_EXTERNAL;
296 sr_info("Disabled external clock.");
297 devc->capture_flags &= ~CAPTURE_FLAG_CLOCK_EXTERNAL;
300 case SR_CONF_CLOCK_EDGE:
301 stropt = g_variant_get_string(data, NULL);
302 if (!strcmp(stropt, external_clock_edges[1])) {
303 sr_info("Triggering on falling edge of external clock.");
304 devc->capture_flags |= CAPTURE_FLAG_INVERT_EXT_CLOCK;
306 sr_info("Triggering on rising edge of external clock.");
307 devc->capture_flags &= ~CAPTURE_FLAG_INVERT_EXT_CLOCK;
310 case SR_CONF_PATTERN_MODE:
311 stropt = g_variant_get_string(data, NULL);
312 if (!strcmp(stropt, STR_PATTERN_NONE)) {
313 sr_info("Disabling test modes.");
315 } else if (!strcmp(stropt, STR_PATTERN_INTERNAL)) {
316 sr_info("Enabling internal test mode.");
317 flag = CAPTURE_FLAG_INTERNAL_TEST_MODE;
318 } else if (!strcmp(stropt, STR_PATTERN_EXTERNAL)) {
319 sr_info("Enabling external test mode.");
320 flag = CAPTURE_FLAG_EXTERNAL_TEST_MODE;
324 devc->capture_flags &= ~CAPTURE_FLAG_INTERNAL_TEST_MODE;
325 devc->capture_flags &= ~CAPTURE_FLAG_EXTERNAL_TEST_MODE;
326 devc->capture_flags |= flag;
329 if (g_variant_get_boolean(data)) {
330 sr_info("Enabling channel swapping.");
331 devc->capture_flags |= CAPTURE_FLAG_SWAP_CHANNELS;
333 sr_info("Disabling channel swapping.");
334 devc->capture_flags &= ~CAPTURE_FLAG_SWAP_CHANNELS;
338 if (g_variant_get_boolean(data)) {
339 sr_info("Enabling RLE.");
340 devc->capture_flags |= CAPTURE_FLAG_RLE;
342 sr_info("Disabling RLE.");
343 devc->capture_flags &= ~CAPTURE_FLAG_RLE;
353 static int config_list(uint32_t key, GVariant **data,
354 const struct sr_dev_inst *sdi,
355 const struct sr_channel_group *cg)
357 struct dev_context *devc;
358 int num_ols_changrp, i;
361 case SR_CONF_SCAN_OPTIONS:
362 case SR_CONF_DEVICE_OPTIONS:
363 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts,
365 case SR_CONF_SAMPLERATE:
366 *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates));
368 case SR_CONF_TRIGGER_MATCH:
369 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
371 case SR_CONF_CLOCK_EDGE:
372 *data = std_gvar_array_str(ARRAY_AND_SIZE(external_clock_edges));
374 case SR_CONF_PATTERN_MODE:
375 *data = g_variant_new_strv(ARRAY_AND_SIZE(patterns));
377 case SR_CONF_LIMIT_SAMPLES:
381 if (devc->max_samples == 0)
382 /* Device didn't specify sample memory size in metadata. */
385 * Channel groups are turned off if no channels in that group are
386 * enabled, making more room for samples for the enabled group.
388 uint32_t channel_mask = ols_channel_mask(sdi);
390 for (i = 0; i < 4; i++) {
391 if (channel_mask & (0xff << (i * 8)))
395 *data = std_gvar_tuple_u64(MIN_NUM_SAMPLES, (num_ols_changrp)
396 ? devc->max_samples / num_ols_changrp : MIN_NUM_SAMPLES);
405 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
408 struct dev_context *devc;
409 struct sr_serial_dev_inst *serial;
414 ret = ols_prepare_acquisition(sdi);
418 /* Start acquisition on the device. */
419 if (send_shortcommand(serial, CMD_ARM_BASIC_TRIGGER) != SR_OK)
422 /* Reset all operational states. */
423 devc->rle_count = devc->num_transfers = 0;
424 devc->num_samples = devc->num_bytes = 0;
425 devc->cnt_bytes = devc->cnt_samples = devc->cnt_samples_rle = 0;
426 memset(devc->sample, 0, 4);
428 std_session_send_df_header(sdi);
430 /* If the device stops sending for longer than it takes to send a byte,
431 * that means it's finished. But wait at least 100 ms to be safe.
433 return serial_source_add(sdi->session, serial, G_IO_IN, 100,
434 ols_receive_data, (struct sr_dev_inst *)sdi);
437 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
439 abort_acquisition(sdi);
444 static struct sr_dev_driver ols_driver_info = {
446 .longname = "Openbench Logic Sniffer & SUMP compatibles",
449 .cleanup = std_cleanup,
451 .dev_list = std_dev_list,
452 .dev_clear = std_dev_clear,
453 .config_get = config_get,
454 .config_set = config_set,
455 .config_list = config_list,
456 .dev_open = std_serial_dev_open,
457 .dev_close = std_serial_dev_close,
458 .dev_acquisition_start = dev_acquisition_start,
459 .dev_acquisition_stop = dev_acquisition_stop,
462 SR_REGISTER_DEV_DRIVER(ols_driver_info);