]> sigrok.org Git - libsigrok.git/blob - src/hardware/pipistrello-ols/protocol.c
66430acb9544ad25f6e148e4d3b895d86ef48e2e
[libsigrok.git] / src / hardware / pipistrello-ols / 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 <config.h>
21 #include "protocol.h"
22
23 SR_PRIV int write_shortcommand(struct dev_context *devc, uint8_t command)
24 {
25         uint8_t buf[1];
26         int bytes_written;
27
28         sr_dbg("Sending cmd 0x%.2x.", command);
29         buf[0] = command;
30         bytes_written = ftdi_write_data(devc->ftdic, buf, 1);
31         if (bytes_written < 0) {
32                 sr_err("Failed to write FTDI data (%d): %s.",
33                        bytes_written, ftdi_get_error_string(devc->ftdic));
34                 return SR_ERR;
35         } else if (bytes_written != 1) {
36                 sr_err("FTDI write error, only %d/%d bytes written: %s.",
37                        bytes_written, 1, ftdi_get_error_string(devc->ftdic));
38                 return SR_ERR;
39         }
40
41         return SR_OK;
42 }
43
44 SR_PRIV int write_longcommand(struct dev_context *devc, uint8_t command, uint8_t *data)
45 {
46         uint8_t buf[5];
47         int bytes_written;
48
49         sr_dbg("Sending cmd 0x%.2x data 0x%.2x%.2x%.2x%.2x.", command,
50                         data[0], data[1], data[2], data[3]);
51         buf[0] = command;
52         buf[1] = data[0];
53         buf[2] = data[1];
54         buf[3] = data[2];
55         buf[4] = data[3];
56         bytes_written = ftdi_write_data(devc->ftdic, buf, 5);
57         if (bytes_written < 0) {
58                 sr_err("Failed to write FTDI data (%d): %s.",
59                        bytes_written, ftdi_get_error_string(devc->ftdic));
60                 return SR_ERR;
61         } else if (bytes_written != 5) {
62                 sr_err("FTDI write error, only %d/%d bytes written: %s.",
63                        bytes_written, 1, ftdi_get_error_string(devc->ftdic));
64                 return SR_ERR;
65         }
66
67         return SR_OK;
68 }
69
70 SR_PRIV int p_ols_open(struct dev_context *devc)
71 {
72         int ret;
73
74         /* Note: Caller checks devc and devc->ftdic. */
75
76         ret = ftdi_set_interface(devc->ftdic, INTERFACE_B);
77         if (ret < 0) {
78                 sr_err("Failed to set FTDI interface B (%d): %s", ret,
79                        ftdi_get_error_string(devc->ftdic));
80                 return SR_ERR;
81         }
82
83         ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, USB_DEVICE_ID,
84                                  USB_IPRODUCT, NULL);
85         if (ret < 0) {
86                 /* Log errors, except for -3 ("device not found"). */
87                 if (ret != -3)
88                         sr_err("Failed to open device (%d): %s", ret,
89                                ftdi_get_error_string(devc->ftdic));
90                 return SR_ERR;
91         }
92
93         if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) {
94                 sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.",
95                        ret, ftdi_get_error_string(devc->ftdic));
96                 goto err_open_close_ftdic;
97         }
98
99         ret = ftdi_set_bitmode(devc->ftdic, 0xff, BITMODE_RESET);
100         if (ret < 0) {
101                 sr_err("Failed to reset the FTDI chip bitmode (%d): %s.",
102                        ret, ftdi_get_error_string(devc->ftdic));
103                 goto err_open_close_ftdic;
104         }
105
106         ret = ftdi_set_latency_timer(devc->ftdic, 16);
107         if (ret < 0) {
108                 sr_err("Failed to set FTDI latency timer (%d): %s.",
109                        ret, ftdi_get_error_string(devc->ftdic));
110                 goto err_open_close_ftdic;
111         }
112
113         ret = ftdi_read_data_set_chunksize(devc->ftdic, 64 * 1024);
114         if (ret < 0) {
115                 sr_err("Failed to set FTDI read data chunk size (%d): %s.",
116                        ret, ftdi_get_error_string(devc->ftdic));
117                 goto err_open_close_ftdic;
118         }
119
120         return SR_OK;
121
122 err_open_close_ftdic:
123         ftdi_usb_close(devc->ftdic);
124
125         return SR_ERR;
126 }
127
128 SR_PRIV int p_ols_close(struct dev_context *devc)
129 {
130         int ret;
131
132         /* Note: Caller checks devc and devc->ftdic. */
133
134         if ((ret = ftdi_usb_close(devc->ftdic)) < 0) {
135                 sr_err("Failed to close FTDI device (%d): %s.",
136                        ret, ftdi_get_error_string(devc->ftdic));
137                 return SR_ERR;
138         }
139
140         return SR_OK;
141 }
142
143 /* Configures the channel mask based on which channels are enabled. */
144 SR_PRIV void pols_channel_mask(const struct sr_dev_inst *sdi)
145 {
146         struct dev_context *devc;
147         struct sr_channel *channel;
148         const GSList *l;
149
150         devc = sdi->priv;
151
152         devc->channel_mask = 0;
153         for (l = sdi->channels; l; l = l->next) {
154                 channel = l->data;
155                 if (channel->enabled)
156                         devc->channel_mask |= 1 << channel->index;
157         }
158 }
159
160 SR_PRIV int pols_convert_trigger(const struct sr_dev_inst *sdi)
161 {
162         struct dev_context *devc;
163         struct sr_trigger *trigger;
164         struct sr_trigger_stage *stage;
165         struct sr_trigger_match *match;
166         const GSList *l, *m;
167         int i;
168
169         devc = sdi->priv;
170
171         devc->num_stages = 0;
172         for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
173                 devc->trigger_mask[i] = 0;
174                 devc->trigger_value[i] = 0;
175                 devc->trigger_edge[i] = 0;
176         }
177
178         if (!(trigger = sr_session_trigger_get(sdi->session)))
179                 return SR_OK;
180
181         devc->num_stages = g_slist_length(trigger->stages);
182         if (devc->num_stages > NUM_TRIGGER_STAGES) {
183                 sr_err("This device only supports %d trigger stages.",
184                                 NUM_TRIGGER_STAGES);
185                 return SR_ERR;
186         }
187
188         for (l = trigger->stages; l; l = l->next) {
189                 stage = l->data;
190                 for (m = stage->matches; m; m = m->next) {
191                         match = m->data;
192                         if (!match->channel->enabled)
193                                 /* Ignore disabled channels with a trigger. */
194                                 continue;
195                         devc->trigger_mask[stage->stage] |= 1 << match->channel->index;
196                         if (match->match == SR_TRIGGER_ONE || match->match == SR_TRIGGER_RISING)
197                                 devc->trigger_value[stage->stage] |= 1 << match->channel->index;
198                         if (match->match == SR_TRIGGER_RISING || match->match == SR_TRIGGER_FALLING)
199                                 devc->trigger_edge[stage->stage] |= 1 << match->channel->index;
200                 }
201         }
202
203         return SR_OK;
204 }
205
206 SR_PRIV struct sr_dev_inst *p_ols_get_metadata(uint8_t *buf, int bytes_read, struct dev_context *devc)
207 {
208         struct sr_dev_inst *sdi;
209         uint32_t tmp_int, ui;
210         uint8_t key, type, token;
211         GString *tmp_str, *devname, *version;
212         guchar tmp_c;
213         int index, i;
214
215         sdi = g_malloc0(sizeof(struct sr_dev_inst));
216         sdi->status = SR_ST_INACTIVE;
217         sdi->priv = devc;
218
219         devname = g_string_new("");
220         version = g_string_new("");
221
222         index = 0;
223         while (index < bytes_read) {
224                 key = buf[index++];
225                 if (key == 0x00) {
226                         sr_dbg("Got metadata key 0x00, metadata ends.");
227                         break;
228                 }
229                 type = key >> 5;
230                 token = key & 0x1f;
231                 switch (type) {
232                 case 0:
233                         /* NULL-terminated string */
234                         tmp_str = g_string_new("");
235                         while ((index < bytes_read) && ((tmp_c = buf[index++]) != '\0'))
236                                 g_string_append_c(tmp_str, tmp_c);
237                         sr_dbg("Got metadata key 0x%.2x value '%s'.",
238                                key, tmp_str->str);
239                         switch (token) {
240                         case 0x01:
241                                 /* Device name */
242                                 devname = g_string_append(devname, tmp_str->str);
243                                 break;
244                         case 0x02:
245                                 /* FPGA firmware version */
246                                 if (version->len)
247                                         g_string_append(version, ", ");
248                                 g_string_append(version, "FPGA version ");
249                                 g_string_append(version, tmp_str->str);
250                                 break;
251                         case 0x03:
252                                 /* Ancillary version */
253                                 if (version->len)
254                                         g_string_append(version, ", ");
255                                 g_string_append(version, "Ancillary version ");
256                                 g_string_append(version, tmp_str->str);
257                                 break;
258                         default:
259                                 sr_info("Unknown token 0x%.2x: '%s'",
260                                         token, tmp_str->str);
261                                 break;
262                         }
263                         g_string_free(tmp_str, TRUE);
264                         break;
265                 case 1:
266                         /* 32-bit unsigned integer */
267                         tmp_int = 0;
268                         for (i = 0; i < 4; i++) {
269                                 tmp_int = (tmp_int << 8) | buf[index++];
270                         }
271                         sr_dbg("Got metadata key 0x%.2x value 0x%.8x.",
272                                key, tmp_int);
273                         switch (token) {
274                         case 0x00:
275                                 /* Number of usable channels */
276                                 for (ui = 0; ui < tmp_int; ui++)
277                                         sr_channel_new(sdi, ui, SR_CHANNEL_LOGIC, TRUE,
278                                                         p_ols_channel_names[ui]);
279                                 break;
280                         case 0x01:
281                                 /* Amount of sample memory available (bytes) */
282                                 devc->max_samplebytes = tmp_int;
283                                 break;
284                         case 0x02:
285                                 /* Amount of dynamic memory available (bytes) */
286                                 /* what is this for? */
287                                 break;
288                         case 0x03:
289                                 /* Maximum sample rate (Hz) */
290                                 devc->max_samplerate = tmp_int;
291                                 break;
292                         case 0x04:
293                                 /* protocol version */
294                                 devc->protocol_version = tmp_int;
295                                 break;
296                         default:
297                                 sr_info("Unknown token 0x%.2x: 0x%.8x.",
298                                         token, tmp_int);
299                                 break;
300                         }
301                         break;
302                 case 2:
303                         /* 8-bit unsigned integer */
304                         tmp_c = buf[index++];
305                         sr_dbg("Got metadata key 0x%.2x value 0x%.2x.",
306                                key, tmp_c);
307                         switch (token) {
308                         case 0x00:
309                                 /* Number of usable channels */
310                                 for (ui = 0; ui < tmp_c; ui++)
311                                         sr_channel_new(sdi, ui, SR_CHANNEL_LOGIC, TRUE,
312                                                         p_ols_channel_names[ui]);
313                                 break;
314                         case 0x01:
315                                 /* protocol version */
316                                 devc->protocol_version = tmp_c;
317                                 break;
318                         default:
319                                 sr_info("Unknown token 0x%.2x: 0x%.2x.",
320                                         token, tmp_c);
321                                 break;
322                         }
323                         break;
324                 default:
325                         /* unknown type */
326                         break;
327                 }
328         }
329
330         sdi->model = devname->str;
331         sdi->version = version->str;
332         g_string_free(devname, FALSE);
333         g_string_free(version, FALSE);
334
335         return sdi;
336 }
337
338 SR_PRIV int p_ols_set_samplerate(const struct sr_dev_inst *sdi,
339                 const uint64_t samplerate)
340 {
341         struct dev_context *devc;
342
343         devc = sdi->priv;
344         if (devc->max_samplerate && samplerate > devc->max_samplerate)
345                 return SR_ERR_SAMPLERATE;
346
347         if (samplerate > CLOCK_RATE) {
348                 sr_info("Enabling demux mode.");
349                 devc->flag_reg |= FLAG_DEMUX;
350                 devc->flag_reg &= ~FLAG_FILTER;
351                 devc->max_channels = NUM_CHANNELS / 2;
352                 devc->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
353         } else {
354                 sr_info("Disabling demux mode.");
355                 devc->flag_reg &= ~FLAG_DEMUX;
356                 devc->flag_reg |= FLAG_FILTER;
357                 devc->max_channels = NUM_CHANNELS;
358                 devc->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
359         }
360
361         /* Calculate actual samplerate used and complain if it is different
362          * from the requested.
363          */
364         devc->cur_samplerate = CLOCK_RATE / (devc->cur_samplerate_divider + 1);
365         if (devc->flag_reg & FLAG_DEMUX)
366                 devc->cur_samplerate *= 2;
367         if (devc->cur_samplerate != samplerate)
368                 sr_info("Can't match samplerate %" PRIu64 ", using %"
369                        PRIu64 ".", samplerate, devc->cur_samplerate);
370
371         return SR_OK;
372 }
373
374 SR_PRIV int p_ols_receive_data(int fd, int revents, void *cb_data)
375 {
376         struct dev_context *devc;
377         struct sr_dev_inst *sdi;
378         struct sr_datafeed_packet packet;
379         struct sr_datafeed_logic logic;
380         uint32_t sample;
381         int num_channels, offset, j;
382         int bytes_read, index;
383         unsigned int i;
384         unsigned char byte;
385
386         (void)fd;
387         (void)revents;
388
389         sdi = cb_data;
390         devc = sdi->priv;
391
392         if (devc->num_transfers++ == 0) {
393                 devc->raw_sample_buf = g_try_malloc(devc->limit_samples * 4);
394                 if (!devc->raw_sample_buf) {
395                         sr_err("Sample buffer malloc failed.");
396                         return FALSE;
397                 }
398                 /* fill with 1010... for debugging */
399                 memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4);
400         }
401
402         if ((devc->num_samples < devc->limit_samples) && (devc->cnt_samples < devc->max_samples)) {
403
404                 num_channels = 0;
405                 for (i = NUM_CHANNELS; i > 0x02; i /= 2) {
406                         if ((devc->flag_reg & i) == 0) {
407                                 num_channels++;
408                         }
409                 }
410
411                 /* Get a block of data. */
412                 bytes_read = ftdi_read_data(devc->ftdic, devc->ftdi_buf, FTDI_BUF_SIZE);
413                 if (bytes_read < 0) {
414                         sr_err("Failed to read FTDI data (%d): %s.",
415                                bytes_read, ftdi_get_error_string(devc->ftdic));
416                         sr_dev_acquisition_stop(sdi);
417                         return FALSE;
418                 }
419                 if (bytes_read == 0) {
420                         sr_spew("Received 0 bytes, nothing to do.");
421                         return TRUE;
422                 }
423
424                 sr_dbg("Received %d bytes", bytes_read);
425
426                 index = 0;
427                 while (index < bytes_read) {
428                         byte = devc->ftdi_buf[index++];
429                         devc->cnt_bytes++;
430
431                         devc->sample[devc->num_bytes++] = byte;
432                         sr_spew("Received byte 0x%.2x.", byte);
433
434                         if ((devc->flag_reg & FLAG_DEMUX) && (devc->flag_reg & FLAG_RLE)) {
435                                 /* RLE in demux mode must be processed differently
436                                 * since in this case the RLE encoder is operating on pairs of samples.
437                                 */
438                                 if (devc->num_bytes == num_channels * 2) {
439                                         devc->cnt_samples += 2;
440                                         devc->cnt_samples_rle += 2;
441                                         /*
442                                          * Got a sample pair. Convert from the OLS's little-endian
443                                          * sample to the local format.
444                                          */
445                                         sample = devc->sample[0] | (devc->sample[1] << 8) \
446                                                         | (devc->sample[2] << 16) | (devc->sample[3] << 24);
447                                         sr_spew("Received sample pair 0x%.*x.", devc->num_bytes * 2, sample);
448
449                                         /*
450                                          * In RLE mode the high bit of the sample pair is the
451                                          * "count" flag, meaning this sample pair is the number
452                                          * of times the previous sample pair occurred.
453                                          */
454                                         if (devc->sample[devc->num_bytes - 1] & 0x80) {
455                                                 /* Clear the high bit. */
456                                                 sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
457                                                 devc->rle_count = sample;
458                                                 devc->cnt_samples_rle += devc->rle_count * 2;
459                                                 sr_dbg("RLE count: %u.", devc->rle_count * 2);
460                                                 devc->num_bytes = 0;
461                                                 continue;
462                                         }
463                                         devc->num_samples += (devc->rle_count + 1) * 2;
464                                         if (devc->num_samples > devc->limit_samples) {
465                                                 /* Save us from overrunning the buffer. */
466                                                 devc->rle_count -= (devc->num_samples - devc->limit_samples) / 2;
467                                                 devc->num_samples = devc->limit_samples;
468                                                 index = bytes_read;
469                                         }
470
471                                         /*
472                                          * Some channel groups may have been turned
473                                          * off, to speed up transfer between the
474                                          * hardware and the PC. Expand that here before
475                                          * submitting it over the session bus --
476                                          * whatever is listening on the bus will be
477                                          * expecting a full 32-bit sample, based on
478                                          * the number of channels.
479                                          */
480                                         j = 0;
481                                         /* expand first sample */
482                                         memset(devc->tmp_sample, 0, 4);
483                                         for (i = 0; i < 2; i++) {
484                                                 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
485                                                         /*
486                                                          * This channel group was
487                                                          * enabled, copy from received
488                                                          * sample.
489                                                          */
490                                                         devc->tmp_sample[i] = devc->sample[j++];
491                                                 }
492                                         }
493                                         /* Clear out the most significant bit of the sample */
494                                         devc->tmp_sample[devc->num_bytes - 1] &= 0x7f;
495                                         sr_spew("Expanded sample 1: 0x%.2x%.2x%.2x%.2x.",
496                                                 devc->tmp_sample[3], devc->tmp_sample[2],
497                                                 devc->tmp_sample[1], devc->tmp_sample[0]);
498
499                                         /* expand second sample */
500                                         memset(devc->tmp_sample2, 0, 4);
501                                         for (i = 0; i < 2; i++) {
502                                                 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
503                                                         /*
504                                                          * This channel group was
505                                                          * enabled, copy from received
506                                                          * sample.
507                                                          */
508                                                         devc->tmp_sample2[i] = devc->sample[j++];
509                                                 }
510                                         }
511                                         /* Clear out the most significant bit of the sample */
512                                         devc->tmp_sample2[devc->num_bytes - 1] &= 0x7f;
513                                         sr_spew("Expanded sample 2: 0x%.2x%.2x%.2x%.2x.",
514                                                 devc->tmp_sample2[3], devc->tmp_sample2[2],
515                                                 devc->tmp_sample2[1], devc->tmp_sample2[0]);
516
517                                         /*
518                                          * OLS sends its sample buffer backwards.
519                                          * store it in reverse order here, so we can dump
520                                          * this on the session bus later.
521                                          */
522                                         offset = (devc->limit_samples - devc->num_samples) * 4;
523                                         for (i = 0; i <= devc->rle_count; i++) {
524                                                 memcpy(devc->raw_sample_buf + offset + (i * 8),
525                                                                          devc->tmp_sample2, 4);
526                                                 memcpy(devc->raw_sample_buf + offset + (4 + (i * 8)),
527                                                                          devc->tmp_sample, 4);
528                                         }
529                                         memset(devc->sample, 0, 4);
530                                         devc->num_bytes = 0;
531                                         devc->rle_count = 0;
532                                 }
533                         }
534                         else {
535                                 if (devc->num_bytes == num_channels) {
536                                         devc->cnt_samples++;
537                                         devc->cnt_samples_rle++;
538                                         /*
539                                          * Got a full sample. Convert from the OLS's little-endian
540                                          * sample to the local format.
541                                          */
542                                         sample = devc->sample[0] | (devc->sample[1] << 8) \
543                                                         | (devc->sample[2] << 16) | (devc->sample[3] << 24);
544                                         sr_spew("Received sample 0x%.*x.", devc->num_bytes * 2, sample);
545                                         if (devc->flag_reg & FLAG_RLE) {
546                                                 /*
547                                                  * In RLE mode the high bit of the sample is the
548                                                  * "count" flag, meaning this sample is the number
549                                                  * of times the previous sample occurred.
550                                                  */
551                                                 if (devc->sample[devc->num_bytes - 1] & 0x80) {
552                                                         /* Clear the high bit. */
553                                                         sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
554                                                         devc->rle_count = sample;
555                                                         devc->cnt_samples_rle += devc->rle_count;
556                                                         sr_dbg("RLE count: %u.", devc->rle_count);
557                                                         devc->num_bytes = 0;
558                                                         continue;
559                                                 }
560                                         }
561                                         devc->num_samples += devc->rle_count + 1;
562                                         if (devc->num_samples > devc->limit_samples) {
563                                                 /* Save us from overrunning the buffer. */
564                                                 devc->rle_count -= devc->num_samples - devc->limit_samples;
565                                                 devc->num_samples = devc->limit_samples;
566                                                 index = bytes_read;
567                                         }
568
569                                         if (num_channels < 4) {
570                                                 /*
571                                                  * Some channel groups may have been turned
572                                                  * off, to speed up transfer between the
573                                                  * hardware and the PC. Expand that here before
574                                                  * submitting it over the session bus --
575                                                  * whatever is listening on the bus will be
576                                                  * expecting a full 32-bit sample, based on
577                                                  * the number of channels.
578                                                  */
579                                                 j = 0;
580                                                 memset(devc->tmp_sample, 0, 4);
581                                                 for (i = 0; i < 4; i++) {
582                                                         if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
583                                                                 /*
584                                                                  * This channel group was
585                                                                  * enabled, copy from received
586                                                                  * sample.
587                                                                  */
588                                                                 devc->tmp_sample[i] = devc->sample[j++];
589                                                         }
590                                                 }
591                                                 memcpy(devc->sample, devc->tmp_sample, 4);
592                                                 sr_spew("Expanded sample: 0x%.8x.", sample);
593                                         }
594
595                                         /*
596                                          * Pipistrello OLS sends its sample buffer backwards.
597                                          * store it in reverse order here, so we can dump
598                                          * this on the session bus later.
599                                          */
600                                         offset = (devc->limit_samples - devc->num_samples) * 4;
601                                         for (i = 0; i <= devc->rle_count; i++) {
602                                                 memcpy(devc->raw_sample_buf + offset + (i * 4),
603                                                                          devc->sample, 4);
604                                         }
605                                         memset(devc->sample, 0, 4);
606                                         devc->num_bytes = 0;
607                                         devc->rle_count = 0;
608                                 }
609                         }
610                 }
611                 return TRUE;
612         } else {
613                 do {
614                         bytes_read = ftdi_read_data(devc->ftdic, devc->ftdi_buf, FTDI_BUF_SIZE);
615                 } while (bytes_read > 0);
616
617                 /*
618                  * We've acquired all the samples we asked for -- we're done.
619                  * Send the (properly-ordered) buffer to the frontend.
620                  */
621                 sr_dbg("Received %d bytes, %d samples, %d decompressed samples.",
622                                 devc->cnt_bytes, devc->cnt_samples,
623                                 devc->cnt_samples_rle);
624                 if (devc->trigger_at != -1) {
625                         /*
626                          * A trigger was set up, so we need to tell the frontend
627                          * about it.
628                          */
629                         if (devc->trigger_at > 0) {
630                                 /* There are pre-trigger samples, send those first. */
631                                 packet.type = SR_DF_LOGIC;
632                                 packet.payload = &logic;
633                                 logic.length = devc->trigger_at * 4;
634                                 logic.unitsize = 4;
635                                 logic.data = devc->raw_sample_buf +
636                                         (devc->limit_samples - devc->num_samples) * 4;
637                                 sr_session_send(sdi, &packet);
638                         }
639
640                         /* Send the trigger. */
641                         packet.type = SR_DF_TRIGGER;
642                         sr_session_send(sdi, &packet);
643
644                         /* Send post-trigger samples. */
645                         packet.type = SR_DF_LOGIC;
646                         packet.payload = &logic;
647                         logic.length = (devc->num_samples * 4) - (devc->trigger_at * 4);
648                         logic.unitsize = 4;
649                         logic.data = devc->raw_sample_buf + devc->trigger_at * 4 +
650                                 (devc->limit_samples - devc->num_samples) * 4;
651                         sr_session_send(sdi, &packet);
652                 } else {
653                         /* no trigger was used */
654                         packet.type = SR_DF_LOGIC;
655                         packet.payload = &logic;
656                         logic.length = devc->num_samples * 4;
657                         logic.unitsize = 4;
658                         logic.data = devc->raw_sample_buf +
659                                 (devc->limit_samples - devc->num_samples) * 4;
660                         sr_session_send(sdi, &packet);
661                 }
662                 g_free(devc->raw_sample_buf);
663
664                 sr_dev_acquisition_stop(sdi);
665         }
666
667         return TRUE;
668 }