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