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