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 5
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 = sr_channel_group_new(sdi, "Logic", NULL);
155 for (i = 0; i < num_logic_channels; i++) {
156 sprintf(channel_name, "D%d", i);
157 ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name);
158 cg->channels = g_slist_append(cg->channels, ch);
162 /* Analog channels, channel groups and pattern generators. */
163 devc->ch_ag = g_hash_table_new(g_direct_hash, g_direct_equal);
164 if (num_analog_channels > 0) {
166 * Have the waveform for analog patterns pre-generated. It's
167 * supposed to be periodic, so the generator just needs to
168 * access the prepared sample data (DDS style).
170 demo_generate_analog_pattern(devc);
173 /* An "Analog" channel group with all analog channels in it. */
174 acg = sr_channel_group_new(sdi, "Analog", NULL);
176 for (i = 0; i < num_analog_channels; i++) {
177 snprintf(channel_name, 16, "A%d", i);
178 ch = sr_channel_new(sdi, i + num_logic_channels, SR_CHANNEL_ANALOG,
180 acg->channels = g_slist_append(acg->channels, ch);
182 /* Every analog channel gets its own channel group as well. */
183 cg = sr_channel_group_new(sdi, channel_name, NULL);
184 cg->channels = g_slist_append(NULL, ch);
186 /* Every channel gets a generator struct. */
187 ag = g_malloc(sizeof(struct analog_gen));
189 ag->mq = SR_MQ_VOLTAGE;
190 ag->mq_flags = SR_MQFLAG_DC;
191 ag->unit = SR_UNIT_VOLT;
192 ag->amplitude = DEFAULT_ANALOG_AMPLITUDE;
193 ag->offset = DEFAULT_ANALOG_OFFSET;
194 sr_analog_init(&ag->packet, &ag->encoding, &ag->meaning, &ag->spec, 2);
195 ag->packet.meaning->channels = cg->channels;
196 ag->packet.meaning->mq = ag->mq;
197 ag->packet.meaning->mqflags = ag->mq_flags;
198 ag->packet.meaning->unit = ag->unit;
199 ag->packet.encoding->digits = DEFAULT_ANALOG_ENCODING_DIGITS;
200 ag->packet.spec->spec_digits = DEFAULT_ANALOG_SPEC_DIGITS;
201 ag->packet.data = devc->analog_patterns[pattern];
202 ag->pattern = pattern;
205 g_hash_table_insert(devc->ch_ag, ch, ag);
207 if (++pattern == ARRAY_SIZE(analog_pattern_str))
214 return std_scan_complete(di, g_slist_append(NULL, sdi));
217 static void clear_helper(struct dev_context *devc)
222 demo_free_analog_pattern(devc);
224 /* Analog generators. */
225 g_hash_table_iter_init(&iter, devc->ch_ag);
226 while (g_hash_table_iter_next(&iter, NULL, &value))
228 g_hash_table_unref(devc->ch_ag);
231 static int dev_clear(const struct sr_dev_driver *di)
233 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
236 static int config_get(uint32_t key, GVariant **data,
237 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
239 struct dev_context *devc;
240 struct sr_channel *ch;
241 struct analog_gen *ag;
250 case SR_CONF_SAMPLERATE:
251 *data = g_variant_new_uint64(devc->cur_samplerate);
253 case SR_CONF_LIMIT_SAMPLES:
254 *data = g_variant_new_uint64(devc->limit_samples);
256 case SR_CONF_LIMIT_MSEC:
257 *data = g_variant_new_uint64(devc->limit_msec);
259 case SR_CONF_LIMIT_FRAMES:
260 *data = g_variant_new_uint64(devc->limit_frames);
262 case SR_CONF_AVERAGING:
263 *data = g_variant_new_boolean(devc->avg);
265 case SR_CONF_AVG_SAMPLES:
266 *data = g_variant_new_uint64(devc->avg_samples);
268 case SR_CONF_MEASURED_QUANTITY:
270 return SR_ERR_CHANNEL_GROUP;
271 /* Any channel in the group will do. */
272 ch = cg->channels->data;
273 if (ch->type != SR_CHANNEL_ANALOG)
275 ag = g_hash_table_lookup(devc->ch_ag, ch);
276 mq_arr[0] = g_variant_new_uint32(ag->mq);
277 mq_arr[1] = g_variant_new_uint64(ag->mq_flags);
278 *data = g_variant_new_tuple(mq_arr, 2);
280 case SR_CONF_PATTERN_MODE:
282 return SR_ERR_CHANNEL_GROUP;
283 /* Any channel in the group will do. */
284 ch = cg->channels->data;
285 if (ch->type == SR_CHANNEL_LOGIC) {
286 pattern = devc->logic_pattern;
287 *data = g_variant_new_string(logic_pattern_str[pattern]);
288 } else if (ch->type == SR_CHANNEL_ANALOG) {
289 ag = g_hash_table_lookup(devc->ch_ag, ch);
290 pattern = ag->pattern;
291 *data = g_variant_new_string(analog_pattern_str[pattern]);
295 case SR_CONF_AMPLITUDE:
297 return SR_ERR_CHANNEL_GROUP;
298 /* Any channel in the group will do. */
299 ch = cg->channels->data;
300 if (ch->type != SR_CHANNEL_ANALOG)
302 ag = g_hash_table_lookup(devc->ch_ag, ch);
303 *data = g_variant_new_double(ag->amplitude);
307 return SR_ERR_CHANNEL_GROUP;
308 /* Any channel in the group will do. */
309 ch = cg->channels->data;
310 if (ch->type != SR_CHANNEL_ANALOG)
312 ag = g_hash_table_lookup(devc->ch_ag, ch);
313 *data = g_variant_new_double(ag->offset);
315 case SR_CONF_CAPTURE_RATIO:
316 *data = g_variant_new_uint64(devc->capture_ratio);
325 static int config_set(uint32_t key, GVariant *data,
326 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
328 struct dev_context *devc;
329 struct analog_gen *ag;
330 struct sr_channel *ch;
331 GVariant *mq_tuple_child;
333 int logic_pattern, analog_pattern;
338 case SR_CONF_SAMPLERATE:
339 devc->cur_samplerate = g_variant_get_uint64(data);
341 case SR_CONF_LIMIT_SAMPLES:
342 devc->limit_msec = 0;
343 devc->limit_samples = g_variant_get_uint64(data);
345 case SR_CONF_LIMIT_MSEC:
346 devc->limit_msec = g_variant_get_uint64(data);
347 devc->limit_samples = 0;
349 case SR_CONF_LIMIT_FRAMES:
350 devc->limit_frames = g_variant_get_uint64(data);
352 case SR_CONF_AVERAGING:
353 devc->avg = g_variant_get_boolean(data);
354 sr_dbg("%s averaging", devc->avg ? "Enabling" : "Disabling");
356 case SR_CONF_AVG_SAMPLES:
357 devc->avg_samples = g_variant_get_uint64(data);
358 sr_dbg("Setting averaging rate to %" PRIu64, devc->avg_samples);
360 case SR_CONF_MEASURED_QUANTITY:
362 return SR_ERR_CHANNEL_GROUP;
363 for (l = cg->channels; l; l = l->next) {
365 if (ch->type != SR_CHANNEL_ANALOG)
367 ag = g_hash_table_lookup(devc->ch_ag, ch);
368 mq_tuple_child = g_variant_get_child_value(data, 0);
369 ag->mq = g_variant_get_uint32(mq_tuple_child);
370 mq_tuple_child = g_variant_get_child_value(data, 1);
371 ag->mq_flags = g_variant_get_uint64(mq_tuple_child);
372 g_variant_unref(mq_tuple_child);
375 case SR_CONF_PATTERN_MODE:
377 return SR_ERR_CHANNEL_GROUP;
378 logic_pattern = std_str_idx(data, ARRAY_AND_SIZE(logic_pattern_str));
379 analog_pattern = std_str_idx(data, ARRAY_AND_SIZE(analog_pattern_str));
380 if (logic_pattern < 0 && analog_pattern < 0)
382 for (l = cg->channels; l; l = l->next) {
384 if (ch->type == SR_CHANNEL_LOGIC) {
385 if (logic_pattern == -1)
387 sr_dbg("Setting logic pattern to %s",
388 logic_pattern_str[logic_pattern]);
389 devc->logic_pattern = logic_pattern;
390 /* Might as well do this now, these are static. */
391 if (logic_pattern == PATTERN_ALL_LOW)
392 memset(devc->logic_data, 0x00, LOGIC_BUFSIZE);
393 else if (logic_pattern == PATTERN_ALL_HIGH)
394 memset(devc->logic_data, 0xff, LOGIC_BUFSIZE);
395 } else if (ch->type == SR_CHANNEL_ANALOG) {
396 if (analog_pattern == -1)
398 sr_dbg("Setting analog pattern for channel %s to %s",
399 ch->name, analog_pattern_str[analog_pattern]);
400 ag = g_hash_table_lookup(devc->ch_ag, ch);
401 ag->pattern = analog_pattern;
406 case SR_CONF_AMPLITUDE:
408 return SR_ERR_CHANNEL_GROUP;
409 for (l = cg->channels; l; l = l->next) {
411 if (ch->type != SR_CHANNEL_ANALOG)
413 ag = g_hash_table_lookup(devc->ch_ag, ch);
414 ag->amplitude = g_variant_get_double(data);
419 return SR_ERR_CHANNEL_GROUP;
420 for (l = cg->channels; l; l = l->next) {
422 if (ch->type != SR_CHANNEL_ANALOG)
424 ag = g_hash_table_lookup(devc->ch_ag, ch);
425 ag->offset = g_variant_get_double(data);
428 case SR_CONF_CAPTURE_RATIO:
429 devc->capture_ratio = g_variant_get_uint64(data);
438 static int config_list(uint32_t key, GVariant **data,
439 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
441 struct sr_channel *ch;
445 case SR_CONF_SCAN_OPTIONS:
446 case SR_CONF_DEVICE_OPTIONS:
447 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
448 case SR_CONF_SAMPLERATE:
449 *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates));
451 case SR_CONF_TRIGGER_MATCH:
452 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
458 ch = cg->channels->data;
460 case SR_CONF_DEVICE_OPTIONS:
461 if (ch->type == SR_CHANNEL_LOGIC)
462 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_logic));
463 else if (ch->type == SR_CHANNEL_ANALOG) {
464 if (strcmp(cg->name, "Analog") == 0)
465 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog_group));
467 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog_channel));
472 case SR_CONF_PATTERN_MODE:
473 /* The analog group (with all 4 channels) shall not have a pattern property. */
474 if (strcmp(cg->name, "Analog") == 0)
477 if (ch->type == SR_CHANNEL_LOGIC)
478 *data = g_variant_new_strv(ARRAY_AND_SIZE(logic_pattern_str));
479 else if (ch->type == SR_CHANNEL_ANALOG)
480 *data = g_variant_new_strv(ARRAY_AND_SIZE(analog_pattern_str));
492 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
494 struct dev_context *devc;
496 struct sr_channel *ch;
499 struct sr_trigger *trigger;
502 devc->sent_samples = 0;
503 devc->sent_frame_samples = 0;
506 if ((trigger = sr_session_trigger_get(sdi->session))) {
507 int pre_trigger_samples = 0;
508 if (devc->limit_samples > 0)
509 pre_trigger_samples = (devc->capture_ratio * devc->limit_samples) / 100;
510 devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples);
512 return SR_ERR_MALLOC;
514 /* Disable all analog channels since using them when there are logic
515 * triggers set up would require having pre-trigger sample buffers
516 * for analog sample data.
518 for (l = sdi->channels; l; l = l->next) {
520 if (ch->type == SR_CHANNEL_ANALOG)
524 devc->trigger_fired = FALSE;
527 * Determine the numbers of logic and analog channels that are
528 * involved in the acquisition. Determine an offset and a mask to
529 * remove excess logic data content before datafeed submission.
531 devc->enabled_logic_channels = 0;
532 devc->enabled_analog_channels = 0;
533 for (l = sdi->channels; l; l = l->next) {
537 if (ch->type == SR_CHANNEL_ANALOG) {
538 devc->enabled_analog_channels++;
541 if (ch->type != SR_CHANNEL_LOGIC)
544 * TODO: Need we create a channel map here, such that the
545 * session datafeed packets will have a dense representation
546 * of the enabled channels' data? For example store channels
547 * D3 and D5 in bit positions 0 and 1 respectively, when all
548 * other channels are disabled? The current implementation
549 * generates a sparse layout, might provide data for logic
550 * channels that are disabled while it might suppress data
551 * from enabled channels at the same time.
553 devc->enabled_logic_channels++;
555 devc->first_partial_logic_index = devc->enabled_logic_channels / 8;
556 bitpos = devc->enabled_logic_channels % 8;
557 mask = (1 << bitpos) - 1;
558 devc->first_partial_logic_mask = mask;
559 sr_dbg("num logic %zu, partial off %zu, mask 0x%02x.",
560 devc->enabled_logic_channels,
561 devc->first_partial_logic_index,
562 devc->first_partial_logic_mask);
564 sr_session_source_add(sdi->session, -1, 0, 100,
565 demo_prepare_data, (struct sr_dev_inst *)sdi);
567 std_session_send_df_header(sdi);
569 if (devc->limit_frames > 0)
570 std_session_send_df_frame_begin(sdi);
572 /* We use this timestamp to decide how many more samples to send. */
573 devc->start_us = g_get_monotonic_time();
580 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
582 struct dev_context *devc;
584 sr_session_source_remove(sdi->session, -1);
587 if (devc->limit_frames > 0)
588 std_session_send_df_frame_end(sdi);
590 std_session_send_df_end(sdi);
593 soft_trigger_logic_free(devc->stl);
600 static struct sr_dev_driver demo_driver_info = {
602 .longname = "Demo driver and pattern generator",
605 .cleanup = std_cleanup,
607 .dev_list = std_dev_list,
608 .dev_clear = dev_clear,
609 .config_get = config_get,
610 .config_set = config_set,
611 .config_list = config_list,
612 .dev_open = std_dummy_dev_open,
613 .dev_close = std_dummy_dev_close,
614 .dev_acquisition_start = dev_acquisition_start,
615 .dev_acquisition_stop = dev_acquisition_stop,
618 SR_REGISTER_DEV_DRIVER(demo_driver_info);