2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2011 Olivier Fauchon <olivier@aixmarseille.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <sigrok-internal.h>
30 #define pipe(fds) _pipe(fds, 4096, _O_BINARY)
35 #define DEMONAME "Demo device"
36 /* size of chunks to send through the session bus */
39 /* Supported patterns which we can generate */
42 * Pattern which spells "sigrok" using '0's (with '1's as "background")
43 * when displayed using the 'bits' output format.
48 * Pattern which consists of (pseudo-)random values on all probes.
53 * Pattern which consists of incrementing numbers.
54 * TODO: Better description.
59 /* FIXME: Should not be global. */
60 GIOChannel *channels[2];
64 uint8_t sample_generator;
65 uint8_t thread_running;
66 uint64_t samples_counter;
68 gpointer session_device_id;
72 static int capabilities[] = {
73 SR_HWCAP_LOGIC_ANALYZER,
75 SR_HWCAP_PATTERN_MODE,
76 SR_HWCAP_LIMIT_SAMPLES,
81 static struct sr_samplerates samplerates = {
88 static const char *pattern_strings[] = {
94 static uint8_t pattern_sigrok[] = {
95 0x4c, 0x92, 0x92, 0x92, 0x64, 0x00, 0x00, 0x00,
96 0x82, 0xfe, 0xfe, 0x82, 0x00, 0x00, 0x00, 0x00,
97 0x7c, 0x82, 0x82, 0x92, 0x74, 0x00, 0x00, 0x00,
98 0xfe, 0x12, 0x12, 0x32, 0xcc, 0x00, 0x00, 0x00,
99 0x7c, 0x82, 0x82, 0x82, 0x7c, 0x00, 0x00, 0x00,
100 0xfe, 0x10, 0x28, 0x44, 0x82, 0x00, 0x00, 0x00,
101 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
102 0xbe, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
105 /* List of struct sr_device_instance, maintained by opendev()/closedev(). */
106 static GSList *device_instances = NULL;
107 static uint64_t cur_samplerate = SR_KHZ(200);
108 static uint64_t limit_samples = 0;
109 static uint64_t limit_msec = 0;
110 static int default_pattern = PATTERN_SIGROK;
111 static GThread *my_thread;
112 static int thread_running;
114 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
116 static int hw_init(const char *deviceinfo)
118 struct sr_device_instance *sdi;
120 /* Avoid compiler warnings. */
121 deviceinfo = deviceinfo;
123 sdi = sr_device_instance_new(0, SR_ST_ACTIVE, DEMONAME, NULL, NULL);
127 device_instances = g_slist_append(device_instances, sdi);
132 static int hw_opendev(int device_index)
134 /* Avoid compiler warnings. */
135 device_index = device_index;
137 /* Nothing needed so far. */
142 static int hw_closedev(int device_index)
144 /* Avoid compiler warnings. */
145 device_index = device_index;
147 /* Nothing needed so far. */
152 static void hw_cleanup(void)
154 /* Nothing needed so far. */
157 static void *hw_get_device_info(int device_index, int device_info_id)
159 struct sr_device_instance *sdi;
162 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
165 switch (device_info_id) {
169 case SR_DI_NUM_PROBES:
170 info = GINT_TO_POINTER(NUM_PROBES);
172 case SR_DI_SAMPLERATES:
175 case SR_DI_CUR_SAMPLERATE:
176 info = &cur_samplerate;
178 case SR_DI_PATTERNMODES:
179 info = &pattern_strings;
186 static int hw_get_status(int device_index)
188 /* Avoid compiler warnings. */
189 device_index = device_index;
194 static int *hw_get_capabilities(void)
199 static int hw_set_configuration(int device_index, int capability, void *value)
204 /* Avoid compiler warnings. */
205 device_index = device_index;
207 if (capability == SR_HWCAP_PROBECONFIG) {
208 /* Nothing to do, but must be supported */
210 } else if (capability == SR_HWCAP_SAMPLERATE) {
211 cur_samplerate = *(uint64_t *)value;
212 sr_dbg("demo: %s: setting samplerate to %" PRIu64, __func__,
215 } else if (capability == SR_HWCAP_LIMIT_SAMPLES) {
216 limit_samples = *(uint64_t *)value;
217 sr_dbg("demo: %s: setting limit_samples to %" PRIu64, __func__,
220 } else if (capability == SR_HWCAP_LIMIT_MSEC) {
221 limit_msec = *(uint64_t *)value;
222 sr_dbg("demo: %s: setting limit_msec to %" PRIu64, __func__,
225 } else if (capability == SR_HWCAP_PATTERN_MODE) {
227 if (!strcmp(stropt, "random")) {
228 default_pattern = PATTERN_RANDOM;
230 } else if (!strcmp(stropt, "incremental")) {
231 default_pattern = PATTERN_INC;
236 sr_dbg("demo: %s: setting pattern to %d", __func__,
245 static void samples_generator(uint8_t *buf, uint64_t size, void *data)
247 static uint64_t p = 0;
248 struct databag *mydata = data;
251 memset(buf, 0, size);
253 switch (mydata->sample_generator) {
255 for (i = 0; i < size; i++) {
256 *(buf + i) = ~(pattern_sigrok[p] >> 1);
261 case PATTERN_RANDOM: /* Random */
262 for (i = 0; i < size; i++)
263 *(buf + i) = (uint8_t)(rand() & 0xff);
265 case PATTERN_INC: /* Simple increment */
266 for (i = 0; i < size; i++)
272 /* Thread function */
273 static void thread_func(void *data)
275 struct databag *mydata = data;
276 uint8_t buf[BUFSIZE];
277 uint64_t nb_to_send = 0;
280 double time_cur, time_last, time_diff;
282 time_last = g_timer_elapsed(mydata->timer, NULL);
284 while (thread_running) {
286 time_cur = g_timer_elapsed(mydata->timer, NULL);
288 time_diff = time_cur - time_last;
289 time_last = time_cur;
291 nb_to_send = cur_samplerate * time_diff;
294 nb_to_send = MIN(nb_to_send,
295 limit_samples - mydata->samples_counter);
297 /* Make sure we don't overflow. */
298 nb_to_send = MIN(nb_to_send, BUFSIZE);
301 samples_generator(buf, nb_to_send, data);
302 mydata->samples_counter += nb_to_send;
304 g_io_channel_write_chars(channels[1], (gchar *)&buf,
305 nb_to_send, (gsize *)&bytes_written, NULL);
308 /* Check if we're done. */
309 if ((limit_msec && time_cur * 1000 > limit_msec) ||
310 (limit_samples && mydata->samples_counter >= limit_samples))
312 close(mydata->pipe_fds[1]);
320 /* Callback handling data */
321 static int receive_data(int fd, int revents, void *user_data)
323 struct sr_datafeed_packet packet;
327 /* Avoid compiler warnings. */
332 g_io_channel_read_chars(channels[0],
333 (gchar *)&c, BUFSIZE, &z, NULL);
336 packet.type = SR_DF_LOGIC;
340 sr_session_bus(user_data, &packet);
344 if (!thread_running && z <= 0)
346 /* Make sure we don't receive more packets */
347 g_io_channel_close(channels[0]);
349 /* Send last packet. */
350 packet.type = SR_DF_END;
351 sr_session_bus(user_data, &packet);
359 static int hw_start_acquisition(int device_index, gpointer session_device_id)
361 struct sr_datafeed_packet *packet;
362 struct sr_datafeed_header *header;
363 struct databag *mydata;
365 /* TODO: 'mydata' is never g_free()'d? */
366 if (!(mydata = g_try_malloc(sizeof(struct databag)))) {
367 sr_err("demo: %s: mydata malloc failed", __func__);
368 return SR_ERR_MALLOC;
371 mydata->sample_generator = default_pattern;
372 mydata->session_device_id = session_device_id;
373 mydata->device_index = device_index;
374 mydata->samples_counter = 0;
376 if (pipe(mydata->pipe_fds))
379 channels[0] = g_io_channel_unix_new(mydata->pipe_fds[0]);
380 channels[1] = g_io_channel_unix_new(mydata->pipe_fds[1]);
382 /* Set channel encoding to binary (default is UTF-8). */
383 g_io_channel_set_encoding(channels[0], NULL, NULL);
384 g_io_channel_set_encoding(channels[1], NULL, NULL);
386 /* Make channels to unbuffered. */
387 g_io_channel_set_buffered(channels[0], FALSE);
388 g_io_channel_set_buffered(channels[1], FALSE);
390 sr_source_add(mydata->pipe_fds[0], G_IO_IN | G_IO_ERR, 40,
391 receive_data, session_device_id);
393 /* Run the demo thread. */
395 /* this needs to be done between g_thread_init() and g_thread_create() */
396 mydata->timer = g_timer_new();
399 g_thread_create((GThreadFunc)thread_func, mydata, TRUE, NULL);
403 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
404 sr_err("demo: %s: packet malloc failed", __func__);
405 return SR_ERR_MALLOC;
408 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
409 sr_err("demo: %s: header malloc failed", __func__);
410 return SR_ERR_MALLOC;
413 packet->type = SR_DF_HEADER;
414 packet->length = sizeof(struct sr_datafeed_header);
415 packet->payload = (unsigned char *)header;
416 header->feed_version = 1;
417 gettimeofday(&header->starttime, NULL);
418 header->samplerate = cur_samplerate;
419 header->protocol_id = SR_PROTO_RAW;
420 header->num_logic_probes = NUM_PROBES;
421 header->num_analog_probes = 0;
422 sr_session_bus(session_device_id, packet);
429 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
431 /* Avoid compiler warnings. */
432 device_index = device_index;
433 session_device_id = session_device_id;
435 /* Stop generate thread. */
439 struct sr_device_plugin demo_plugin_info = {
441 .longname = "Demo driver and pattern generator",
444 .cleanup = hw_cleanup,
445 .opendev = hw_opendev,
446 .closedev = hw_closedev,
447 .get_device_info = hw_get_device_info,
448 .get_status = hw_get_status,
449 .get_capabilities = hw_get_capabilities,
450 .set_configuration = hw_set_configuration,
451 .start_acquisition = hw_start_acquisition,
452 .stop_acquisition = hw_stop_acquisition,