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,
82 static const uint32_t devopts_cg_analog_channel[] = {
83 SR_CONF_MEASURED_QUANTITY | SR_CONF_GET | SR_CONF_SET,
84 SR_CONF_PATTERN_MODE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
85 SR_CONF_AMPLITUDE | SR_CONF_GET | SR_CONF_SET,
88 static const int32_t trigger_matches[] = {
96 static const uint64_t samplerates[] = {
102 static GSList *scan(struct sr_dev_driver *di, GSList *options)
104 struct dev_context *devc;
105 struct sr_dev_inst *sdi;
106 struct sr_channel *ch;
107 struct sr_channel_group *cg, *acg;
108 struct sr_config *src;
109 struct analog_gen *ag;
111 int num_logic_channels, num_analog_channels, pattern, i;
112 uint64_t limit_frames;
113 char channel_name[16];
115 num_logic_channels = DEFAULT_NUM_LOGIC_CHANNELS;
116 num_analog_channels = DEFAULT_NUM_ANALOG_CHANNELS;
117 limit_frames = DEFAULT_LIMIT_FRAMES;
118 for (l = options; l; l = l->next) {
121 case SR_CONF_NUM_LOGIC_CHANNELS:
122 num_logic_channels = g_variant_get_int32(src->data);
124 case SR_CONF_NUM_ANALOG_CHANNELS:
125 num_analog_channels = g_variant_get_int32(src->data);
127 case SR_CONF_LIMIT_FRAMES:
128 limit_frames = g_variant_get_uint64(src->data);
133 sdi = g_malloc0(sizeof(struct sr_dev_inst));
134 sdi->status = SR_ST_INACTIVE;
135 sdi->model = g_strdup("Demo device");
137 devc = g_malloc0(sizeof(struct dev_context));
138 devc->cur_samplerate = SR_KHZ(200);
139 devc->num_logic_channels = num_logic_channels;
140 devc->logic_unitsize = (devc->num_logic_channels + 7) / 8;
141 devc->all_logic_channels_mask = 1UL << 0;
142 devc->all_logic_channels_mask <<= devc->num_logic_channels;
143 devc->all_logic_channels_mask--;
144 devc->logic_pattern = DEFAULT_LOGIC_PATTERN;
145 devc->num_analog_channels = num_analog_channels;
146 devc->limit_frames = limit_frames;
147 devc->capture_ratio = 20;
150 if (num_logic_channels > 0) {
151 /* Logic channels, all in one channel group. */
152 cg = g_malloc0(sizeof(struct sr_channel_group));
153 cg->name = g_strdup("Logic");
154 for (i = 0; i < num_logic_channels; i++) {
155 sprintf(channel_name, "D%d", i);
156 ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name);
157 cg->channels = g_slist_append(cg->channels, ch);
159 sdi->channel_groups = g_slist_append(NULL, cg);
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 = g_malloc0(sizeof(struct sr_channel_group));
175 acg->name = g_strdup("Analog");
176 sdi->channel_groups = g_slist_append(sdi->channel_groups, acg);
178 for (i = 0; i < num_analog_channels; i++) {
179 snprintf(channel_name, 16, "A%d", i);
180 ch = sr_channel_new(sdi, i + num_logic_channels, SR_CHANNEL_ANALOG,
182 acg->channels = g_slist_append(acg->channels, ch);
184 /* Every analog channel gets its own channel group as well. */
185 cg = g_malloc0(sizeof(struct sr_channel_group));
186 cg->name = g_strdup(channel_name);
187 cg->channels = g_slist_append(NULL, ch);
188 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
190 /* Every channel gets a generator struct. */
191 ag = g_malloc(sizeof(struct analog_gen));
193 ag->mq = SR_MQ_VOLTAGE;
194 ag->mq_flags = SR_MQFLAG_DC;
195 ag->unit = SR_UNIT_VOLT;
196 ag->amplitude = DEFAULT_ANALOG_AMPLITUDE;
197 ag->offset = DEFAULT_ANALOG_OFFSET;
198 sr_analog_init(&ag->packet, &ag->encoding, &ag->meaning, &ag->spec, 2);
199 ag->packet.meaning->channels = cg->channels;
200 ag->packet.meaning->mq = ag->mq;
201 ag->packet.meaning->mqflags = ag->mq_flags;
202 ag->packet.meaning->unit = ag->unit;
203 ag->packet.encoding->digits = DEFAULT_ANALOG_ENCODING_DIGITS;
204 ag->packet.spec->spec_digits = DEFAULT_ANALOG_SPEC_DIGITS;
205 ag->packet.data = devc->analog_patterns[pattern];
206 ag->pattern = pattern;
209 g_hash_table_insert(devc->ch_ag, ch, ag);
211 if (++pattern == ARRAY_SIZE(analog_pattern_str))
218 return std_scan_complete(di, g_slist_append(NULL, sdi));
221 static void clear_helper(struct dev_context *devc)
226 /* Analog generators. */
227 g_hash_table_iter_init(&iter, devc->ch_ag);
228 while (g_hash_table_iter_next(&iter, NULL, &value))
230 g_hash_table_unref(devc->ch_ag);
233 static int dev_clear(const struct sr_dev_driver *di)
235 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
238 static int config_get(uint32_t key, GVariant **data,
239 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
241 struct dev_context *devc;
242 struct sr_channel *ch;
243 struct analog_gen *ag;
252 case SR_CONF_SAMPLERATE:
253 *data = g_variant_new_uint64(devc->cur_samplerate);
255 case SR_CONF_LIMIT_SAMPLES:
256 *data = g_variant_new_uint64(devc->limit_samples);
258 case SR_CONF_LIMIT_MSEC:
259 *data = g_variant_new_uint64(devc->limit_msec);
261 case SR_CONF_LIMIT_FRAMES:
262 *data = g_variant_new_uint64(devc->limit_frames);
264 case SR_CONF_AVERAGING:
265 *data = g_variant_new_boolean(devc->avg);
267 case SR_CONF_AVG_SAMPLES:
268 *data = g_variant_new_uint64(devc->avg_samples);
270 case SR_CONF_MEASURED_QUANTITY:
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)
277 ag = g_hash_table_lookup(devc->ch_ag, ch);
278 mq_arr[0] = g_variant_new_uint32(ag->mq);
279 mq_arr[1] = g_variant_new_uint64(ag->mq_flags);
280 *data = g_variant_new_tuple(mq_arr, 2);
282 case SR_CONF_PATTERN_MODE:
284 return SR_ERR_CHANNEL_GROUP;
285 /* Any channel in the group will do. */
286 ch = cg->channels->data;
287 if (ch->type == SR_CHANNEL_LOGIC) {
288 pattern = devc->logic_pattern;
289 *data = g_variant_new_string(logic_pattern_str[pattern]);
290 } else if (ch->type == SR_CHANNEL_ANALOG) {
291 ag = g_hash_table_lookup(devc->ch_ag, ch);
292 pattern = ag->pattern;
293 *data = g_variant_new_string(analog_pattern_str[pattern]);
297 case SR_CONF_AMPLITUDE:
299 return SR_ERR_CHANNEL_GROUP;
300 /* Any channel in the group will do. */
301 ch = cg->channels->data;
302 if (ch->type != SR_CHANNEL_ANALOG)
304 ag = g_hash_table_lookup(devc->ch_ag, ch);
305 *data = g_variant_new_double(ag->amplitude);
307 case SR_CONF_CAPTURE_RATIO:
308 *data = g_variant_new_uint64(devc->capture_ratio);
317 static int config_set(uint32_t key, GVariant *data,
318 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
320 struct dev_context *devc;
321 struct analog_gen *ag;
322 struct sr_channel *ch;
323 GVariant *mq_tuple_child;
325 int logic_pattern, analog_pattern;
330 case SR_CONF_SAMPLERATE:
331 devc->cur_samplerate = g_variant_get_uint64(data);
333 case SR_CONF_LIMIT_SAMPLES:
334 devc->limit_msec = 0;
335 devc->limit_samples = g_variant_get_uint64(data);
337 case SR_CONF_LIMIT_MSEC:
338 devc->limit_msec = g_variant_get_uint64(data);
339 devc->limit_samples = 0;
341 case SR_CONF_LIMIT_FRAMES:
342 devc->limit_frames = g_variant_get_uint64(data);
344 case SR_CONF_AVERAGING:
345 devc->avg = g_variant_get_boolean(data);
346 sr_dbg("%s averaging", devc->avg ? "Enabling" : "Disabling");
348 case SR_CONF_AVG_SAMPLES:
349 devc->avg_samples = g_variant_get_uint64(data);
350 sr_dbg("Setting averaging rate to %" PRIu64, devc->avg_samples);
352 case SR_CONF_MEASURED_QUANTITY:
354 return SR_ERR_CHANNEL_GROUP;
355 for (l = cg->channels; l; l = l->next) {
357 if (ch->type != SR_CHANNEL_ANALOG)
359 ag = g_hash_table_lookup(devc->ch_ag, ch);
360 mq_tuple_child = g_variant_get_child_value(data, 0);
361 ag->mq = g_variant_get_uint32(mq_tuple_child);
362 mq_tuple_child = g_variant_get_child_value(data, 1);
363 ag->mq_flags = g_variant_get_uint64(mq_tuple_child);
364 g_variant_unref(mq_tuple_child);
367 case SR_CONF_PATTERN_MODE:
369 return SR_ERR_CHANNEL_GROUP;
370 logic_pattern = std_str_idx(data, ARRAY_AND_SIZE(logic_pattern_str));
371 analog_pattern = std_str_idx(data, ARRAY_AND_SIZE(analog_pattern_str));
372 if (logic_pattern < 0 && analog_pattern < 0)
374 for (l = cg->channels; l; l = l->next) {
376 if (ch->type == SR_CHANNEL_LOGIC) {
377 if (logic_pattern == -1)
379 sr_dbg("Setting logic pattern to %s",
380 logic_pattern_str[logic_pattern]);
381 devc->logic_pattern = logic_pattern;
382 /* Might as well do this now, these are static. */
383 if (logic_pattern == PATTERN_ALL_LOW)
384 memset(devc->logic_data, 0x00, LOGIC_BUFSIZE);
385 else if (logic_pattern == PATTERN_ALL_HIGH)
386 memset(devc->logic_data, 0xff, LOGIC_BUFSIZE);
387 } else if (ch->type == SR_CHANNEL_ANALOG) {
388 if (analog_pattern == -1)
390 sr_dbg("Setting analog pattern for channel %s to %s",
391 ch->name, analog_pattern_str[analog_pattern]);
392 ag = g_hash_table_lookup(devc->ch_ag, ch);
393 ag->pattern = analog_pattern;
398 case SR_CONF_AMPLITUDE:
400 return SR_ERR_CHANNEL_GROUP;
401 for (l = cg->channels; l; l = l->next) {
403 if (ch->type != SR_CHANNEL_ANALOG)
405 ag = g_hash_table_lookup(devc->ch_ag, ch);
406 ag->amplitude = g_variant_get_double(data);
409 case SR_CONF_CAPTURE_RATIO:
410 devc->capture_ratio = g_variant_get_uint64(data);
419 static int config_list(uint32_t key, GVariant **data,
420 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
422 struct sr_channel *ch;
426 case SR_CONF_SCAN_OPTIONS:
427 case SR_CONF_DEVICE_OPTIONS:
428 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
429 case SR_CONF_SAMPLERATE:
430 *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates));
432 case SR_CONF_TRIGGER_MATCH:
433 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
439 ch = cg->channels->data;
441 case SR_CONF_DEVICE_OPTIONS:
442 if (ch->type == SR_CHANNEL_LOGIC)
443 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_logic));
444 else if (ch->type == SR_CHANNEL_ANALOG) {
445 if (strcmp(cg->name, "Analog") == 0)
446 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog_group));
448 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog_channel));
453 case SR_CONF_PATTERN_MODE:
454 /* The analog group (with all 4 channels) shall not have a pattern property. */
455 if (strcmp(cg->name, "Analog") == 0)
458 if (ch->type == SR_CHANNEL_LOGIC)
459 *data = g_variant_new_strv(ARRAY_AND_SIZE(logic_pattern_str));
460 else if (ch->type == SR_CHANNEL_ANALOG)
461 *data = g_variant_new_strv(ARRAY_AND_SIZE(analog_pattern_str));
473 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
475 struct dev_context *devc;
477 struct sr_channel *ch;
480 struct sr_trigger *trigger;
483 devc->sent_samples = 0;
484 devc->sent_frame_samples = 0;
487 if ((trigger = sr_session_trigger_get(sdi->session))) {
488 int pre_trigger_samples = 0;
489 if (devc->limit_samples > 0)
490 pre_trigger_samples = (devc->capture_ratio * devc->limit_samples) / 100;
491 devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples);
493 return SR_ERR_MALLOC;
495 /* Disable all analog channels since using them when there are logic
496 * triggers set up would require having pre-trigger sample buffers
497 * for analog sample data.
499 for (l = sdi->channels; l; l = l->next) {
501 if (ch->type == SR_CHANNEL_ANALOG)
505 devc->trigger_fired = FALSE;
508 * Determine the numbers of logic and analog channels that are
509 * involved in the acquisition. Determine an offset and a mask to
510 * remove excess logic data content before datafeed submission.
512 devc->enabled_logic_channels = 0;
513 devc->enabled_analog_channels = 0;
514 for (l = sdi->channels; l; l = l->next) {
518 if (ch->type == SR_CHANNEL_ANALOG) {
519 devc->enabled_analog_channels++;
522 if (ch->type != SR_CHANNEL_LOGIC)
525 * TODO: Need we create a channel map here, such that the
526 * session datafeed packets will have a dense representation
527 * of the enabled channels' data? For example store channels
528 * D3 and D5 in bit positions 0 and 1 respectively, when all
529 * other channels are disabled? The current implementation
530 * generates a sparse layout, might provide data for logic
531 * channels that are disabled while it might suppress data
532 * from enabled channels at the same time.
534 devc->enabled_logic_channels++;
536 devc->first_partial_logic_index = devc->enabled_logic_channels / 8;
537 bitpos = devc->enabled_logic_channels % 8;
538 mask = (1 << bitpos) - 1;
539 devc->first_partial_logic_mask = mask;
540 sr_dbg("num logic %zu, partial off %zu, mask 0x%02x.",
541 devc->enabled_logic_channels,
542 devc->first_partial_logic_index,
543 devc->first_partial_logic_mask);
545 sr_session_source_add(sdi->session, -1, 0, 100,
546 demo_prepare_data, (struct sr_dev_inst *)sdi);
548 std_session_send_df_header(sdi);
550 if (devc->limit_frames > 0)
551 std_session_send_frame_begin(sdi);
553 /* We use this timestamp to decide how many more samples to send. */
554 devc->start_us = g_get_monotonic_time();
561 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
563 struct dev_context *devc;
565 sr_session_source_remove(sdi->session, -1);
568 if (devc->limit_frames > 0)
569 std_session_send_frame_end(sdi);
571 std_session_send_df_end(sdi);
574 soft_trigger_logic_free(devc->stl);
581 static struct sr_dev_driver demo_driver_info = {
583 .longname = "Demo driver and pattern generator",
586 .cleanup = std_cleanup,
588 .dev_list = std_dev_list,
589 .dev_clear = dev_clear,
590 .config_get = config_get,
591 .config_set = config_set,
592 .config_list = config_list,
593 .dev_open = std_dummy_dev_open,
594 .dev_close = std_dummy_dev_close,
595 .dev_acquisition_start = dev_acquisition_start,
596 .dev_acquisition_stop = dev_acquisition_stop,
599 SR_REGISTER_DEV_DRIVER(demo_driver_info);