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>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
27 #include <libsigrok/libsigrok.h>
28 #include "libsigrok-internal.h"
31 #define DEFAULT_NUM_LOGIC_CHANNELS 8
32 #define DEFAULT_LOGIC_PATTERN PATTERN_SIGROK
34 #define DEFAULT_NUM_ANALOG_CHANNELS 4
35 #define DEFAULT_ANALOG_ENCODING_DIGITS 4
36 #define DEFAULT_ANALOG_SPEC_DIGITS 4
37 #define DEFAULT_ANALOG_AMPLITUDE 10
39 /* Note: No spaces allowed because of sigrok-cli. */
40 static const char *logic_pattern_str[] = {
52 static const uint32_t scanopts[] = {
53 SR_CONF_NUM_LOGIC_CHANNELS,
54 SR_CONF_NUM_ANALOG_CHANNELS,
58 static const uint32_t drvopts[] = {
60 SR_CONF_LOGIC_ANALYZER,
64 static const uint32_t devopts[] = {
66 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
67 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
68 SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
69 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
70 SR_CONF_AVERAGING | SR_CONF_GET | SR_CONF_SET,
71 SR_CONF_AVG_SAMPLES | SR_CONF_GET | SR_CONF_SET,
72 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
73 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
76 static const uint32_t devopts_cg_logic[] = {
77 SR_CONF_PATTERN_MODE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
80 static const uint32_t devopts_cg_analog_group[] = {
81 SR_CONF_AMPLITUDE | SR_CONF_GET | SR_CONF_SET,
84 static const uint32_t devopts_cg_analog_channel[] = {
85 SR_CONF_PATTERN_MODE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
86 SR_CONF_AMPLITUDE | SR_CONF_GET | SR_CONF_SET,
89 static const int32_t trigger_matches[] = {
97 static const uint64_t samplerates[] = {
103 static GSList *scan(struct sr_dev_driver *di, GSList *options)
105 struct dev_context *devc;
106 struct sr_dev_inst *sdi;
107 struct sr_channel *ch;
108 struct sr_channel_group *cg, *acg;
109 struct sr_config *src;
110 struct analog_gen *ag;
112 int num_logic_channels, num_analog_channels, pattern, i;
113 uint64_t limit_frames;
114 char channel_name[16];
116 num_logic_channels = DEFAULT_NUM_LOGIC_CHANNELS;
117 num_analog_channels = DEFAULT_NUM_ANALOG_CHANNELS;
118 limit_frames = DEFAULT_LIMIT_FRAMES;
119 for (l = options; l; l = l->next) {
122 case SR_CONF_NUM_LOGIC_CHANNELS:
123 num_logic_channels = g_variant_get_int32(src->data);
125 case SR_CONF_NUM_ANALOG_CHANNELS:
126 num_analog_channels = g_variant_get_int32(src->data);
128 case SR_CONF_LIMIT_FRAMES:
129 limit_frames = g_variant_get_uint64(src->data);
134 sdi = g_malloc0(sizeof(struct sr_dev_inst));
135 sdi->status = SR_ST_INACTIVE;
136 sdi->model = g_strdup("Demo device");
138 devc = g_malloc0(sizeof(struct dev_context));
139 devc->cur_samplerate = SR_KHZ(200);
140 devc->num_logic_channels = num_logic_channels;
141 devc->logic_unitsize = (devc->num_logic_channels + 7) / 8;
142 devc->all_logic_channels_mask = 1UL << 0;
143 devc->all_logic_channels_mask <<= devc->num_logic_channels;
144 devc->all_logic_channels_mask--;
145 devc->logic_pattern = DEFAULT_LOGIC_PATTERN;
146 devc->num_analog_channels = num_analog_channels;
147 devc->limit_frames = limit_frames;
148 devc->capture_ratio = 20;
151 if (num_logic_channels > 0) {
152 /* Logic channels, all in one channel group. */
153 cg = g_malloc0(sizeof(struct sr_channel_group));
154 cg->name = g_strdup("Logic");
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);
160 sdi->channel_groups = g_slist_append(NULL, cg);
163 /* Analog channels, channel groups and pattern generators. */
164 devc->ch_ag = g_hash_table_new(g_direct_hash, g_direct_equal);
165 if (num_analog_channels > 0) {
167 /* An "Analog" channel group with all analog channels in it. */
168 acg = g_malloc0(sizeof(struct sr_channel_group));
169 acg->name = g_strdup("Analog");
170 sdi->channel_groups = g_slist_append(sdi->channel_groups, acg);
172 for (i = 0; i < num_analog_channels; i++) {
173 snprintf(channel_name, 16, "A%d", i);
174 ch = sr_channel_new(sdi, i + num_logic_channels, SR_CHANNEL_ANALOG,
176 acg->channels = g_slist_append(acg->channels, ch);
178 /* Every analog channel gets its own channel group as well. */
179 cg = g_malloc0(sizeof(struct sr_channel_group));
180 cg->name = g_strdup(channel_name);
181 cg->channels = g_slist_append(NULL, ch);
182 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
184 /* Every channel gets a generator struct. */
185 ag = g_malloc(sizeof(struct analog_gen));
187 ag->amplitude = DEFAULT_ANALOG_AMPLITUDE;
188 sr_analog_init(&ag->packet, &ag->encoding, &ag->meaning, &ag->spec, 2);
189 ag->packet.meaning->channels = cg->channels;
190 ag->packet.meaning->mq = SR_MQ_VOLTAGE;
191 ag->packet.meaning->mqflags = SR_MQFLAG_DC;
192 ag->packet.meaning->unit = SR_UNIT_VOLT;
193 ag->packet.encoding->digits = DEFAULT_ANALOG_ENCODING_DIGITS;
194 ag->packet.spec->spec_digits = DEFAULT_ANALOG_SPEC_DIGITS;
195 ag->packet.data = ag->pattern_data;
196 ag->pattern = pattern;
199 g_hash_table_insert(devc->ch_ag, ch, ag);
201 if (++pattern == ARRAY_SIZE(analog_pattern_str))
208 return std_scan_complete(di, g_slist_append(NULL, sdi));
211 static void clear_helper(struct dev_context *devc)
216 /* Analog generators. */
217 g_hash_table_iter_init(&iter, devc->ch_ag);
218 while (g_hash_table_iter_next(&iter, NULL, &value))
220 g_hash_table_unref(devc->ch_ag);
223 static int dev_clear(const struct sr_dev_driver *di)
225 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
228 static int config_get(uint32_t key, GVariant **data,
229 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
231 struct dev_context *devc;
232 struct sr_channel *ch;
233 struct analog_gen *ag;
241 case SR_CONF_SAMPLERATE:
242 *data = g_variant_new_uint64(devc->cur_samplerate);
244 case SR_CONF_LIMIT_SAMPLES:
245 *data = g_variant_new_uint64(devc->limit_samples);
247 case SR_CONF_LIMIT_MSEC:
248 *data = g_variant_new_uint64(devc->limit_msec);
250 case SR_CONF_LIMIT_FRAMES:
251 *data = g_variant_new_uint64(devc->limit_frames);
253 case SR_CONF_AVERAGING:
254 *data = g_variant_new_boolean(devc->avg);
256 case SR_CONF_AVG_SAMPLES:
257 *data = g_variant_new_uint64(devc->avg_samples);
259 case SR_CONF_PATTERN_MODE:
261 return SR_ERR_CHANNEL_GROUP;
262 /* Any channel in the group will do. */
263 ch = cg->channels->data;
264 if (ch->type == SR_CHANNEL_LOGIC) {
265 pattern = devc->logic_pattern;
266 *data = g_variant_new_string(logic_pattern_str[pattern]);
267 } else if (ch->type == SR_CHANNEL_ANALOG) {
268 ag = g_hash_table_lookup(devc->ch_ag, ch);
269 pattern = ag->pattern;
270 *data = g_variant_new_string(analog_pattern_str[pattern]);
274 case SR_CONF_AMPLITUDE:
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 *data = g_variant_new_double(ag->amplitude);
284 case SR_CONF_CAPTURE_RATIO:
285 *data = g_variant_new_uint64(devc->capture_ratio);
294 static int config_set(uint32_t key, GVariant *data,
295 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
297 struct dev_context *devc;
298 struct analog_gen *ag;
299 struct sr_channel *ch;
301 int logic_pattern, analog_pattern;
306 case SR_CONF_SAMPLERATE:
307 devc->cur_samplerate = g_variant_get_uint64(data);
309 case SR_CONF_LIMIT_SAMPLES:
310 devc->limit_msec = 0;
311 devc->limit_samples = g_variant_get_uint64(data);
313 case SR_CONF_LIMIT_MSEC:
314 devc->limit_msec = g_variant_get_uint64(data);
315 devc->limit_samples = 0;
317 case SR_CONF_LIMIT_FRAMES:
318 devc->limit_frames = g_variant_get_uint64(data);
320 case SR_CONF_AVERAGING:
321 devc->avg = g_variant_get_boolean(data);
322 sr_dbg("%s averaging", devc->avg ? "Enabling" : "Disabling");
324 case SR_CONF_AVG_SAMPLES:
325 devc->avg_samples = g_variant_get_uint64(data);
326 sr_dbg("Setting averaging rate to %" PRIu64, devc->avg_samples);
328 case SR_CONF_PATTERN_MODE:
330 return SR_ERR_CHANNEL_GROUP;
331 logic_pattern = std_str_idx(data, ARRAY_AND_SIZE(logic_pattern_str));
332 analog_pattern = std_str_idx(data, ARRAY_AND_SIZE(analog_pattern_str));
333 if (logic_pattern < 0 && analog_pattern < 0)
335 for (l = cg->channels; l; l = l->next) {
337 if (ch->type == SR_CHANNEL_LOGIC) {
338 if (logic_pattern == -1)
340 sr_dbg("Setting logic pattern to %s",
341 logic_pattern_str[logic_pattern]);
342 devc->logic_pattern = logic_pattern;
343 /* Might as well do this now, these are static. */
344 if (logic_pattern == PATTERN_ALL_LOW)
345 memset(devc->logic_data, 0x00, LOGIC_BUFSIZE);
346 else if (logic_pattern == PATTERN_ALL_HIGH)
347 memset(devc->logic_data, 0xff, LOGIC_BUFSIZE);
348 } else if (ch->type == SR_CHANNEL_ANALOG) {
349 if (analog_pattern == -1)
351 sr_dbg("Setting analog pattern for channel %s to %s",
352 ch->name, analog_pattern_str[analog_pattern]);
353 ag = g_hash_table_lookup(devc->ch_ag, ch);
354 ag->pattern = analog_pattern;
359 case SR_CONF_AMPLITUDE:
361 return SR_ERR_CHANNEL_GROUP;
362 for (l = cg->channels; l; l = l->next) {
364 if (ch->type != SR_CHANNEL_ANALOG)
366 ag = g_hash_table_lookup(devc->ch_ag, ch);
367 ag->amplitude = g_variant_get_double(data);
370 case SR_CONF_CAPTURE_RATIO:
371 devc->capture_ratio = g_variant_get_uint64(data);
380 static int config_list(uint32_t key, GVariant **data,
381 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
383 struct sr_channel *ch;
387 case SR_CONF_SCAN_OPTIONS:
388 case SR_CONF_DEVICE_OPTIONS:
389 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
390 case SR_CONF_SAMPLERATE:
391 *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates));
393 case SR_CONF_TRIGGER_MATCH:
394 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
400 ch = cg->channels->data;
402 case SR_CONF_DEVICE_OPTIONS:
403 if (ch->type == SR_CHANNEL_LOGIC)
404 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_logic));
405 else if (ch->type == SR_CHANNEL_ANALOG) {
406 if (strcmp(cg->name, "Analog") == 0)
407 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog_group));
409 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog_channel));
414 case SR_CONF_PATTERN_MODE:
415 /* The analog group (with all 4 channels) shall not have a pattern property. */
416 if (strcmp(cg->name, "Analog") == 0)
419 if (ch->type == SR_CHANNEL_LOGIC)
420 *data = g_variant_new_strv(ARRAY_AND_SIZE(logic_pattern_str));
421 else if (ch->type == SR_CHANNEL_ANALOG)
422 *data = g_variant_new_strv(ARRAY_AND_SIZE(analog_pattern_str));
434 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
436 struct dev_context *devc;
438 struct sr_channel *ch;
443 struct sr_trigger *trigger;
446 devc->sent_samples = 0;
447 devc->sent_frame_samples = 0;
450 if ((trigger = sr_session_trigger_get(sdi->session))) {
451 int pre_trigger_samples = 0;
452 if (devc->limit_samples > 0)
453 pre_trigger_samples = (devc->capture_ratio * devc->limit_samples) / 100;
454 devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples);
456 return SR_ERR_MALLOC;
458 /* Disable all analog channels since using them when there are logic
459 * triggers set up would require having pre-trigger sample buffers
460 * for analog sample data.
462 for (l = sdi->channels; l; l = l->next) {
464 if (ch->type == SR_CHANNEL_ANALOG)
468 devc->trigger_fired = FALSE;
471 * Determine the numbers of logic and analog channels that are
472 * involved in the acquisition. Determine an offset and a mask to
473 * remove excess logic data content before datafeed submission.
475 devc->enabled_logic_channels = 0;
476 devc->enabled_analog_channels = 0;
477 for (l = sdi->channels; l; l = l->next) {
481 if (ch->type == SR_CHANNEL_ANALOG) {
482 devc->enabled_analog_channels++;
485 if (ch->type != SR_CHANNEL_LOGIC)
488 * TODO: Need we create a channel map here, such that the
489 * session datafeed packets will have a dense representation
490 * of the enabled channels' data? For example store channels
491 * D3 and D5 in bit positions 0 and 1 respectively, when all
492 * other channels are disabled? The current implementation
493 * generates a sparse layout, might provide data for logic
494 * channels that are disabled while it might suppress data
495 * from enabled channels at the same time.
497 devc->enabled_logic_channels++;
499 devc->first_partial_logic_index = devc->enabled_logic_channels / 8;
500 bitpos = devc->enabled_logic_channels % 8;
501 mask = (1 << bitpos) - 1;
502 devc->first_partial_logic_mask = mask;
503 sr_dbg("num logic %zu, partial off %zu, mask 0x%02x.",
504 devc->enabled_logic_channels,
505 devc->first_partial_logic_index,
506 devc->first_partial_logic_mask);
509 * Have the waveform for analog patterns pre-generated. It's
510 * supposed to be periodic, so the generator just needs to
511 * access the prepared sample data (DDS style).
513 g_hash_table_iter_init(&iter, devc->ch_ag);
514 while (g_hash_table_iter_next(&iter, NULL, &value))
515 demo_generate_analog_pattern(value, devc->cur_samplerate);
517 sr_session_source_add(sdi->session, -1, 0, 100,
518 demo_prepare_data, (struct sr_dev_inst *)sdi);
520 std_session_send_df_header(sdi);
522 if (devc->limit_frames > 0)
523 std_session_send_frame_begin(sdi);
525 /* We use this timestamp to decide how many more samples to send. */
526 devc->start_us = g_get_monotonic_time();
533 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
535 struct dev_context *devc;
537 sr_session_source_remove(sdi->session, -1);
540 if (devc->limit_frames > 0)
541 std_session_send_frame_end(sdi);
543 std_session_send_df_end(sdi);
546 soft_trigger_logic_free(devc->stl);
553 static struct sr_dev_driver demo_driver_info = {
555 .longname = "Demo driver and pattern generator",
558 .cleanup = std_cleanup,
560 .dev_list = std_dev_list,
561 .dev_clear = dev_clear,
562 .config_get = config_get,
563 .config_set = config_set,
564 .config_list = config_list,
565 .dev_open = std_dummy_dev_open,
566 .dev_close = std_dummy_dev_close,
567 .dev_acquisition_start = dev_acquisition_start,
568 .dev_acquisition_stop = dev_acquisition_stop,
571 SR_REGISTER_DEV_DRIVER(demo_driver_info);