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_AMPLITUDE 10
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_PATTERN_MODE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
84 SR_CONF_AMPLITUDE | SR_CONF_GET | SR_CONF_SET,
87 static const int32_t trigger_matches[] = {
95 static const uint64_t samplerates[] = {
101 static GSList *scan(struct sr_dev_driver *di, GSList *options)
103 struct dev_context *devc;
104 struct sr_dev_inst *sdi;
105 struct sr_channel *ch;
106 struct sr_channel_group *cg, *acg;
107 struct sr_config *src;
108 struct analog_gen *ag;
110 int num_logic_channels, num_analog_channels, pattern, i;
111 uint64_t limit_frames;
112 char channel_name[16];
114 num_logic_channels = DEFAULT_NUM_LOGIC_CHANNELS;
115 num_analog_channels = DEFAULT_NUM_ANALOG_CHANNELS;
116 limit_frames = DEFAULT_LIMIT_FRAMES;
117 for (l = options; l; l = l->next) {
120 case SR_CONF_NUM_LOGIC_CHANNELS:
121 num_logic_channels = g_variant_get_int32(src->data);
123 case SR_CONF_NUM_ANALOG_CHANNELS:
124 num_analog_channels = g_variant_get_int32(src->data);
126 case SR_CONF_LIMIT_FRAMES:
127 limit_frames = g_variant_get_uint64(src->data);
132 sdi = g_malloc0(sizeof(struct sr_dev_inst));
133 sdi->status = SR_ST_INACTIVE;
134 sdi->model = g_strdup("Demo device");
136 devc = g_malloc0(sizeof(struct dev_context));
137 devc->cur_samplerate = SR_KHZ(200);
138 devc->num_logic_channels = num_logic_channels;
139 devc->logic_unitsize = (devc->num_logic_channels + 7) / 8;
140 devc->all_logic_channels_mask = 1UL << 0;
141 devc->all_logic_channels_mask <<= devc->num_logic_channels;
142 devc->all_logic_channels_mask--;
143 devc->logic_pattern = DEFAULT_LOGIC_PATTERN;
144 devc->num_analog_channels = num_analog_channels;
145 devc->limit_frames = limit_frames;
147 if (num_logic_channels > 0) {
148 /* Logic channels, all in one channel group. */
149 cg = g_malloc0(sizeof(struct sr_channel_group));
150 cg->name = g_strdup("Logic");
151 for (i = 0; i < num_logic_channels; i++) {
152 sprintf(channel_name, "D%d", i);
153 ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name);
154 cg->channels = g_slist_append(cg->channels, ch);
156 sdi->channel_groups = g_slist_append(NULL, cg);
159 /* Analog channels, channel groups and pattern generators. */
160 devc->ch_ag = g_hash_table_new(g_direct_hash, g_direct_equal);
161 if (num_analog_channels > 0) {
163 /* An "Analog" channel group with all analog channels in it. */
164 acg = g_malloc0(sizeof(struct sr_channel_group));
165 acg->name = g_strdup("Analog");
166 sdi->channel_groups = g_slist_append(sdi->channel_groups, acg);
168 for (i = 0; i < num_analog_channels; i++) {
169 snprintf(channel_name, 16, "A%d", i);
170 ch = sr_channel_new(sdi, i + num_logic_channels, SR_CHANNEL_ANALOG,
172 acg->channels = g_slist_append(acg->channels, ch);
174 /* Every analog channel gets its own channel group as well. */
175 cg = g_malloc0(sizeof(struct sr_channel_group));
176 cg->name = g_strdup(channel_name);
177 cg->channels = g_slist_append(NULL, ch);
178 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
180 /* Every channel gets a generator struct. */
181 ag = g_malloc(sizeof(struct analog_gen));
183 ag->amplitude = DEFAULT_ANALOG_AMPLITUDE;
184 sr_analog_init(&ag->packet, &ag->encoding, &ag->meaning, &ag->spec, 2);
185 ag->packet.meaning->channels = cg->channels;
186 ag->packet.meaning->mq = 0;
187 ag->packet.meaning->mqflags = 0;
188 ag->packet.meaning->unit = SR_UNIT_VOLT;
189 ag->packet.data = ag->pattern_data;
190 ag->pattern = pattern;
193 g_hash_table_insert(devc->ch_ag, ch, ag);
195 if (++pattern == ARRAY_SIZE(analog_pattern_str))
202 return std_scan_complete(di, g_slist_append(NULL, sdi));
205 static void clear_helper(struct dev_context *devc)
210 /* Analog generators. */
211 g_hash_table_iter_init(&iter, devc->ch_ag);
212 while (g_hash_table_iter_next(&iter, NULL, &value))
214 g_hash_table_unref(devc->ch_ag);
217 static int dev_clear(const struct sr_dev_driver *di)
219 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
222 static int config_get(uint32_t key, GVariant **data,
223 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
225 struct dev_context *devc;
226 struct sr_channel *ch;
227 struct analog_gen *ag;
235 case SR_CONF_SAMPLERATE:
236 *data = g_variant_new_uint64(devc->cur_samplerate);
238 case SR_CONF_LIMIT_SAMPLES:
239 *data = g_variant_new_uint64(devc->limit_samples);
241 case SR_CONF_LIMIT_MSEC:
242 *data = g_variant_new_uint64(devc->limit_msec);
244 case SR_CONF_LIMIT_FRAMES:
245 *data = g_variant_new_uint64(devc->limit_frames);
247 case SR_CONF_AVERAGING:
248 *data = g_variant_new_boolean(devc->avg);
250 case SR_CONF_AVG_SAMPLES:
251 *data = g_variant_new_uint64(devc->avg_samples);
253 case SR_CONF_PATTERN_MODE:
255 return SR_ERR_CHANNEL_GROUP;
256 /* Any channel in the group will do. */
257 ch = cg->channels->data;
258 if (ch->type == SR_CHANNEL_LOGIC) {
259 pattern = devc->logic_pattern;
260 *data = g_variant_new_string(logic_pattern_str[pattern]);
261 } else if (ch->type == SR_CHANNEL_ANALOG) {
262 ag = g_hash_table_lookup(devc->ch_ag, ch);
263 pattern = ag->pattern;
264 *data = g_variant_new_string(analog_pattern_str[pattern]);
268 case SR_CONF_AMPLITUDE:
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 *data = g_variant_new_double(ag->amplitude);
278 case SR_CONF_CAPTURE_RATIO:
279 *data = g_variant_new_uint64(devc->capture_ratio);
288 static int config_set(uint32_t key, GVariant *data,
289 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
291 struct dev_context *devc;
292 struct analog_gen *ag;
293 struct sr_channel *ch;
295 int logic_pattern, analog_pattern;
300 case SR_CONF_SAMPLERATE:
301 devc->cur_samplerate = g_variant_get_uint64(data);
303 case SR_CONF_LIMIT_SAMPLES:
304 devc->limit_msec = 0;
305 devc->limit_samples = g_variant_get_uint64(data);
307 case SR_CONF_LIMIT_MSEC:
308 devc->limit_msec = g_variant_get_uint64(data);
309 devc->limit_samples = 0;
311 case SR_CONF_LIMIT_FRAMES:
312 devc->limit_frames = g_variant_get_uint64(data);
314 case SR_CONF_AVERAGING:
315 devc->avg = g_variant_get_boolean(data);
316 sr_dbg("%s averaging", devc->avg ? "Enabling" : "Disabling");
318 case SR_CONF_AVG_SAMPLES:
319 devc->avg_samples = g_variant_get_uint64(data);
320 sr_dbg("Setting averaging rate to %" PRIu64, devc->avg_samples);
322 case SR_CONF_PATTERN_MODE:
324 return SR_ERR_CHANNEL_GROUP;
325 logic_pattern = std_str_idx(data, ARRAY_AND_SIZE(logic_pattern_str));
326 analog_pattern = std_str_idx(data, ARRAY_AND_SIZE(analog_pattern_str));
327 if (logic_pattern < 0 && analog_pattern < 0)
329 for (l = cg->channels; l; l = l->next) {
331 if (ch->type == SR_CHANNEL_LOGIC) {
332 if (logic_pattern == -1)
334 sr_dbg("Setting logic pattern to %s",
335 logic_pattern_str[logic_pattern]);
336 devc->logic_pattern = logic_pattern;
337 /* Might as well do this now, these are static. */
338 if (logic_pattern == PATTERN_ALL_LOW)
339 memset(devc->logic_data, 0x00, LOGIC_BUFSIZE);
340 else if (logic_pattern == PATTERN_ALL_HIGH)
341 memset(devc->logic_data, 0xff, LOGIC_BUFSIZE);
342 } else if (ch->type == SR_CHANNEL_ANALOG) {
343 if (analog_pattern == -1)
345 sr_dbg("Setting analog pattern for channel %s to %s",
346 ch->name, analog_pattern_str[analog_pattern]);
347 ag = g_hash_table_lookup(devc->ch_ag, ch);
348 ag->pattern = analog_pattern;
353 case SR_CONF_AMPLITUDE:
355 return SR_ERR_CHANNEL_GROUP;
356 for (l = cg->channels; l; l = l->next) {
358 if (ch->type != SR_CHANNEL_ANALOG)
360 ag = g_hash_table_lookup(devc->ch_ag, ch);
361 ag->amplitude = g_variant_get_double(data);
364 case SR_CONF_CAPTURE_RATIO:
365 devc->capture_ratio = g_variant_get_uint64(data);
374 static int config_list(uint32_t key, GVariant **data,
375 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
377 struct sr_channel *ch;
381 case SR_CONF_SCAN_OPTIONS:
382 case SR_CONF_DEVICE_OPTIONS:
383 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
384 case SR_CONF_SAMPLERATE:
385 *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates));
387 case SR_CONF_TRIGGER_MATCH:
388 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
394 ch = cg->channels->data;
396 case SR_CONF_DEVICE_OPTIONS:
397 if (ch->type == SR_CHANNEL_LOGIC)
398 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_logic));
399 else if (ch->type == SR_CHANNEL_ANALOG) {
400 if (strcmp(cg->name, "Analog") == 0)
401 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog_group));
403 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog_channel));
408 case SR_CONF_PATTERN_MODE:
409 /* The analog group (with all 4 channels) shall not have a pattern property. */
410 if (strcmp(cg->name, "Analog") == 0)
413 if (ch->type == SR_CHANNEL_LOGIC)
414 *data = g_variant_new_strv(ARRAY_AND_SIZE(logic_pattern_str));
415 else if (ch->type == SR_CHANNEL_ANALOG)
416 *data = g_variant_new_strv(ARRAY_AND_SIZE(analog_pattern_str));
428 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
430 struct dev_context *devc;
432 struct sr_channel *ch;
439 devc->sent_samples = 0;
440 devc->sent_frame_samples = 0;
443 * Determine the numbers of logic and analog channels that are
444 * involved in the acquisition. Determine an offset and a mask to
445 * remove excess logic data content before datafeed submission.
447 devc->enabled_logic_channels = 0;
448 devc->enabled_analog_channels = 0;
449 for (l = sdi->channels; l; l = l->next) {
453 if (ch->type == SR_CHANNEL_ANALOG) {
454 devc->enabled_analog_channels++;
457 if (ch->type != SR_CHANNEL_LOGIC)
460 * TODO: Need we create a channel map here, such that the
461 * session datafeed packets will have a dense representation
462 * of the enabled channels' data? For example store channels
463 * D3 and D5 in bit positions 0 and 1 respectively, when all
464 * other channels are disabled? The current implementation
465 * generates a sparse layout, might provide data for logic
466 * channels that are disabled while it might suppress data
467 * from enabled channels at the same time.
469 devc->enabled_logic_channels++;
471 devc->first_partial_logic_index = devc->enabled_logic_channels / 8;
472 bitpos = devc->enabled_logic_channels % 8;
473 mask = (1 << bitpos) - 1;
474 devc->first_partial_logic_mask = mask;
475 sr_dbg("num logic %zu, partial off %zu, mask 0x%02x.",
476 devc->enabled_logic_channels,
477 devc->first_partial_logic_index,
478 devc->first_partial_logic_mask);
481 * Have the waveform for analog patterns pre-generated. It's
482 * supposed to be periodic, so the generator just needs to
483 * access the prepared sample data (DDS style).
485 g_hash_table_iter_init(&iter, devc->ch_ag);
486 while (g_hash_table_iter_next(&iter, NULL, &value))
487 demo_generate_analog_pattern(value, devc->cur_samplerate);
489 sr_session_source_add(sdi->session, -1, 0, 100,
490 demo_prepare_data, (struct sr_dev_inst *)sdi);
492 std_session_send_df_header(sdi);
494 if (devc->limit_frames > 0)
495 std_session_send_frame_begin(sdi);
497 /* We use this timestamp to decide how many more samples to send. */
498 devc->start_us = g_get_monotonic_time();
505 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
507 struct dev_context *devc;
509 sr_session_source_remove(sdi->session, -1);
512 if (devc->limit_frames > 0)
513 std_session_send_frame_end(sdi);
515 std_session_send_df_end(sdi);
520 static struct sr_dev_driver demo_driver_info = {
522 .longname = "Demo driver and pattern generator",
525 .cleanup = std_cleanup,
527 .dev_list = std_dev_list,
528 .dev_clear = dev_clear,
529 .config_get = config_get,
530 .config_set = config_set,
531 .config_list = config_list,
532 .dev_open = std_dummy_dev_open,
533 .dev_close = std_dummy_dev_close,
534 .dev_acquisition_start = dev_acquisition_start,
535 .dev_acquisition_stop = dev_acquisition_stop,
538 SR_REGISTER_DEV_DRIVER(demo_driver_info);