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
36 #define DEFAULT_ANALOG_ENCODING_DIGITS 4
37 #define DEFAULT_ANALOG_SPEC_DIGITS 4
38 #define DEFAULT_ANALOG_AMPLITUDE 10
40 /* Note: No spaces allowed because of sigrok-cli. */
41 static const char *logic_pattern_str[] = {
53 static const uint32_t scanopts[] = {
54 SR_CONF_NUM_LOGIC_CHANNELS,
55 SR_CONF_NUM_ANALOG_CHANNELS,
59 static const uint32_t drvopts[] = {
61 SR_CONF_LOGIC_ANALYZER,
65 static const uint32_t devopts[] = {
67 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
68 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
69 SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
70 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
71 SR_CONF_AVERAGING | SR_CONF_GET | SR_CONF_SET,
72 SR_CONF_AVG_SAMPLES | SR_CONF_GET | SR_CONF_SET,
73 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
74 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
77 static const uint32_t devopts_cg_logic[] = {
78 SR_CONF_PATTERN_MODE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
81 static const uint32_t devopts_cg_analog_group[] = {
82 SR_CONF_AMPLITUDE | SR_CONF_GET | SR_CONF_SET,
85 static const uint32_t devopts_cg_analog_channel[] = {
86 SR_CONF_MEASURED_QUANTITY | SR_CONF_GET | SR_CONF_SET,
87 SR_CONF_PATTERN_MODE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
88 SR_CONF_AMPLITUDE | SR_CONF_GET | SR_CONF_SET,
91 static const int32_t trigger_matches[] = {
99 static const uint64_t samplerates[] = {
105 static GSList *scan(struct sr_dev_driver *di, GSList *options)
107 struct dev_context *devc;
108 struct sr_dev_inst *sdi;
109 struct sr_channel *ch;
110 struct sr_channel_group *cg, *acg;
111 struct sr_config *src;
112 struct analog_gen *ag;
114 int num_logic_channels, num_analog_channels, pattern, i;
115 uint64_t limit_frames;
116 char channel_name[16];
118 num_logic_channels = DEFAULT_NUM_LOGIC_CHANNELS;
119 num_analog_channels = DEFAULT_NUM_ANALOG_CHANNELS;
120 limit_frames = DEFAULT_LIMIT_FRAMES;
121 for (l = options; l; l = l->next) {
124 case SR_CONF_NUM_LOGIC_CHANNELS:
125 num_logic_channels = g_variant_get_int32(src->data);
127 case SR_CONF_NUM_ANALOG_CHANNELS:
128 num_analog_channels = g_variant_get_int32(src->data);
130 case SR_CONF_LIMIT_FRAMES:
131 limit_frames = g_variant_get_uint64(src->data);
136 sdi = g_malloc0(sizeof(struct sr_dev_inst));
137 sdi->status = SR_ST_INACTIVE;
138 sdi->model = g_strdup("Demo device");
140 devc = g_malloc0(sizeof(struct dev_context));
141 devc->cur_samplerate = SR_KHZ(200);
142 devc->num_logic_channels = num_logic_channels;
143 devc->logic_unitsize = (devc->num_logic_channels + 7) / 8;
144 devc->all_logic_channels_mask = 1UL << 0;
145 devc->all_logic_channels_mask <<= devc->num_logic_channels;
146 devc->all_logic_channels_mask--;
147 devc->logic_pattern = DEFAULT_LOGIC_PATTERN;
148 devc->num_analog_channels = num_analog_channels;
149 devc->limit_frames = limit_frames;
150 devc->capture_ratio = 20;
153 if (num_logic_channels > 0) {
154 /* Logic channels, all in one channel group. */
155 cg = g_malloc0(sizeof(struct sr_channel_group));
156 cg->name = g_strdup("Logic");
157 for (i = 0; i < num_logic_channels; i++) {
158 sprintf(channel_name, "D%d", i);
159 ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name);
160 cg->channels = g_slist_append(cg->channels, ch);
162 sdi->channel_groups = g_slist_append(NULL, cg);
165 /* Analog channels, channel groups and pattern generators. */
166 devc->ch_ag = g_hash_table_new(g_direct_hash, g_direct_equal);
167 if (num_analog_channels > 0) {
169 /* An "Analog" channel group with all analog channels in it. */
170 acg = g_malloc0(sizeof(struct sr_channel_group));
171 acg->name = g_strdup("Analog");
172 sdi->channel_groups = g_slist_append(sdi->channel_groups, acg);
174 for (i = 0; i < num_analog_channels; i++) {
175 snprintf(channel_name, 16, "A%d", i);
176 ch = sr_channel_new(sdi, i + num_logic_channels, SR_CHANNEL_ANALOG,
178 acg->channels = g_slist_append(acg->channels, ch);
180 /* Every analog channel gets its own channel group as well. */
181 cg = g_malloc0(sizeof(struct sr_channel_group));
182 cg->name = g_strdup(channel_name);
183 cg->channels = g_slist_append(NULL, ch);
184 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
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 sr_analog_init(&ag->packet, &ag->encoding, &ag->meaning, &ag->spec, 2);
194 ag->packet.meaning->channels = cg->channels;
195 ag->packet.meaning->mq = ag->mq;
196 ag->packet.meaning->mqflags = ag->mq_flags;
197 ag->packet.meaning->unit = ag->unit;
198 ag->packet.encoding->digits = DEFAULT_ANALOG_ENCODING_DIGITS;
199 ag->packet.spec->spec_digits = DEFAULT_ANALOG_SPEC_DIGITS;
200 ag->packet.data = ag->pattern_data;
201 ag->pattern = pattern;
204 g_hash_table_insert(devc->ch_ag, ch, ag);
206 if (++pattern == ARRAY_SIZE(analog_pattern_str))
213 return std_scan_complete(di, g_slist_append(NULL, sdi));
216 static void clear_helper(struct dev_context *devc)
221 /* Analog generators. */
222 g_hash_table_iter_init(&iter, devc->ch_ag);
223 while (g_hash_table_iter_next(&iter, NULL, &value))
225 g_hash_table_unref(devc->ch_ag);
228 static int dev_clear(const struct sr_dev_driver *di)
230 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
233 static int config_get(uint32_t key, GVariant **data,
234 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
236 struct dev_context *devc;
237 struct sr_channel *ch;
238 struct analog_gen *ag;
247 case SR_CONF_SAMPLERATE:
248 *data = g_variant_new_uint64(devc->cur_samplerate);
250 case SR_CONF_LIMIT_SAMPLES:
251 *data = g_variant_new_uint64(devc->limit_samples);
253 case SR_CONF_LIMIT_MSEC:
254 *data = g_variant_new_uint64(devc->limit_msec);
256 case SR_CONF_LIMIT_FRAMES:
257 *data = g_variant_new_uint64(devc->limit_frames);
259 case SR_CONF_AVERAGING:
260 *data = g_variant_new_boolean(devc->avg);
262 case SR_CONF_AVG_SAMPLES:
263 *data = g_variant_new_uint64(devc->avg_samples);
265 case SR_CONF_MEASURED_QUANTITY:
267 return SR_ERR_CHANNEL_GROUP;
268 /* Any channel in the group will do. */
269 ch = cg->channels->data;
270 if (ch->type != SR_CHANNEL_ANALOG)
272 ag = g_hash_table_lookup(devc->ch_ag, ch);
273 mq_arr[0] = g_variant_new_uint32(ag->mq);
274 mq_arr[1] = g_variant_new_uint64(ag->mq_flags);
275 *data = g_variant_new_tuple(mq_arr, 2);
277 case SR_CONF_PATTERN_MODE:
279 return SR_ERR_CHANNEL_GROUP;
280 /* Any channel in the group will do. */
281 ch = cg->channels->data;
282 if (ch->type == SR_CHANNEL_LOGIC) {
283 pattern = devc->logic_pattern;
284 *data = g_variant_new_string(logic_pattern_str[pattern]);
285 } else if (ch->type == SR_CHANNEL_ANALOG) {
286 ag = g_hash_table_lookup(devc->ch_ag, ch);
287 pattern = ag->pattern;
288 *data = g_variant_new_string(analog_pattern_str[pattern]);
292 case SR_CONF_AMPLITUDE:
294 return SR_ERR_CHANNEL_GROUP;
295 /* Any channel in the group will do. */
296 ch = cg->channels->data;
297 if (ch->type != SR_CHANNEL_ANALOG)
299 ag = g_hash_table_lookup(devc->ch_ag, ch);
300 *data = g_variant_new_double(ag->amplitude);
302 case SR_CONF_CAPTURE_RATIO:
303 *data = g_variant_new_uint64(devc->capture_ratio);
312 static int config_set(uint32_t key, GVariant *data,
313 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
315 struct dev_context *devc;
316 struct analog_gen *ag;
317 struct sr_channel *ch;
318 GVariant *mq_tuple_child;
320 int logic_pattern, analog_pattern;
325 case SR_CONF_SAMPLERATE:
326 devc->cur_samplerate = g_variant_get_uint64(data);
328 case SR_CONF_LIMIT_SAMPLES:
329 devc->limit_msec = 0;
330 devc->limit_samples = g_variant_get_uint64(data);
332 case SR_CONF_LIMIT_MSEC:
333 devc->limit_msec = g_variant_get_uint64(data);
334 devc->limit_samples = 0;
336 case SR_CONF_LIMIT_FRAMES:
337 devc->limit_frames = g_variant_get_uint64(data);
339 case SR_CONF_AVERAGING:
340 devc->avg = g_variant_get_boolean(data);
341 sr_dbg("%s averaging", devc->avg ? "Enabling" : "Disabling");
343 case SR_CONF_AVG_SAMPLES:
344 devc->avg_samples = g_variant_get_uint64(data);
345 sr_dbg("Setting averaging rate to %" PRIu64, devc->avg_samples);
347 case SR_CONF_MEASURED_QUANTITY:
349 return SR_ERR_CHANNEL_GROUP;
350 for (l = cg->channels; l; l = l->next) {
352 if (ch->type != SR_CHANNEL_ANALOG)
354 ag = g_hash_table_lookup(devc->ch_ag, ch);
355 mq_tuple_child = g_variant_get_child_value(data, 0);
356 ag->mq = g_variant_get_uint32(mq_tuple_child);
357 mq_tuple_child = g_variant_get_child_value(data, 1);
358 ag->mq_flags = g_variant_get_uint64(mq_tuple_child);
359 g_variant_unref(mq_tuple_child);
362 case SR_CONF_PATTERN_MODE:
364 return SR_ERR_CHANNEL_GROUP;
365 logic_pattern = std_str_idx(data, ARRAY_AND_SIZE(logic_pattern_str));
366 analog_pattern = std_str_idx(data, ARRAY_AND_SIZE(analog_pattern_str));
367 if (logic_pattern < 0 && analog_pattern < 0)
369 for (l = cg->channels; l; l = l->next) {
371 if (ch->type == SR_CHANNEL_LOGIC) {
372 if (logic_pattern == -1)
374 sr_dbg("Setting logic pattern to %s",
375 logic_pattern_str[logic_pattern]);
376 devc->logic_pattern = logic_pattern;
377 /* Might as well do this now, these are static. */
378 if (logic_pattern == PATTERN_ALL_LOW)
379 memset(devc->logic_data, 0x00, LOGIC_BUFSIZE);
380 else if (logic_pattern == PATTERN_ALL_HIGH)
381 memset(devc->logic_data, 0xff, LOGIC_BUFSIZE);
382 } else if (ch->type == SR_CHANNEL_ANALOG) {
383 if (analog_pattern == -1)
385 sr_dbg("Setting analog pattern for channel %s to %s",
386 ch->name, analog_pattern_str[analog_pattern]);
387 ag = g_hash_table_lookup(devc->ch_ag, ch);
388 ag->pattern = analog_pattern;
393 case SR_CONF_AMPLITUDE:
395 return SR_ERR_CHANNEL_GROUP;
396 for (l = cg->channels; l; l = l->next) {
398 if (ch->type != SR_CHANNEL_ANALOG)
400 ag = g_hash_table_lookup(devc->ch_ag, ch);
401 ag->amplitude = g_variant_get_double(data);
404 case SR_CONF_CAPTURE_RATIO:
405 devc->capture_ratio = g_variant_get_uint64(data);
414 static int config_list(uint32_t key, GVariant **data,
415 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
417 struct sr_channel *ch;
421 case SR_CONF_SCAN_OPTIONS:
422 case SR_CONF_DEVICE_OPTIONS:
423 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
424 case SR_CONF_SAMPLERATE:
425 *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates));
427 case SR_CONF_TRIGGER_MATCH:
428 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
434 ch = cg->channels->data;
436 case SR_CONF_DEVICE_OPTIONS:
437 if (ch->type == SR_CHANNEL_LOGIC)
438 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_logic));
439 else if (ch->type == SR_CHANNEL_ANALOG) {
440 if (strcmp(cg->name, "Analog") == 0)
441 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog_group));
443 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog_channel));
448 case SR_CONF_PATTERN_MODE:
449 /* The analog group (with all 4 channels) shall not have a pattern property. */
450 if (strcmp(cg->name, "Analog") == 0)
453 if (ch->type == SR_CHANNEL_LOGIC)
454 *data = g_variant_new_strv(ARRAY_AND_SIZE(logic_pattern_str));
455 else if (ch->type == SR_CHANNEL_ANALOG)
456 *data = g_variant_new_strv(ARRAY_AND_SIZE(analog_pattern_str));
468 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
470 struct dev_context *devc;
472 struct sr_channel *ch;
477 struct sr_trigger *trigger;
480 devc->sent_samples = 0;
481 devc->sent_frame_samples = 0;
484 if ((trigger = sr_session_trigger_get(sdi->session))) {
485 int pre_trigger_samples = 0;
486 if (devc->limit_samples > 0)
487 pre_trigger_samples = (devc->capture_ratio * devc->limit_samples) / 100;
488 devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples);
490 return SR_ERR_MALLOC;
492 /* Disable all analog channels since using them when there are logic
493 * triggers set up would require having pre-trigger sample buffers
494 * for analog sample data.
496 for (l = sdi->channels; l; l = l->next) {
498 if (ch->type == SR_CHANNEL_ANALOG)
502 devc->trigger_fired = FALSE;
505 * Determine the numbers of logic and analog channels that are
506 * involved in the acquisition. Determine an offset and a mask to
507 * remove excess logic data content before datafeed submission.
509 devc->enabled_logic_channels = 0;
510 devc->enabled_analog_channels = 0;
511 for (l = sdi->channels; l; l = l->next) {
515 if (ch->type == SR_CHANNEL_ANALOG) {
516 devc->enabled_analog_channels++;
519 if (ch->type != SR_CHANNEL_LOGIC)
522 * TODO: Need we create a channel map here, such that the
523 * session datafeed packets will have a dense representation
524 * of the enabled channels' data? For example store channels
525 * D3 and D5 in bit positions 0 and 1 respectively, when all
526 * other channels are disabled? The current implementation
527 * generates a sparse layout, might provide data for logic
528 * channels that are disabled while it might suppress data
529 * from enabled channels at the same time.
531 devc->enabled_logic_channels++;
533 devc->first_partial_logic_index = devc->enabled_logic_channels / 8;
534 bitpos = devc->enabled_logic_channels % 8;
535 mask = (1 << bitpos) - 1;
536 devc->first_partial_logic_mask = mask;
537 sr_dbg("num logic %zu, partial off %zu, mask 0x%02x.",
538 devc->enabled_logic_channels,
539 devc->first_partial_logic_index,
540 devc->first_partial_logic_mask);
543 * Have the waveform for analog patterns pre-generated. It's
544 * supposed to be periodic, so the generator just needs to
545 * access the prepared sample data (DDS style).
547 g_hash_table_iter_init(&iter, devc->ch_ag);
548 while (g_hash_table_iter_next(&iter, NULL, &value))
549 demo_generate_analog_pattern(value, devc->cur_samplerate);
551 sr_session_source_add(sdi->session, -1, 0, 100,
552 demo_prepare_data, (struct sr_dev_inst *)sdi);
554 std_session_send_df_header(sdi);
556 if (devc->limit_frames > 0)
557 std_session_send_frame_begin(sdi);
559 /* We use this timestamp to decide how many more samples to send. */
560 devc->start_us = g_get_monotonic_time();
567 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
569 struct dev_context *devc;
571 sr_session_source_remove(sdi->session, -1);
574 if (devc->limit_frames > 0)
575 std_session_send_frame_end(sdi);
577 std_session_send_df_end(sdi);
580 soft_trigger_logic_free(devc->stl);
587 static struct sr_dev_driver demo_driver_info = {
589 .longname = "Demo driver and pattern generator",
592 .cleanup = std_cleanup,
594 .dev_list = std_dev_list,
595 .dev_clear = dev_clear,
596 .config_get = config_get,
597 .config_set = config_set,
598 .config_list = config_list,
599 .dev_open = std_dummy_dev_open,
600 .dev_close = std_dummy_dev_close,
601 .dev_acquisition_start = dev_acquisition_start,
602 .dev_acquisition_stop = dev_acquisition_stop,
605 SR_REGISTER_DEV_DRIVER(demo_driver_info);