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/>.
22 static const int32_t hwcaps[] = {
23 SR_CONF_LOGIC_ANALYZER,
26 SR_CONF_CAPTURE_RATIO,
27 SR_CONF_LIMIT_SAMPLES,
29 SR_CONF_EXTERNAL_CLOCK,
34 #define STR_PATTERN_NONE "None"
35 #define STR_PATTERN_EXTERNAL "External"
36 #define STR_PATTERN_INTERNAL "Internal"
38 /* Supported methods of test pattern outputs */
41 * Capture pins 31:16 (unbuffered wing) output a test pattern
42 * that can captured on pins 0:15.
46 /** Route test pattern internally to capture buffer. */
50 static const char *patterns[] = {
56 /* Channels are numbered 0-31 (on the PCB silkscreen). */
57 SR_PRIV const char *p_ols_channel_names[NUM_CHANNELS + 1] = {
58 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
59 "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23",
60 "24", "25", "26", "27", "28", "29", "30", "31",
64 /* Default supported samplerates, can be overridden by device metadata. */
65 static const uint64_t samplerates[] = {
71 SR_PRIV struct sr_dev_driver p_ols_driver_info;
72 static struct sr_dev_driver *di = &p_ols_driver_info;
74 static int init(struct sr_context *sr_ctx)
76 return std_init(sr_ctx, di, LOG_PREFIX);
79 static GSList *scan(GSList *options)
81 struct sr_dev_inst *sdi;
82 struct drv_context *drvc;
83 struct dev_context *devc;
95 /* Allocate memory for our private device context. */
96 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
97 sr_err("Device context malloc failed.");
98 goto err_free_nothing;
101 /* Device-specific settings */
102 devc->max_samples = devc->max_samplerate = devc->protocol_version = 0;
104 /* Acquisition settings */
105 devc->limit_samples = devc->capture_ratio = 0;
106 devc->trigger_at = -1;
107 devc->channel_mask = 0xffffffff;
110 /* Allocate memory for the incoming ftdi data. */
111 if (!(devc->ftdi_buf = g_try_malloc0(FTDI_BUF_SIZE))) {
112 sr_err("ftdi_buf malloc failed.");
116 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
117 if (!(devc->ftdic = ftdi_new())) {
118 sr_err("Failed to initialize libftdi.");
119 goto err_free_ftdi_buf;;
122 /* Try to open the FTDI device */
123 if (p_ols_open(devc) != SR_OK) {
127 /* The discovery procedure is like this: first send the Reset
128 * command (0x00) 5 times, since the device could be anywhere
129 * in a 5-byte command. Then send the ID command (0x02).
130 * If the device responds with 4 bytes ("OLS1" or "SLA1"), we
135 for (i = 0; i < 5; i++) {
136 if ((ret = write_shortcommand(devc, CMD_RESET)) != SR_OK) {
141 sr_err("Could not reset device. Quitting.");
142 goto err_close_ftdic;
144 write_shortcommand(devc, CMD_ID);
146 /* Read the response data. */
147 bytes_read = ftdi_read_data(devc->ftdic, (uint8_t *)buf, 4);
148 if (bytes_read < 0) {
149 sr_err("Failed to read FTDI data (%d): %s.",
150 bytes_read, ftdi_get_error_string(devc->ftdic));
151 goto err_close_ftdic;
153 if (bytes_read == 0) {
154 goto err_close_ftdic;
157 if (strncmp(buf, "1SLO", 4) && strncmp(buf, "1ALS", 4))
158 goto err_close_ftdic;
160 /* Definitely using the OLS protocol, check if it supports
161 * the metadata command.
163 write_shortcommand(devc, CMD_METADATA);
165 /* Read the metadata. */
166 bytes_read = ftdi_read_data(devc->ftdic, (uint8_t *)buf, 64);
167 if (bytes_read < 0) {
168 sr_err("Failed to read FTDI data (%d): %s.",
169 bytes_read, ftdi_get_error_string(devc->ftdic));
170 goto err_close_ftdic;
172 if (bytes_read == 0) {
173 goto err_close_ftdic;
176 /* Close device. We'll reopen it again when we need it. */
179 /* Parse the metadata. */
180 sdi = p_ols_get_metadata((uint8_t *)buf, bytes_read, devc);
183 /* Configure samplerate and divider. */
184 if (p_ols_set_samplerate(sdi, DEFAULT_SAMPLERATE) != SR_OK)
185 sr_dbg("Failed to set default samplerate (%"PRIu64").",
187 /* Clear trigger masks, values and stages. */
188 p_ols_configure_channels(sdi);
190 drvc->instances = g_slist_append(drvc->instances, sdi);
191 devices = g_slist_append(devices, sdi);
198 ftdi_free(devc->ftdic); /* NOT free() or g_free()! */
200 g_free(devc->ftdi_buf);
208 static GSList *dev_list(void)
210 return ((struct drv_context *)(di->priv))->instances;
213 static void clear_helper(void *priv)
215 struct dev_context *devc;
219 ftdi_free(devc->ftdic);
220 g_free(devc->ftdi_buf);
223 static int dev_clear(void)
225 return std_dev_clear(di, clear_helper);
228 static int cleanup(void)
234 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
235 const struct sr_channel_group *cg)
237 struct dev_context *devc;
246 case SR_CONF_SAMPLERATE:
247 *data = g_variant_new_uint64(devc->cur_samplerate);
249 case SR_CONF_CAPTURE_RATIO:
250 *data = g_variant_new_uint64(devc->capture_ratio);
252 case SR_CONF_LIMIT_SAMPLES:
253 *data = g_variant_new_uint64(devc->limit_samples);
255 case SR_CONF_PATTERN_MODE:
256 if (devc->flag_reg & FLAG_EXTERNAL_TEST_MODE)
257 *data = g_variant_new_string(STR_PATTERN_EXTERNAL);
258 else if (devc->flag_reg & FLAG_INTERNAL_TEST_MODE)
259 *data = g_variant_new_string(STR_PATTERN_INTERNAL);
261 *data = g_variant_new_string(STR_PATTERN_NONE);
264 *data = g_variant_new_boolean(devc->flag_reg & FLAG_RLE ? TRUE : FALSE);
266 case SR_CONF_EXTERNAL_CLOCK:
267 *data = g_variant_new_boolean(devc->flag_reg & FLAG_CLOCK_EXTERNAL ? TRUE : FALSE);
276 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
277 const struct sr_channel_group *cg)
279 struct dev_context *devc;
287 if (sdi->status != SR_ST_ACTIVE)
288 return SR_ERR_DEV_CLOSED;
293 case SR_CONF_SAMPLERATE:
294 tmp_u64 = g_variant_get_uint64(data);
295 if (tmp_u64 < samplerates[0] || tmp_u64 > samplerates[1])
296 return SR_ERR_SAMPLERATE;
297 ret = p_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)
303 devc->limit_samples = tmp_u64;
306 case SR_CONF_CAPTURE_RATIO:
307 devc->capture_ratio = g_variant_get_uint64(data);
308 if (devc->capture_ratio < 0 || devc->capture_ratio > 100) {
309 devc->capture_ratio = 0;
314 case SR_CONF_EXTERNAL_CLOCK:
315 if (g_variant_get_boolean(data)) {
316 sr_info("Enabling external clock.");
317 devc->flag_reg |= FLAG_CLOCK_EXTERNAL;
319 sr_info("Disabled external clock.");
320 devc->flag_reg &= ~FLAG_CLOCK_EXTERNAL;
324 case SR_CONF_PATTERN_MODE:
325 stropt = g_variant_get_string(data, NULL);
328 if (!strcmp(stropt, STR_PATTERN_NONE)) {
329 sr_info("Disabling test modes.");
331 }else if (!strcmp(stropt, STR_PATTERN_INTERNAL)) {
332 sr_info("Enabling internal test mode.");
333 flag = FLAG_INTERNAL_TEST_MODE;
334 } else if (!strcmp(stropt, STR_PATTERN_EXTERNAL)) {
335 sr_info("Enabling external test mode.");
336 flag = FLAG_EXTERNAL_TEST_MODE;
340 if (flag != 0xffff) {
341 devc->flag_reg &= ~(FLAG_INTERNAL_TEST_MODE | FLAG_EXTERNAL_TEST_MODE);
342 devc->flag_reg |= flag;
346 if (g_variant_get_boolean(data)) {
347 sr_info("Enabling channel swapping.");
348 devc->flag_reg |= FLAG_SWAP_CHANNELS;
350 sr_info("Disabling channel swapping.");
351 devc->flag_reg &= ~FLAG_SWAP_CHANNELS;
357 if (g_variant_get_boolean(data)) {
358 sr_info("Enabling RLE.");
359 devc->flag_reg |= FLAG_RLE;
361 sr_info("Disabling RLE.");
362 devc->flag_reg &= ~FLAG_RLE;
373 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
374 const struct sr_channel_group *cg)
376 struct dev_context *devc;
377 GVariant *gvar, *grange[2];
384 case SR_CONF_DEVICE_OPTIONS:
385 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
386 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
388 case SR_CONF_SAMPLERATE:
389 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
390 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates,
391 ARRAY_SIZE(samplerates), sizeof(uint64_t));
392 g_variant_builder_add(&gvb, "{sv}", "samplerate-steps", gvar);
393 *data = g_variant_builder_end(&gvb);
395 case SR_CONF_TRIGGER_TYPE:
396 *data = g_variant_new_string(TRIGGER_TYPE);
398 case SR_CONF_PATTERN_MODE:
399 *data = g_variant_new_strv(patterns, ARRAY_SIZE(patterns));
401 case SR_CONF_LIMIT_SAMPLES:
405 if (devc->flag_reg & FLAG_RLE)
407 if (devc->max_samples == 0)
408 /* Device didn't specify sample memory size in metadata. */
411 * Channel groups are turned off if no channels in that group are
412 * enabled, making more room for samples for the enabled group.
414 p_ols_configure_channels(sdi);
416 for (i = 0; i < 4; i++) {
417 if (devc->channel_mask & (0xff << (i * 8)))
420 if (num_channels == 0) {
421 /* This can happen, but shouldn't cause too much drama.
422 * However we can't continue because the code below would
426 grange[0] = g_variant_new_uint64(MIN_NUM_SAMPLES);
427 grange[1] = g_variant_new_uint64(devc->max_samples / num_channels);
428 *data = g_variant_new_tuple(grange, 2);
437 static int dev_open(struct sr_dev_inst *sdi)
439 struct dev_context *devc;
443 if (p_ols_open(devc) != SR_OK) {
446 sdi->status = SR_ST_ACTIVE;
451 static int dev_close(struct sr_dev_inst *sdi)
454 struct dev_context *devc;
459 if (sdi->status == SR_ST_ACTIVE) {
460 sr_dbg("Status ACTIVE, closing device.");
461 ret = p_ols_close(devc);
463 sr_spew("Status not ACTIVE, nothing to do.");
466 sdi->status = SR_ST_INACTIVE;
472 static int set_trigger(const struct sr_dev_inst *sdi, int stage)
474 struct dev_context *devc;
479 cmd = CMD_SET_TRIGGER_MASK + stage * 4;
480 arg[0] = devc->trigger_mask[stage] & 0xff;
481 arg[1] = (devc->trigger_mask[stage] >> 8) & 0xff;
482 arg[2] = (devc->trigger_mask[stage] >> 16) & 0xff;
483 arg[3] = (devc->trigger_mask[stage] >> 24) & 0xff;
484 if (write_longcommand(devc, cmd, arg) != SR_OK)
487 cmd = CMD_SET_TRIGGER_VALUE + stage * 4;
488 arg[0] = devc->trigger_value[stage] & 0xff;
489 arg[1] = (devc->trigger_value[stage] >> 8) & 0xff;
490 arg[2] = (devc->trigger_value[stage] >> 16) & 0xff;
491 arg[3] = (devc->trigger_value[stage] >> 24) & 0xff;
492 if (write_longcommand(devc, cmd, arg) != SR_OK)
495 cmd = CMD_SET_TRIGGER_CONFIG + stage * 4;
496 arg[0] = arg[1] = arg[3] = 0x00;
498 if (stage == devc->num_stages)
499 /* Last stage, fire when this one matches. */
500 arg[3] |= TRIGGER_START;
501 if (write_longcommand(devc, cmd, arg) != SR_OK)
507 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
510 struct dev_context *devc;
511 uint32_t samplecount, readcount, delaycount;
512 uint8_t changrp_mask, arg[4];
516 if (sdi->status != SR_ST_ACTIVE)
517 return SR_ERR_DEV_CLOSED;
521 if (p_ols_configure_channels(sdi) != SR_OK) {
522 sr_err("Failed to configure channels.");
527 * Enable/disable channel groups in the flag register according to the
528 * channel mask. Calculate this here, because num_channels is needed
529 * to limit readcount.
533 for (i = 0; i < 4; i++) {
534 if (devc->channel_mask & (0xff << (i * 8))) {
535 changrp_mask |= (1 << i);
541 * Limit readcount to prevent reading past the end of the hardware
544 sr_dbg("max_samples = %d", devc->max_samples);
545 sr_dbg("limit_samples = %d", devc->limit_samples);
546 samplecount = MIN(devc->max_samples / num_channels, devc->limit_samples);
547 readcount = samplecount / 4;
548 sr_dbg("Samplecount = %d", samplecount);
550 /* Rather read too many samples than too few. */
551 if (samplecount % 4 != 0)
554 /* Basic triggers. */
555 if (devc->trigger_mask[0] != 0x00000000) {
556 /* At least one channel has a trigger on it. */
557 delaycount = readcount * (1 - devc->capture_ratio / 100.0);
558 devc->trigger_at = (readcount - delaycount) * 4 - devc->num_stages;
559 for (i = 0; i <= devc->num_stages; i++) {
560 sr_dbg("Setting stage %d trigger.", i);
561 if ((ret = set_trigger(sdi, i)) != SR_OK)
565 /* No triggers configured, force trigger on first stage. */
566 sr_dbg("Forcing trigger at stage 0.");
567 if ((ret = set_trigger(sdi, 0)) != SR_OK)
569 delaycount = readcount;
573 sr_dbg("Setting samplerate to %" PRIu64 "Hz (divider %u)",
574 devc->cur_samplerate, devc->cur_samplerate_divider);
575 arg[0] = devc->cur_samplerate_divider & 0xff;
576 arg[1] = (devc->cur_samplerate_divider & 0xff00) >> 8;
577 arg[2] = (devc->cur_samplerate_divider & 0xff0000) >> 16;
579 if (write_longcommand(devc, CMD_SET_DIVIDER, arg) != SR_OK)
581 /* Send extended sample limit and pre/post-trigger capture ratio. */
582 arg[0] = ((readcount - 1) & 0xff);
583 arg[1] = ((readcount - 1) & 0xff00) >> 8;
584 arg[2] = ((readcount - 1) & 0xff0000) >> 16;
585 arg[3] = ((readcount - 1) & 0xff000000) >> 24;
586 if (write_longcommand(devc, CMD_CAPTURE_COUNT, arg) != SR_OK)
588 arg[0] = ((delaycount - 1) & 0xff);
589 arg[1] = ((delaycount - 1) & 0xff00) >> 8;
590 arg[2] = ((delaycount - 1) & 0xff0000) >> 16;
591 arg[3] = ((delaycount - 1) & 0xff000000) >> 24;
592 if (write_longcommand(devc, CMD_CAPTURE_DELAY, arg) != SR_OK)
595 sr_dbg("Setting intpat %s, extpat %s, RLE %s, noise_filter %s, demux %s",
596 devc->flag_reg & FLAG_INTERNAL_TEST_MODE ? "on": "off",
597 devc->flag_reg & FLAG_EXTERNAL_TEST_MODE ? "on": "off",
598 devc->flag_reg & FLAG_RLE ? "on" : "off",
599 devc->flag_reg & FLAG_FILTER ? "on": "off",
600 devc->flag_reg & FLAG_DEMUX ? "on" : "off");
602 /* 1 means "disable channel". */
603 devc->flag_reg |= ~(changrp_mask << 2) & 0x3c;
604 arg[0] = devc->flag_reg & 0xff;
605 arg[1] = devc->flag_reg >> 8;
606 arg[2] = arg[3] = 0x00;
607 if (write_longcommand(devc, CMD_SET_FLAGS, arg) != SR_OK)
610 /* Start acquisition on the device. */
611 if (write_shortcommand(devc, CMD_RUN) != SR_OK)
614 /* Reset all operational states. */
615 devc->rle_count = devc->num_transfers = 0;
616 devc->num_samples = devc->num_bytes = 0;
617 devc->cnt_bytes = devc->cnt_samples = devc->cnt_samples_rle = 0;
618 memset(devc->sample, 0, 4);
620 /* Send header packet to the session bus. */
621 std_session_send_df_header(cb_data, LOG_PREFIX);
623 /* Hook up a dummy handler to receive data from the device. */
624 sr_source_add(-1, G_IO_IN, 0, p_ols_receive_data, (void *)sdi);
631 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
633 struct dev_context *devc;
634 struct sr_datafeed_packet packet;
638 sr_dbg("Stopping acquisition.");
639 write_shortcommand(devc, CMD_RESET);
640 write_shortcommand(devc, CMD_RESET);
641 write_shortcommand(devc, CMD_RESET);
642 write_shortcommand(devc, CMD_RESET);
643 write_shortcommand(devc, CMD_RESET);
645 sr_source_remove(-1);
647 /* Send end packet to the session bus. */
648 sr_dbg("Sending SR_DF_END.");
649 packet.type = SR_DF_END;
650 sr_session_send(cb_data, &packet);
655 SR_PRIV struct sr_dev_driver p_ols_driver_info = {
657 .longname = "Pipistrello OLS",
662 .dev_list = dev_list,
663 .dev_clear = dev_clear,
664 .config_get = config_get,
665 .config_set = config_set,
666 .config_list = config_list,
667 .dev_open = dev_open,
668 .dev_close = dev_close,
669 .dev_acquisition_start = dev_acquisition_start,
670 .dev_acquisition_stop = dev_acquisition_stop,