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 = 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 demo_free_analog_pattern(devc);
230 /* Analog generators. */
231 g_hash_table_iter_init(&iter, devc->ch_ag);
232 while (g_hash_table_iter_next(&iter, NULL, &value))
234 g_hash_table_unref(devc->ch_ag);
237 static int dev_clear(const struct sr_dev_driver *di)
239 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
242 static int config_get(uint32_t key, GVariant **data,
243 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
245 struct dev_context *devc;
246 struct sr_channel *ch;
247 struct analog_gen *ag;
256 case SR_CONF_SAMPLERATE:
257 *data = g_variant_new_uint64(devc->cur_samplerate);
259 case SR_CONF_LIMIT_SAMPLES:
260 *data = g_variant_new_uint64(devc->limit_samples);
262 case SR_CONF_LIMIT_MSEC:
263 *data = g_variant_new_uint64(devc->limit_msec);
265 case SR_CONF_LIMIT_FRAMES:
266 *data = g_variant_new_uint64(devc->limit_frames);
268 case SR_CONF_AVERAGING:
269 *data = g_variant_new_boolean(devc->avg);
271 case SR_CONF_AVG_SAMPLES:
272 *data = g_variant_new_uint64(devc->avg_samples);
274 case SR_CONF_MEASURED_QUANTITY:
276 return SR_ERR_CHANNEL_GROUP;
277 /* Any channel in the group will do. */
278 ch = cg->channels->data;
279 if (ch->type != SR_CHANNEL_ANALOG)
281 ag = g_hash_table_lookup(devc->ch_ag, ch);
282 mq_arr[0] = g_variant_new_uint32(ag->mq);
283 mq_arr[1] = g_variant_new_uint64(ag->mq_flags);
284 *data = g_variant_new_tuple(mq_arr, 2);
286 case SR_CONF_PATTERN_MODE:
288 return SR_ERR_CHANNEL_GROUP;
289 /* Any channel in the group will do. */
290 ch = cg->channels->data;
291 if (ch->type == SR_CHANNEL_LOGIC) {
292 pattern = devc->logic_pattern;
293 *data = g_variant_new_string(logic_pattern_str[pattern]);
294 } else if (ch->type == SR_CHANNEL_ANALOG) {
295 ag = g_hash_table_lookup(devc->ch_ag, ch);
296 pattern = ag->pattern;
297 *data = g_variant_new_string(analog_pattern_str[pattern]);
301 case SR_CONF_AMPLITUDE:
303 return SR_ERR_CHANNEL_GROUP;
304 /* Any channel in the group will do. */
305 ch = cg->channels->data;
306 if (ch->type != SR_CHANNEL_ANALOG)
308 ag = g_hash_table_lookup(devc->ch_ag, ch);
309 *data = g_variant_new_double(ag->amplitude);
313 return SR_ERR_CHANNEL_GROUP;
314 /* Any channel in the group will do. */
315 ch = cg->channels->data;
316 if (ch->type != SR_CHANNEL_ANALOG)
318 ag = g_hash_table_lookup(devc->ch_ag, ch);
319 *data = g_variant_new_double(ag->offset);
321 case SR_CONF_CAPTURE_RATIO:
322 *data = g_variant_new_uint64(devc->capture_ratio);
331 static int config_set(uint32_t key, GVariant *data,
332 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
334 struct dev_context *devc;
335 struct analog_gen *ag;
336 struct sr_channel *ch;
337 GVariant *mq_tuple_child;
339 int logic_pattern, analog_pattern;
344 case SR_CONF_SAMPLERATE:
345 devc->cur_samplerate = g_variant_get_uint64(data);
347 case SR_CONF_LIMIT_SAMPLES:
348 devc->limit_msec = 0;
349 devc->limit_samples = g_variant_get_uint64(data);
351 case SR_CONF_LIMIT_MSEC:
352 devc->limit_msec = g_variant_get_uint64(data);
353 devc->limit_samples = 0;
355 case SR_CONF_LIMIT_FRAMES:
356 devc->limit_frames = g_variant_get_uint64(data);
358 case SR_CONF_AVERAGING:
359 devc->avg = g_variant_get_boolean(data);
360 sr_dbg("%s averaging", devc->avg ? "Enabling" : "Disabling");
362 case SR_CONF_AVG_SAMPLES:
363 devc->avg_samples = g_variant_get_uint64(data);
364 sr_dbg("Setting averaging rate to %" PRIu64, devc->avg_samples);
366 case SR_CONF_MEASURED_QUANTITY:
368 return SR_ERR_CHANNEL_GROUP;
369 for (l = cg->channels; l; l = l->next) {
371 if (ch->type != SR_CHANNEL_ANALOG)
373 ag = g_hash_table_lookup(devc->ch_ag, ch);
374 mq_tuple_child = g_variant_get_child_value(data, 0);
375 ag->mq = g_variant_get_uint32(mq_tuple_child);
376 mq_tuple_child = g_variant_get_child_value(data, 1);
377 ag->mq_flags = g_variant_get_uint64(mq_tuple_child);
378 g_variant_unref(mq_tuple_child);
381 case SR_CONF_PATTERN_MODE:
383 return SR_ERR_CHANNEL_GROUP;
384 logic_pattern = std_str_idx(data, ARRAY_AND_SIZE(logic_pattern_str));
385 analog_pattern = std_str_idx(data, ARRAY_AND_SIZE(analog_pattern_str));
386 if (logic_pattern < 0 && analog_pattern < 0)
388 for (l = cg->channels; l; l = l->next) {
390 if (ch->type == SR_CHANNEL_LOGIC) {
391 if (logic_pattern == -1)
393 sr_dbg("Setting logic pattern to %s",
394 logic_pattern_str[logic_pattern]);
395 devc->logic_pattern = logic_pattern;
396 /* Might as well do this now, these are static. */
397 if (logic_pattern == PATTERN_ALL_LOW)
398 memset(devc->logic_data, 0x00, LOGIC_BUFSIZE);
399 else if (logic_pattern == PATTERN_ALL_HIGH)
400 memset(devc->logic_data, 0xff, LOGIC_BUFSIZE);
401 } else if (ch->type == SR_CHANNEL_ANALOG) {
402 if (analog_pattern == -1)
404 sr_dbg("Setting analog pattern for channel %s to %s",
405 ch->name, analog_pattern_str[analog_pattern]);
406 ag = g_hash_table_lookup(devc->ch_ag, ch);
407 ag->pattern = analog_pattern;
412 case SR_CONF_AMPLITUDE:
414 return SR_ERR_CHANNEL_GROUP;
415 for (l = cg->channels; l; l = l->next) {
417 if (ch->type != SR_CHANNEL_ANALOG)
419 ag = g_hash_table_lookup(devc->ch_ag, ch);
420 ag->amplitude = g_variant_get_double(data);
425 return SR_ERR_CHANNEL_GROUP;
426 for (l = cg->channels; l; l = l->next) {
428 if (ch->type != SR_CHANNEL_ANALOG)
430 ag = g_hash_table_lookup(devc->ch_ag, ch);
431 ag->offset = g_variant_get_double(data);
434 case SR_CONF_CAPTURE_RATIO:
435 devc->capture_ratio = g_variant_get_uint64(data);
444 static int config_list(uint32_t key, GVariant **data,
445 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
447 struct sr_channel *ch;
451 case SR_CONF_SCAN_OPTIONS:
452 case SR_CONF_DEVICE_OPTIONS:
453 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
454 case SR_CONF_SAMPLERATE:
455 *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates));
457 case SR_CONF_TRIGGER_MATCH:
458 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
464 ch = cg->channels->data;
466 case SR_CONF_DEVICE_OPTIONS:
467 if (ch->type == SR_CHANNEL_LOGIC)
468 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_logic));
469 else if (ch->type == SR_CHANNEL_ANALOG) {
470 if (strcmp(cg->name, "Analog") == 0)
471 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog_group));
473 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog_channel));
478 case SR_CONF_PATTERN_MODE:
479 /* The analog group (with all 4 channels) shall not have a pattern property. */
480 if (strcmp(cg->name, "Analog") == 0)
483 if (ch->type == SR_CHANNEL_LOGIC)
484 *data = g_variant_new_strv(ARRAY_AND_SIZE(logic_pattern_str));
485 else if (ch->type == SR_CHANNEL_ANALOG)
486 *data = g_variant_new_strv(ARRAY_AND_SIZE(analog_pattern_str));
498 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
500 struct dev_context *devc;
502 struct sr_channel *ch;
505 struct sr_trigger *trigger;
508 devc->sent_samples = 0;
509 devc->sent_frame_samples = 0;
512 if ((trigger = sr_session_trigger_get(sdi->session))) {
513 int pre_trigger_samples = 0;
514 if (devc->limit_samples > 0)
515 pre_trigger_samples = (devc->capture_ratio * devc->limit_samples) / 100;
516 devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples);
518 return SR_ERR_MALLOC;
520 /* Disable all analog channels since using them when there are logic
521 * triggers set up would require having pre-trigger sample buffers
522 * for analog sample data.
524 for (l = sdi->channels; l; l = l->next) {
526 if (ch->type == SR_CHANNEL_ANALOG)
530 devc->trigger_fired = FALSE;
533 * Determine the numbers of logic and analog channels that are
534 * involved in the acquisition. Determine an offset and a mask to
535 * remove excess logic data content before datafeed submission.
537 devc->enabled_logic_channels = 0;
538 devc->enabled_analog_channels = 0;
539 for (l = sdi->channels; l; l = l->next) {
543 if (ch->type == SR_CHANNEL_ANALOG) {
544 devc->enabled_analog_channels++;
547 if (ch->type != SR_CHANNEL_LOGIC)
550 * TODO: Need we create a channel map here, such that the
551 * session datafeed packets will have a dense representation
552 * of the enabled channels' data? For example store channels
553 * D3 and D5 in bit positions 0 and 1 respectively, when all
554 * other channels are disabled? The current implementation
555 * generates a sparse layout, might provide data for logic
556 * channels that are disabled while it might suppress data
557 * from enabled channels at the same time.
559 devc->enabled_logic_channels++;
561 devc->first_partial_logic_index = devc->enabled_logic_channels / 8;
562 bitpos = devc->enabled_logic_channels % 8;
563 mask = (1 << bitpos) - 1;
564 devc->first_partial_logic_mask = mask;
565 sr_dbg("num logic %zu, partial off %zu, mask 0x%02x.",
566 devc->enabled_logic_channels,
567 devc->first_partial_logic_index,
568 devc->first_partial_logic_mask);
570 sr_session_source_add(sdi->session, -1, 0, 100,
571 demo_prepare_data, (struct sr_dev_inst *)sdi);
573 std_session_send_df_header(sdi);
575 if (devc->limit_frames > 0)
576 std_session_send_frame_begin(sdi);
578 /* We use this timestamp to decide how many more samples to send. */
579 devc->start_us = g_get_monotonic_time();
586 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
588 struct dev_context *devc;
590 sr_session_source_remove(sdi->session, -1);
593 if (devc->limit_frames > 0)
594 std_session_send_frame_end(sdi);
596 std_session_send_df_end(sdi);
599 soft_trigger_logic_free(devc->stl);
606 static struct sr_dev_driver demo_driver_info = {
608 .longname = "Demo driver and pattern generator",
611 .cleanup = std_cleanup,
613 .dev_list = std_dev_list,
614 .dev_clear = dev_clear,
615 .config_get = config_get,
616 .config_set = config_set,
617 .config_list = config_list,
618 .dev_open = std_dummy_dev_open,
619 .dev_close = std_dummy_dev_close,
620 .dev_acquisition_start = dev_acquisition_start,
621 .dev_acquisition_stop = dev_acquisition_stop,
624 SR_REGISTER_DEV_DRIVER(demo_driver_info);