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>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #define pipe(fds) _pipe(fds, 4096, _O_BINARY)
32 #include "libsigrok.h"
33 #include "libsigrok-internal.h"
35 /* Message logging helpers with subsystem-specific prefix string. */
36 #define LOG_PREFIX "demo: "
37 #define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
38 #define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
39 #define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
40 #define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
41 #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
42 #define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args)
44 /* TODO: Number of probes should be configurable. */
47 #define DEMONAME "Demo device"
49 /* The size of chunks to send through the session bus. */
50 /* TODO: Should be configurable. */
51 #define BUFSIZE 1024*1024
59 /* Supported patterns which we can generate */
67 static const char *pattern_strings[] = {
75 /* Private, per-device-instance driver context. */
77 struct sr_dev_inst *sdi;
80 uint64_t cur_samplerate;
81 uint64_t limit_samples;
83 uint8_t sample_generator;
84 uint64_t samples_counter;
90 uint16_t trigger_mask;
91 uint16_t trigger_value;
92 uint16_t trigger_edge;
95 static const int hwcaps[] = {
96 SR_CONF_LOGIC_ANALYZER,
100 SR_CONF_LIMIT_SAMPLES,
105 static const int hwoptions[] = {
106 SR_CONF_PATTERN_MODE,
109 static const uint64_t samplerates[] = {
128 /* We name the probes 0-7 on our demo driver. */
129 static const char *probe_names[NUM_PROBES + 1] = {
130 "Channel 0", "Channel 1", "Channel 2", "Channel 3",
131 "Channel 4", "Channel 5", "Channel 6", "Channel 7",
132 "Channel 8", "Channel 9", "Channel 10", "Channel 11",
133 "Channel 12", "Channel 13", "Channel 14", "Channel 15",
137 /* Private, per-device-instance driver context. */
138 /* TODO: struct context as with the other drivers. */
140 /* List of struct sr_dev_inst, maintained by dev_open()/dev_close(). */
141 SR_PRIV struct sr_dev_driver demo_driver_info;
142 static struct sr_dev_driver *di = &demo_driver_info;
144 extern struct ds_trigger *trigger;
146 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
148 static int clear_instances(void)
150 /* Nothing needed so far. */
155 static int hw_init(struct sr_context *sr_ctx)
157 return std_hw_init(sr_ctx, di, LOG_PREFIX);
160 static GSList *hw_scan(GSList *options)
162 struct sr_dev_inst *sdi;
163 struct sr_probe *probe;
164 struct drv_context *drvc;
165 struct dev_context *devc;
175 sdi = sr_dev_inst_new(LOGIC, 0, SR_ST_ACTIVE, DEMONAME, NULL, NULL);
177 sr_err("Device instance creation failed.");
182 devices = g_slist_append(devices, sdi);
183 drvc->instances = g_slist_append(drvc->instances, sdi);
185 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
186 sr_err("Device context malloc failed.");
191 devc->cur_samplerate = SR_MHZ(200);
192 devc->limit_samples = 0;
193 devc->limit_msec = 0;
194 devc->sample_generator = PATTERN_SINE;
198 if (sdi->mode == LOGIC) {
199 for (i = 0; probe_names[i]; i++) {
200 if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE,
203 sdi->probes = g_slist_append(sdi->probes, probe);
205 } else if (sdi->mode == ANALOG) {
206 for (i = 0; i < DS_MAX_ANALOG_PROBES_NUM; i++) {
207 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
210 sdi->probes = g_slist_append(sdi->probes, probe);
217 static GSList *hw_dev_list(void)
219 return ((struct drv_context *)(di->priv))->instances;
222 static int hw_dev_open(struct sr_dev_inst *sdi)
226 sdi->status = SR_ST_ACTIVE;
231 static int hw_dev_close(struct sr_dev_inst *sdi)
235 sdi->status = SR_ST_INACTIVE;
240 static int hw_cleanup(void)
243 struct sr_dev_inst *sdi;
244 struct drv_context *drvc;
247 if (!(drvc = di->priv))
250 /* Properly close and free all devices. */
251 for (l = drvc->instances; l; l = l->next) {
252 if (!(sdi = l->data)) {
253 /* Log error, but continue cleaning up the rest. */
254 sr_err("%s: sdi was NULL, continuing", __func__);
258 sr_dev_inst_free(sdi);
260 g_slist_free(drvc->instances);
261 drvc->instances = NULL;
266 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi)
268 struct dev_context *const devc = sdi->priv;
271 case SR_CONF_SAMPLERATE:
272 *data = g_variant_new_uint64(devc->cur_samplerate);
274 case SR_CONF_LIMIT_SAMPLES:
275 *data = g_variant_new_uint64(devc->limit_samples);
277 case SR_CONF_LIMIT_MSEC:
278 *data = g_variant_new_uint64(devc->limit_msec);
280 case SR_CONF_DEVICE_MODE:
281 *data = g_variant_new_string(mode_strings[sdi->mode]);
283 case SR_CONF_PATTERN_MODE:
284 *data = g_variant_new_string(pattern_strings[devc->sample_generator]);
293 static int config_set(int id, GVariant *data, struct sr_dev_inst *sdi)
297 struct sr_probe *probe;
299 struct dev_context *const devc = sdi->priv;
301 if (sdi->status != SR_ST_ACTIVE)
302 return SR_ERR_DEV_CLOSED;
304 if (id == SR_CONF_SAMPLERATE) {
305 devc->cur_samplerate = g_variant_get_uint64(data);
306 sr_dbg("%s: setting samplerate to %" PRIu64, __func__,
307 devc->cur_samplerate);
309 } else if (id == SR_CONF_LIMIT_SAMPLES) {
310 devc->limit_msec = 0;
311 devc->limit_samples = g_variant_get_uint64(data);
312 sr_dbg("%s: setting limit_samples to %" PRIu64, __func__,
313 devc->limit_samples);
315 } else if (id == SR_CONF_LIMIT_MSEC) {
316 devc->limit_msec = g_variant_get_uint64(data);
317 devc->limit_samples = 0;
318 sr_dbg("%s: setting limit_msec to %" PRIu64, __func__,
321 } else if (id == SR_CONF_DEVICE_MODE) {
322 stropt = g_variant_get_string(data, NULL);
324 if (!strcmp(stropt, mode_strings[LOGIC])) {
326 sr_dev_probes_free(sdi);
327 for (i = 0; probe_names[i]; i++) {
328 if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE,
332 sdi->probes = g_slist_append(sdi->probes, probe);
334 } else if (!strcmp(stropt, mode_strings[ANALOG])) {
336 sr_dev_probes_free(sdi);
337 for (i = 0; i < DS_MAX_ANALOG_PROBES_NUM; i++) {
338 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
342 sdi->probes = g_slist_append(sdi->probes, probe);
347 sr_dbg("%s: setting mode to %d", __func__, sdi->mode);
348 }else if (id == SR_CONF_PATTERN_MODE) {
349 stropt = g_variant_get_string(data, NULL);
351 if (!strcmp(stropt, pattern_strings[PATTERN_SINE])) {
352 devc->sample_generator = PATTERN_SINE;
353 } else if (!strcmp(stropt, pattern_strings[PATTERN_SQUARE])) {
354 devc->sample_generator = PATTERN_SQUARE;
355 } else if (!strcmp(stropt, pattern_strings[PATTERN_TRIANGLE])) {
356 devc->sample_generator = PATTERN_TRIANGLE;
357 } else if (!strcmp(stropt, pattern_strings[PATTERN_SAWTOOTH])) {
358 devc->sample_generator = PATTERN_SAWTOOTH;
359 } else if (!strcmp(stropt, pattern_strings[PATTERN_RANDOM])) {
360 devc->sample_generator = PATTERN_RANDOM;
364 sr_dbg("%s: setting pattern to %d",
365 __func__, devc->sample_generator);
373 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
381 case SR_CONF_DEVICE_OPTIONS:
382 // *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
383 // hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
384 *data = g_variant_new_from_data(G_VARIANT_TYPE("ai"),
385 hwcaps, ARRAY_SIZE(hwcaps)*sizeof(int32_t), TRUE, NULL, NULL);
387 case SR_CONF_DEVICE_CONFIGS:
388 // *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
389 // hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
390 *data = g_variant_new_from_data(G_VARIANT_TYPE("ai"),
391 hwoptions, ARRAY_SIZE(hwoptions)*sizeof(int32_t), TRUE, NULL, NULL);
393 case SR_CONF_SAMPLERATE:
394 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
395 // gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates,
396 // ARRAY_SIZE(samplerates), sizeof(uint64_t));
397 gvar = g_variant_new_from_data(G_VARIANT_TYPE("at"),
398 samplerates, ARRAY_SIZE(samplerates)*sizeof(uint64_t), TRUE, NULL, NULL);
399 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
400 *data = g_variant_builder_end(&gvb);
402 case SR_CONF_DEVICE_MODE:
403 *data = g_variant_new_strv(mode_strings, ARRAY_SIZE(mode_strings));
405 case SR_CONF_PATTERN_MODE:
406 *data = g_variant_new_strv(pattern_strings, ARRAY_SIZE(pattern_strings));
415 static void samples_generator(uint16_t *buf, uint64_t size,
416 struct dev_context *devc)
418 static uint16_t p = 0;
422 switch (devc->sample_generator) {
423 case PATTERN_SINE: /* Sine */
424 for (i = 0; i < size; i++) {
425 if (i%CONST_LEN == 0) {
426 demo_data = 0x8000 * sin(2 * PI * p / 0xffff) + 0x8000;
429 *(buf + i) = demo_data;
433 for (i = 0; i < size; i++) {
434 if (i%CONST_LEN == 0) {
435 demo_data = p > 0x7fff ? 0xf000 : 0x1000;
438 *(buf + i) = demo_data;
441 case PATTERN_TRIANGLE:
442 for (i = 0; i < size; i++) {
443 if (i%CONST_LEN == 0) {
444 demo_data = p > 0x7fff ? 0xffff * (2.0f * (0x10000 - p) / 0x10000) :
445 0xffff * (2.0f * p / 0x10000);
448 *(buf + i) = demo_data;
451 case PATTERN_SAWTOOTH:
452 for (i = 0; i < size; i++) {
453 if (i%CONST_LEN == 0) {
457 *(buf + i) = demo_data;
460 case PATTERN_RANDOM: /* Random */
461 for (i = 0; i < size; i++) {
462 if (i%CONST_LEN == 0)
463 demo_data = (uint16_t)(rand() * (0x10000 * 1.0f / RAND_MAX));
464 *(buf + i) = demo_data;
468 sr_err("Unknown pattern: %d.", devc->sample_generator);
473 /* Callback handling data */
474 static int receive_data(int fd, int revents, const struct sr_dev_inst *sdi)
476 struct dev_context *devc = sdi->priv;
477 struct sr_datafeed_packet packet;
478 struct sr_datafeed_logic logic;
479 struct sr_datafeed_analog analog;
480 //uint16_t buf[BUFSIZE];
482 static uint64_t samples_to_send, expected_samplenum, sending_now;
483 int64_t time, elapsed;
484 static uint16_t last_sample = 0;
491 if (!(buf = g_try_malloc(BUFSIZE*sizeof(uint16_t)))) {
492 sr_err("buf for receive_data malloc failed.");
496 /* How many "virtual" samples should we have collected by now? */
497 time = g_get_monotonic_time();
498 elapsed = time - devc->starttime;
499 devc->starttime = time;
500 expected_samplenum = elapsed * devc->cur_samplerate / 1000000;
501 /* Of those, how many do we still have to send? */
502 //samples_to_send = (expected_samplenum - devc->samples_counter) / CONST_LEN * CONST_LEN;
503 samples_to_send = expected_samplenum / CONST_LEN * CONST_LEN;
505 if (devc->limit_samples) {
506 if (sdi->mode == LOGIC)
507 samples_to_send = MIN(samples_to_send,
508 devc->limit_samples - devc->samples_counter);
509 else if (sdi->mode == ANALOG)
510 samples_to_send = MIN(samples_to_send,
511 devc->limit_samples);
514 while (samples_to_send > 0) {
515 sending_now = MIN(samples_to_send, BUFSIZE);
516 samples_generator(buf, sending_now, devc);
518 if (devc->trigger_stage != 0) {
519 for (i = 0; i < sending_now; i++) {
520 if (devc->trigger_edge == 0) {
521 if ((*(buf + i) | devc->trigger_mask) ==
522 (devc->trigger_value | devc->trigger_mask)) {
523 devc->trigger_stage = 0;
527 cur_sample = *(buf + i);
528 if (((last_sample & devc->trigger_edge) ==
529 (~devc->trigger_value & devc->trigger_edge)) &&
530 ((cur_sample | devc->trigger_mask) ==
531 (devc->trigger_value | devc->trigger_mask)) &&
532 ((cur_sample & devc->trigger_edge) ==
533 (devc->trigger_value & devc->trigger_edge))) {
534 devc->trigger_stage = 0;
537 last_sample = cur_sample;
540 if (devc->trigger_stage == 0) {
541 struct ds_trigger_pos demo_trigger_pos;
542 demo_trigger_pos.real_pos = i;
543 packet.type = SR_DF_TRIGGER;
544 packet.payload = &demo_trigger_pos;
545 sr_session_send(sdi, &packet);
549 if (devc->trigger_stage == 0){
550 samples_to_send -= sending_now;
551 if (sdi->mode == LOGIC) {
552 packet.type = SR_DF_LOGIC;
553 packet.payload = &logic;
554 logic.length = sending_now * (NUM_PROBES >> 3);
555 logic.unitsize = (NUM_PROBES >> 3);
557 } else if (sdi->mode == ANALOG) {
558 packet.type = SR_DF_ANALOG;
559 packet.payload = &analog;
560 analog.probes = sdi->probes;
561 analog.num_samples = sending_now;
562 analog.mq = SR_MQ_VOLTAGE;
563 analog.unit = SR_UNIT_VOLT;
564 analog.mqflags = SR_MQFLAG_AC;
568 sr_session_send(sdi, &packet);
569 if (sdi->mode == LOGIC)
570 devc->samples_counter += sending_now;
571 else if (sdi->mode == ANALOG)
572 devc->samples_counter = (devc->samples_counter + sending_now) % devc->limit_samples;
578 if (sdi->mode == LOGIC &&
579 devc->limit_samples &&
580 devc->samples_counter >= devc->limit_samples) {
581 sr_info("Requested number of samples reached.");
582 hw_dev_acquisition_stop(sdi, NULL);
592 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
595 struct dev_context *const devc = sdi->priv;
599 if (sdi->status != SR_ST_ACTIVE)
600 return SR_ERR_DEV_CLOSED;
602 //devc->cb_data = cb_data;
603 devc->samples_counter = 0;
609 if (!trigger->trigger_en || sdi->mode == ANALOG) {
610 devc->trigger_stage = 0;
612 devc->trigger_mask = ds_trigger_get_mask0(TriggerStages);
613 devc->trigger_value = ds_trigger_get_value0(TriggerStages);
614 devc->trigger_edge = ds_trigger_get_edge0(TriggerStages);
615 if (devc->trigger_edge != 0)
616 devc->trigger_stage = 2;
618 devc->trigger_stage = 1;
622 * Setting two channels connected by a pipe is a remnant from when the
623 * demo driver generated data in a thread, and collected and sent the
624 * data in the main program loop.
625 * They are kept here because it provides a convenient way of setting
626 * up a timeout-based polling mechanism.
628 if (pipe(devc->pipe_fds)) {
629 /* TODO: Better error message. */
630 sr_err("%s: pipe() failed", __func__);
634 devc->channel = g_io_channel_unix_new(devc->pipe_fds[0]);
636 g_io_channel_set_flags(devc->channel, G_IO_FLAG_NONBLOCK, NULL);
638 /* Set channel encoding to binary (default is UTF-8). */
639 g_io_channel_set_encoding(devc->channel, NULL, NULL);
641 /* Make channels to unbuffered. */
642 g_io_channel_set_buffered(devc->channel, FALSE);
644 sr_session_source_add_channel(devc->channel, G_IO_IN | G_IO_ERR,
645 100, receive_data, sdi);
647 /* Send header packet to the session bus. */
648 //std_session_send_df_header(cb_data, LOG_PREFIX);
649 std_session_send_df_header(sdi, LOG_PREFIX);
651 /* We use this timestamp to decide how many more samples to send. */
652 devc->starttime = g_get_monotonic_time();
657 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
659 struct dev_context *const devc = sdi->priv;
660 struct sr_datafeed_packet packet;
664 sr_dbg("Stopping aquisition.");
667 sr_session_source_remove_channel(devc->channel);
668 g_io_channel_shutdown(devc->channel, FALSE, NULL);
669 g_io_channel_unref(devc->channel);
670 devc->channel = NULL;
672 /* Send last packet. */
673 packet.type = SR_DF_END;
674 sr_session_send(sdi, &packet);
679 static int hw_dev_test(struct sr_dev_inst *sdi)
687 SR_PRIV struct sr_dev_driver demo_driver_info = {
689 .longname = "Demo driver and pattern generator",
692 .cleanup = hw_cleanup,
694 .dev_list = hw_dev_list,
695 .dev_clear = clear_instances,
696 .config_get = config_get,
697 .config_set = config_set,
698 .config_list = config_list,
699 .dev_open = hw_dev_open,
700 .dev_close = hw_dev_close,
701 .dev_test = hw_dev_test,
702 .dev_acquisition_start = hw_dev_acquisition_start,
703 .dev_acquisition_stop = hw_dev_acquisition_stop,