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