]> sigrok.org Git - libsigrok.git/blob - src/hardware/demo/api.c
demo: introduce graycode generator mode
[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_AMPLITUDE        10
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 };
54
55 static const uint32_t drvopts[] = {
56         SR_CONF_DEMO_DEV,
57         SR_CONF_LOGIC_ANALYZER,
58         SR_CONF_OSCILLOSCOPE,
59 };
60
61 static const uint32_t devopts[] = {
62         SR_CONF_CONTINUOUS,
63         SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
64         SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
65         SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
66         SR_CONF_AVERAGING | SR_CONF_GET | SR_CONF_SET,
67         SR_CONF_AVG_SAMPLES | SR_CONF_GET | SR_CONF_SET,
68 };
69
70 static const uint32_t devopts_cg_logic[] = {
71         SR_CONF_PATTERN_MODE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
72 };
73
74 static const uint32_t devopts_cg_analog_group[] = {
75         SR_CONF_AMPLITUDE | SR_CONF_GET | SR_CONF_SET,
76 };
77
78 static const uint32_t devopts_cg_analog_channel[] = {
79         SR_CONF_PATTERN_MODE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
80         SR_CONF_AMPLITUDE | SR_CONF_GET | SR_CONF_SET,
81 };
82
83 static const uint64_t samplerates[] = {
84         SR_HZ(1),
85         SR_GHZ(1),
86         SR_HZ(1),
87 };
88
89 static GSList *scan(struct sr_dev_driver *di, GSList *options)
90 {
91         struct dev_context *devc;
92         struct sr_dev_inst *sdi;
93         struct sr_channel *ch;
94         struct sr_channel_group *cg, *acg;
95         struct sr_config *src;
96         struct analog_gen *ag;
97         GSList *l;
98         int num_logic_channels, num_analog_channels, pattern, i;
99         char channel_name[16];
100
101         num_logic_channels = DEFAULT_NUM_LOGIC_CHANNELS;
102         num_analog_channels = DEFAULT_NUM_ANALOG_CHANNELS;
103         for (l = options; l; l = l->next) {
104                 src = l->data;
105                 switch (src->key) {
106                 case SR_CONF_NUM_LOGIC_CHANNELS:
107                         num_logic_channels = g_variant_get_int32(src->data);
108                         break;
109                 case SR_CONF_NUM_ANALOG_CHANNELS:
110                         num_analog_channels = g_variant_get_int32(src->data);
111                         break;
112                 }
113         }
114
115         sdi = g_malloc0(sizeof(struct sr_dev_inst));
116         sdi->status = SR_ST_INACTIVE;
117         sdi->model = g_strdup("Demo device");
118
119         devc = g_malloc0(sizeof(struct dev_context));
120         devc->cur_samplerate = SR_KHZ(200);
121         devc->num_logic_channels = num_logic_channels;
122         devc->logic_unitsize = (devc->num_logic_channels + 7) / 8;
123         devc->all_logic_channels_mask = 1UL << 0;
124         devc->all_logic_channels_mask <<= devc->num_logic_channels;
125         devc->all_logic_channels_mask--;
126         devc->logic_pattern = DEFAULT_LOGIC_PATTERN;
127         devc->num_analog_channels = num_analog_channels;
128
129         if (num_logic_channels > 0) {
130                 /* Logic channels, all in one channel group. */
131                 cg = g_malloc0(sizeof(struct sr_channel_group));
132                 cg->name = g_strdup("Logic");
133                 for (i = 0; i < num_logic_channels; i++) {
134                         sprintf(channel_name, "D%d", i);
135                         ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name);
136                         cg->channels = g_slist_append(cg->channels, ch);
137                 }
138                 sdi->channel_groups = g_slist_append(NULL, cg);
139         }
140
141         /* Analog channels, channel groups and pattern generators. */
142         devc->ch_ag = g_hash_table_new(g_direct_hash, g_direct_equal);
143         if (num_analog_channels > 0) {
144                 pattern = 0;
145                 /* An "Analog" channel group with all analog channels in it. */
146                 acg = g_malloc0(sizeof(struct sr_channel_group));
147                 acg->name = g_strdup("Analog");
148                 sdi->channel_groups = g_slist_append(sdi->channel_groups, acg);
149
150                 for (i = 0; i < num_analog_channels; i++) {
151                         snprintf(channel_name, 16, "A%d", i);
152                         ch = sr_channel_new(sdi, i + num_logic_channels, SR_CHANNEL_ANALOG,
153                                         TRUE, channel_name);
154                         acg->channels = g_slist_append(acg->channels, ch);
155
156                         /* Every analog channel gets its own channel group as well. */
157                         cg = g_malloc0(sizeof(struct sr_channel_group));
158                         cg->name = g_strdup(channel_name);
159                         cg->channels = g_slist_append(NULL, ch);
160                         sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
161
162                         /* Every channel gets a generator struct. */
163                         ag = g_malloc(sizeof(struct analog_gen));
164                         ag->ch = ch;
165                         ag->amplitude = DEFAULT_ANALOG_AMPLITUDE;
166                         sr_analog_init(&ag->packet, &ag->encoding, &ag->meaning, &ag->spec, 2);
167                         ag->packet.meaning->channels = cg->channels;
168                         ag->packet.meaning->mq = 0;
169                         ag->packet.meaning->mqflags = 0;
170                         ag->packet.meaning->unit = SR_UNIT_VOLT;
171                         ag->packet.data = ag->pattern_data;
172                         ag->pattern = pattern;
173                         ag->avg_val = 0.0f;
174                         ag->num_avgs = 0;
175                         g_hash_table_insert(devc->ch_ag, ch, ag);
176
177                         if (++pattern == ARRAY_SIZE(analog_pattern_str))
178                                 pattern = 0;
179                 }
180         }
181
182         sdi->priv = devc;
183
184         return std_scan_complete(di, g_slist_append(NULL, sdi));
185 }
186
187 static void clear_helper(struct dev_context *devc)
188 {
189         GHashTableIter iter;
190         void *value;
191
192         /* Analog generators. */
193         g_hash_table_iter_init(&iter, devc->ch_ag);
194         while (g_hash_table_iter_next(&iter, NULL, &value))
195                 g_free(value);
196         g_hash_table_unref(devc->ch_ag);
197 }
198
199 static int dev_clear(const struct sr_dev_driver *di)
200 {
201         return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
202 }
203
204 static int config_get(uint32_t key, GVariant **data,
205         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
206 {
207         struct dev_context *devc;
208         struct sr_channel *ch;
209         struct analog_gen *ag;
210         int pattern;
211
212         if (!sdi)
213                 return SR_ERR_ARG;
214
215         devc = sdi->priv;
216         switch (key) {
217         case SR_CONF_SAMPLERATE:
218                 *data = g_variant_new_uint64(devc->cur_samplerate);
219                 break;
220         case SR_CONF_LIMIT_SAMPLES:
221                 *data = g_variant_new_uint64(devc->limit_samples);
222                 break;
223         case SR_CONF_LIMIT_MSEC:
224                 *data = g_variant_new_uint64(devc->limit_msec);
225                 break;
226         case SR_CONF_AVERAGING:
227                 *data = g_variant_new_boolean(devc->avg);
228                 break;
229         case SR_CONF_AVG_SAMPLES:
230                 *data = g_variant_new_uint64(devc->avg_samples);
231                 break;
232         case SR_CONF_PATTERN_MODE:
233                 if (!cg)
234                         return SR_ERR_CHANNEL_GROUP;
235                 /* Any channel in the group will do. */
236                 ch = cg->channels->data;
237                 if (ch->type == SR_CHANNEL_LOGIC) {
238                         pattern = devc->logic_pattern;
239                         *data = g_variant_new_string(logic_pattern_str[pattern]);
240                 } else if (ch->type == SR_CHANNEL_ANALOG) {
241                         ag = g_hash_table_lookup(devc->ch_ag, ch);
242                         pattern = ag->pattern;
243                         *data = g_variant_new_string(analog_pattern_str[pattern]);
244                 } else
245                         return SR_ERR_BUG;
246                 break;
247         case SR_CONF_AMPLITUDE:
248                 if (!cg)
249                         return SR_ERR_CHANNEL_GROUP;
250                 /* Any channel in the group will do. */
251                 ch = cg->channels->data;
252                 if (ch->type != SR_CHANNEL_ANALOG)
253                         return SR_ERR_ARG;
254                 ag = g_hash_table_lookup(devc->ch_ag, ch);
255                 *data = g_variant_new_double(ag->amplitude);
256                 break;
257         default:
258                 return SR_ERR_NA;
259         }
260
261         return SR_OK;
262 }
263
264 static int config_set(uint32_t key, GVariant *data,
265         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
266 {
267         struct dev_context *devc;
268         struct analog_gen *ag;
269         struct sr_channel *ch;
270         GSList *l;
271         int logic_pattern, analog_pattern;
272
273         devc = sdi->priv;
274
275         switch (key) {
276         case SR_CONF_SAMPLERATE:
277                 devc->cur_samplerate = g_variant_get_uint64(data);
278                 break;
279         case SR_CONF_LIMIT_SAMPLES:
280                 devc->limit_msec = 0;
281                 devc->limit_samples = g_variant_get_uint64(data);
282                 break;
283         case SR_CONF_LIMIT_MSEC:
284                 devc->limit_msec = g_variant_get_uint64(data);
285                 devc->limit_samples = 0;
286                 break;
287         case SR_CONF_AVERAGING:
288                 devc->avg = g_variant_get_boolean(data);
289                 sr_dbg("%s averaging", devc->avg ? "Enabling" : "Disabling");
290                 break;
291         case SR_CONF_AVG_SAMPLES:
292                 devc->avg_samples = g_variant_get_uint64(data);
293                 sr_dbg("Setting averaging rate to %" PRIu64, devc->avg_samples);
294                 break;
295         case SR_CONF_PATTERN_MODE:
296                 if (!cg)
297                         return SR_ERR_CHANNEL_GROUP;
298                 logic_pattern = std_str_idx(data, ARRAY_AND_SIZE(logic_pattern_str));
299                 analog_pattern = std_str_idx(data, ARRAY_AND_SIZE(analog_pattern_str));
300                 if (logic_pattern < 0 && analog_pattern < 0)
301                         return SR_ERR_ARG;
302                 for (l = cg->channels; l; l = l->next) {
303                         ch = l->data;
304                         if (ch->type == SR_CHANNEL_LOGIC) {
305                                 if (logic_pattern == -1)
306                                         return SR_ERR_ARG;
307                                 sr_dbg("Setting logic pattern to %s",
308                                                 logic_pattern_str[logic_pattern]);
309                                 devc->logic_pattern = logic_pattern;
310                                 /* Might as well do this now, these are static. */
311                                 if (logic_pattern == PATTERN_ALL_LOW)
312                                         memset(devc->logic_data, 0x00, LOGIC_BUFSIZE);
313                                 else if (logic_pattern == PATTERN_ALL_HIGH)
314                                         memset(devc->logic_data, 0xff, LOGIC_BUFSIZE);
315                         } else if (ch->type == SR_CHANNEL_ANALOG) {
316                                 if (analog_pattern == -1)
317                                         return SR_ERR_ARG;
318                                 sr_dbg("Setting analog pattern for channel %s to %s",
319                                                 ch->name, analog_pattern_str[analog_pattern]);
320                                 ag = g_hash_table_lookup(devc->ch_ag, ch);
321                                 ag->pattern = analog_pattern;
322                         } else
323                                 return SR_ERR_BUG;
324                 }
325                 break;
326         case SR_CONF_AMPLITUDE:
327                 if (!cg)
328                         return SR_ERR_CHANNEL_GROUP;
329                 for (l = cg->channels; l; l = l->next) {
330                         ch = l->data;
331                         if (ch->type != SR_CHANNEL_ANALOG)
332                                 return SR_ERR_ARG;
333                         ag = g_hash_table_lookup(devc->ch_ag, ch);
334                         ag->amplitude = g_variant_get_double(data);
335                 }
336                 break;
337         default:
338                 return SR_ERR_NA;
339         }
340
341         return SR_OK;
342 }
343
344 static int config_list(uint32_t key, GVariant **data,
345         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
346 {
347         struct sr_channel *ch;
348
349         if (!cg) {
350                 switch (key) {
351                 case SR_CONF_SCAN_OPTIONS:
352                 case SR_CONF_DEVICE_OPTIONS:
353                         return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
354                 case SR_CONF_SAMPLERATE:
355                         *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates));
356                         break;
357                 default:
358                         return SR_ERR_NA;
359                 }
360         } else {
361                 ch = cg->channels->data;
362                 switch (key) {
363                 case SR_CONF_DEVICE_OPTIONS:
364                         if (ch->type == SR_CHANNEL_LOGIC)
365                                 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_logic));
366                         else if (ch->type == SR_CHANNEL_ANALOG) {
367                                 if (strcmp(cg->name, "Analog") == 0)
368                                         *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog_group));
369                                 else
370                                         *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog_channel));
371                         }
372                         else
373                                 return SR_ERR_BUG;
374                         break;
375                 case SR_CONF_PATTERN_MODE:
376                         /* The analog group (with all 4 channels) shall not have a pattern property. */
377                         if (strcmp(cg->name, "Analog") == 0)
378                                 return SR_ERR_NA;
379
380                         if (ch->type == SR_CHANNEL_LOGIC)
381                                 *data = g_variant_new_strv(ARRAY_AND_SIZE(logic_pattern_str));
382                         else if (ch->type == SR_CHANNEL_ANALOG)
383                                 *data = g_variant_new_strv(ARRAY_AND_SIZE(analog_pattern_str));
384                         else
385                                 return SR_ERR_BUG;
386                         break;
387                 default:
388                         return SR_ERR_NA;
389                 }
390         }
391
392         return SR_OK;
393 }
394
395 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
396 {
397         struct dev_context *devc;
398         GSList *l;
399         struct sr_channel *ch;
400         int bitpos;
401         uint8_t mask;
402         GHashTableIter iter;
403         void *value;
404
405         devc = sdi->priv;
406         devc->sent_samples = 0;
407         devc->sent_frame_samples = 0;
408
409         /*
410          * Determine the numbers of logic and analog channels that are
411          * involved in the acquisition. Determine an offset and a mask to
412          * remove excess logic data content before datafeed submission.
413          */
414         devc->enabled_logic_channels = 0;
415         devc->enabled_analog_channels = 0;
416         for (l = sdi->channels; l; l = l->next) {
417                 ch = l->data;
418                 if (!ch->enabled)
419                         continue;
420                 if (ch->type == SR_CHANNEL_ANALOG) {
421                         devc->enabled_analog_channels++;
422                         continue;
423                 }
424                 if (ch->type != SR_CHANNEL_LOGIC)
425                         continue;
426                 /*
427                  * TODO: Need we create a channel map here, such that the
428                  * session datafeed packets will have a dense representation
429                  * of the enabled channels' data? For example store channels
430                  * D3 and D5 in bit positions 0 and 1 respectively, when all
431                  * other channels are disabled? The current implementation
432                  * generates a sparse layout, might provide data for logic
433                  * channels that are disabled while it might suppress data
434                  * from enabled channels at the same time.
435                  */
436                 devc->enabled_logic_channels++;
437         }
438         devc->first_partial_logic_index = devc->enabled_logic_channels / 8;
439         bitpos = devc->enabled_logic_channels % 8;
440         mask = (1 << bitpos) - 1;
441         devc->first_partial_logic_mask = mask;
442         sr_dbg("num logic %zu, partial off %zu, mask 0x%02x.",
443                 devc->enabled_logic_channels,
444                 devc->first_partial_logic_index,
445                 devc->first_partial_logic_mask);
446
447         /*
448          * Have the waveform for analog patterns pre-generated. It's
449          * supposed to be periodic, so the generator just needs to
450          * access the prepared sample data (DDS style).
451          */
452         g_hash_table_iter_init(&iter, devc->ch_ag);
453         while (g_hash_table_iter_next(&iter, NULL, &value))
454                 demo_generate_analog_pattern(value, devc->cur_samplerate);
455
456         sr_session_source_add(sdi->session, -1, 0, 100,
457                         demo_prepare_data, (struct sr_dev_inst *)sdi);
458
459         std_session_send_df_header(sdi);
460
461         if (SAMPLES_PER_FRAME > 0)
462                 std_session_send_frame_begin(sdi);
463
464         /* We use this timestamp to decide how many more samples to send. */
465         devc->start_us = g_get_monotonic_time();
466         devc->spent_us = 0;
467         devc->step = 0;
468
469         return SR_OK;
470 }
471
472 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
473 {
474         sr_session_source_remove(sdi->session, -1);
475
476         if (SAMPLES_PER_FRAME > 0)
477                 std_session_send_frame_end(sdi);
478
479         std_session_send_df_end(sdi);
480
481         return SR_OK;
482 }
483
484 static struct sr_dev_driver demo_driver_info = {
485         .name = "demo",
486         .longname = "Demo driver and pattern generator",
487         .api_version = 1,
488         .init = std_init,
489         .cleanup = std_cleanup,
490         .scan = scan,
491         .dev_list = std_dev_list,
492         .dev_clear = dev_clear,
493         .config_get = config_get,
494         .config_set = config_set,
495         .config_list = config_list,
496         .dev_open = std_dummy_dev_open,
497         .dev_close = std_dummy_dev_close,
498         .dev_acquisition_start = dev_acquisition_start,
499         .dev_acquisition_stop = dev_acquisition_stop,
500         .context = NULL,
501 };
502 SR_REGISTER_DEV_DRIVER(demo_driver_info);