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