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