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