]> sigrok.org Git - libsigrok.git/blob - src/hardware/openbench-logic-sniffer/protocol.c
ols: Fix serial port timeout.
[libsigrok.git] / src / hardware / openbench-logic-sniffer / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 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 "protocol.h"
21 #include <libserialport.h>
22
23 extern SR_PRIV struct sr_dev_driver ols_driver_info;
24 static struct sr_dev_driver *di = &ols_driver_info;
25
26 SR_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;
33         if (serial_write_blocking(serial, buf, 1, serial_timeout(serial, 1)) != 1)
34                 return SR_ERR;
35
36         return SR_OK;
37 }
38
39 SR_PRIV int send_longcommand(struct sr_serial_dev_inst *serial,
40                 uint8_t command, uint8_t *data)
41 {
42         char buf[5];
43
44         sr_dbg("Sending cmd 0x%.2x data 0x%.2x%.2x%.2x%.2x.", command,
45                         data[0], data[1], data[2], data[3]);
46         buf[0] = command;
47         buf[1] = data[0];
48         buf[2] = data[1];
49         buf[3] = data[2];
50         buf[4] = data[3];
51         if (serial_write_blocking(serial, buf, 5, serial_timeout(serial, 1)) != 5)
52                 return SR_ERR;
53
54         return SR_OK;
55 }
56
57 /* Configures the channel mask based on which channels are enabled. */
58 SR_PRIV void ols_channel_mask(const struct sr_dev_inst *sdi)
59 {
60         struct dev_context *devc;
61         struct sr_channel *channel;
62         const GSList *l;
63
64         devc = sdi->priv;
65
66         devc->channel_mask = 0;
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
74 SR_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;
86         for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
87                 devc->trigger_mask[i] = 0;
88                 devc->trigger_value[i] = 0;
89         }
90
91         if (!(trigger = sr_session_trigger_get(sdi->session)))
92                 return SR_OK;
93
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         }
100
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;
111                 }
112         }
113
114         return SR_OK;
115 }
116
117 SR_PRIV struct dev_context *ols_dev_new(void)
118 {
119         struct dev_context *devc;
120
121         if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
122                 sr_err("Device context malloc failed.");
123                 return NULL;
124         }
125
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;
131         devc->trigger_at = -1;
132         devc->channel_mask = 0xffffffff;
133         devc->flag_reg = 0;
134
135         return devc;
136 }
137
138 SR_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;
142         struct sr_channel *ch;
143         uint32_t tmp_int, ui;
144         uint8_t key, type, token;
145         int delay_ms;
146         GString *tmp_str, *devname, *version;
147         guchar tmp_c;
148
149         sdi = sr_dev_inst_new(SR_ST_INACTIVE, NULL, NULL, NULL);
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) {
159                 delay_ms = serial_timeout(serial, 1);
160                 if (serial_read_blocking(serial, &key, 1, delay_ms) != 1)
161                         break;
162                 if (key == 0x00) {
163                         sr_dbg("Got metadata key 0x00, metadata ends.");
164                         break;
165                 }
166                 type = key >> 5;
167                 token = key & 0x1f;
168                 switch (type) {
169                 case 0:
170                         /* NULL-terminated string */
171                         tmp_str = g_string_new("");
172                         delay_ms = serial_timeout(serial, 1);
173                         while (serial_read_blocking(serial, &tmp_c, 1, delay_ms) == 1 && tmp_c != '\0')
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 */
205                         delay_ms = serial_timeout(serial, 4);
206                         if (serial_read_blocking(serial, &tmp_int, 4, delay_ms) != 4)
207                                 break;
208                         tmp_int = RB32(&tmp_int);
209                         sr_dbg("Got metadata key 0x%.2x value 0x%.8x.",
210                                key, tmp_int);
211                         switch (token) {
212                         case 0x00:
213                                 /* Number of usable channels */
214                                 for (ui = 0; ui < tmp_int; ui++) {
215                                         if (!(ch = sr_channel_new(ui, SR_CHANNEL_LOGIC, TRUE,
216                                                         ols_channel_names[ui])))
217                                                 return 0;
218                                         sdi->channels = g_slist_append(sdi->channels, ch);
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 */
245                         delay_ms = serial_timeout(serial, 1);
246                         if (serial_read_blocking(serial, &tmp_c, 1, delay_ms) != 1)
247                                 break;
248                         sr_dbg("Got metadata key 0x%.2x value 0x%.2x.",
249                                key, tmp_c);
250                         switch (token) {
251                         case 0x00:
252                                 /* Number of usable channels */
253                                 for (ui = 0; ui < tmp_c; ui++) {
254                                         if (!(ch = sr_channel_new(ui, SR_CHANNEL_LOGIC, TRUE,
255                                                         ols_channel_names[ui])))
256                                                 return 0;
257                                         sdi->channels = g_slist_append(sdi->channels, ch);
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
284 SR_PRIV int ols_set_samplerate(const struct sr_dev_inst *sdi,
285                 const uint64_t samplerate)
286 {
287         struct dev_context *devc;
288
289         devc = sdi->priv;
290         if (devc->max_samplerate && samplerate > devc->max_samplerate)
291                 return SR_ERR_SAMPLERATE;
292
293         if (samplerate > CLOCK_RATE) {
294                 sr_info("Enabling demux mode.");
295                 devc->flag_reg |= FLAG_DEMUX;
296                 devc->flag_reg &= ~FLAG_FILTER;
297                 devc->max_channels = NUM_CHANNELS / 2;
298                 devc->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
299         } else {
300                 sr_info("Disabling demux mode.");
301                 devc->flag_reg &= ~FLAG_DEMUX;
302                 devc->flag_reg |= FLAG_FILTER;
303                 devc->max_channels = NUM_CHANNELS;
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)
314                 sr_info("Can't match samplerate %" PRIu64 ", using %"
315                        PRIu64 ".", samplerate, devc->cur_samplerate);
316
317         return SR_OK;
318 }
319
320 SR_PRIV void abort_acquisition(const struct sr_dev_inst *sdi)
321 {
322         struct sr_datafeed_packet packet;
323         struct sr_serial_dev_inst *serial;
324
325         serial = sdi->conn;
326         serial_source_remove(sdi->session, serial);
327
328         /* Terminate session */
329         packet.type = SR_DF_END;
330         sr_session_send(sdi, &packet);
331 }
332
333 SR_PRIV int ols_receive_data(int fd, int revents, void *cb_data)
334 {
335         struct dev_context *devc;
336         struct sr_dev_inst *sdi;
337         struct sr_serial_dev_inst *serial;
338         struct sr_datafeed_packet packet;
339         struct sr_datafeed_logic logic;
340         uint32_t sample;
341         int num_ols_changrp, offset, j;
342         unsigned int i;
343         unsigned char byte;
344
345         (void)fd;
346
347         sdi = cb_data;
348         serial = sdi->conn;
349         devc = sdi->priv;
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                  */
358                 serial_source_remove(sdi->session, serial);
359                 serial_source_add(sdi->session, serial, G_IO_IN, 30,
360                                 ols_receive_data, cb_data);
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
370         num_ols_changrp = 0;
371         for (i = NUM_CHANNELS; i > 0x02; i /= 2) {
372                 if ((devc->flag_reg & i) == 0) {
373                         num_ols_changrp++;
374                 }
375         }
376
377         if (revents == G_IO_IN && devc->num_samples < devc->limit_samples) {
378                 if (serial_read_nonblocking(serial, &byte, 1) != 1)
379                         return FALSE;
380                 devc->cnt_bytes++;
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;
387                 sr_spew("Received byte 0x%.2x.", byte);
388                 if (devc->num_bytes == num_ols_changrp) {
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                          */
395                         sample = devc->sample[0] | (devc->sample[1] << 8) \
396                                         | (devc->sample[2] << 16) | (devc->sample[3] << 24);
397                         sr_dbg("Received sample 0x%.*x.", devc->num_bytes * 2, sample);
398                         if (devc->flag_reg & FLAG_RLE) {
399                                 /*
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.
403                                  */
404                                 if (devc->sample[devc->num_bytes - 1] & 0x80) {
405                                         /* Clear the high bit. */
406                                         sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
407                                         devc->rle_count = sample;
408                                         devc->cnt_samples_rle += devc->rle_count;
409                                         sr_dbg("RLE count: %u.", devc->rle_count);
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
421                         if (num_ols_changrp < 4) {
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
429                                  * the number of channels.
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++];
441                                         } else if (devc->flag_reg & FLAG_DEMUX && (i > 2)) {
442                                                 /* group 2 & 3 get added to 0 & 1 */
443                                                 devc->tmp_sample[i - 2] = devc->sample[j++];
444                                         }
445                                 }
446                                 memcpy(devc->sample, devc->tmp_sample, 4);
447                                 sr_spew("Expanded sample: 0x%.8x.", sample);
448                         }
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                 sr_dbg("Received %d bytes, %d samples, %d decompressed samples.",
471                                 devc->cnt_bytes, devc->cnt_samples,
472                                 devc->cnt_samples_rle);
473                 if (devc->trigger_at != -1) {
474                         /*
475                          * A trigger was set up, so we need to tell the frontend
476                          * about it.
477                          */
478                         if (devc->trigger_at > 0) {
479                                 /* There are pre-trigger samples, send those first. */
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
489                         /* Send the trigger. */
490                         packet.type = SR_DF_TRIGGER;
491                         sr_session_send(cb_data, &packet);
492
493                         /* Send post-trigger samples. */
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
513                 serial_flush(serial);
514                 abort_acquisition(sdi);
515         }
516
517         return TRUE;
518 }