]> sigrok.org Git - libsigrok.git/blame - src/hardware/openbench-logic-sniffer/protocol.c
Only sr_dev_inst_free() should free channel groups.
[libsigrok.git] / src / hardware / openbench-logic-sniffer / protocol.c
CommitLineData
0aba65da 1/*
50985c20 2 * This file is part of the libsigrok project.
0aba65da 3 *
13d8e03c 4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
0aba65da
UH
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 "protocol.h"
bf72f649 21#include <libserialport.h>
0aba65da 22
2239728c 23extern SR_PRIV struct sr_dev_driver ols_driver_info;
0aba65da
UH
24static struct sr_dev_driver *di = &ols_driver_info;
25
26SR_PRIV int send_shortcommand(struct sr_serial_dev_inst *serial,
27 uint8_t command)
28{
29 char buf[1];
30
31 sr_dbg("Sending cmd 0x%.2x.", command);
32 buf[0] = command;
f4d3a4fb 33 if (serial_write_blocking(serial, buf, 1, serial_timeout(serial, 1)) != 1)
0aba65da
UH
34 return SR_ERR;
35
36 return SR_OK;
37}
38
39SR_PRIV int send_longcommand(struct sr_serial_dev_inst *serial,
016e72f3 40 uint8_t command, uint8_t *data)
0aba65da
UH
41{
42 char buf[5];
43
016e72f3
BV
44 sr_dbg("Sending cmd 0x%.2x data 0x%.2x%.2x%.2x%.2x.", command,
45 data[0], data[1], data[2], data[3]);
0aba65da 46 buf[0] = command;
016e72f3
BV
47 buf[1] = data[0];
48 buf[2] = data[1];
49 buf[3] = data[2];
50 buf[4] = data[3];
f4d3a4fb 51 if (serial_write_blocking(serial, buf, 5, serial_timeout(serial, 1)) != 5)
0aba65da
UH
52 return SR_ERR;
53
54 return SR_OK;
55}
56
91fd0f72
BV
57/* Configures the channel mask based on which channels are enabled. */
58SR_PRIV void ols_channel_mask(const struct sr_dev_inst *sdi)
0aba65da
UH
59{
60 struct dev_context *devc;
91fd0f72 61 struct sr_channel *channel;
0aba65da 62 const GSList *l;
0aba65da
UH
63
64 devc = sdi->priv;
65
ba7dd8bb 66 devc->channel_mask = 0;
91fd0f72
BV
67 for (l = sdi->channels; l; l = l->next) {
68 channel = l->data;
69 if (channel->enabled)
70 devc->channel_mask |= 1 << channel->index;
71 }
72}
73
74SR_PRIV int ols_convert_trigger(const struct sr_dev_inst *sdi)
75{
76 struct dev_context *devc;
77 struct sr_trigger *trigger;
78 struct sr_trigger_stage *stage;
79 struct sr_trigger_match *match;
80 const GSList *l, *m;
81 int i;
82
83 devc = sdi->priv;
84
85 devc->num_stages = 0;
0aba65da
UH
86 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
87 devc->trigger_mask[i] = 0;
88 devc->trigger_value[i] = 0;
89 }
90
0812c40e 91 if (!(trigger = sr_session_trigger_get(sdi->session)))
91fd0f72 92 return SR_OK;
0aba65da 93
91fd0f72
BV
94 devc->num_stages = g_slist_length(trigger->stages);
95 if (devc->num_stages > NUM_TRIGGER_STAGES) {
96 sr_err("This device only supports %d trigger stages.",
97 NUM_TRIGGER_STAGES);
98 return SR_ERR;
99 }
b1de0407 100
91fd0f72
BV
101 for (l = trigger->stages; l; l = l->next) {
102 stage = l->data;
103 for (m = stage->matches; m; m = m->next) {
104 match = m->data;
105 if (!match->channel->enabled)
106 /* Ignore disabled channels with a trigger. */
107 continue;
108 devc->trigger_mask[stage->stage] |= 1 << match->channel->index;
109 if (match->match == SR_TRIGGER_ONE)
110 devc->trigger_value[stage->stage] |= 1 << match->channel->index;
0aba65da 111 }
0aba65da
UH
112 }
113
114 return SR_OK;
115}
116
0aba65da
UH
117SR_PRIV struct dev_context *ols_dev_new(void)
118{
119 struct dev_context *devc;
120
bf256783 121 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
0aba65da
UH
122 sr_err("Device context malloc failed.");
123 return NULL;
124 }
125
bf256783
BV
126 /* Device-specific settings */
127 devc->max_samples = devc->max_samplerate = devc->protocol_version = 0;
128
129 /* Acquisition settings */
130 devc->limit_samples = devc->capture_ratio = 0;
0aba65da 131 devc->trigger_at = -1;
ba7dd8bb 132 devc->channel_mask = 0xffffffff;
bf256783
BV
133 devc->flag_reg = 0;
134
0aba65da
UH
135 return devc;
136}
137
138SR_PRIV struct sr_dev_inst *get_metadata(struct sr_serial_dev_inst *serial)
139{
140 struct sr_dev_inst *sdi;
141 struct dev_context *devc;
ba7dd8bb 142 struct sr_channel *ch;
0aba65da
UH
143 uint32_t tmp_int, ui;
144 uint8_t key, type, token;
f4d3a4fb 145 int delay_ms;
0aba65da
UH
146 GString *tmp_str, *devname, *version;
147 guchar tmp_c;
148
aed4ad0b 149 sdi = sr_dev_inst_new(SR_ST_INACTIVE, NULL, NULL, NULL);
0aba65da
UH
150 sdi->driver = di;
151 devc = ols_dev_new();
152 sdi->priv = devc;
153
154 devname = g_string_new("");
155 version = g_string_new("");
156
157 key = 0xff;
158 while (key) {
f4d3a4fb
BV
159 delay_ms = serial_timeout(serial, 1);
160 if (serial_read_blocking(serial, &key, 1, delay_ms) != 1)
0aba65da 161 break;
625763e2
BV
162 if (key == 0x00) {
163 sr_dbg("Got metadata key 0x00, metadata ends.");
164 break;
165 }
0aba65da
UH
166 type = key >> 5;
167 token = key & 0x1f;
168 switch (type) {
169 case 0:
170 /* NULL-terminated string */
171 tmp_str = g_string_new("");
f4d3a4fb
BV
172 delay_ms = serial_timeout(serial, 1);
173 while (serial_read_blocking(serial, &tmp_c, 1, delay_ms) == 1 && tmp_c != '\0')
0aba65da
UH
174 g_string_append_c(tmp_str, tmp_c);
175 sr_dbg("Got metadata key 0x%.2x value '%s'.",
176 key, tmp_str->str);
177 switch (token) {
178 case 0x01:
179 /* Device name */
180 devname = g_string_append(devname, tmp_str->str);
181 break;
182 case 0x02:
183 /* FPGA firmware version */
184 if (version->len)
185 g_string_append(version, ", ");
186 g_string_append(version, "FPGA version ");
187 g_string_append(version, tmp_str->str);
188 break;
189 case 0x03:
190 /* Ancillary version */
191 if (version->len)
192 g_string_append(version, ", ");
193 g_string_append(version, "Ancillary version ");
194 g_string_append(version, tmp_str->str);
195 break;
196 default:
197 sr_info("ols: unknown token 0x%.2x: '%s'",
198 token, tmp_str->str);
199 break;
200 }
201 g_string_free(tmp_str, TRUE);
202 break;
203 case 1:
204 /* 32-bit unsigned integer */
f4d3a4fb
BV
205 delay_ms = serial_timeout(serial, 4);
206 if (serial_read_blocking(serial, &tmp_int, 4, delay_ms) != 4)
0aba65da 207 break;
016e72f3 208 tmp_int = RB32(&tmp_int);
0aba65da
UH
209 sr_dbg("Got metadata key 0x%.2x value 0x%.8x.",
210 key, tmp_int);
211 switch (token) {
212 case 0x00:
ba7dd8bb 213 /* Number of usable channels */
0aba65da 214 for (ui = 0; ui < tmp_int; ui++) {
3f239f08 215 if (!(ch = sr_channel_new(ui, SR_CHANNEL_LOGIC, TRUE,
ba7dd8bb 216 ols_channel_names[ui])))
0aba65da 217 return 0;
ba7dd8bb 218 sdi->channels = g_slist_append(sdi->channels, ch);
0aba65da
UH
219 }
220 break;
221 case 0x01:
222 /* Amount of sample memory available (bytes) */
223 devc->max_samples = tmp_int;
224 break;
225 case 0x02:
226 /* Amount of dynamic memory available (bytes) */
227 /* what is this for? */
228 break;
229 case 0x03:
230 /* Maximum sample rate (hz) */
231 devc->max_samplerate = tmp_int;
232 break;
233 case 0x04:
234 /* protocol version */
235 devc->protocol_version = tmp_int;
236 break;
237 default:
238 sr_info("Unknown token 0x%.2x: 0x%.8x.",
239 token, tmp_int);
240 break;
241 }
242 break;
243 case 2:
244 /* 8-bit unsigned integer */
f4d3a4fb
BV
245 delay_ms = serial_timeout(serial, 1);
246 if (serial_read_blocking(serial, &tmp_c, 1, delay_ms) != 1)
0aba65da
UH
247 break;
248 sr_dbg("Got metadata key 0x%.2x value 0x%.2x.",
249 key, tmp_c);
250 switch (token) {
251 case 0x00:
ba7dd8bb 252 /* Number of usable channels */
0aba65da 253 for (ui = 0; ui < tmp_c; ui++) {
3f239f08 254 if (!(ch = sr_channel_new(ui, SR_CHANNEL_LOGIC, TRUE,
ba7dd8bb 255 ols_channel_names[ui])))
0aba65da 256 return 0;
ba7dd8bb 257 sdi->channels = g_slist_append(sdi->channels, ch);
0aba65da
UH
258 }
259 break;
260 case 0x01:
261 /* protocol version */
262 devc->protocol_version = tmp_c;
263 break;
264 default:
265 sr_info("Unknown token 0x%.2x: 0x%.2x.",
266 token, tmp_c);
267 break;
268 }
269 break;
270 default:
271 /* unknown type */
272 break;
273 }
274 }
275
276 sdi->model = devname->str;
277 sdi->version = version->str;
278 g_string_free(devname, FALSE);
279 g_string_free(version, FALSE);
280
281 return sdi;
282}
283
284SR_PRIV int ols_set_samplerate(const struct sr_dev_inst *sdi,
e46aa4f6 285 const uint64_t samplerate)
0aba65da
UH
286{
287 struct dev_context *devc;
288
289 devc = sdi->priv;
e46aa4f6 290 if (devc->max_samplerate && samplerate > devc->max_samplerate)
0aba65da
UH
291 return SR_ERR_SAMPLERATE;
292
293 if (samplerate > CLOCK_RATE) {
6ebe0039 294 sr_info("Enabling demux mode.");
0aba65da 295 devc->flag_reg |= FLAG_DEMUX;
6ebe0039 296 devc->flag_reg &= ~FLAG_FILTER;
3f239f08 297 devc->max_channels = NUM_CHANNELS / 2;
0aba65da
UH
298 devc->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
299 } else {
6ebe0039 300 sr_info("Disabling demux mode.");
0aba65da 301 devc->flag_reg &= ~FLAG_DEMUX;
6a53bde6 302 devc->flag_reg |= FLAG_FILTER;
3f239f08 303 devc->max_channels = NUM_CHANNELS;
0aba65da
UH
304 devc->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
305 }
306
307 /* Calculate actual samplerate used and complain if it is different
308 * from the requested.
309 */
310 devc->cur_samplerate = CLOCK_RATE / (devc->cur_samplerate_divider + 1);
311 if (devc->flag_reg & FLAG_DEMUX)
312 devc->cur_samplerate *= 2;
313 if (devc->cur_samplerate != samplerate)
e46aa4f6 314 sr_info("Can't match samplerate %" PRIu64 ", using %"
0aba65da
UH
315 PRIu64 ".", samplerate, devc->cur_samplerate);
316
317 return SR_OK;
318}
319
320SR_PRIV void abort_acquisition(const struct sr_dev_inst *sdi)
321{
322 struct sr_datafeed_packet packet;
459a0f26 323 struct sr_serial_dev_inst *serial;
0aba65da 324
459a0f26 325 serial = sdi->conn;
102f1239 326 serial_source_remove(sdi->session, serial);
0aba65da
UH
327
328 /* Terminate session */
329 packet.type = SR_DF_END;
330 sr_session_send(sdi, &packet);
331}
332
333SR_PRIV int ols_receive_data(int fd, int revents, void *cb_data)
334{
459a0f26 335 struct dev_context *devc;
625763e2 336 struct sr_dev_inst *sdi;
459a0f26 337 struct sr_serial_dev_inst *serial;
0aba65da
UH
338 struct sr_datafeed_packet packet;
339 struct sr_datafeed_logic logic;
fe9ac252 340 uint32_t sample;
91fd0f72 341 int num_ols_changrp, offset, j;
b1de0407 342 unsigned int i;
0aba65da 343 unsigned char byte;
625763e2
BV
344
345 (void)fd;
346
347 sdi = cb_data;
348 serial = sdi->conn;
349 devc = sdi->priv;
0aba65da
UH
350
351 if (devc->num_transfers++ == 0) {
352 /*
353 * First time round, means the device started sending data,
354 * and will not stop until done. If it stops sending for
355 * longer than it takes to send a byte, that means it's
356 * finished. We'll double that to 30ms to be sure...
357 */
102f1239
BV
358 serial_source_remove(sdi->session, serial);
359 serial_source_add(sdi->session, serial, G_IO_IN, 30,
360 ols_receive_data, cb_data);
0aba65da
UH
361 devc->raw_sample_buf = g_try_malloc(devc->limit_samples * 4);
362 if (!devc->raw_sample_buf) {
363 sr_err("Sample buffer malloc failed.");
364 return FALSE;
365 }
366 /* fill with 1010... for debugging */
367 memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4);
368 }
369
91fd0f72 370 num_ols_changrp = 0;
3f239f08 371 for (i = NUM_CHANNELS; i > 0x02; i /= 2) {
f51acd69 372 if ((devc->flag_reg & i) == 0) {
91fd0f72 373 num_ols_changrp++;
f51acd69 374 }
0aba65da
UH
375 }
376
faf72024 377 if (revents == G_IO_IN && devc->num_samples < devc->limit_samples) {
9f5d4c3c 378 if (serial_read_nonblocking(serial, &byte, 1) != 1)
0aba65da 379 return FALSE;
625763e2 380 devc->cnt_bytes++;
0aba65da
UH
381
382 /* Ignore it if we've read enough. */
383 if (devc->num_samples >= devc->limit_samples)
384 return TRUE;
385
386 devc->sample[devc->num_bytes++] = byte;
6d16fdfb 387 sr_spew("Received byte 0x%.2x.", byte);
91fd0f72 388 if (devc->num_bytes == num_ols_changrp) {
625763e2
BV
389 devc->cnt_samples++;
390 devc->cnt_samples_rle++;
391 /*
392 * Got a full sample. Convert from the OLS's little-endian
393 * sample to the local format.
394 */
fe9ac252
BV
395 sample = devc->sample[0] | (devc->sample[1] << 8) \
396 | (devc->sample[2] << 16) | (devc->sample[3] << 24);
625763e2 397 sr_dbg("Received sample 0x%.*x.", devc->num_bytes * 2, sample);
0aba65da
UH
398 if (devc->flag_reg & FLAG_RLE) {
399 /*
00d04d3b
BV
400 * In RLE mode the high bit of the sample is the
401 * "count" flag, meaning this sample is the number
402 * of times the previous sample occurred.
0aba65da
UH
403 */
404 if (devc->sample[devc->num_bytes - 1] & 0x80) {
00d04d3b
BV
405 /* Clear the high bit. */
406 sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
fe9ac252 407 devc->rle_count = sample;
625763e2 408 devc->cnt_samples_rle += devc->rle_count;
00d04d3b 409 sr_dbg("RLE count: %u.", devc->rle_count);
0aba65da
UH
410 devc->num_bytes = 0;
411 return TRUE;
412 }
413 }
414 devc->num_samples += devc->rle_count + 1;
415 if (devc->num_samples > devc->limit_samples) {
416 /* Save us from overrunning the buffer. */
417 devc->rle_count -= devc->num_samples - devc->limit_samples;
418 devc->num_samples = devc->limit_samples;
419 }
420
91fd0f72 421 if (num_ols_changrp < 4) {
0aba65da
UH
422 /*
423 * Some channel groups may have been turned
424 * off, to speed up transfer between the
425 * hardware and the PC. Expand that here before
426 * submitting it over the session bus --
427 * whatever is listening on the bus will be
428 * expecting a full 32-bit sample, based on
ba7dd8bb 429 * the number of channels.
0aba65da
UH
430 */
431 j = 0;
432 memset(devc->tmp_sample, 0, 4);
433 for (i = 0; i < 4; i++) {
434 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
435 /*
436 * This channel group was
437 * enabled, copy from received
438 * sample.
439 */
440 devc->tmp_sample[i] = devc->sample[j++];
f51acd69 441 } else if (devc->flag_reg & FLAG_DEMUX && (i > 2)) {
b1de0407 442 /* group 2 & 3 get added to 0 & 1 */
f51acd69 443 devc->tmp_sample[i - 2] = devc->sample[j++];
0aba65da
UH
444 }
445 }
446 memcpy(devc->sample, devc->tmp_sample, 4);
625763e2 447 sr_spew("Expanded sample: 0x%.8x.", sample);
0aba65da
UH
448 }
449
625763e2
BV
450 /*
451 * the OLS sends its sample buffer backwards.
0aba65da
UH
452 * store it in reverse order here, so we can dump
453 * this on the session bus later.
454 */
455 offset = (devc->limit_samples - devc->num_samples) * 4;
456 for (i = 0; i <= devc->rle_count; i++) {
457 memcpy(devc->raw_sample_buf + offset + (i * 4),
458 devc->sample, 4);
459 }
460 memset(devc->sample, 0, 4);
461 devc->num_bytes = 0;
462 devc->rle_count = 0;
463 }
464 } else {
465 /*
466 * This is the main loop telling us a timeout was reached, or
467 * we've acquired all the samples we asked for -- we're done.
468 * Send the (properly-ordered) buffer to the frontend.
469 */
625763e2
BV
470 sr_dbg("Received %d bytes, %d samples, %d decompressed samples.",
471 devc->cnt_bytes, devc->cnt_samples,
472 devc->cnt_samples_rle);
0aba65da 473 if (devc->trigger_at != -1) {
625763e2
BV
474 /*
475 * A trigger was set up, so we need to tell the frontend
0aba65da
UH
476 * about it.
477 */
478 if (devc->trigger_at > 0) {
625763e2 479 /* There are pre-trigger samples, send those first. */
0aba65da
UH
480 packet.type = SR_DF_LOGIC;
481 packet.payload = &logic;
482 logic.length = devc->trigger_at * 4;
483 logic.unitsize = 4;
484 logic.data = devc->raw_sample_buf +
485 (devc->limit_samples - devc->num_samples) * 4;
486 sr_session_send(cb_data, &packet);
487 }
488
625763e2 489 /* Send the trigger. */
0aba65da
UH
490 packet.type = SR_DF_TRIGGER;
491 sr_session_send(cb_data, &packet);
492
625763e2 493 /* Send post-trigger samples. */
0aba65da
UH
494 packet.type = SR_DF_LOGIC;
495 packet.payload = &logic;
496 logic.length = (devc->num_samples * 4) - (devc->trigger_at * 4);
497 logic.unitsize = 4;
498 logic.data = devc->raw_sample_buf + devc->trigger_at * 4 +
499 (devc->limit_samples - devc->num_samples) * 4;
500 sr_session_send(cb_data, &packet);
501 } else {
502 /* no trigger was used */
503 packet.type = SR_DF_LOGIC;
504 packet.payload = &logic;
505 logic.length = devc->num_samples * 4;
506 logic.unitsize = 4;
507 logic.data = devc->raw_sample_buf +
508 (devc->limit_samples - devc->num_samples) * 4;
509 sr_session_send(cb_data, &packet);
510 }
511 g_free(devc->raw_sample_buf);
512
459a0f26 513 serial_flush(serial);
0aba65da 514 abort_acquisition(sdi);
0aba65da
UH
515 }
516
517 return TRUE;
518}