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