]> sigrok.org Git - libsigrok.git/blame_incremental - hardware/openbench-logic-sniffer/ols.c
OLS driver overhaul
[libsigrok.git] / hardware / openbench-logic-sniffer / ols.c
... / ...
CommitLineData
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 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 <stdio.h>
21#include <stdint.h>
22#include <stdlib.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <unistd.h>
27#ifdef _WIN32
28#include <windows.h>
29#else
30#include <termios.h>
31#endif
32#include <string.h>
33#include <sys/time.h>
34#include <inttypes.h>
35#ifdef _WIN32
36/* TODO */
37#else
38#include <arpa/inet.h>
39#endif
40#include <glib.h>
41#include <sigrok.h>
42#include <sigrok-internal.h>
43#include "ols.h"
44
45#ifdef _WIN32
46#define O_NONBLOCK FIONBIO
47#endif
48
49
50static int capabilities[] = {
51 SR_HWCAP_LOGIC_ANALYZER,
52 SR_HWCAP_SAMPLERATE,
53 SR_HWCAP_CAPTURE_RATIO,
54 SR_HWCAP_LIMIT_SAMPLES,
55 0,
56};
57
58/* default supported samplerates, can be overridden by device metadata */
59static struct sr_samplerates samplerates = {
60 SR_HZ(10),
61 SR_MHZ(200),
62 SR_HZ(1),
63 NULL,
64};
65
66/* List of struct sr_serial_device_instance */
67static GSList *device_instances = NULL;
68
69
70
71static int send_shortcommand(int fd, uint8_t command)
72{
73 char buf[1];
74
75 g_debug("ols: sending cmd 0x%.2x", command);
76 buf[0] = command;
77 if (serial_write(fd, buf, 1) != 1)
78 return SR_ERR;
79
80 return SR_OK;
81}
82
83static int send_longcommand(int fd, uint8_t command, uint32_t data)
84{
85 char buf[5];
86
87 g_debug("ols: sending cmd 0x%.2x data 0x%.8x", command, data);
88 buf[0] = command;
89 buf[1] = (data & 0xff000000) >> 24;
90 buf[2] = (data & 0xff0000) >> 16;
91 buf[3] = (data & 0xff00) >> 8;
92 buf[4] = data & 0xff;
93 if (serial_write(fd, buf, 5) != 5)
94 return SR_ERR;
95
96 return SR_OK;
97}
98
99static int configure_probes(struct ols_device *ols, GSList *probes)
100{
101 struct sr_probe *probe;
102 GSList *l;
103 int probe_bit, stage, i;
104 char *tc;
105
106 ols->probe_mask = 0;
107 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
108 ols->trigger_mask[i] = 0;
109 ols->trigger_value[i] = 0;
110 }
111
112 ols->num_stages = 0;
113 for (l = probes; l; l = l->next) {
114 probe = (struct sr_probe *)l->data;
115 if (!probe->enabled)
116 continue;
117
118 /*
119 * Set up the probe mask for later configuration into the
120 * flag register.
121 */
122 probe_bit = 1 << (probe->index - 1);
123 ols->probe_mask |= probe_bit;
124
125 if (!probe->trigger)
126 continue;
127
128 /* Configure trigger mask and value. */
129 stage = 0;
130 for (tc = probe->trigger; tc && *tc; tc++) {
131 ols->trigger_mask[stage] |= probe_bit;
132 if (*tc == '1')
133 ols->trigger_value[stage] |= probe_bit;
134 stage++;
135 if (stage > 3)
136 /*
137 * TODO: Only supporting parallel mode, with
138 * up to 4 stages.
139 */
140 return SR_ERR;
141 }
142 if (stage > ols->num_stages)
143 ols->num_stages = stage;
144 }
145
146 return SR_OK;
147}
148
149static uint32_t reverse16(uint32_t in)
150{
151 uint32_t out;
152
153 out = (in & 0xff) << 8;
154 out |= (in & 0xff00) >> 8;
155 out |= (in & 0xff0000) << 8;
156 out |= (in & 0xff000000) >> 8;
157
158 return out;
159}
160
161static uint32_t reverse32(uint32_t in)
162{
163 uint32_t out;
164
165 out = (in & 0xff) << 24;
166 out |= (in & 0xff00) << 8;
167 out |= (in & 0xff0000) >> 8;
168 out |= (in & 0xff000000) >> 24;
169
170 return out;
171}
172
173static struct ols_device *ols_device_new(void)
174{
175 struct ols_device *ols;
176
177 ols = g_malloc0(sizeof(struct ols_device));
178 ols->trigger_at = -1;
179 ols->probe_mask = 0xffffffff;
180 ols->cur_samplerate = SR_KHZ(200);
181
182 return ols;
183}
184
185static struct sr_device_instance *get_metadata(int fd)
186{
187 struct sr_device_instance *sdi;
188 struct ols_device *ols;
189 uint32_t tmp_int;
190 uint8_t key, type, token;
191 GString *tmp_str, *devicename, *version;
192 gchar tmp_c;
193
194 sdi = sr_device_instance_new(0, SR_ST_INACTIVE, NULL, NULL, NULL);
195 ols = ols_device_new();
196 sdi->priv = ols;
197
198 devicename = g_string_new("");
199 version = g_string_new("");
200
201 key = 0xff;
202 while (key) {
203 if (serial_read(fd, &key, 1) != 1 || key == 0x00)
204 break;
205 type = key >> 5;
206 token = key & 0x1f;
207 switch (type) {
208 case 0:
209 /* NULL-terminated string */
210 tmp_str = g_string_new("");
211 while (serial_read(fd, &tmp_c, 1) == 1 && tmp_c != '\0')
212 g_string_append_c(tmp_str, tmp_c);
213 g_debug("ols: got metadata key 0x%.2x value '%s'", key, tmp_str->str);
214 switch (token) {
215 case 0x01:
216 /* Device name */
217 devicename = g_string_append(devicename, tmp_str->str);
218 break;
219 case 0x02:
220 /* FPGA firmware version */
221 if (version->len)
222 g_string_append(version, ", ");
223 g_string_append(version, "FPGA version ");
224 g_string_append(version, tmp_str->str);
225 break;
226 case 0x03:
227 /* Ancillary version */
228 if (version->len)
229 g_string_append(version, ", ");
230 g_string_append(version, "Ancillary version ");
231 g_string_append(version, tmp_str->str);
232 break;
233 default:
234 g_message("ols: unknown token 0x%.2x: '%s'", token, tmp_str->str);
235 break;
236 }
237 g_string_free(tmp_str, TRUE);
238 break;
239 case 1:
240 /* 32-bit unsigned integer */
241 if (serial_read(fd, &tmp_int, 4) != 4)
242 break;
243 tmp_int = reverse32(tmp_int);
244 g_debug("ols: got metadata key 0x%.2x value 0x%.8x", key, tmp_int);
245 switch (token) {
246 case 0x00:
247 /* Number of usable probes */
248 ols->num_probes = tmp_int;
249 break;
250 case 0x01:
251 /* Amount of sample memory available (bytes) */
252 ols->max_samples = tmp_int;
253 break;
254 case 0x02:
255 /* Amount of dynamic memory available (bytes) */
256 /* what is this for? */
257 break;
258 case 0x03:
259 /* Maximum sample rate (hz) */
260 ols->max_samplerate = tmp_int;
261 break;
262 case 0x04:
263 /* protocol version */
264 ols->protocol_version = tmp_int;
265 break;
266 default:
267 g_message("ols: unknown token 0x%.2x: 0x%.8x", token, tmp_int);
268 break;
269 }
270 break;
271 case 2:
272 /* 8-bit unsigned integer */
273 if (serial_read(fd, &tmp_c, 1) != 1)
274 break;
275 g_debug("ols: got metadata key 0x%.2x value 0x%.2x", key, tmp_c);
276 switch (token) {
277 case 0x00:
278 /* Number of usable probes */
279 ols->num_probes = tmp_c;
280 break;
281 case 0x01:
282 /* protocol version */
283 ols->protocol_version = tmp_c;
284 break;
285 default:
286 g_message("ols: unknown token 0x%.2x: 0x%.2x", token, tmp_c);
287 break;
288 }
289 break;
290 default:
291 /* unknown type */
292 break;
293 }
294 }
295
296 sdi->model = devicename->str;
297 sdi->version = version->str;
298 g_string_free(devicename, FALSE);
299 g_string_free(version, FALSE);
300
301 return sdi;
302}
303
304
305static int hw_init(const char *deviceinfo)
306{
307 struct sr_device_instance *sdi;
308 struct ols_device *ols;
309 GSList *ports, *l;
310 GPollFD *fds, probefd;
311 int devcnt, final_devcnt, num_ports, fd, ret, i;
312 char buf[8], **device_names, **serial_params;
313
314 if (deviceinfo)
315 ports = g_slist_append(NULL, strdup(deviceinfo));
316 else
317 /* No specific device given, so scan all serial ports. */
318 ports = list_serial_ports();
319
320 num_ports = g_slist_length(ports);
321 fds = calloc(1, num_ports * sizeof(GPollFD));
322 device_names = malloc(num_ports * sizeof(char *));
323 serial_params = malloc(num_ports * sizeof(char *));
324 devcnt = 0;
325 for (l = ports; l; l = l->next) {
326 /* The discovery procedure is like this: first send the Reset
327 * command (0x00) 5 times, since the device could be anywhere
328 * in a 5-byte command. Then send the ID command (0x02).
329 * If the device responds with 4 bytes ("OLS1" or "SLA1"), we
330 * have a match.
331 *
332 * Since it may take the device a while to respond at 115Kb/s,
333 * we do all the sending first, then wait for all of them to
334 * respond with g_poll().
335 */
336 g_message("ols: probing %s...", (char *)l->data);
337 fd = serial_open(l->data, O_RDWR | O_NONBLOCK);
338 if (fd != -1) {
339 serial_params[devcnt] = serial_backup_params(fd);
340 serial_set_params(fd, 115200, 8, 0, 1, 2);
341 ret = SR_OK;
342 for (i = 0; i < 5; i++) {
343 if ((ret = send_shortcommand(fd,
344 CMD_RESET)) != SR_OK) {
345 /* Serial port is not writable. */
346 break;
347 }
348 }
349 if (ret != SR_OK) {
350 serial_restore_params(fd,
351 serial_params[devcnt]);
352 serial_close(fd);
353 continue;
354 }
355 send_shortcommand(fd, CMD_ID);
356 fds[devcnt].fd = fd;
357 fds[devcnt].events = G_IO_IN;
358 device_names[devcnt] = strdup(l->data);
359 devcnt++;
360 }
361 free(l->data);
362 }
363
364 /* 2ms isn't enough for reliable transfer with pl2303, let's try 10 */
365 usleep(10000);
366
367 final_devcnt = 0;
368 g_poll(fds, devcnt, 1);
369
370 for (i = 0; i < devcnt; i++) {
371 if (fds[i].revents != G_IO_IN)
372 continue;
373 if (serial_read(fds[i].fd, buf, 4) != 4)
374 continue;
375 if (strncmp(buf, "1SLO", 4) && strncmp(buf, "1ALS", 4))
376 continue;
377
378 /* definitely using the OLS protocol, check if it supports
379 * the metadata command
380 */
381 send_shortcommand(fds[i].fd, CMD_METADATA);
382 probefd.fd = fds[i].fd;
383 probefd.events = G_IO_IN;
384 if (g_poll(&probefd, 1, 10) > 0) {
385 /* got metadata */
386 sdi = get_metadata(fds[i].fd);
387 sdi->index = final_devcnt;
388 } else {
389 /* not an OLS -- some other board that uses the sump protocol */
390 sdi = sr_device_instance_new(final_devcnt, SR_ST_INACTIVE,
391 "Sump", "Logic Analyzer", "v1.0");
392 ols = ols_device_new();
393 ols->num_probes = 32;
394 sdi->priv = ols;
395 }
396 sdi->serial = sr_serial_device_instance_new(device_names[i], -1);
397 device_instances = g_slist_append(device_instances, sdi);
398 final_devcnt++;
399 serial_close(fds[i].fd);
400 fds[i].fd = 0;
401
402 }
403
404 /* clean up after all the probing */
405 for (i = 0; i < devcnt; i++) {
406 if (fds[i].fd != 0) {
407 serial_restore_params(fds[i].fd, serial_params[i]);
408 serial_close(fds[i].fd);
409 }
410 free(serial_params[i]);
411 free(device_names[i]);
412 }
413
414 free(fds);
415 free(device_names);
416 free(serial_params);
417 g_slist_free(ports);
418
419 return final_devcnt;
420}
421
422static int hw_opendev(int device_index)
423{
424 struct sr_device_instance *sdi;
425
426 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
427 return SR_ERR;
428
429 sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR);
430 if (sdi->serial->fd == -1)
431 return SR_ERR;
432
433 sdi->status = SR_ST_ACTIVE;
434
435 return SR_OK;
436}
437
438static void hw_closedev(int device_index)
439{
440 struct sr_device_instance *sdi;
441
442 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
443 return;
444
445 if (sdi->serial->fd != -1) {
446 serial_close(sdi->serial->fd);
447 sdi->serial->fd = -1;
448 sdi->status = SR_ST_INACTIVE;
449 }
450}
451
452static void hw_cleanup(void)
453{
454 GSList *l;
455 struct sr_device_instance *sdi;
456
457 /* Properly close all devices. */
458 for (l = device_instances; l; l = l->next) {
459 sdi = l->data;
460 if (sdi->serial->fd != -1)
461 serial_close(sdi->serial->fd);
462 sr_device_instance_free(sdi);
463 }
464 g_slist_free(device_instances);
465 device_instances = NULL;
466}
467
468static void *hw_get_device_info(int device_index, int device_info_id)
469{
470 struct sr_device_instance *sdi;
471 struct ols_device *ols;
472 void *info;
473
474 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
475 return NULL;
476 ols = sdi->priv;
477
478 info = NULL;
479 switch (device_info_id) {
480 case SR_DI_INSTANCE:
481 info = sdi;
482 break;
483 case SR_DI_NUM_PROBES:
484 info = GINT_TO_POINTER(NUM_PROBES);
485 break;
486 case SR_DI_SAMPLERATES:
487 info = &samplerates;
488 break;
489 case SR_DI_TRIGGER_TYPES:
490 info = (char *)TRIGGER_TYPES;
491 break;
492 case SR_DI_CUR_SAMPLERATE:
493 info = &ols->cur_samplerate;
494 break;
495 }
496
497 return info;
498}
499
500static int hw_get_status(int device_index)
501{
502 struct sr_device_instance *sdi;
503
504 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
505 return SR_ST_NOT_FOUND;
506
507 return sdi->status;
508}
509
510static int *hw_get_capabilities(void)
511{
512 return capabilities;
513}
514
515static int set_configuration_samplerate(struct sr_device_instance *sdi,
516 uint64_t samplerate)
517{
518 struct ols_device *ols;
519
520 ols = sdi->priv;
521 if (ols->max_samplerate) {
522 if (samplerate > ols->max_samplerate)
523 return SR_ERR_SAMPLERATE;
524 } else if (samplerate < samplerates.low || samplerate > samplerates.high)
525 return SR_ERR_SAMPLERATE;
526
527 ols->cur_samplerate = samplerate;
528 if (samplerate > CLOCK_RATE) {
529 ols->flag_reg |= FLAG_DEMUX;
530 ols->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
531 } else {
532 ols->flag_reg &= ~FLAG_DEMUX;
533 ols->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
534 }
535
536 return SR_OK;
537}
538
539static int hw_set_configuration(int device_index, int capability, void *value)
540{
541 struct sr_device_instance *sdi;
542 struct ols_device *ols;
543 int ret;
544 uint64_t *tmp_u64;
545
546 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
547 return SR_ERR;
548 ols = sdi->priv;
549
550 if (sdi->status != SR_ST_ACTIVE)
551 return SR_ERR;
552
553 switch (capability) {
554 case SR_HWCAP_SAMPLERATE:
555 tmp_u64 = value;
556 ret = set_configuration_samplerate(sdi, *tmp_u64);
557 break;
558 case SR_HWCAP_PROBECONFIG:
559 ret = configure_probes(ols, (GSList *) value);
560 break;
561 case SR_HWCAP_LIMIT_SAMPLES:
562 tmp_u64 = value;
563 if (*tmp_u64 < MIN_NUM_SAMPLES)
564 return SR_ERR;
565 ols->limit_samples = *tmp_u64;
566 g_message("ols: sample limit %" PRIu64, ols->limit_samples);
567 ret = SR_OK;
568 break;
569 case SR_HWCAP_CAPTURE_RATIO:
570 tmp_u64 = value;
571 ols->capture_ratio = *tmp_u64;
572 if (ols->capture_ratio < 0 || ols->capture_ratio > 100) {
573 ols->capture_ratio = 0;
574 ret = SR_ERR;
575 } else
576 ret = SR_OK;
577 break;
578 default:
579 ret = SR_ERR;
580 }
581
582 return ret;
583}
584
585static int receive_data(int fd, int revents, void *user_data)
586{
587 struct sr_datafeed_packet packet;
588 struct sr_device_instance *sdi;
589 struct ols_device *ols;
590 GSList *l;
591 int count, buflen, num_channels, offset, i, j;
592 unsigned char byte, *buffer;
593
594 /* find this device's ols_device struct by its fd */
595 ols = NULL;
596 for (l = device_instances; l; l = l->next) {
597 sdi = l->data;
598 if (sdi->serial->fd == fd) {
599 ols = sdi->priv;
600 break;
601 }
602 }
603 if (!ols)
604 /* shouldn't happen */
605 return TRUE;
606
607 if (ols->num_transfers++ == 0) {
608 /*
609 * First time round, means the device started sending data,
610 * and will not stop until done. If it stops sending for
611 * longer than it takes to send a byte, that means it's
612 * finished. We'll double that to 30ms to be sure...
613 */
614 sr_source_remove(fd);
615 sr_source_add(fd, G_IO_IN, 30, receive_data, user_data);
616 ols->raw_sample_buf = malloc(ols->limit_samples * 4);
617 /* fill with 1010... for debugging */
618 memset(ols->raw_sample_buf, 0x82, ols->limit_samples * 4);
619 }
620
621 num_channels = 0;
622 for (i = 0x20; i > 0x02; i /= 2) {
623 if ((ols->flag_reg & i) == 0)
624 num_channels++;
625 }
626
627 if (revents == G_IO_IN
628 && ols->num_transfers / num_channels <= ols->limit_samples) {
629 if (serial_read(fd, &byte, 1) != 1)
630 return FALSE;
631
632 ols->sample[ols->num_bytes++] = byte;
633 g_debug("ols: received byte 0x%.2x", byte);
634 if (ols->num_bytes == num_channels) {
635 /* Got a full sample. */
636 g_debug("ols: received sample 0x%.*x", ols->num_bytes * 2, (int) *ols->sample);
637 if (ols->flag_reg & FLAG_RLE) {
638 /*
639 * In RLE mode -1 should never come in as a
640 * sample, because bit 31 is the "count" flag.
641 * TODO: Endianness may be wrong here, could be
642 * sample[3].
643 */
644 if (ols->sample[0] & 0x80
645 && !(ols->last_sample[0] & 0x80)) {
646 count = (int)(*ols->sample) & 0x7fffffff;
647 buffer = g_malloc(count);
648 buflen = 0;
649 for (i = 0; i < count; i++) {
650 memcpy(buffer + buflen, ols->last_sample, 4);
651 buflen += 4;
652 }
653 } else {
654 /*
655 * Just a single sample, next sample
656 * will probably be a count referring
657 * to this -- but this one is still a
658 * part of the stream.
659 */
660 buffer = ols->sample;
661 buflen = 4;
662 }
663 } else {
664 /* No compression. */
665 buffer = ols->sample;
666 buflen = 4;
667 }
668
669 if (num_channels < 4) {
670 /*
671 * Some channel groups may have been turned
672 * off, to speed up transfer between the
673 * hardware and the PC. Expand that here before
674 * submitting it over the session bus --
675 * whatever is listening on the bus will be
676 * expecting a full 32-bit sample, based on
677 * the number of probes.
678 */
679 j = 0;
680 memset(ols->tmp_sample, 0, 4);
681 for (i = 0; i < 4; i++) {
682 if (((ols->flag_reg >> 2) & (1 << i)) == 0) {
683 /*
684 * This channel group was
685 * enabled, copy from received
686 * sample.
687 */
688 ols->tmp_sample[i] = ols->sample[j++];
689 }
690 }
691 memcpy(ols->sample, ols->tmp_sample, 4);
692 g_debug("ols: full sample 0x%.8x", (int) *ols->sample);
693 }
694
695 /* the OLS sends its sample buffer backwards.
696 * store it in reverse order here, so we can dump
697 * this on the session bus later.
698 */
699 offset = (ols->limit_samples - ols->num_transfers / num_channels) * 4;
700 memcpy(ols->raw_sample_buf + offset, ols->sample, 4);
701
702 if (buffer == ols->sample)
703 memcpy(ols->last_sample, buffer, num_channels);
704 else
705 g_free(buffer);
706
707 memset(ols->sample, 0, 4);
708 ols->num_bytes = 0;
709 }
710 } else {
711 /*
712 * This is the main loop telling us a timeout was reached, or
713 * we've acquired all the samples we asked for -- we're done.
714 * Send the (properly-ordered) buffer to the frontend.
715 */
716 if (ols->trigger_at != -1) {
717 /* a trigger was set up, so we need to tell the frontend
718 * about it.
719 */
720 if (ols->trigger_at > 0) {
721 /* there are pre-trigger samples, send those first */
722 packet.type = SR_DF_LOGIC;
723 packet.length = ols->trigger_at * 4;
724 packet.unitsize = 4;
725 packet.payload = ols->raw_sample_buf;
726 sr_session_bus(user_data, &packet);
727 }
728
729 packet.type = SR_DF_TRIGGER;
730 packet.length = 0;
731 sr_session_bus(user_data, &packet);
732
733 packet.type = SR_DF_LOGIC;
734 packet.length = (ols->limit_samples * 4) - (ols->trigger_at * 4);
735 packet.unitsize = 4;
736 packet.payload = ols->raw_sample_buf + ols->trigger_at * 4;
737 sr_session_bus(user_data, &packet);
738 } else {
739 packet.type = SR_DF_LOGIC;
740 packet.length = ols->limit_samples * 4;
741 packet.unitsize = 4;
742 packet.payload = ols->raw_sample_buf;
743 sr_session_bus(user_data, &packet);
744 }
745 free(ols->raw_sample_buf);
746
747 serial_flush(fd);
748 serial_close(fd);
749 packet.type = SR_DF_END;
750 packet.length = 0;
751 sr_session_bus(user_data, &packet);
752 }
753
754 return TRUE;
755}
756
757static int hw_start_acquisition(int device_index, gpointer session_device_id)
758{
759 struct sr_datafeed_packet *packet;
760 struct sr_datafeed_header *header;
761 struct sr_device_instance *sdi;
762 struct ols_device *ols;
763 uint32_t trigger_config[4];
764 uint32_t data;
765 uint16_t readcount, delaycount;
766 uint8_t changrp_mask;
767 int i;
768
769 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
770 return SR_ERR;
771 ols = sdi->priv;
772
773 if (sdi->status != SR_ST_ACTIVE)
774 return SR_ERR;
775
776 readcount = ols->limit_samples / 4;
777
778 memset(trigger_config, 0, 16);
779 trigger_config[ols->num_stages - 1] |= 0x08;
780 if (ols->trigger_mask[0]) {
781 delaycount = readcount * (1 - ols->capture_ratio / 100.0);
782 ols->trigger_at = (readcount - delaycount) * 4 - ols->num_stages;
783
784 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
785 reverse32(ols->trigger_mask[0])) != SR_OK)
786 return SR_ERR;
787 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
788 reverse32(ols->trigger_value[0])) != SR_OK)
789 return SR_ERR;
790 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
791 trigger_config[0]) != SR_OK)
792 return SR_ERR;
793
794 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_1,
795 reverse32(ols->trigger_mask[1])) != SR_OK)
796 return SR_ERR;
797 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_1,
798 reverse32(ols->trigger_value[1])) != SR_OK)
799 return SR_ERR;
800 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_1,
801 trigger_config[1]) != SR_OK)
802 return SR_ERR;
803
804 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_2,
805 reverse32(ols->trigger_mask[2])) != SR_OK)
806 return SR_ERR;
807 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_2,
808 reverse32(ols->trigger_value[2])) != SR_OK)
809 return SR_ERR;
810 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_2,
811 trigger_config[2]) != SR_OK)
812 return SR_ERR;
813
814 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_3,
815 reverse32(ols->trigger_mask[3])) != SR_OK)
816 return SR_ERR;
817 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_3,
818 reverse32(ols->trigger_value[3])) != SR_OK)
819 return SR_ERR;
820 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_3,
821 trigger_config[3]) != SR_OK)
822 return SR_ERR;
823 } else {
824 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
825 ols->trigger_mask[0]) != SR_OK)
826 return SR_ERR;
827 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
828 ols->trigger_value[0]) != SR_OK)
829 return SR_ERR;
830 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
831 0x00000008) != SR_OK)
832 return SR_ERR;
833 delaycount = readcount;
834 }
835
836 g_message("ols: setting samplerate to %" PRIu64 " Hz (divider %u, demux %s)",
837 ols->cur_samplerate, ols->cur_samplerate_divider,
838 ols->flag_reg & FLAG_DEMUX ? "on" : "off");
839 if (send_longcommand(sdi->serial->fd, CMD_SET_DIVIDER,
840 reverse32(ols->cur_samplerate_divider)) != SR_OK)
841 return SR_ERR;
842
843 /* Send sample limit and pre/post-trigger capture ratio. */
844 data = ((readcount - 1) & 0xffff) << 16;
845 data |= (delaycount - 1) & 0xffff;
846 if (send_longcommand(sdi->serial->fd, CMD_CAPTURE_SIZE, reverse16(data)) != SR_OK)
847 return SR_ERR;
848
849 /*
850 * Enable/disable channel groups in the flag register according to the
851 * probe mask.
852 */
853 changrp_mask = 0;
854 for (i = 0; i < 4; i++) {
855 if (ols->probe_mask & (0xff << (i * 8)))
856 changrp_mask |= (1 << i);
857 }
858
859 /* The flag register wants them here, and 1 means "disable channel". */
860 ols->flag_reg |= ~(changrp_mask << 2) & 0x3c;
861 ols->flag_reg |= FLAG_FILTER;
862 data = ols->flag_reg << 24;
863 if (send_longcommand(sdi->serial->fd, CMD_SET_FLAGS, data) != SR_OK)
864 return SR_ERR;
865
866 /* Start acquisition on the device. */
867 if (send_shortcommand(sdi->serial->fd, CMD_RUN) != SR_OK)
868 return SR_ERR;
869
870 sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data,
871 session_device_id);
872
873 /* Send header packet to the session bus. */
874 packet = g_malloc(sizeof(struct sr_datafeed_packet));
875 header = g_malloc(sizeof(struct sr_datafeed_header));
876 if (!packet || !header)
877 return SR_ERR;
878 packet->type = SR_DF_HEADER;
879 packet->length = sizeof(struct sr_datafeed_header);
880 packet->payload = (unsigned char *)header;
881 header->feed_version = 1;
882 gettimeofday(&header->starttime, NULL);
883 header->samplerate = ols->cur_samplerate;
884 header->protocol_id = SR_PROTO_RAW;
885 header->num_logic_probes = NUM_PROBES;
886 header->num_analog_probes = 0;
887 sr_session_bus(session_device_id, packet);
888 g_free(header);
889 g_free(packet);
890
891 return SR_OK;
892}
893
894static void hw_stop_acquisition(int device_index, gpointer session_device_id)
895{
896 struct sr_datafeed_packet packet;
897
898 /* Avoid compiler warnings. */
899 device_index = device_index;
900
901 packet.type = SR_DF_END;
902 packet.length = 0;
903 sr_session_bus(session_device_id, &packet);
904}
905
906struct sr_device_plugin ols_plugin_info = {
907 "ols",
908 "Openbench Logic Sniffer",
909 1,
910 hw_init,
911 hw_cleanup,
912 hw_opendev,
913 hw_closedev,
914 hw_get_device_info,
915 hw_get_status,
916 hw_get_capabilities,
917 hw_set_configuration,
918 hw_start_acquisition,
919 hw_stop_acquisition,
920};