2 * This file is part of the sigrok project.
4 * Copyright (C) 2010-2012 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 #define SERIALCOMM "115200/8n1"
24 static const int hwcaps[] = {
25 SR_HWCAP_LOGIC_ANALYZER,
27 SR_HWCAP_CAPTURE_RATIO,
28 SR_HWCAP_LIMIT_SAMPLES,
33 /* Probes are numbered 0-31 (on the PCB silkscreen). */
34 SR_PRIV const char *ols_probe_names[NUM_PROBES + 1] = {
70 /* default supported samplerates, can be overridden by device metadata */
71 static const struct sr_samplerates samplerates = {
78 SR_PRIV struct sr_dev_driver ols_driver_info;
79 static struct sr_dev_driver *di = &ols_driver_info;
81 static int hw_init(struct sr_context *sr_ctx)
83 struct drv_context *drvc;
85 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
86 sr_err("Driver context malloc failed.");
89 drvc->sr_ctx = sr_ctx;
95 static GSList *hw_scan(GSList *options)
98 struct sr_dev_inst *sdi;
99 struct drv_context *drvc;
100 struct dev_context *devc;
101 struct sr_probe *probe;
102 struct sr_serial_dev_inst *serial;
106 const char *conn, *serialcomm;
113 conn = serialcomm = NULL;
114 for (l = options; l; l = l->next) {
116 switch (opt->hwopt) {
120 case SR_HWOPT_SERIALCOMM:
121 serialcomm = opt->value;
128 if (serialcomm == NULL)
129 serialcomm = SERIALCOMM;
131 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
134 /* The discovery procedure is like this: first send the Reset
135 * command (0x00) 5 times, since the device could be anywhere
136 * in a 5-byte command. Then send the ID command (0x02).
137 * If the device responds with 4 bytes ("OLS1" or "SLA1"), we
140 sr_info("Probing %s.", conn);
141 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
145 for (i = 0; i < 5; i++) {
146 if ((ret = send_shortcommand(serial, CMD_RESET)) != SR_OK) {
147 sr_err("Port %s is not writable.", conn);
152 serial_close(serial);
153 sr_err("Could not use port %s. Quitting.", conn);
156 send_shortcommand(serial, CMD_ID);
158 /* Wait 10ms for a response. */
161 probefd.fd = serial->fd;
162 probefd.events = G_IO_IN;
163 g_poll(&probefd, 1, 1);
165 if (probefd.revents != G_IO_IN)
167 if (serial_read(serial, buf, 4) != 4)
169 if (strncmp(buf, "1SLO", 4) && strncmp(buf, "1ALS", 4))
172 /* Definitely using the OLS protocol, check if it supports
173 * the metadata command.
175 send_shortcommand(serial, CMD_METADATA);
176 if (g_poll(&probefd, 1, 10) > 0) {
178 sdi = get_metadata(serial);
182 /* Not an OLS -- some other board that uses the sump protocol. */
183 sdi = sr_dev_inst_new(0, SR_ST_INACTIVE,
184 "Sump", "Logic Analyzer", "v1.0");
186 devc = ols_dev_new();
187 for (i = 0; i < 32; i++) {
188 if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE,
189 ols_probe_names[i])))
191 sdi->probes = g_slist_append(sdi->probes, probe);
195 devc->serial = serial;
196 drvc->instances = g_slist_append(drvc->instances, sdi);
197 devices = g_slist_append(devices, sdi);
199 serial_close(serial);
204 static GSList *hw_dev_list(void)
206 struct drv_context *drvc;
210 return drvc->instances;
213 static int hw_dev_open(struct sr_dev_inst *sdi)
215 struct dev_context *devc;
219 if (serial_open(devc->serial, SERIAL_RDWR) != SR_OK)
222 sdi->status = SR_ST_ACTIVE;
227 static int hw_dev_close(struct sr_dev_inst *sdi)
229 struct dev_context *devc;
233 if (devc->serial && devc->serial->fd != -1) {
234 serial_close(devc->serial);
235 sdi->status = SR_ST_INACTIVE;
241 static int hw_cleanup(void)
244 struct sr_dev_inst *sdi;
245 struct drv_context *drvc;
246 struct dev_context *devc;
249 if (!(drvc = di->priv))
252 /* Properly close and free all devices. */
253 for (l = drvc->instances; l; l = l->next) {
254 if (!(sdi = l->data)) {
255 /* Log error, but continue cleaning up the rest. */
256 sr_err("%s: sdi was NULL, continuing", __func__);
260 if (!(devc = sdi->priv)) {
261 /* Log error, but continue cleaning up the rest. */
262 sr_err("%s: sdi->priv was NULL, continuing", __func__);
267 sr_serial_dev_inst_free(devc->serial);
268 sr_dev_inst_free(sdi);
270 g_slist_free(drvc->instances);
271 drvc->instances = NULL;
276 static int hw_info_get(int info_id, const void **data,
277 const struct sr_dev_inst *sdi)
279 struct dev_context *devc;
285 case SR_DI_NUM_PROBES:
286 *data = GINT_TO_POINTER(1);
288 case SR_DI_PROBE_NAMES:
289 *data = ols_probe_names;
291 case SR_DI_SAMPLERATES:
292 *data = &samplerates;
294 case SR_DI_TRIGGER_TYPES:
295 *data = (char *)TRIGGER_TYPES;
297 case SR_DI_CUR_SAMPLERATE:
300 *data = &devc->cur_samplerate;
311 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
314 struct dev_context *devc;
316 const uint64_t *tmp_u64;
320 if (sdi->status != SR_ST_ACTIVE)
324 case SR_HWCAP_SAMPLERATE:
325 ret = ols_set_samplerate(sdi, *(const uint64_t *)value,
328 case SR_HWCAP_LIMIT_SAMPLES:
330 if (*tmp_u64 < MIN_NUM_SAMPLES)
332 if (*tmp_u64 > devc->max_samples)
333 sr_err("Sample limit exceeds hardware maximum.");
334 devc->limit_samples = *tmp_u64;
335 sr_info("Sample limit is %" PRIu64 ".", devc->limit_samples);
338 case SR_HWCAP_CAPTURE_RATIO:
339 devc->capture_ratio = *(const uint64_t *)value;
340 if (devc->capture_ratio < 0 || devc->capture_ratio > 100) {
341 devc->capture_ratio = 0;
347 if (GPOINTER_TO_INT(value)) {
348 sr_info("Enabling RLE.");
349 devc->flag_reg |= FLAG_RLE;
360 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
363 struct sr_datafeed_packet *packet;
364 struct sr_datafeed_header *header;
365 struct sr_datafeed_meta_logic meta;
366 struct dev_context *devc;
367 uint32_t trigger_config[4];
369 uint16_t readcount, delaycount;
370 uint8_t changrp_mask;
376 if (sdi->status != SR_ST_ACTIVE)
379 if (ols_configure_probes(sdi) != SR_OK) {
380 sr_err("Failed to configure probes.");
385 * Enable/disable channel groups in the flag register according to the
386 * probe mask. Calculate this here, because num_channels is needed
387 * to limit readcount.
391 for (i = 0; i < 4; i++) {
392 if (devc->probe_mask & (0xff << (i * 8))) {
393 changrp_mask |= (1 << i);
399 * Limit readcount to prevent reading past the end of the hardware
402 readcount = MIN(devc->max_samples / num_channels, devc->limit_samples) / 4;
404 memset(trigger_config, 0, 16);
405 trigger_config[devc->num_stages - 1] |= 0x08;
406 if (devc->trigger_mask[0]) {
407 delaycount = readcount * (1 - devc->capture_ratio / 100.0);
408 devc->trigger_at = (readcount - delaycount) * 4 - devc->num_stages;
410 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_MASK_0,
411 reverse32(devc->trigger_mask[0])) != SR_OK)
413 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_VALUE_0,
414 reverse32(devc->trigger_value[0])) != SR_OK)
416 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_CONFIG_0,
417 trigger_config[0]) != SR_OK)
420 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_MASK_1,
421 reverse32(devc->trigger_mask[1])) != SR_OK)
423 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_VALUE_1,
424 reverse32(devc->trigger_value[1])) != SR_OK)
426 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_CONFIG_1,
427 trigger_config[1]) != SR_OK)
430 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_MASK_2,
431 reverse32(devc->trigger_mask[2])) != SR_OK)
433 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_VALUE_2,
434 reverse32(devc->trigger_value[2])) != SR_OK)
436 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_CONFIG_2,
437 trigger_config[2]) != SR_OK)
440 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_MASK_3,
441 reverse32(devc->trigger_mask[3])) != SR_OK)
443 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_VALUE_3,
444 reverse32(devc->trigger_value[3])) != SR_OK)
446 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_CONFIG_3,
447 trigger_config[3]) != SR_OK)
450 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_MASK_0,
451 devc->trigger_mask[0]) != SR_OK)
453 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_VALUE_0,
454 devc->trigger_value[0]) != SR_OK)
456 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_CONFIG_0,
457 0x00000008) != SR_OK)
459 delaycount = readcount;
462 sr_info("Setting samplerate to %" PRIu64 "Hz (divider %u, "
463 "demux %s)", devc->cur_samplerate, devc->cur_samplerate_divider,
464 devc->flag_reg & FLAG_DEMUX ? "on" : "off");
465 if (send_longcommand(devc->serial, CMD_SET_DIVIDER,
466 reverse32(devc->cur_samplerate_divider)) != SR_OK)
469 /* Send sample limit and pre/post-trigger capture ratio. */
470 data = ((readcount - 1) & 0xffff) << 16;
471 data |= (delaycount - 1) & 0xffff;
472 if (send_longcommand(devc->serial, CMD_CAPTURE_SIZE, reverse16(data)) != SR_OK)
475 /* The flag register wants them here, and 1 means "disable channel". */
476 devc->flag_reg |= ~(changrp_mask << 2) & 0x3c;
477 devc->flag_reg |= FLAG_FILTER;
479 data = (devc->flag_reg << 24) | ((devc->flag_reg << 8) & 0xff0000);
480 if (send_longcommand(devc->serial, CMD_SET_FLAGS, data) != SR_OK)
483 /* Start acquisition on the device. */
484 if (send_shortcommand(devc->serial, CMD_RUN) != SR_OK)
487 sr_source_add(devc->serial->fd, G_IO_IN, -1, ols_receive_data,
490 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
491 sr_err("Datafeed packet malloc failed.");
492 return SR_ERR_MALLOC;
495 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
496 sr_err("Datafeed header malloc failed.");
498 return SR_ERR_MALLOC;
501 /* Send header packet to the session bus. */
502 packet->type = SR_DF_HEADER;
503 packet->payload = (unsigned char *)header;
504 header->feed_version = 1;
505 gettimeofday(&header->starttime, NULL);
506 sr_session_send(cb_data, packet);
508 /* Send metadata about the SR_DF_LOGIC packets to come. */
509 packet->type = SR_DF_META_LOGIC;
510 packet->payload = &meta;
511 meta.samplerate = devc->cur_samplerate;
512 meta.num_probes = NUM_PROBES;
513 sr_session_send(cb_data, packet);
521 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
522 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
524 /* Avoid compiler warnings. */
527 abort_acquisition(sdi);
532 SR_PRIV struct sr_dev_driver ols_driver_info = {
534 .longname = "Openbench Logic Sniffer",
537 .cleanup = hw_cleanup,
539 .dev_list = hw_dev_list,
540 .dev_clear = hw_cleanup,
541 .dev_open = hw_dev_open,
542 .dev_close = hw_dev_close,
543 .info_get = hw_info_get,
544 .dev_config_set = hw_dev_config_set,
545 .dev_acquisition_start = hw_dev_acquisition_start,
546 .dev_acquisition_stop = hw_dev_acquisition_stop,