]> sigrok.org Git - libsigrok.git/blame - hardware/openbench-logic-sniffer/protocol.c
GPL headers: Use correct project name.
[libsigrok.git] / 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"
21
2239728c 22extern SR_PRIV struct sr_dev_driver ols_driver_info;
0aba65da
UH
23static struct sr_dev_driver *di = &ols_driver_info;
24
25SR_PRIV int send_shortcommand(struct sr_serial_dev_inst *serial,
26 uint8_t command)
27{
28 char buf[1];
29
30 sr_dbg("Sending cmd 0x%.2x.", command);
31 buf[0] = command;
32 if (serial_write(serial, buf, 1) != 1)
33 return SR_ERR;
34
35 return SR_OK;
36}
37
38SR_PRIV int send_longcommand(struct sr_serial_dev_inst *serial,
39 uint8_t command, uint32_t data)
40{
41 char buf[5];
42
43 sr_dbg("Sending cmd 0x%.2x data 0x%.8x.", command, data);
44 buf[0] = command;
45 buf[1] = (data & 0xff000000) >> 24;
46 buf[2] = (data & 0xff0000) >> 16;
47 buf[3] = (data & 0xff00) >> 8;
48 buf[4] = data & 0xff;
49 if (serial_write(serial, buf, 5) != 5)
50 return SR_ERR;
51
52 return SR_OK;
53}
54
55SR_PRIV int ols_configure_probes(const struct sr_dev_inst *sdi)
56{
57 struct dev_context *devc;
58 const struct sr_probe *probe;
59 const GSList *l;
60 int probe_bit, stage, i;
61 char *tc;
62
63 devc = sdi->priv;
64
65 devc->probe_mask = 0;
66 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
67 devc->trigger_mask[i] = 0;
68 devc->trigger_value[i] = 0;
69 }
70
71 devc->num_stages = 0;
72 for (l = sdi->probes; l; l = l->next) {
73 probe = (const struct sr_probe *)l->data;
74 if (!probe->enabled)
75 continue;
76
77 /*
78 * Set up the probe mask for later configuration into the
79 * flag register.
80 */
81 probe_bit = 1 << (probe->index);
82 devc->probe_mask |= probe_bit;
83
84 if (!probe->trigger)
85 continue;
86
87 /* Configure trigger mask and value. */
88 stage = 0;
89 for (tc = probe->trigger; tc && *tc; tc++) {
90 devc->trigger_mask[stage] |= probe_bit;
91 if (*tc == '1')
92 devc->trigger_value[stage] |= probe_bit;
93 stage++;
94 if (stage > 3)
95 /*
96 * TODO: Only supporting parallel mode, with
97 * up to 4 stages.
98 */
99 return SR_ERR;
100 }
101 if (stage > devc->num_stages)
102 devc->num_stages = stage;
103 }
104
105 return SR_OK;
106}
107
108SR_PRIV uint32_t reverse16(uint32_t in)
109{
110 uint32_t out;
111
112 out = (in & 0xff) << 8;
113 out |= (in & 0xff00) >> 8;
114 out |= (in & 0xff0000) << 8;
115 out |= (in & 0xff000000) >> 8;
116
117 return out;
118}
119
120SR_PRIV uint32_t reverse32(uint32_t in)
121{
122 uint32_t out;
123
124 out = (in & 0xff) << 24;
125 out |= (in & 0xff00) << 8;
126 out |= (in & 0xff0000) >> 8;
127 out |= (in & 0xff000000) >> 24;
128
129 return out;
130}
131
132SR_PRIV struct dev_context *ols_dev_new(void)
133{
134 struct dev_context *devc;
135
bf256783 136 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
0aba65da
UH
137 sr_err("Device context malloc failed.");
138 return NULL;
139 }
140
bf256783
BV
141 /* Device-specific settings */
142 devc->max_samples = devc->max_samplerate = devc->protocol_version = 0;
143
144 /* Acquisition settings */
145 devc->limit_samples = devc->capture_ratio = 0;
0aba65da
UH
146 devc->trigger_at = -1;
147 devc->probe_mask = 0xffffffff;
bf256783
BV
148 devc->flag_reg = 0;
149
0aba65da
UH
150 return devc;
151}
152
153SR_PRIV struct sr_dev_inst *get_metadata(struct sr_serial_dev_inst *serial)
154{
155 struct sr_dev_inst *sdi;
156 struct dev_context *devc;
157 struct sr_probe *probe;
158 uint32_t tmp_int, ui;
159 uint8_t key, type, token;
160 GString *tmp_str, *devname, *version;
161 guchar tmp_c;
162
163 sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, NULL, NULL, NULL);
164 sdi->driver = di;
165 devc = ols_dev_new();
166 sdi->priv = devc;
167
168 devname = g_string_new("");
169 version = g_string_new("");
170
171 key = 0xff;
172 while (key) {
173 if (serial_read(serial, &key, 1) != 1 || key == 0x00)
174 break;
175 type = key >> 5;
176 token = key & 0x1f;
177 switch (type) {
178 case 0:
179 /* NULL-terminated string */
180 tmp_str = g_string_new("");
181 while (serial_read(serial, &tmp_c, 1) == 1 && tmp_c != '\0')
182 g_string_append_c(tmp_str, tmp_c);
183 sr_dbg("Got metadata key 0x%.2x value '%s'.",
184 key, tmp_str->str);
185 switch (token) {
186 case 0x01:
187 /* Device name */
188 devname = g_string_append(devname, tmp_str->str);
189 break;
190 case 0x02:
191 /* FPGA firmware version */
192 if (version->len)
193 g_string_append(version, ", ");
194 g_string_append(version, "FPGA version ");
195 g_string_append(version, tmp_str->str);
196 break;
197 case 0x03:
198 /* Ancillary version */
199 if (version->len)
200 g_string_append(version, ", ");
201 g_string_append(version, "Ancillary version ");
202 g_string_append(version, tmp_str->str);
203 break;
204 default:
205 sr_info("ols: unknown token 0x%.2x: '%s'",
206 token, tmp_str->str);
207 break;
208 }
209 g_string_free(tmp_str, TRUE);
210 break;
211 case 1:
212 /* 32-bit unsigned integer */
213 if (serial_read(serial, &tmp_int, 4) != 4)
214 break;
215 tmp_int = reverse32(tmp_int);
216 sr_dbg("Got metadata key 0x%.2x value 0x%.8x.",
217 key, tmp_int);
218 switch (token) {
219 case 0x00:
220 /* Number of usable probes */
221 for (ui = 0; ui < tmp_int; ui++) {
222 if (!(probe = sr_probe_new(ui, SR_PROBE_LOGIC, TRUE,
223 ols_probe_names[ui])))
224 return 0;
225 sdi->probes = g_slist_append(sdi->probes, probe);
226 }
227 break;
228 case 0x01:
229 /* Amount of sample memory available (bytes) */
230 devc->max_samples = tmp_int;
231 break;
232 case 0x02:
233 /* Amount of dynamic memory available (bytes) */
234 /* what is this for? */
235 break;
236 case 0x03:
237 /* Maximum sample rate (hz) */
238 devc->max_samplerate = tmp_int;
239 break;
240 case 0x04:
241 /* protocol version */
242 devc->protocol_version = tmp_int;
243 break;
244 default:
245 sr_info("Unknown token 0x%.2x: 0x%.8x.",
246 token, tmp_int);
247 break;
248 }
249 break;
250 case 2:
251 /* 8-bit unsigned integer */
252 if (serial_read(serial, &tmp_c, 1) != 1)
253 break;
254 sr_dbg("Got metadata key 0x%.2x value 0x%.2x.",
255 key, tmp_c);
256 switch (token) {
257 case 0x00:
258 /* Number of usable probes */
259 for (ui = 0; ui < tmp_c; ui++) {
260 if (!(probe = sr_probe_new(ui, SR_PROBE_LOGIC, TRUE,
261 ols_probe_names[ui])))
262 return 0;
263 sdi->probes = g_slist_append(sdi->probes, probe);
264 }
265 break;
266 case 0x01:
267 /* protocol version */
268 devc->protocol_version = tmp_c;
269 break;
270 default:
271 sr_info("Unknown token 0x%.2x: 0x%.2x.",
272 token, tmp_c);
273 break;
274 }
275 break;
276 default:
277 /* unknown type */
278 break;
279 }
280 }
281
282 sdi->model = devname->str;
283 sdi->version = version->str;
284 g_string_free(devname, FALSE);
285 g_string_free(version, FALSE);
286
287 return sdi;
288}
289
290SR_PRIV int ols_set_samplerate(const struct sr_dev_inst *sdi,
e46aa4f6 291 const uint64_t samplerate)
0aba65da
UH
292{
293 struct dev_context *devc;
294
295 devc = sdi->priv;
e46aa4f6 296 if (devc->max_samplerate && samplerate > devc->max_samplerate)
0aba65da
UH
297 return SR_ERR_SAMPLERATE;
298
299 if (samplerate > CLOCK_RATE) {
300 devc->flag_reg |= FLAG_DEMUX;
301 devc->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
302 } else {
303 devc->flag_reg &= ~FLAG_DEMUX;
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
BV
325 serial = sdi->conn;
326 sr_source_remove(serial->fd);
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
BV
335 struct drv_context *drvc;
336 struct dev_context *devc;
337 struct sr_serial_dev_inst *serial;
0aba65da
UH
338 struct sr_datafeed_packet packet;
339 struct sr_datafeed_logic logic;
340 struct sr_dev_inst *sdi;
0aba65da 341 GSList *l;
fe9ac252 342 uint32_t sample;
0aba65da
UH
343 int num_channels, offset, i, j;
344 unsigned char byte;
345
346 drvc = di->priv;
347
348 /* Find this device's devc struct by its fd. */
349 devc = NULL;
350 for (l = drvc->instances; l; l = l->next) {
351 sdi = l->data;
352 devc = sdi->priv;
459a0f26
BV
353 serial = sdi->conn;
354 if (serial->fd == fd)
0aba65da
UH
355 break;
356 devc = NULL;
357 }
358 if (!devc)
359 /* Shouldn't happen. */
360 return TRUE;
361
362 if (devc->num_transfers++ == 0) {
363 /*
364 * First time round, means the device started sending data,
365 * and will not stop until done. If it stops sending for
366 * longer than it takes to send a byte, that means it's
367 * finished. We'll double that to 30ms to be sure...
368 */
369 sr_source_remove(fd);
370 sr_source_add(fd, G_IO_IN, 30, ols_receive_data, cb_data);
371 devc->raw_sample_buf = g_try_malloc(devc->limit_samples * 4);
372 if (!devc->raw_sample_buf) {
373 sr_err("Sample buffer malloc failed.");
374 return FALSE;
375 }
376 /* fill with 1010... for debugging */
377 memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4);
378 }
379
380 num_channels = 0;
381 for (i = 0x20; i > 0x02; i /= 2) {
382 if ((devc->flag_reg & i) == 0)
383 num_channels++;
384 }
385
386 if (revents == G_IO_IN) {
459a0f26 387 if (serial_read(serial, &byte, 1) != 1)
0aba65da
UH
388 return FALSE;
389
390 /* Ignore it if we've read enough. */
391 if (devc->num_samples >= devc->limit_samples)
392 return TRUE;
393
394 devc->sample[devc->num_bytes++] = byte;
395 sr_dbg("Received byte 0x%.2x.", byte);
396 if (devc->num_bytes == num_channels) {
397 /* Got a full sample. */
fe9ac252
BV
398 sample = devc->sample[0] | (devc->sample[1] << 8) \
399 | (devc->sample[2] << 16) | (devc->sample[3] << 24);
400 sr_dbg("Received sample 0x%.*x.", devc->num_bytes * 2, sample);
0aba65da
UH
401 if (devc->flag_reg & FLAG_RLE) {
402 /*
403 * In RLE mode -1 should never come in as a
404 * sample, because bit 31 is the "count" flag.
405 */
406 if (devc->sample[devc->num_bytes - 1] & 0x80) {
407 devc->sample[devc->num_bytes - 1] &= 0x7f;
408 /*
409 * FIXME: This will only work on
410 * little-endian systems.
411 */
fe9ac252 412 devc->rle_count = sample;
0aba65da
UH
413 sr_dbg("RLE count: %d.", devc->rle_count);
414 devc->num_bytes = 0;
415 return TRUE;
416 }
417 }
418 devc->num_samples += devc->rle_count + 1;
419 if (devc->num_samples > devc->limit_samples) {
420 /* Save us from overrunning the buffer. */
421 devc->rle_count -= devc->num_samples - devc->limit_samples;
422 devc->num_samples = devc->limit_samples;
423 }
424
425 if (num_channels < 4) {
426 /*
427 * Some channel groups may have been turned
428 * off, to speed up transfer between the
429 * hardware and the PC. Expand that here before
430 * submitting it over the session bus --
431 * whatever is listening on the bus will be
432 * expecting a full 32-bit sample, based on
433 * the number of probes.
434 */
435 j = 0;
436 memset(devc->tmp_sample, 0, 4);
437 for (i = 0; i < 4; i++) {
438 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
439 /*
440 * This channel group was
441 * enabled, copy from received
442 * sample.
443 */
444 devc->tmp_sample[i] = devc->sample[j++];
445 }
446 }
447 memcpy(devc->sample, devc->tmp_sample, 4);
fe9ac252 448 sr_dbg("Full sample: 0x%.8x.", sample);
0aba65da
UH
449 }
450
451 /* the OLS sends its sample buffer backwards.
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 */
470 if (devc->trigger_at != -1) {
471 /* a trigger was set up, so we need to tell the frontend
472 * about it.
473 */
474 if (devc->trigger_at > 0) {
475 /* there are pre-trigger samples, send those first */
476 packet.type = SR_DF_LOGIC;
477 packet.payload = &logic;
478 logic.length = devc->trigger_at * 4;
479 logic.unitsize = 4;
480 logic.data = devc->raw_sample_buf +
481 (devc->limit_samples - devc->num_samples) * 4;
482 sr_session_send(cb_data, &packet);
483 }
484
485 /* send the trigger */
486 packet.type = SR_DF_TRIGGER;
487 sr_session_send(cb_data, &packet);
488
489 /* send post-trigger samples */
490 packet.type = SR_DF_LOGIC;
491 packet.payload = &logic;
492 logic.length = (devc->num_samples * 4) - (devc->trigger_at * 4);
493 logic.unitsize = 4;
494 logic.data = devc->raw_sample_buf + devc->trigger_at * 4 +
495 (devc->limit_samples - devc->num_samples) * 4;
496 sr_session_send(cb_data, &packet);
497 } else {
498 /* no trigger was used */
499 packet.type = SR_DF_LOGIC;
500 packet.payload = &logic;
501 logic.length = devc->num_samples * 4;
502 logic.unitsize = 4;
503 logic.data = devc->raw_sample_buf +
504 (devc->limit_samples - devc->num_samples) * 4;
505 sr_session_send(cb_data, &packet);
506 }
507 g_free(devc->raw_sample_buf);
508
459a0f26 509 serial_flush(serial);
0aba65da 510 abort_acquisition(sdi);
459a0f26 511 serial_close(serial);
0aba65da
UH
512 }
513
514 return TRUE;
515}