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