2 * This file is part of the libsigrok project.
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 * Copyright (C) 2019 Frank Stettner <frank-stettner@gmx.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <http://www.gnu.org/licenses/>.
28 #include <libsigrok/libsigrok.h>
29 #include "libsigrok-internal.h"
32 #define DEFAULT_NUM_LOGIC_CHANNELS 8
33 #define DEFAULT_LOGIC_PATTERN PATTERN_SIGROK
35 #define DEFAULT_NUM_ANALOG_CHANNELS 4
37 /* Note: No spaces allowed because of sigrok-cli. */
38 static const char *logic_pattern_str[] = {
50 static const uint32_t scanopts[] = {
51 SR_CONF_NUM_LOGIC_CHANNELS,
52 SR_CONF_NUM_ANALOG_CHANNELS,
56 static const uint32_t drvopts[] = {
58 SR_CONF_LOGIC_ANALYZER,
62 static const uint32_t devopts[] = {
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,
74 static const uint32_t devopts_cg_logic[] = {
75 SR_CONF_PATTERN_MODE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
78 static const uint32_t devopts_cg_analog_group[] = {
79 SR_CONF_AMPLITUDE | SR_CONF_GET | SR_CONF_SET,
80 SR_CONF_OFFSET | SR_CONF_GET | SR_CONF_SET,
83 static const uint32_t devopts_cg_analog_channel[] = {
84 SR_CONF_MEASURED_QUANTITY | SR_CONF_GET | SR_CONF_SET,
85 SR_CONF_PATTERN_MODE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
86 SR_CONF_AMPLITUDE | SR_CONF_GET | SR_CONF_SET,
87 SR_CONF_OFFSET | SR_CONF_GET | SR_CONF_SET,
90 static const int32_t trigger_matches[] = {
98 static const uint64_t samplerates[] = {
104 static GSList *scan(struct sr_dev_driver *di, GSList *options)
106 struct dev_context *devc;
107 struct sr_dev_inst *sdi;
108 struct sr_channel *ch;
109 struct sr_channel_group *cg, *acg;
110 struct sr_config *src;
111 struct analog_gen *ag;
113 int num_logic_channels, num_analog_channels, pattern, i;
114 uint64_t limit_frames;
115 char channel_name[16];
117 num_logic_channels = DEFAULT_NUM_LOGIC_CHANNELS;
118 num_analog_channels = DEFAULT_NUM_ANALOG_CHANNELS;
119 limit_frames = DEFAULT_LIMIT_FRAMES;
120 for (l = options; l; l = l->next) {
123 case SR_CONF_NUM_LOGIC_CHANNELS:
124 num_logic_channels = g_variant_get_int32(src->data);
126 case SR_CONF_NUM_ANALOG_CHANNELS:
127 num_analog_channels = g_variant_get_int32(src->data);
129 case SR_CONF_LIMIT_FRAMES:
130 limit_frames = g_variant_get_uint64(src->data);
135 sdi = g_malloc0(sizeof(struct sr_dev_inst));
136 sdi->status = SR_ST_INACTIVE;
137 sdi->model = g_strdup("Demo device");
139 devc = g_malloc0(sizeof(struct dev_context));
140 devc->cur_samplerate = SR_KHZ(200);
141 devc->num_logic_channels = num_logic_channels;
142 devc->logic_unitsize = (devc->num_logic_channels + 7) / 8;
143 devc->all_logic_channels_mask = 1UL << 0;
144 devc->all_logic_channels_mask <<= devc->num_logic_channels;
145 devc->all_logic_channels_mask--;
146 devc->logic_pattern = DEFAULT_LOGIC_PATTERN;
147 devc->num_analog_channels = num_analog_channels;
148 devc->limit_frames = limit_frames;
149 devc->capture_ratio = 20;
152 if (num_logic_channels > 0) {
153 /* Logic channels, all in one channel group. */
154 cg = g_malloc0(sizeof(struct sr_channel_group));
155 cg->name = g_strdup("Logic");
156 for (i = 0; i < num_logic_channels; i++) {
157 sprintf(channel_name, "D%d", i);
158 ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name);
159 cg->channels = g_slist_append(cg->channels, ch);
161 sdi->channel_groups = g_slist_append(NULL, cg);
164 /* Analog channels, channel groups and pattern generators. */
165 devc->ch_ag = g_hash_table_new(g_direct_hash, g_direct_equal);
166 if (num_analog_channels > 0) {
168 * Have the waveform for analog patterns pre-generated. It's
169 * supposed to be periodic, so the generator just needs to
170 * access the prepared sample data (DDS style).
172 demo_generate_analog_pattern(devc);
175 /* An "Analog" channel group with all analog channels in it. */
176 acg = g_malloc0(sizeof(struct sr_channel_group));
177 acg->name = g_strdup("Analog");
178 sdi->channel_groups = g_slist_append(sdi->channel_groups, acg);
180 for (i = 0; i < num_analog_channels; i++) {
181 snprintf(channel_name, 16, "A%d", i);
182 ch = sr_channel_new(sdi, i + num_logic_channels, SR_CHANNEL_ANALOG,
184 acg->channels = g_slist_append(acg->channels, ch);
186 /* Every analog channel gets its own channel group as well. */
187 cg = g_malloc0(sizeof(struct sr_channel_group));
188 cg->name = g_strdup(channel_name);
189 cg->channels = g_slist_append(NULL, ch);
190 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
192 /* Every channel gets a generator struct. */
193 ag = g_malloc(sizeof(struct analog_gen));
195 ag->mq = SR_MQ_VOLTAGE;
196 ag->mq_flags = SR_MQFLAG_DC;
197 ag->unit = SR_UNIT_VOLT;
198 ag->amplitude = DEFAULT_ANALOG_AMPLITUDE;
199 ag->offset = DEFAULT_ANALOG_OFFSET;
200 sr_analog_init(&ag->packet, &ag->encoding, &ag->meaning, &ag->spec, 2);
201 ag->packet.meaning->channels = cg->channels;
202 ag->packet.meaning->mq = ag->mq;
203 ag->packet.meaning->mqflags = ag->mq_flags;
204 ag->packet.meaning->unit = ag->unit;
205 ag->packet.encoding->digits = DEFAULT_ANALOG_ENCODING_DIGITS;
206 ag->packet.spec->spec_digits = DEFAULT_ANALOG_SPEC_DIGITS;
207 ag->packet.data = devc->analog_patterns[pattern];
208 ag->pattern = pattern;
211 g_hash_table_insert(devc->ch_ag, ch, ag);
213 if (++pattern == ARRAY_SIZE(analog_pattern_str))
220 return std_scan_complete(di, g_slist_append(NULL, sdi));
223 static void clear_helper(struct dev_context *devc)
228 /* Analog generators. */
229 g_hash_table_iter_init(&iter, devc->ch_ag);
230 while (g_hash_table_iter_next(&iter, NULL, &value))
232 g_hash_table_unref(devc->ch_ag);
235 static int dev_clear(const struct sr_dev_driver *di)
237 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
240 static int config_get(uint32_t key, GVariant **data,
241 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
243 struct dev_context *devc;
244 struct sr_channel *ch;
245 struct analog_gen *ag;
254 case SR_CONF_SAMPLERATE:
255 *data = g_variant_new_uint64(devc->cur_samplerate);
257 case SR_CONF_LIMIT_SAMPLES:
258 *data = g_variant_new_uint64(devc->limit_samples);
260 case SR_CONF_LIMIT_MSEC:
261 *data = g_variant_new_uint64(devc->limit_msec);
263 case SR_CONF_LIMIT_FRAMES:
264 *data = g_variant_new_uint64(devc->limit_frames);
266 case SR_CONF_AVERAGING:
267 *data = g_variant_new_boolean(devc->avg);
269 case SR_CONF_AVG_SAMPLES:
270 *data = g_variant_new_uint64(devc->avg_samples);
272 case SR_CONF_MEASURED_QUANTITY:
274 return SR_ERR_CHANNEL_GROUP;
275 /* Any channel in the group will do. */
276 ch = cg->channels->data;
277 if (ch->type != SR_CHANNEL_ANALOG)
279 ag = g_hash_table_lookup(devc->ch_ag, ch);
280 mq_arr[0] = g_variant_new_uint32(ag->mq);
281 mq_arr[1] = g_variant_new_uint64(ag->mq_flags);
282 *data = g_variant_new_tuple(mq_arr, 2);
284 case SR_CONF_PATTERN_MODE:
286 return SR_ERR_CHANNEL_GROUP;
287 /* Any channel in the group will do. */
288 ch = cg->channels->data;
289 if (ch->type == SR_CHANNEL_LOGIC) {
290 pattern = devc->logic_pattern;
291 *data = g_variant_new_string(logic_pattern_str[pattern]);
292 } else if (ch->type == SR_CHANNEL_ANALOG) {
293 ag = g_hash_table_lookup(devc->ch_ag, ch);
294 pattern = ag->pattern;
295 *data = g_variant_new_string(analog_pattern_str[pattern]);
299 case SR_CONF_AMPLITUDE:
301 return SR_ERR_CHANNEL_GROUP;
302 /* Any channel in the group will do. */
303 ch = cg->channels->data;
304 if (ch->type != SR_CHANNEL_ANALOG)
306 ag = g_hash_table_lookup(devc->ch_ag, ch);
307 *data = g_variant_new_double(ag->amplitude);
311 return SR_ERR_CHANNEL_GROUP;
312 /* Any channel in the group will do. */
313 ch = cg->channels->data;
314 if (ch->type != SR_CHANNEL_ANALOG)
316 ag = g_hash_table_lookup(devc->ch_ag, ch);
317 *data = g_variant_new_double(ag->offset);
319 case SR_CONF_CAPTURE_RATIO:
320 *data = g_variant_new_uint64(devc->capture_ratio);
329 static int config_set(uint32_t key, GVariant *data,
330 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
332 struct dev_context *devc;
333 struct analog_gen *ag;
334 struct sr_channel *ch;
335 GVariant *mq_tuple_child;
337 int logic_pattern, analog_pattern;
342 case SR_CONF_SAMPLERATE:
343 devc->cur_samplerate = g_variant_get_uint64(data);
345 case SR_CONF_LIMIT_SAMPLES:
346 devc->limit_msec = 0;
347 devc->limit_samples = g_variant_get_uint64(data);
349 case SR_CONF_LIMIT_MSEC:
350 devc->limit_msec = g_variant_get_uint64(data);
351 devc->limit_samples = 0;
353 case SR_CONF_LIMIT_FRAMES:
354 devc->limit_frames = g_variant_get_uint64(data);
356 case SR_CONF_AVERAGING:
357 devc->avg = g_variant_get_boolean(data);
358 sr_dbg("%s averaging", devc->avg ? "Enabling" : "Disabling");
360 case SR_CONF_AVG_SAMPLES:
361 devc->avg_samples = g_variant_get_uint64(data);
362 sr_dbg("Setting averaging rate to %" PRIu64, devc->avg_samples);
364 case SR_CONF_MEASURED_QUANTITY:
366 return SR_ERR_CHANNEL_GROUP;
367 for (l = cg->channels; l; l = l->next) {
369 if (ch->type != SR_CHANNEL_ANALOG)
371 ag = g_hash_table_lookup(devc->ch_ag, ch);
372 mq_tuple_child = g_variant_get_child_value(data, 0);
373 ag->mq = g_variant_get_uint32(mq_tuple_child);
374 mq_tuple_child = g_variant_get_child_value(data, 1);
375 ag->mq_flags = g_variant_get_uint64(mq_tuple_child);
376 g_variant_unref(mq_tuple_child);
379 case SR_CONF_PATTERN_MODE:
381 return SR_ERR_CHANNEL_GROUP;
382 logic_pattern = std_str_idx(data, ARRAY_AND_SIZE(logic_pattern_str));
383 analog_pattern = std_str_idx(data, ARRAY_AND_SIZE(analog_pattern_str));
384 if (logic_pattern < 0 && analog_pattern < 0)
386 for (l = cg->channels; l; l = l->next) {
388 if (ch->type == SR_CHANNEL_LOGIC) {
389 if (logic_pattern == -1)
391 sr_dbg("Setting logic pattern to %s",
392 logic_pattern_str[logic_pattern]);
393 devc->logic_pattern = logic_pattern;
394 /* Might as well do this now, these are static. */
395 if (logic_pattern == PATTERN_ALL_LOW)
396 memset(devc->logic_data, 0x00, LOGIC_BUFSIZE);
397 else if (logic_pattern == PATTERN_ALL_HIGH)
398 memset(devc->logic_data, 0xff, LOGIC_BUFSIZE);
399 } else if (ch->type == SR_CHANNEL_ANALOG) {
400 if (analog_pattern == -1)
402 sr_dbg("Setting analog pattern for channel %s to %s",
403 ch->name, analog_pattern_str[analog_pattern]);
404 ag = g_hash_table_lookup(devc->ch_ag, ch);
405 ag->pattern = analog_pattern;
410 case SR_CONF_AMPLITUDE:
412 return SR_ERR_CHANNEL_GROUP;
413 for (l = cg->channels; l; l = l->next) {
415 if (ch->type != SR_CHANNEL_ANALOG)
417 ag = g_hash_table_lookup(devc->ch_ag, ch);
418 ag->amplitude = g_variant_get_double(data);
423 return SR_ERR_CHANNEL_GROUP;
424 for (l = cg->channels; l; l = l->next) {
426 if (ch->type != SR_CHANNEL_ANALOG)
428 ag = g_hash_table_lookup(devc->ch_ag, ch);
429 ag->offset = g_variant_get_double(data);
432 case SR_CONF_CAPTURE_RATIO:
433 devc->capture_ratio = g_variant_get_uint64(data);
442 static int config_list(uint32_t key, GVariant **data,
443 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
445 struct sr_channel *ch;
449 case SR_CONF_SCAN_OPTIONS:
450 case SR_CONF_DEVICE_OPTIONS:
451 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
452 case SR_CONF_SAMPLERATE:
453 *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates));
455 case SR_CONF_TRIGGER_MATCH:
456 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
462 ch = cg->channels->data;
464 case SR_CONF_DEVICE_OPTIONS:
465 if (ch->type == SR_CHANNEL_LOGIC)
466 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_logic));
467 else if (ch->type == SR_CHANNEL_ANALOG) {
468 if (strcmp(cg->name, "Analog") == 0)
469 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog_group));
471 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog_channel));
476 case SR_CONF_PATTERN_MODE:
477 /* The analog group (with all 4 channels) shall not have a pattern property. */
478 if (strcmp(cg->name, "Analog") == 0)
481 if (ch->type == SR_CHANNEL_LOGIC)
482 *data = g_variant_new_strv(ARRAY_AND_SIZE(logic_pattern_str));
483 else if (ch->type == SR_CHANNEL_ANALOG)
484 *data = g_variant_new_strv(ARRAY_AND_SIZE(analog_pattern_str));
496 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
498 struct dev_context *devc;
500 struct sr_channel *ch;
503 struct sr_trigger *trigger;
506 devc->sent_samples = 0;
507 devc->sent_frame_samples = 0;
510 if ((trigger = sr_session_trigger_get(sdi->session))) {
511 int pre_trigger_samples = 0;
512 if (devc->limit_samples > 0)
513 pre_trigger_samples = (devc->capture_ratio * devc->limit_samples) / 100;
514 devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples);
516 return SR_ERR_MALLOC;
518 /* Disable all analog channels since using them when there are logic
519 * triggers set up would require having pre-trigger sample buffers
520 * for analog sample data.
522 for (l = sdi->channels; l; l = l->next) {
524 if (ch->type == SR_CHANNEL_ANALOG)
528 devc->trigger_fired = FALSE;
531 * Determine the numbers of logic and analog channels that are
532 * involved in the acquisition. Determine an offset and a mask to
533 * remove excess logic data content before datafeed submission.
535 devc->enabled_logic_channels = 0;
536 devc->enabled_analog_channels = 0;
537 for (l = sdi->channels; l; l = l->next) {
541 if (ch->type == SR_CHANNEL_ANALOG) {
542 devc->enabled_analog_channels++;
545 if (ch->type != SR_CHANNEL_LOGIC)
548 * TODO: Need we create a channel map here, such that the
549 * session datafeed packets will have a dense representation
550 * of the enabled channels' data? For example store channels
551 * D3 and D5 in bit positions 0 and 1 respectively, when all
552 * other channels are disabled? The current implementation
553 * generates a sparse layout, might provide data for logic
554 * channels that are disabled while it might suppress data
555 * from enabled channels at the same time.
557 devc->enabled_logic_channels++;
559 devc->first_partial_logic_index = devc->enabled_logic_channels / 8;
560 bitpos = devc->enabled_logic_channels % 8;
561 mask = (1 << bitpos) - 1;
562 devc->first_partial_logic_mask = mask;
563 sr_dbg("num logic %zu, partial off %zu, mask 0x%02x.",
564 devc->enabled_logic_channels,
565 devc->first_partial_logic_index,
566 devc->first_partial_logic_mask);
568 sr_session_source_add(sdi->session, -1, 0, 100,
569 demo_prepare_data, (struct sr_dev_inst *)sdi);
571 std_session_send_df_header(sdi);
573 if (devc->limit_frames > 0)
574 std_session_send_frame_begin(sdi);
576 /* We use this timestamp to decide how many more samples to send. */
577 devc->start_us = g_get_monotonic_time();
584 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
586 struct dev_context *devc;
588 sr_session_source_remove(sdi->session, -1);
591 if (devc->limit_frames > 0)
592 std_session_send_frame_end(sdi);
594 std_session_send_df_end(sdi);
597 soft_trigger_logic_free(devc->stl);
604 static struct sr_dev_driver demo_driver_info = {
606 .longname = "Demo driver and pattern generator",
609 .cleanup = std_cleanup,
611 .dev_list = std_dev_list,
612 .dev_clear = dev_clear,
613 .config_get = config_get,
614 .config_set = config_set,
615 .config_list = config_list,
616 .dev_open = std_dummy_dev_open,
617 .dev_close = std_dummy_dev_close,
618 .dev_acquisition_start = dev_acquisition_start,
619 .dev_acquisition_stop = dev_acquisition_stop,
622 SR_REGISTER_DEV_DRIVER(demo_driver_info);