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