]> sigrok.org Git - libsigrok.git/blob - src/hardware/demo/api.c
Consistently use the "Sysclk" spelling everywhere.
[libsigrok.git] / src / hardware / demo / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5  * Copyright (C) 2011 Olivier Fauchon <olivier@aixmarseille.com>
6  * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
7  * Copyright (C) 2015 Bartosz Golaszewski <bgolaszewski@baylibre.com>
8  * Copyright (C) 2019 Frank Stettner <frank-stettner@gmx.net>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, see <http://www.gnu.org/licenses/>.
22  */
23
24 #include <config.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <math.h>
28 #include <libsigrok/libsigrok.h>
29 #include "libsigrok-internal.h"
30 #include "protocol.h"
31
32 #define DEFAULT_NUM_LOGIC_CHANNELS              8
33 #define DEFAULT_LOGIC_PATTERN                   PATTERN_SIGROK
34
35 #define DEFAULT_NUM_ANALOG_CHANNELS             4
36
37 /* Note: No spaces allowed because of sigrok-cli. */
38 static const char *logic_pattern_str[] = {
39         "sigrok",
40         "random",
41         "incremental",
42         "walking-one",
43         "walking-zero",
44         "all-low",
45         "all-high",
46         "squid",
47         "graycode",
48 };
49
50 static const uint32_t scanopts[] = {
51         SR_CONF_NUM_LOGIC_CHANNELS,
52         SR_CONF_NUM_ANALOG_CHANNELS,
53         SR_CONF_LIMIT_FRAMES,
54 };
55
56 static const uint32_t drvopts[] = {
57         SR_CONF_DEMO_DEV,
58         SR_CONF_LOGIC_ANALYZER,
59         SR_CONF_OSCILLOSCOPE,
60 };
61
62 static const uint32_t devopts[] = {
63         SR_CONF_CONTINUOUS,
64         SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
65         SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
66         SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
67         SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
68         SR_CONF_AVERAGING | SR_CONF_GET | SR_CONF_SET,
69         SR_CONF_AVG_SAMPLES | SR_CONF_GET | SR_CONF_SET,
70         SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
71         SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
72 };
73
74 static const uint32_t devopts_cg_logic[] = {
75         SR_CONF_PATTERN_MODE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
76 };
77
78 static const uint32_t devopts_cg_analog_group[] = {
79         SR_CONF_AMPLITUDE | SR_CONF_GET | SR_CONF_SET,
80         SR_CONF_OFFSET | SR_CONF_GET | SR_CONF_SET,
81 };
82
83 static const uint32_t devopts_cg_analog_channel[] = {
84         SR_CONF_MEASURED_QUANTITY | SR_CONF_GET | SR_CONF_SET,
85         SR_CONF_PATTERN_MODE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
86         SR_CONF_AMPLITUDE | SR_CONF_GET | SR_CONF_SET,
87         SR_CONF_OFFSET | SR_CONF_GET | SR_CONF_SET,
88 };
89
90 static const int32_t trigger_matches[] = {
91         SR_TRIGGER_ZERO,
92         SR_TRIGGER_ONE,
93         SR_TRIGGER_RISING,
94         SR_TRIGGER_FALLING,
95         SR_TRIGGER_EDGE,
96 };
97
98 static const uint64_t samplerates[] = {
99         SR_HZ(1),
100         SR_GHZ(1),
101         SR_HZ(1),
102 };
103
104 static GSList *scan(struct sr_dev_driver *di, GSList *options)
105 {
106         struct dev_context *devc;
107         struct sr_dev_inst *sdi;
108         struct sr_channel *ch;
109         struct sr_channel_group *cg, *acg;
110         struct sr_config *src;
111         struct analog_gen *ag;
112         GSList *l;
113         int num_logic_channels, num_analog_channels, pattern, i;
114         uint64_t limit_frames;
115         char channel_name[16];
116
117         num_logic_channels = DEFAULT_NUM_LOGIC_CHANNELS;
118         num_analog_channels = DEFAULT_NUM_ANALOG_CHANNELS;
119         limit_frames = DEFAULT_LIMIT_FRAMES;
120         for (l = options; l; l = l->next) {
121                 src = l->data;
122                 switch (src->key) {
123                 case SR_CONF_NUM_LOGIC_CHANNELS:
124                         num_logic_channels = g_variant_get_int32(src->data);
125                         break;
126                 case SR_CONF_NUM_ANALOG_CHANNELS:
127                         num_analog_channels = g_variant_get_int32(src->data);
128                         break;
129                 case SR_CONF_LIMIT_FRAMES:
130                         limit_frames = g_variant_get_uint64(src->data);
131                         break;
132                 }
133         }
134
135         sdi = g_malloc0(sizeof(struct sr_dev_inst));
136         sdi->status = SR_ST_INACTIVE;
137         sdi->model = g_strdup("Demo device");
138
139         devc = g_malloc0(sizeof(struct dev_context));
140         devc->cur_samplerate = SR_KHZ(200);
141         devc->num_logic_channels = num_logic_channels;
142         devc->logic_unitsize = (devc->num_logic_channels + 7) / 8;
143         devc->all_logic_channels_mask = 1UL << 0;
144         devc->all_logic_channels_mask <<= devc->num_logic_channels;
145         devc->all_logic_channels_mask--;
146         devc->logic_pattern = DEFAULT_LOGIC_PATTERN;
147         devc->num_analog_channels = num_analog_channels;
148         devc->limit_frames = limit_frames;
149         devc->capture_ratio = 20;
150         devc->stl = NULL;
151
152         if (num_logic_channels > 0) {
153                 /* Logic channels, all in one channel group. */
154                 cg = g_malloc0(sizeof(struct sr_channel_group));
155                 cg->name = g_strdup("Logic");
156                 for (i = 0; i < num_logic_channels; i++) {
157                         sprintf(channel_name, "D%d", i);
158                         ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name);
159                         cg->channels = g_slist_append(cg->channels, ch);
160                 }
161                 sdi->channel_groups = g_slist_append(NULL, cg);
162         }
163
164         /* Analog channels, channel groups and pattern generators. */
165         devc->ch_ag = g_hash_table_new(g_direct_hash, g_direct_equal);
166         if (num_analog_channels > 0) {
167                 /*
168                  * Have the waveform for analog patterns pre-generated. It's
169                  * supposed to be periodic, so the generator just needs to
170                  * access the prepared sample data (DDS style).
171                  */
172                 demo_generate_analog_pattern(devc);
173
174                 pattern = 0;
175                 /* An "Analog" channel group with all analog channels in it. */
176                 acg = g_malloc0(sizeof(struct sr_channel_group));
177                 acg->name = g_strdup("Analog");
178                 sdi->channel_groups = g_slist_append(sdi->channel_groups, acg);
179
180                 for (i = 0; i < num_analog_channels; i++) {
181                         snprintf(channel_name, 16, "A%d", i);
182                         ch = sr_channel_new(sdi, i + num_logic_channels, SR_CHANNEL_ANALOG,
183                                         TRUE, channel_name);
184                         acg->channels = g_slist_append(acg->channels, ch);
185
186                         /* Every analog channel gets its own channel group as well. */
187                         cg = g_malloc0(sizeof(struct sr_channel_group));
188                         cg->name = g_strdup(channel_name);
189                         cg->channels = g_slist_append(NULL, ch);
190                         sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
191
192                         /* Every channel gets a generator struct. */
193                         ag = g_malloc(sizeof(struct analog_gen));
194                         ag->ch = ch;
195                         ag->mq = SR_MQ_VOLTAGE;
196                         ag->mq_flags = SR_MQFLAG_DC;
197                         ag->unit = SR_UNIT_VOLT;
198                         ag->amplitude = DEFAULT_ANALOG_AMPLITUDE;
199                         ag->offset = DEFAULT_ANALOG_OFFSET;
200                         sr_analog_init(&ag->packet, &ag->encoding, &ag->meaning, &ag->spec, 2);
201                         ag->packet.meaning->channels = cg->channels;
202                         ag->packet.meaning->mq = ag->mq;
203                         ag->packet.meaning->mqflags = ag->mq_flags;
204                         ag->packet.meaning->unit = ag->unit;
205                         ag->packet.encoding->digits = DEFAULT_ANALOG_ENCODING_DIGITS;
206                         ag->packet.spec->spec_digits = DEFAULT_ANALOG_SPEC_DIGITS;
207                         ag->packet.data = devc->analog_patterns[pattern];
208                         ag->pattern = pattern;
209                         ag->avg_val = 0.0f;
210                         ag->num_avgs = 0;
211                         g_hash_table_insert(devc->ch_ag, ch, ag);
212
213                         if (++pattern == ARRAY_SIZE(analog_pattern_str))
214                                 pattern = 0;
215                 }
216         }
217
218         sdi->priv = devc;
219
220         return std_scan_complete(di, g_slist_append(NULL, sdi));
221 }
222
223 static void clear_helper(struct dev_context *devc)
224 {
225         GHashTableIter iter;
226         void *value;
227
228         /* Analog generators. */
229         g_hash_table_iter_init(&iter, devc->ch_ag);
230         while (g_hash_table_iter_next(&iter, NULL, &value))
231                 g_free(value);
232         g_hash_table_unref(devc->ch_ag);
233 }
234
235 static int dev_clear(const struct sr_dev_driver *di)
236 {
237         return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
238 }
239
240 static int config_get(uint32_t key, GVariant **data,
241         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
242 {
243         struct dev_context *devc;
244         struct sr_channel *ch;
245         struct analog_gen *ag;
246         GVariant *mq_arr[2];
247         int pattern;
248
249         if (!sdi)
250                 return SR_ERR_ARG;
251
252         devc = sdi->priv;
253         switch (key) {
254         case SR_CONF_SAMPLERATE:
255                 *data = g_variant_new_uint64(devc->cur_samplerate);
256                 break;
257         case SR_CONF_LIMIT_SAMPLES:
258                 *data = g_variant_new_uint64(devc->limit_samples);
259                 break;
260         case SR_CONF_LIMIT_MSEC:
261                 *data = g_variant_new_uint64(devc->limit_msec);
262                 break;
263         case SR_CONF_LIMIT_FRAMES:
264                 *data = g_variant_new_uint64(devc->limit_frames);
265                 break;
266         case SR_CONF_AVERAGING:
267                 *data = g_variant_new_boolean(devc->avg);
268                 break;
269         case SR_CONF_AVG_SAMPLES:
270                 *data = g_variant_new_uint64(devc->avg_samples);
271                 break;
272         case SR_CONF_MEASURED_QUANTITY:
273                 if (!cg)
274                         return SR_ERR_CHANNEL_GROUP;
275                 /* Any channel in the group will do. */
276                 ch = cg->channels->data;
277                 if (ch->type != SR_CHANNEL_ANALOG)
278                         return SR_ERR_ARG;
279                 ag = g_hash_table_lookup(devc->ch_ag, ch);
280                 mq_arr[0] = g_variant_new_uint32(ag->mq);
281                 mq_arr[1] = g_variant_new_uint64(ag->mq_flags);
282                 *data = g_variant_new_tuple(mq_arr, 2);
283                 break;
284         case SR_CONF_PATTERN_MODE:
285                 if (!cg)
286                         return SR_ERR_CHANNEL_GROUP;
287                 /* Any channel in the group will do. */
288                 ch = cg->channels->data;
289                 if (ch->type == SR_CHANNEL_LOGIC) {
290                         pattern = devc->logic_pattern;
291                         *data = g_variant_new_string(logic_pattern_str[pattern]);
292                 } else if (ch->type == SR_CHANNEL_ANALOG) {
293                         ag = g_hash_table_lookup(devc->ch_ag, ch);
294                         pattern = ag->pattern;
295                         *data = g_variant_new_string(analog_pattern_str[pattern]);
296                 } else
297                         return SR_ERR_BUG;
298                 break;
299         case SR_CONF_AMPLITUDE:
300                 if (!cg)
301                         return SR_ERR_CHANNEL_GROUP;
302                 /* Any channel in the group will do. */
303                 ch = cg->channels->data;
304                 if (ch->type != SR_CHANNEL_ANALOG)
305                         return SR_ERR_ARG;
306                 ag = g_hash_table_lookup(devc->ch_ag, ch);
307                 *data = g_variant_new_double(ag->amplitude);
308                 break;
309         case SR_CONF_OFFSET:
310                 if (!cg)
311                         return SR_ERR_CHANNEL_GROUP;
312                 /* Any channel in the group will do. */
313                 ch = cg->channels->data;
314                 if (ch->type != SR_CHANNEL_ANALOG)
315                         return SR_ERR_ARG;
316                 ag = g_hash_table_lookup(devc->ch_ag, ch);
317                 *data = g_variant_new_double(ag->offset);
318                 break;
319         case SR_CONF_CAPTURE_RATIO:
320                 *data = g_variant_new_uint64(devc->capture_ratio);
321                 break;
322         default:
323                 return SR_ERR_NA;
324         }
325
326         return SR_OK;
327 }
328
329 static int config_set(uint32_t key, GVariant *data,
330         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
331 {
332         struct dev_context *devc;
333         struct analog_gen *ag;
334         struct sr_channel *ch;
335         GVariant *mq_tuple_child;
336         GSList *l;
337         int logic_pattern, analog_pattern;
338
339         devc = sdi->priv;
340
341         switch (key) {
342         case SR_CONF_SAMPLERATE:
343                 devc->cur_samplerate = g_variant_get_uint64(data);
344                 break;
345         case SR_CONF_LIMIT_SAMPLES:
346                 devc->limit_msec = 0;
347                 devc->limit_samples = g_variant_get_uint64(data);
348                 break;
349         case SR_CONF_LIMIT_MSEC:
350                 devc->limit_msec = g_variant_get_uint64(data);
351                 devc->limit_samples = 0;
352                 break;
353         case SR_CONF_LIMIT_FRAMES:
354                 devc->limit_frames = g_variant_get_uint64(data);
355                 break;
356         case SR_CONF_AVERAGING:
357                 devc->avg = g_variant_get_boolean(data);
358                 sr_dbg("%s averaging", devc->avg ? "Enabling" : "Disabling");
359                 break;
360         case SR_CONF_AVG_SAMPLES:
361                 devc->avg_samples = g_variant_get_uint64(data);
362                 sr_dbg("Setting averaging rate to %" PRIu64, devc->avg_samples);
363                 break;
364         case SR_CONF_MEASURED_QUANTITY:
365                 if (!cg)
366                         return SR_ERR_CHANNEL_GROUP;
367                 for (l = cg->channels; l; l = l->next) {
368                         ch = l->data;
369                         if (ch->type != SR_CHANNEL_ANALOG)
370                                 return SR_ERR_ARG;
371                         ag = g_hash_table_lookup(devc->ch_ag, ch);
372                         mq_tuple_child = g_variant_get_child_value(data, 0);
373                         ag->mq = g_variant_get_uint32(mq_tuple_child);
374                         mq_tuple_child = g_variant_get_child_value(data, 1);
375                         ag->mq_flags = g_variant_get_uint64(mq_tuple_child);
376                         g_variant_unref(mq_tuple_child);
377                 }
378                 break;
379         case SR_CONF_PATTERN_MODE:
380                 if (!cg)
381                         return SR_ERR_CHANNEL_GROUP;
382                 logic_pattern = std_str_idx(data, ARRAY_AND_SIZE(logic_pattern_str));
383                 analog_pattern = std_str_idx(data, ARRAY_AND_SIZE(analog_pattern_str));
384                 if (logic_pattern < 0 && analog_pattern < 0)
385                         return SR_ERR_ARG;
386                 for (l = cg->channels; l; l = l->next) {
387                         ch = l->data;
388                         if (ch->type == SR_CHANNEL_LOGIC) {
389                                 if (logic_pattern == -1)
390                                         return SR_ERR_ARG;
391                                 sr_dbg("Setting logic pattern to %s",
392                                                 logic_pattern_str[logic_pattern]);
393                                 devc->logic_pattern = logic_pattern;
394                                 /* Might as well do this now, these are static. */
395                                 if (logic_pattern == PATTERN_ALL_LOW)
396                                         memset(devc->logic_data, 0x00, LOGIC_BUFSIZE);
397                                 else if (logic_pattern == PATTERN_ALL_HIGH)
398                                         memset(devc->logic_data, 0xff, LOGIC_BUFSIZE);
399                         } else if (ch->type == SR_CHANNEL_ANALOG) {
400                                 if (analog_pattern == -1)
401                                         return SR_ERR_ARG;
402                                 sr_dbg("Setting analog pattern for channel %s to %s",
403                                                 ch->name, analog_pattern_str[analog_pattern]);
404                                 ag = g_hash_table_lookup(devc->ch_ag, ch);
405                                 ag->pattern = analog_pattern;
406                         } else
407                                 return SR_ERR_BUG;
408                 }
409                 break;
410         case SR_CONF_AMPLITUDE:
411                 if (!cg)
412                         return SR_ERR_CHANNEL_GROUP;
413                 for (l = cg->channels; l; l = l->next) {
414                         ch = l->data;
415                         if (ch->type != SR_CHANNEL_ANALOG)
416                                 return SR_ERR_ARG;
417                         ag = g_hash_table_lookup(devc->ch_ag, ch);
418                         ag->amplitude = g_variant_get_double(data);
419                 }
420                 break;
421         case SR_CONF_OFFSET:
422                 if (!cg)
423                         return SR_ERR_CHANNEL_GROUP;
424                 for (l = cg->channels; l; l = l->next) {
425                         ch = l->data;
426                         if (ch->type != SR_CHANNEL_ANALOG)
427                                 return SR_ERR_ARG;
428                         ag = g_hash_table_lookup(devc->ch_ag, ch);
429                         ag->offset = g_variant_get_double(data);
430                 }
431                 break;
432         case SR_CONF_CAPTURE_RATIO:
433                 devc->capture_ratio = g_variant_get_uint64(data);
434                 break;
435         default:
436                 return SR_ERR_NA;
437         }
438
439         return SR_OK;
440 }
441
442 static int config_list(uint32_t key, GVariant **data,
443         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
444 {
445         struct sr_channel *ch;
446
447         if (!cg) {
448                 switch (key) {
449                 case SR_CONF_SCAN_OPTIONS:
450                 case SR_CONF_DEVICE_OPTIONS:
451                         return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
452                 case SR_CONF_SAMPLERATE:
453                         *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates));
454                         break;
455                 case SR_CONF_TRIGGER_MATCH:
456                         *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
457                         break;
458                 default:
459                         return SR_ERR_NA;
460                 }
461         } else {
462                 ch = cg->channels->data;
463                 switch (key) {
464                 case SR_CONF_DEVICE_OPTIONS:
465                         if (ch->type == SR_CHANNEL_LOGIC)
466                                 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_logic));
467                         else if (ch->type == SR_CHANNEL_ANALOG) {
468                                 if (strcmp(cg->name, "Analog") == 0)
469                                         *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog_group));
470                                 else
471                                         *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog_channel));
472                         }
473                         else
474                                 return SR_ERR_BUG;
475                         break;
476                 case SR_CONF_PATTERN_MODE:
477                         /* The analog group (with all 4 channels) shall not have a pattern property. */
478                         if (strcmp(cg->name, "Analog") == 0)
479                                 return SR_ERR_NA;
480
481                         if (ch->type == SR_CHANNEL_LOGIC)
482                                 *data = g_variant_new_strv(ARRAY_AND_SIZE(logic_pattern_str));
483                         else if (ch->type == SR_CHANNEL_ANALOG)
484                                 *data = g_variant_new_strv(ARRAY_AND_SIZE(analog_pattern_str));
485                         else
486                                 return SR_ERR_BUG;
487                         break;
488                 default:
489                         return SR_ERR_NA;
490                 }
491         }
492
493         return SR_OK;
494 }
495
496 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
497 {
498         struct dev_context *devc;
499         GSList *l;
500         struct sr_channel *ch;
501         int bitpos;
502         uint8_t mask;
503         struct sr_trigger *trigger;
504
505         devc = sdi->priv;
506         devc->sent_samples = 0;
507         devc->sent_frame_samples = 0;
508
509         /* Setup triggers */
510         if ((trigger = sr_session_trigger_get(sdi->session))) {
511                 int pre_trigger_samples = 0;
512                 if (devc->limit_samples > 0)
513                         pre_trigger_samples = (devc->capture_ratio * devc->limit_samples) / 100;
514                 devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples);
515                 if (!devc->stl)
516                         return SR_ERR_MALLOC;
517
518                 /* Disable all analog channels since using them when there are logic
519                  * triggers set up would require having pre-trigger sample buffers
520                  * for analog sample data.
521                  */
522                 for (l = sdi->channels; l; l = l->next) {
523                         ch = l->data;
524                         if (ch->type == SR_CHANNEL_ANALOG)
525                                 ch->enabled = FALSE;
526                 }
527         }
528         devc->trigger_fired = FALSE;
529
530         /*
531          * Determine the numbers of logic and analog channels that are
532          * involved in the acquisition. Determine an offset and a mask to
533          * remove excess logic data content before datafeed submission.
534          */
535         devc->enabled_logic_channels = 0;
536         devc->enabled_analog_channels = 0;
537         for (l = sdi->channels; l; l = l->next) {
538                 ch = l->data;
539                 if (!ch->enabled)
540                         continue;
541                 if (ch->type == SR_CHANNEL_ANALOG) {
542                         devc->enabled_analog_channels++;
543                         continue;
544                 }
545                 if (ch->type != SR_CHANNEL_LOGIC)
546                         continue;
547                 /*
548                  * TODO: Need we create a channel map here, such that the
549                  * session datafeed packets will have a dense representation
550                  * of the enabled channels' data? For example store channels
551                  * D3 and D5 in bit positions 0 and 1 respectively, when all
552                  * other channels are disabled? The current implementation
553                  * generates a sparse layout, might provide data for logic
554                  * channels that are disabled while it might suppress data
555                  * from enabled channels at the same time.
556                  */
557                 devc->enabled_logic_channels++;
558         }
559         devc->first_partial_logic_index = devc->enabled_logic_channels / 8;
560         bitpos = devc->enabled_logic_channels % 8;
561         mask = (1 << bitpos) - 1;
562         devc->first_partial_logic_mask = mask;
563         sr_dbg("num logic %zu, partial off %zu, mask 0x%02x.",
564                 devc->enabled_logic_channels,
565                 devc->first_partial_logic_index,
566                 devc->first_partial_logic_mask);
567
568         sr_session_source_add(sdi->session, -1, 0, 100,
569                         demo_prepare_data, (struct sr_dev_inst *)sdi);
570
571         std_session_send_df_header(sdi);
572
573         if (devc->limit_frames > 0)
574                 std_session_send_frame_begin(sdi);
575
576         /* We use this timestamp to decide how many more samples to send. */
577         devc->start_us = g_get_monotonic_time();
578         devc->spent_us = 0;
579         devc->step = 0;
580
581         return SR_OK;
582 }
583
584 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
585 {
586         struct dev_context *devc;
587
588         sr_session_source_remove(sdi->session, -1);
589
590         devc = sdi->priv;
591         if (devc->limit_frames > 0)
592                 std_session_send_frame_end(sdi);
593
594         std_session_send_df_end(sdi);
595
596         if (devc->stl) {
597                 soft_trigger_logic_free(devc->stl);
598                 devc->stl = NULL;
599         }
600
601         return SR_OK;
602 }
603
604 static struct sr_dev_driver demo_driver_info = {
605         .name = "demo",
606         .longname = "Demo driver and pattern generator",
607         .api_version = 1,
608         .init = std_init,
609         .cleanup = std_cleanup,
610         .scan = scan,
611         .dev_list = std_dev_list,
612         .dev_clear = dev_clear,
613         .config_get = config_get,
614         .config_set = config_set,
615         .config_list = config_list,
616         .dev_open = std_dummy_dev_open,
617         .dev_close = std_dummy_dev_close,
618         .dev_acquisition_start = dev_acquisition_start,
619         .dev_acquisition_stop = dev_acquisition_stop,
620         .context = NULL,
621 };
622 SR_REGISTER_DEV_DRIVER(demo_driver_info);