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