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
32 #define pipe(fds) _pipe(fds, 4096, _O_BINARY)
41 #define DEMONAME "Demo device"
42 /* size of chunks to send through the session bus */
56 GIOChannel *channels[2];
60 uint8_t sample_generator;
61 uint8_t thread_running;
62 uint64_t samples_counter;
64 gpointer session_device_id;
68 static int capabilities[] = {
76 static const char *patternmodes[] = {
84 static uint8_t genmode_default[] = {
85 0x4c, 0x92, 0x92, 0x92, 0x64, 0x00, 0x00, 0x00,
86 0x82, 0xfe, 0xfe, 0x82, 0x00, 0x00, 0x00, 0x00,
87 0x7c, 0x82, 0x82, 0x92, 0x74, 0x00, 0x00, 0x00,
88 0xfe, 0x12, 0x12, 0x32, 0xcc, 0x00, 0x00, 0x00,
89 0x7c, 0x82, 0x82, 0x82, 0x7c, 0x00, 0x00, 0x00,
90 0xfe, 0x10, 0x28, 0x44, 0x82, 0x00, 0x00, 0x00,
91 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
92 0xbe, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
96 /* List of struct sigrok_device_instance, maintained by opendev()/closedev(). */
97 static GSList *device_instances = NULL;
98 static uint64_t cur_samplerate = KHZ(200);
99 static uint64_t limit_samples = 0;
100 static uint64_t limit_msec = 0;
101 static int default_genmode = GENMODE_DEFAULT;
102 static GThread *my_thread;
103 static int thread_running;
105 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
107 static int hw_init(char *deviceinfo)
109 struct sigrok_device_instance *sdi;
111 /* Avoid compiler warnings. */
112 deviceinfo = deviceinfo;
114 sdi = sigrok_device_instance_new(0, ST_ACTIVE, DEMONAME, NULL, NULL);
118 device_instances = g_slist_append(device_instances, sdi);
123 static int hw_opendev(int device_index)
125 /* Avoid compiler warnings. */
126 device_index = device_index;
128 /* Nothing needed so far. */
132 static void hw_closedev(int device_index)
134 /* Avoid compiler warnings. */
135 device_index = device_index;
137 /* Nothing needed so far. */
140 static void hw_cleanup(void)
142 /* Nothing needed so far. */
145 static void *hw_get_device_info(int device_index, int device_info_id)
147 struct sigrok_device_instance *sdi;
150 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
153 switch (device_info_id) {
158 info = GINT_TO_POINTER(NUM_PROBES);
160 case DI_CUR_SAMPLERATE:
161 info = &cur_samplerate;
163 case DI_PATTERNMODES:
164 info = &patternmodes;
168 info = GINT_TO_POINTER(PROBE_TYPE_ANALOG);
176 static int hw_get_status(int device_index)
178 /* Avoid compiler warnings. */
179 device_index = device_index;
184 static int *hw_get_capabilities(void)
189 static int hw_set_configuration(int device_index, int capability, void *value)
195 /* Avoid compiler warnings. */
196 device_index = device_index;
198 if (capability == HWCAP_PROBECONFIG) {
201 } else if (capability == HWCAP_LIMIT_SAMPLES) {
203 limit_samples = *tmp_u64;
205 } else if (capability == HWCAP_LIMIT_MSEC) {
207 limit_msec = *tmp_u64;
209 } else if (capability == HWCAP_PATTERN_MODE) {
211 if (!strcmp(stropt, "random")) {
212 default_genmode = GENMODE_RANDOM;
214 } else if (!strcmp(stropt, "incremental")) {
215 default_genmode = GENMODE_INC;
217 } else if (!strcmp(stropt, "sine")) {
218 default_genmode = GENMODE_SINE;
230 static void samples_generator(uint8_t *buf, uint64_t size, void *data)
232 struct databag *mydata = data;
236 * We will simulate a device with 8 logic probes and 1 analog probe.
237 * This fictional device sends the data packed: 8 bits for 8 logic
238 * probes and 16 bits for the analog probe, in this order.
240 * I could just generate a properly formatted DF_ANALOG packet here,
241 * but I will leave the formatting to receive_data() to make its code
242 * more like a real hardware driver.
244 memset(buf, 0, size * 3);
246 switch (mydata->sample_generator) {
248 case GENMODE_DEFAULT:
250 for (i = 0; i < size * 3; i += 3) {
252 *(uint16_t *) (buf + i + 1) =
253 (uint16_t) (sin(i / 3) * 256 * 30);
257 for (i = 0; i < size * 3; i += 3) {
258 *(buf + i) = (uint8_t)(rand() & 0xff);
259 *(uint16_t *) (buf + i + 1) = (uint16_t)(rand() & 0xffff);
263 for (i = 0; i < size * 3; i += 3) {
265 *(uint16_t *)(buf + i + 1) = i / 3 * 256 * 10;
271 memset(buf, 0, size);
273 switch (mydata->sample_generator) {
274 case GENMODE_DEFAULT:
276 for (i = 0; i < size; i++) {
277 *(buf + i) = ~(genmode_default[p] >> 1);
282 case GENMODE_RANDOM: /* Random */
283 for (i = 0; i < size; i++)
284 *(buf + i) = (uint8_t)(rand() & 0xff);
286 case GENMODE_INC: /* Simple increment */
287 for (i = 0; i < size; i++)
294 /* Thread function */
295 static void thread_func(void *data)
297 struct databag *mydata = data;
298 uint8_t buf[BUFSIZE];
299 uint64_t nb_to_send = 0;
302 double time_cur, time_last, time_diff;
304 time_last = g_timer_elapsed(mydata->timer, NULL);
306 while (thread_running) {
308 time_cur = g_timer_elapsed(mydata->timer, NULL);
310 time_diff = time_cur - time_last;
311 time_last = time_cur;
313 nb_to_send = cur_samplerate * time_diff;
316 nb_to_send = MIN(nb_to_send,
317 limit_samples - mydata->samples_counter);
319 /* Make sure we don't overflow. */
321 nb_to_send = MIN(nb_to_send, BUFSIZE / 3);
323 nb_to_send = MIN(nb_to_send, BUFSIZE);
327 samples_generator(buf, nb_to_send, data);
328 mydata->samples_counter += nb_to_send;
330 g_io_channel_write_chars(channels[1], (gchar *) &buf,
331 nb_to_send * 3, (gsize *) &bytes_written, NULL);
333 g_io_channel_write_chars(channels[1], (gchar *) &buf,
334 nb_to_send, (gsize *) &bytes_written, NULL);
338 /* Check if we're done. */
339 if ((limit_msec && time_cur * 1000 > limit_msec) ||
340 (limit_samples && mydata->samples_counter >= limit_samples))
342 close(mydata->pipe_fds[1]);
350 /* Callback handling data */
351 static int receive_data(int fd, int revents, void *user_data)
353 struct datafeed_packet packet;
357 struct analog_sample *sample;
359 int sample_size = sizeof(struct analog_sample) +
360 (NUM_PROBES * sizeof(struct analog_probe));
364 /* Avoid compiler warnings. */
369 g_io_channel_read_chars(channels[0],
370 (gchar *) &c, BUFSIZE, (gsize *) &z, NULL);
374 packet.type = DF_ANALOG;
376 packet.length = (z / 3) * sample_size;
377 packet.unitsize = sample_size;
379 buf = malloc(sample_size * packet.length);
383 /* Craft our packet. */
384 for (i = 0; i < z / 3; i++) {
385 sample = (struct analog_sample *) (buf + (i * sample_size));
386 sample->num_probes = NUM_PROBES;
389 for (x = 0; x < NUM_PROBES - 1; x++) {
390 sample->probes[x].val =
392 sample->probes[x].res = 1;
395 /* 1 Analog probe, 16 bit adc */
396 for (; x < NUM_PROBES; x++) {
397 sample->probes[x].val =
398 *(uint16_t *) (c + i * 3 + 1);
399 sample->probes[x].val &= ((1 << 16) - 1);
400 sample->probes[x].res = 16;
405 packet.payload = buf;
406 session_bus(user_data, &packet);
409 packet.type = DF_LOGIC;
413 session_bus(user_data, &packet);
418 if (!thread_running && z <= 0)
420 /* Make sure we don't receive more packets */
421 g_io_channel_close(channels[0]);
423 /* Send last packet. */
424 packet.type = DF_END;
425 session_bus(user_data, &packet);
433 static int hw_start_acquisition(int device_index, gpointer session_device_id)
435 struct datafeed_packet *packet;
436 struct datafeed_header *header;
437 struct databag *mydata;
439 mydata = malloc(sizeof(struct databag));
441 return SIGROK_ERR_MALLOC;
443 mydata->sample_generator = default_genmode;
444 mydata->session_device_id = session_device_id;
445 mydata->device_index = device_index;
446 mydata->samples_counter = 0;
448 if (pipe(mydata->pipe_fds))
451 channels[0] = g_io_channel_unix_new(mydata->pipe_fds[0]);
452 channels[1] = g_io_channel_unix_new(mydata->pipe_fds[1]);
454 /* Set channel encoding to binary (default is UTF-8). */
455 g_io_channel_set_encoding(channels[0], NULL, NULL);
456 g_io_channel_set_encoding(channels[1], NULL, NULL);
458 /* Make channels to unbuffered. */
459 g_io_channel_set_buffered(channels[0], FALSE);
460 g_io_channel_set_buffered(channels[1], FALSE);
462 source_add(mydata->pipe_fds[0], G_IO_IN | G_IO_ERR, 40, receive_data,
465 /* Run the demo thread. */
467 mydata->timer = g_timer_new();
470 g_thread_create((GThreadFunc)thread_func, mydata, TRUE, NULL);
474 packet = malloc(sizeof(struct datafeed_packet));
475 header = malloc(sizeof(struct datafeed_header));
476 if (!packet || !header)
477 return SIGROK_ERR_MALLOC;
479 packet->type = DF_HEADER;
480 packet->length = sizeof(struct datafeed_header);
481 packet->payload = (unsigned char *)header;
483 packet->unitsize = sizeof(struct analog_sample) +
484 (NUM_PROBES * sizeof(struct analog_probe));
486 header->feed_version = 1;
487 gettimeofday(&header->starttime, NULL);
488 header->samplerate = cur_samplerate;
489 header->protocol_id = PROTO_RAW;
490 header->num_logic_probes = NUM_PROBES;
491 header->num_analog_probes = 0;
492 session_bus(session_device_id, packet);
499 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
501 /* Avoid compiler warnings. */
502 device_index = device_index;
503 session_device_id = session_device_id;
505 /* Stop generate thread. */
509 struct device_plugin demo_plugin_info = {
519 hw_set_configuration,
520 hw_start_acquisition,