]> sigrok.org Git - libsigrok.git/blame - hardware/openbench-logic-sniffer/ols.c
Support for analog probes
[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>
926b866c 27#ifndef _WIN32
a1bb33af 28#include <termios.h>
926b866c 29#endif
a1bb33af
UH
30#include <string.h>
31#include <sys/time.h>
32#include <inttypes.h>
926b866c
UH
33#ifdef _WIN32
34/* TODO */
35#else
6937bb75 36#include <arpa/inet.h>
926b866c 37#endif
a1bb33af 38#include <glib.h>
43fc7885 39#include <sigrok.h>
a1bb33af 40
43fc7885 41#define NUM_PROBES 32
a1bb33af
UH
42#define NUM_TRIGGER_STAGES 4
43#define TRIGGER_TYPES "01"
44#define SERIAL_SPEED B115200
43fc7885 45#define CLOCK_RATE 100000000
a1bb33af 46
43fc7885
UH
47/* Command opcodes */
48#define CMD_RESET 0x00
49#define CMD_ID 0x02
50#define CMD_SET_FLAGS 0x82
a1bb33af 51#define CMD_SET_DIVIDER 0x80
43fc7885
UH
52#define CMD_RUN 0x01
53#define CMD_CAPTURE_SIZE 0x81
a1bb33af
UH
54#define CMD_SET_TRIGGER_MASK_0 0xc0
55#define CMD_SET_TRIGGER_MASK_1 0xc4
56#define CMD_SET_TRIGGER_MASK_2 0xc8
57#define CMD_SET_TRIGGER_MASK_3 0xcc
43fc7885
UH
58#define CMD_SET_TRIGGER_VALUE_0 0xc1
59#define CMD_SET_TRIGGER_VALUE_1 0xc5
60#define CMD_SET_TRIGGER_VALUE_2 0xc9
61#define CMD_SET_TRIGGER_VALUE_3 0xcd
a1bb33af
UH
62#define CMD_SET_TRIGGER_CONFIG_0 0xc2
63#define CMD_SET_TRIGGER_CONFIG_1 0xc6
64#define CMD_SET_TRIGGER_CONFIG_2 0xca
65#define CMD_SET_TRIGGER_CONFIG_3 0xce
66
43fc7885
UH
67/* Bitmasks for CMD_FLAGS */
68#define FLAG_DEMUX 0x01
a1bb33af 69#define FLAG_FILTER 0x02
43fc7885
UH
70#define FLAG_CHANNELGROUP_1 0x04
71#define FLAG_CHANNELGROUP_2 0x08
72#define FLAG_CHANNELGROUP_3 0x10
73#define FLAG_CHANNELGROUP_4 0x20
74#define FLAG_CLOCK_EXTERNAL 0x40
75#define FLAG_CLOCK_INVERTED 0x80
76#define FLAG_RLE 0x0100
a1bb33af
UH
77
78static int capabilities[] = {
79 HWCAP_LOGIC_ANALYZER,
80 HWCAP_SAMPLERATE,
81 HWCAP_CAPTURE_RATIO,
82 HWCAP_LIMIT_SAMPLES,
43fc7885 83 0,
a1bb33af
UH
84};
85
86static struct samplerates samplerates = {
6937bb75 87 10,
a1bb33af
UH
88 MHZ(200),
89 1,
43fc7885 90 0,
a1bb33af
UH
91};
92
43fc7885 93/* List of struct serial_device_instance */
a1bb33af
UH
94static GSList *device_instances = NULL;
95
43fc7885 96/* Current state of the flag register */
6937bb75 97static uint32_t flag_reg = 0;
a1bb33af
UH
98
99static uint64_t cur_samplerate = 0;
100static uint64_t limit_samples = 0;
43fc7885
UH
101/*
102 * Pre/post trigger capture ratio, in percentage.
103 * 0 means no pre-trigger data.
104 */
6937bb75 105static int capture_ratio = 0;
a803c0db 106static int trigger_at = -1;
43fc7885
UH
107static uint32_t probe_mask = 0xffffffff;
108static uint32_t trigger_mask[4] = { 0, 0, 0, 0 };
109static uint32_t trigger_value[4] = { 0, 0, 0, 0 };
a803c0db 110static int num_stages = 0;
a1bb33af 111
6937bb75 112static int send_shortcommand(int fd, uint8_t command)
a1bb33af
UH
113{
114 char buf[1];
115
edc508d4 116 g_debug("ols: sending cmd 0x%.2x", command);
a1bb33af 117 buf[0] = command;
43fc7885 118 if (write(fd, buf, 1) != 1)
e31b636d 119 return SIGROK_ERR;
a1bb33af
UH
120
121 return SIGROK_OK;
122}
123
6937bb75 124static int send_longcommand(int fd, uint8_t command, uint32_t data)
a1bb33af
UH
125{
126 char buf[5];
127
edc508d4 128 g_debug("ols: sending cmd 0x%.2x data 0x%.8x", command, data);
a1bb33af 129 buf[0] = command;
6937bb75
BV
130 buf[1] = (data & 0xff000000) >> 24;
131 buf[2] = (data & 0xff0000) >> 16;
132 buf[3] = (data & 0xff00) >> 8;
133 buf[4] = data & 0xff;
43fc7885 134 if (write(fd, buf, 5) != 5)
e31b636d 135 return SIGROK_ERR;
a1bb33af
UH
136
137 return SIGROK_OK;
138}
139
140static int configure_probes(GSList *probes)
141{
142 struct probe *probe;
143 GSList *l;
6937bb75 144 int probe_bit, stage, i;
a1bb33af
UH
145 char *tc;
146
147 probe_mask = 0;
43fc7885 148 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
a1bb33af
UH
149 trigger_mask[i] = 0;
150 trigger_value[i] = 0;
151 }
152
a803c0db 153 num_stages = 0;
43fc7885
UH
154 for (l = probes; l; l = l->next) {
155 probe = (struct probe *)l->data;
156 if (!probe->enabled)
6937bb75
BV
157 continue;
158
43fc7885
UH
159 /*
160 * Set up the probe mask for later configuration into the
161 * flag register.
162 */
a1bb33af
UH
163 probe_bit = 1 << (probe->index - 1);
164 probe_mask |= probe_bit;
6937bb75 165
a803c0db 166 if (!probe->trigger)
6937bb75
BV
167 continue;
168
43fc7885 169 /* Configure trigger mask and value. */
6937bb75 170 stage = 0;
43fc7885 171 for (tc = probe->trigger; tc && *tc; tc++) {
6937bb75 172 trigger_mask[stage] |= probe_bit;
43fc7885 173 if (*tc == '1')
6937bb75
BV
174 trigger_value[stage] |= probe_bit;
175 stage++;
43fc7885
UH
176 if (stage > 3)
177 /*
178 * TODO: Only supporting parallel mode, with
179 * up to 4 stages.
180 */
54dc4bc7 181 return SIGROK_ERR;
a1bb33af 182 }
a803c0db
BV
183 if (stage > num_stages)
184 num_stages = stage;
a1bb33af
UH
185 }
186
a1bb33af
UH
187 return SIGROK_OK;
188}
189
a803c0db 190static uint32_t reverse16(uint32_t in)
6937bb75
BV
191{
192 uint32_t out;
193
a803c0db
BV
194 out = (in & 0xff) << 8;
195 out |= (in & 0xff00) >> 8;
196 out |= (in & 0xff0000) << 8;
197 out |= (in & 0xff000000) >> 8;
198
199 return out;
200}
201
202static uint32_t reverse32(uint32_t in)
203{
204 uint32_t out;
205
206 out = (in & 0xff) << 24;
207 out |= (in & 0xff00) << 8;
208 out |= (in & 0xff0000) >> 8;
209 out |= (in & 0xff000000) >> 24;
210
211 return out;
6937bb75
BV
212}
213
a1bb33af
UH
214static int hw_init(char *deviceinfo)
215{
216 struct sigrok_device_instance *sdi;
217 GSList *ports, *l;
218 GPollFD *fds;
6937bb75 219 int devcnt, final_devcnt, num_ports, fd, ret, i;
d02a535e 220 char buf[8], **device_names, **serial_params;
a1bb33af 221
43fc7885 222 if (deviceinfo)
6937bb75 223 ports = g_slist_append(NULL, strdup(deviceinfo));
a1bb33af 224 else
43fc7885 225 /* No specific device given, so scan all serial ports. */
a1bb33af
UH
226 ports = list_serial_ports();
227
228 num_ports = g_slist_length(ports);
6937bb75 229 fds = calloc(1, num_ports * sizeof(GPollFD));
43fc7885
UH
230 device_names = malloc(num_ports * sizeof(char *));
231 serial_params = malloc(num_ports * sizeof(char *));
a1bb33af 232 devcnt = 0;
43fc7885
UH
233 for (l = ports; l; l = l->next) {
234 /* The discovery procedure is like this: first send the Reset
235 * command (0x00) 5 times, since the device could be anywhere
236 * in a 5-byte command. Then send the ID command (0x02).
237 * If the device responds with 4 bytes ("OLS1" or "SLA1"), we
238 * have a match.
239 *
240 * Since it may take the device a while to respond at 115Kb/s,
241 * we do all the sending first, then wait for all of them to
242 * respond with g_poll().
a1bb33af 243 */
e6ac9ac8 244 g_message("ols: probing %s...", (char *)l->data);
d02a535e 245 fd = serial_open(l->data, O_RDWR | O_NONBLOCK);
43fc7885 246 if (fd != -1) {
d02a535e
BV
247 serial_params[devcnt] = serial_backup_params(fd);
248 serial_set_params(fd, 115200, 8, 0, 1, 2);
6937bb75 249 ret = SIGROK_OK;
43fc7885
UH
250 for (i = 0; i < 5; i++) {
251 if ((ret = send_shortcommand(fd,
252 CMD_RESET)) != SIGROK_OK) {
253 /* Serial port is not writable. */
6937bb75
BV
254 break;
255 }
a1bb33af 256 }
43fc7885
UH
257 if (ret != SIGROK_OK) {
258 serial_restore_params(fd,
259 serial_params[devcnt]);
d02a535e 260 serial_close(fd);
6937bb75 261 continue;
d02a535e 262 }
6937bb75
BV
263 send_shortcommand(fd, CMD_ID);
264 fds[devcnt].fd = fd;
265 fds[devcnt].events = G_IO_IN;
266 device_names[devcnt] = strdup(l->data);
267 devcnt++;
a1bb33af 268 }
6937bb75 269 free(l->data);
a1bb33af
UH
270 }
271
5b15b41e
PS
272 /* 2ms isn't enough for reliable transfer with pl2303, let's try 10 */
273 usleep(10000);
a1bb33af
UH
274
275 final_devcnt = 0;
276 g_poll(fds, devcnt, 1);
43fc7885
UH
277 for (i = 0; i < devcnt; i++) {
278 if (fds[i].revents == G_IO_IN) {
279 if (read(fds[i].fd, buf, 4) == 4) {
280 if (!strncmp(buf, "1SLO", 4)
281 || !strncmp(buf, "1ALS", 4)) {
282 if (!strncmp(buf, "1SLO", 4))
283 sdi = sigrok_device_instance_new
284 (final_devcnt, ST_INACTIVE,
285 "Openbench",
286 "Logic Sniffer", "v1.0");
a1bb33af 287 else
43fc7885
UH
288 sdi = sigrok_device_instance_new
289 (final_devcnt, ST_INACTIVE,
e6ac9ac8 290 "Openbench", "Logic Sniffer",
43fc7885
UH
291 "v1.0");
292 sdi->serial = serial_device_instance_new
293 (device_names[i], -1);
294 device_instances =
295 g_slist_append(device_instances, sdi);
a1bb33af 296 final_devcnt++;
d02a535e 297 serial_close(fds[i].fd);
a1bb33af
UH
298 fds[i].fd = 0;
299 }
300 }
6937bb75 301 free(device_names[i]);
a1bb33af
UH
302 }
303
43fc7885 304 if (fds[i].fd != 0) {
d02a535e
BV
305 serial_restore_params(fds[i].fd, serial_params[i]);
306 serial_close(fds[i].fd);
6937bb75 307 }
d02a535e 308 free(serial_params[i]);
a1bb33af
UH
309 }
310
6937bb75
BV
311 free(fds);
312 free(device_names);
d02a535e 313 free(serial_params);
a1bb33af
UH
314 g_slist_free(ports);
315
6937bb75
BV
316 cur_samplerate = samplerates.low;
317
a1bb33af
UH
318 return final_devcnt;
319}
320
a1bb33af
UH
321static int hw_opendev(int device_index)
322{
323 struct sigrok_device_instance *sdi;
324
43fc7885 325 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
e31b636d 326 return SIGROK_ERR;
a1bb33af 327
d02a535e 328 sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR);
43fc7885 329 if (sdi->serial->fd == -1)
e31b636d 330 return SIGROK_ERR;
a1bb33af
UH
331
332 sdi->status = ST_ACTIVE;
333
334 return SIGROK_OK;
335}
336
a1bb33af
UH
337static void hw_closedev(int device_index)
338{
339 struct sigrok_device_instance *sdi;
340
43fc7885 341 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
a1bb33af
UH
342 return;
343
43fc7885 344 if (sdi->serial->fd != -1) {
d02a535e 345 serial_close(sdi->serial->fd);
a1bb33af
UH
346 sdi->serial->fd = -1;
347 sdi->status = ST_INACTIVE;
348 }
a1bb33af
UH
349}
350
a1bb33af
UH
351static void hw_cleanup(void)
352{
353 GSList *l;
354 struct sigrok_device_instance *sdi;
355
43fc7885
UH
356 /* Properly close all devices. */
357 for (l = device_instances; l; l = l->next) {
a1bb33af 358 sdi = l->data;
43fc7885 359 if (sdi->serial->fd != -1)
d02a535e 360 serial_close(sdi->serial->fd);
a1bb33af
UH
361 sigrok_device_instance_free(sdi);
362 }
363 g_slist_free(device_instances);
364 device_instances = NULL;
a1bb33af
UH
365}
366
a1bb33af
UH
367static void *hw_get_device_info(int device_index, int device_info_id)
368{
369 struct sigrok_device_instance *sdi;
370 void *info;
371
43fc7885 372 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
a1bb33af
UH
373 return NULL;
374
375 info = NULL;
43fc7885 376 switch (device_info_id) {
a1bb33af
UH
377 case DI_INSTANCE:
378 info = sdi;
379 break;
380 case DI_NUM_PROBES:
381 info = GINT_TO_POINTER(NUM_PROBES);
382 break;
383 case DI_SAMPLERATES:
384 info = &samplerates;
385 break;
386 case DI_TRIGGER_TYPES:
43fc7885 387 info = (char *)TRIGGER_TYPES;
a1bb33af 388 break;
4c100f32 389 case DI_CUR_SAMPLERATE:
a1bb33af
UH
390 info = &cur_samplerate;
391 break;
392 }
393
394 return info;
395}
396
a1bb33af
UH
397static int hw_get_status(int device_index)
398{
399 struct sigrok_device_instance *sdi;
400
43fc7885 401 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
a1bb33af
UH
402 return ST_NOT_FOUND;
403
404 return sdi->status;
405}
406
a1bb33af
UH
407static int *hw_get_capabilities(void)
408{
a1bb33af
UH
409 return capabilities;
410}
411
43fc7885
UH
412static int set_configuration_samplerate(struct sigrok_device_instance *sdi,
413 uint64_t samplerate)
a1bb33af
UH
414{
415 uint32_t divider;
416
43fc7885 417 if (samplerate < samplerates.low || samplerate > samplerates.high)
e31b636d 418 return SIGROK_ERR_SAMPLERATE;
a1bb33af 419
43fc7885 420 if (samplerate > CLOCK_RATE) {
a1bb33af 421 flag_reg |= FLAG_DEMUX;
6937bb75 422 divider = (CLOCK_RATE * 2 / samplerate) - 1;
43fc7885 423 } else {
6937bb75
BV
424 flag_reg &= ~FLAG_DEMUX;
425 divider = (CLOCK_RATE / samplerate) - 1;
a1bb33af 426 }
a1bb33af 427
e6ac9ac8 428 g_message("ols: setting samplerate to %" PRIu64 " Hz (divider %u, demux %s)",
a803c0db 429 samplerate, divider, flag_reg & FLAG_DEMUX ? "on" : "off");
43fc7885 430
a803c0db 431 if (send_longcommand(sdi->serial->fd, CMD_SET_DIVIDER, reverse32(divider)) != SIGROK_OK)
e31b636d 432 return SIGROK_ERR;
a1bb33af
UH
433 cur_samplerate = samplerate;
434
435 return SIGROK_OK;
436}
437
a1bb33af
UH
438static int hw_set_configuration(int device_index, int capability, void *value)
439{
440 struct sigrok_device_instance *sdi;
441 int ret;
442 uint64_t *tmp_u64;
443
43fc7885 444 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
e31b636d 445 return SIGROK_ERR;
a1bb33af 446
43fc7885 447 if (sdi->status != ST_ACTIVE)
e31b636d 448 return SIGROK_ERR;
a1bb33af 449
a803c0db
BV
450 switch (capability) {
451 case HWCAP_SAMPLERATE:
a1bb33af
UH
452 tmp_u64 = value;
453 ret = set_configuration_samplerate(sdi, *tmp_u64);
a803c0db
BV
454 break;
455 case HWCAP_PROBECONFIG:
43fc7885 456 ret = configure_probes((GSList *) value);
a803c0db
BV
457 break;
458 case HWCAP_LIMIT_SAMPLES:
2458ea65
BV
459 tmp_u64 = value;
460 limit_samples = *tmp_u64;
e6ac9ac8 461 g_message("ols: sample limit %" PRIu64, limit_samples);
a1bb33af 462 ret = SIGROK_OK;
a803c0db
BV
463 break;
464 case HWCAP_CAPTURE_RATIO:
465 tmp_u64 = value;
466 capture_ratio = *tmp_u64;
43fc7885 467 if (capture_ratio < 0 || capture_ratio > 100) {
a1bb33af 468 capture_ratio = 0;
e31b636d 469 ret = SIGROK_ERR;
43fc7885 470 } else
a1bb33af 471 ret = SIGROK_OK;
a803c0db
BV
472 break;
473 default:
e31b636d 474 ret = SIGROK_ERR;
43fc7885 475 }
a1bb33af
UH
476
477 return ret;
478}
479
a1bb33af
UH
480static int receive_data(int fd, int revents, void *user_data)
481{
afc8e4de 482 static unsigned int num_transfers = 0;
a1bb33af 483 static int num_bytes = 0;
43fc7885
UH
484 static char last_sample[4] = { 0xff, 0xff, 0xff, 0xff };
485 static unsigned char sample[4] = { 0, 0, 0, 0 };
486 static unsigned char tmp_sample[4];
a803c0db
BV
487 static unsigned char *raw_sample_buf = NULL;
488 int count, buflen, num_channels, offset, i, j;
a1bb33af
UH
489 struct datafeed_packet packet;
490 unsigned char byte, *buffer;
491
43fc7885
UH
492 if (num_transfers++ == 0) {
493 /*
494 * First time round, means the device started sending data,
495 * and will not stop until done. If it stops sending for
496 * longer than it takes to send a byte, that means it's
497 * finished. We'll double that to 30ms to be sure...
a1bb33af
UH
498 */
499 source_remove(fd);
a803c0db
BV
500 source_add(fd, G_IO_IN, 30, receive_data, user_data);
501 raw_sample_buf = malloc(limit_samples * 4);
502 /* fill with 1010... for debugging */
503 memset(raw_sample_buf, 0x82, limit_samples * 4);
a1bb33af
UH
504 }
505
6937bb75 506 num_channels = 0;
43fc7885
UH
507 for (i = 0x20; i > 0x02; i /= 2) {
508 if ((flag_reg & i) == 0)
6937bb75 509 num_channels++;
43fc7885 510 }
6937bb75 511
43fc7885
UH
512 if (revents == G_IO_IN
513 && num_transfers / num_channels <= limit_samples) {
514 if (read(fd, &byte, 1) != 1)
a1bb33af
UH
515 return FALSE;
516
517 sample[num_bytes++] = byte;
e6ac9ac8 518 g_debug("ols: received byte 0x%.2x", byte);
43fc7885 519 if (num_bytes == num_channels) {
e6ac9ac8 520 g_debug("ols: received sample 0x%.*x", num_bytes * 2, (int) *sample);
43fc7885
UH
521 /* Got a full sample. */
522 if (flag_reg & FLAG_RLE) {
523 /*
524 * In RLE mode -1 should never come in as a
525 * sample, because bit 31 is the "count" flag.
526 * TODO: Endianness may be wrong here, could be
527 * sample[3].
528 */
529 if (sample[0] & 0x80
530 && !(last_sample[0] & 0x80)) {
531 count = (int)(*sample) & 0x7fffffff;
a1bb33af
UH
532 buffer = g_malloc(count);
533 buflen = 0;
43fc7885 534 for (i = 0; i < count; i++) {
a803c0db 535 memcpy(buffer + buflen, last_sample, 4);
a1bb33af
UH
536 buflen += 4;
537 }
43fc7885
UH
538 } else {
539 /*
540 * Just a single sample, next sample
541 * will probably be a count referring
542 * to this -- but this one is still a
543 * part of the stream.
a1bb33af
UH
544 */
545 buffer = sample;
546 buflen = 4;
547 }
43fc7885
UH
548 } else {
549 /* No compression. */
a1bb33af
UH
550 buffer = sample;
551 buflen = 4;
552 }
553
43fc7885
UH
554 if (num_channels < 4) {
555 /*
556 * Some channel groups may have been turned
557 * off, to speed up transfer between the
558 * hardware and the PC. Expand that here before
559 * submitting it over the session bus --
560 * whatever is listening on the bus will be
561 * expecting a full 32-bit sample, based on
562 * the number of probes.
6937bb75
BV
563 */
564 j = 0;
565 memset(tmp_sample, 0, 4);
43fc7885 566 for (i = 0; i < 4; i++) {
a803c0db 567 if (((flag_reg >> 2) & (1 << i)) == 0) {
43fc7885
UH
568 /*
569 * This channel group was
570 * enabled, copy from received
571 * sample.
572 */
6937bb75
BV
573 tmp_sample[i] = sample[j++];
574 }
575 }
576 memcpy(sample, tmp_sample, 4);
e6ac9ac8 577 g_debug("ols: full sample 0x%.8x", (int) *sample);
6937bb75
BV
578 }
579
a803c0db
BV
580 /* the OLS sends its sample buffer backwards.
581 * store it in reverse order here, so we can dump
582 * this on the session bus later.
583 */
f0d1b53e 584 offset = (limit_samples - num_transfers / num_channels) * 4;
a803c0db
BV
585 memcpy(raw_sample_buf + offset, sample, 4);
586
43fc7885 587 if (buffer == sample)
6937bb75 588 memcpy(last_sample, buffer, num_channels);
a1bb33af
UH
589 else
590 g_free(buffer);
591
6937bb75 592 memset(sample, 0, 4);
a1bb33af
UH
593 num_bytes = 0;
594 }
43fc7885
UH
595 } else {
596 /*
597 * This is the main loop telling us a timeout was reached, or
598 * we've acquired all the samples we asked for -- we're done.
a803c0db 599 * Send the (properly-ordered) buffer to the frontend.
43fc7885 600 */
a803c0db
BV
601 if (trigger_at != -1) {
602 /* a trigger was set up, so we need to tell the frontend
603 * about it.
604 */
605 if (trigger_at > 0) {
606 /* there are pre-trigger samples, send those first */
4c046c6b 607 packet.type = DF_LOGIC;
a803c0db 608 packet.length = trigger_at * 4;
4c046c6b 609 packet.unitsize = 4;
a803c0db
BV
610 packet.payload = raw_sample_buf;
611 session_bus(user_data, &packet);
612 }
613
614 packet.type = DF_TRIGGER;
615 packet.length = 0;
616 session_bus(user_data, &packet);
617
4c046c6b 618 packet.type = DF_LOGIC;
a803c0db 619 packet.length = (limit_samples * 4) - (trigger_at * 4);
4c046c6b 620 packet.unitsize = 4;
a803c0db
BV
621 packet.payload = raw_sample_buf + trigger_at * 4;
622 session_bus(user_data, &packet);
623 } else {
4c046c6b 624 packet.type = DF_LOGIC;
a803c0db 625 packet.length = limit_samples * 4;
4c046c6b 626 packet.unitsize = 4;
a803c0db
BV
627 packet.payload = raw_sample_buf;
628 session_bus(user_data, &packet);
629 }
630 free(raw_sample_buf);
631
06d64eb8 632 serial_flush(fd);
d02a535e 633 serial_close(fd);
a1bb33af
UH
634 packet.type = DF_END;
635 packet.length = 0;
636 session_bus(user_data, &packet);
637 }
638
639 return TRUE;
640}
641
a1bb33af
UH
642static int hw_start_acquisition(int device_index, gpointer session_device_id)
643{
43fc7885 644 int i;
a1bb33af
UH
645 struct datafeed_packet *packet;
646 struct datafeed_header *header;
647 struct sigrok_device_instance *sdi;
a803c0db 648 uint32_t trigger_config[4];
a1bb33af 649 uint32_t data;
6937bb75
BV
650 uint16_t readcount, delaycount;
651 uint8_t changrp_mask;
a1bb33af 652
43fc7885 653 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
e31b636d 654 return SIGROK_ERR;
a1bb33af 655
43fc7885 656 if (sdi->status != ST_ACTIVE)
e31b636d 657 return SIGROK_ERR;
a1bb33af 658
a803c0db
BV
659 readcount = limit_samples / 4;
660
661 memset(trigger_config, 0, 16);
662 trigger_config[num_stages-1] |= 0x08;
43fc7885 663 if (trigger_mask[0]) {
a803c0db
BV
664 delaycount = readcount * (1 - capture_ratio / 100.0);
665 trigger_at = (readcount - delaycount) * 4 - num_stages;
a803c0db 666
43fc7885 667 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
a803c0db 668 reverse32(trigger_mask[0])) != SIGROK_OK)
54dc4bc7 669 return SIGROK_ERR;
a803c0db
BV
670 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
671 reverse32(trigger_value[0])) != SIGROK_OK)
54dc4bc7 672 return SIGROK_ERR;
a803c0db
BV
673 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
674 trigger_config[0]) != SIGROK_OK)
54dc4bc7 675 return SIGROK_ERR;
6937bb75 676
a803c0db
BV
677 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_1,
678 reverse32(trigger_mask[1])) != SIGROK_OK)
54dc4bc7 679 return SIGROK_ERR;
43fc7885 680 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_1,
a803c0db 681 reverse32(trigger_value[1])) != SIGROK_OK)
54dc4bc7 682 return SIGROK_ERR;
a803c0db
BV
683 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_1,
684 trigger_config[1]) != SIGROK_OK)
54dc4bc7 685 return SIGROK_ERR;
6937bb75 686
a803c0db
BV
687 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_2,
688 reverse32(trigger_mask[2])) != SIGROK_OK)
54dc4bc7 689 return SIGROK_ERR;
a803c0db
BV
690 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_2,
691 reverse32(trigger_value[2])) != SIGROK_OK)
54dc4bc7 692 return SIGROK_ERR;
43fc7885 693 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_2,
a803c0db
BV
694 trigger_config[2]) != SIGROK_OK)
695 return SIGROK_ERR;
696
697 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_3,
698 reverse32(trigger_mask[3])) != SIGROK_OK)
699 return SIGROK_ERR;
700 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_3,
701 reverse32(trigger_value[3])) != SIGROK_OK)
54dc4bc7 702 return SIGROK_ERR;
43fc7885 703 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_3,
a803c0db 704 trigger_config[3]) != SIGROK_OK)
54dc4bc7 705 return SIGROK_ERR;
6937bb75 706 } else {
43fc7885
UH
707 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
708 trigger_mask[0]) != SIGROK_OK)
54dc4bc7 709 return SIGROK_ERR;
43fc7885
UH
710 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
711 trigger_value[0]) != SIGROK_OK)
54dc4bc7 712 return SIGROK_ERR;
43fc7885
UH
713 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
714 0x00000008) != SIGROK_OK)
54dc4bc7 715 return SIGROK_ERR;
a803c0db 716 delaycount = readcount;
6937bb75 717 }
a1bb33af 718
6937bb75 719 set_configuration_samplerate(sdi, cur_samplerate);
a1bb33af 720
43fc7885 721 /* Send sample limit and pre/post-trigger capture ratio. */
a803c0db
BV
722 data = ((readcount - 1) & 0xffff) << 16;
723 data |= (delaycount - 1) & 0xffff;
724 if (send_longcommand(sdi->serial->fd, CMD_CAPTURE_SIZE, reverse16(data)) != SIGROK_OK)
e31b636d 725 return SIGROK_ERR;
a1bb33af 726
43fc7885
UH
727 /*
728 * Enable/disable channel groups in the flag register according to the
a803c0db 729 * probe mask.
6937bb75
BV
730 */
731 changrp_mask = 0;
43fc7885
UH
732 for (i = 0; i < 4; i++) {
733 if (probe_mask & (0xff << (i * 8)))
a803c0db 734 changrp_mask |= (1 << i);
6937bb75 735 }
43fc7885
UH
736
737 /* The flag register wants them here, and 1 means "disable channel". */
6937bb75 738 flag_reg |= ~(changrp_mask << 2) & 0x3c;
a803c0db 739 flag_reg |= FLAG_FILTER;
6937bb75 740 data = flag_reg << 24;
43fc7885 741 if (send_longcommand(sdi->serial->fd, CMD_SET_FLAGS, data) != SIGROK_OK)
54dc4bc7 742 return SIGROK_ERR;
a1bb33af 743
43fc7885
UH
744 /* Start acquisition on the device. */
745 if (send_shortcommand(sdi->serial->fd, CMD_RUN) != SIGROK_OK)
e31b636d 746 return SIGROK_ERR;
a1bb33af 747
43fc7885
UH
748 source_add(sdi->serial->fd, G_IO_IN, -1, receive_data,
749 session_device_id);
a1bb33af 750
43fc7885 751 /* Send header packet to the session bus. */
a1bb33af
UH
752 packet = g_malloc(sizeof(struct datafeed_packet));
753 header = g_malloc(sizeof(struct datafeed_header));
43fc7885 754 if (!packet || !header)
e31b636d 755 return SIGROK_ERR;
a1bb33af
UH
756 packet->type = DF_HEADER;
757 packet->length = sizeof(struct datafeed_header);
43fc7885 758 packet->payload = (unsigned char *)header;
a1bb33af
UH
759 header->feed_version = 1;
760 gettimeofday(&header->starttime, NULL);
4c100f32 761 header->samplerate = cur_samplerate;
a1bb33af
UH
762 header->protocol_id = PROTO_RAW;
763 header->num_probes = NUM_PROBES;
764 session_bus(session_device_id, packet);
765 g_free(header);
766 g_free(packet);
767
768 return SIGROK_OK;
769}
770
a1bb33af
UH
771static void hw_stop_acquisition(int device_index, gpointer session_device_id)
772{
773 struct datafeed_packet packet;
774
afc8e4de
UH
775 /* QUICK HACK */
776 device_index = device_index;
777
a1bb33af
UH
778 packet.type = DF_END;
779 packet.length = 0;
780 session_bus(session_device_id, &packet);
a1bb33af
UH
781}
782
a1bb33af 783struct device_plugin ols_plugin_info = {
e6ac9ac8 784 "ols",
a1bb33af
UH
785 1,
786 hw_init,
787 hw_cleanup,
a1bb33af
UH
788 hw_opendev,
789 hw_closedev,
790 hw_get_device_info,
791 hw_get_status,
792 hw_get_capabilities,
793 hw_set_configuration,
794 hw_start_acquisition,
43fc7885 795 hw_stop_acquisition,
a1bb33af 796};