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;
171 static int hw_get_status(int device_index)
173 /* Avoid compiler warnings. */
174 device_index = device_index;
179 static int *hw_get_capabilities(void)
184 static int hw_set_configuration(int device_index, int capability, void *value)
190 /* Avoid compiler warnings. */
191 device_index = device_index;
193 if (capability == HWCAP_PROBECONFIG) {
196 } else if (capability == HWCAP_LIMIT_SAMPLES) {
198 limit_samples = *tmp_u64;
200 } else if (capability == HWCAP_LIMIT_MSEC) {
202 limit_msec = *tmp_u64;
204 } else if (capability == HWCAP_PATTERN_MODE) {
206 if (!strcmp(stropt, "random")) {
207 default_genmode = GENMODE_RANDOM;
209 } else if (!strcmp(stropt, "incremental")) {
210 default_genmode = GENMODE_INC;
212 } else if (!strcmp(stropt, "sine")) {
213 default_genmode = GENMODE_SINE;
225 static void samples_generator(uint8_t *buf, uint64_t size, void *data)
227 struct databag *mydata = data;
231 * We will simulate a device with 8 logic probes and 1 analog probe.
232 * This fictional device sends the data packed: 8 bits for 8 logic
233 * probes and 16 bits for the analog probe, in this order.
235 * I could just generate a properly formatted DF_ANALOG packet here,
236 * but I will leave the formatting to receive_data() to make its code
237 * more like a real hardware driver.
239 memset(buf, 0, size * 3);
241 switch (mydata->sample_generator) {
243 case GENMODE_DEFAULT:
245 for (i = 0; i < size * 3; i += 3) {
247 *(uint16_t *) (buf + i + 1) =
248 (uint16_t) (sin(i / 3) * 256 * 30);
252 for (i = 0; i < size * 3; i += 3) {
253 *(buf + i) = (uint8_t)(rand() & 0xff);
254 *(uint16_t *) (buf + i + 1) = (uint16_t)(rand() & 0xffff);
258 for (i = 0; i < size * 3; i += 3) {
260 *(uint16_t *)(buf + i + 1) = i / 3 * 256 * 10;
266 memset(buf, 0, size);
268 switch (mydata->sample_generator) {
269 case GENMODE_DEFAULT:
271 for (i = 0; i < size; i++) {
272 *(buf + i) = ~(genmode_default[p] >> 1);
277 case GENMODE_RANDOM: /* Random */
278 for (i = 0; i < size; i++)
279 *(buf + i) = (uint8_t)(rand() & 0xff);
281 case GENMODE_INC: /* Simple increment */
282 for (i = 0; i < size; i++)
289 /* Thread function */
290 static void thread_func(void *data)
292 struct databag *mydata = data;
293 uint8_t buf[BUFSIZE];
294 uint64_t nb_to_send = 0;
297 double time_cur, time_last, time_diff;
299 time_last = g_timer_elapsed(mydata->timer, NULL);
301 while (thread_running) {
303 time_cur = g_timer_elapsed(mydata->timer, NULL);
305 time_diff = time_cur - time_last;
306 time_last = time_cur;
308 nb_to_send = cur_samplerate * time_diff;
311 nb_to_send = MIN(nb_to_send,
312 limit_samples - mydata->samples_counter);
314 /* Make sure we don't overflow. */
316 nb_to_send = MIN(nb_to_send, BUFSIZE / 3);
318 nb_to_send = MIN(nb_to_send, BUFSIZE);
322 samples_generator(buf, nb_to_send, data);
323 mydata->samples_counter += nb_to_send;
325 g_io_channel_write_chars(channels[1], (gchar *) &buf,
326 nb_to_send * 3, (gsize *) &bytes_written, NULL);
328 g_io_channel_write_chars(channels[1], (gchar *) &buf,
329 nb_to_send, (gsize *) &bytes_written, NULL);
333 /* Check if we're done. */
334 if ((limit_msec && time_cur * 1000 > limit_msec) ||
335 (limit_samples && mydata->samples_counter >= limit_samples))
337 close(mydata->pipe_fds[1]);
345 /* Callback handling data */
346 static int receive_data(int fd, int revents, void *user_data)
348 struct datafeed_packet packet;
352 struct analog_sample *sample;
354 int sample_size = sizeof(struct analog_sample) +
355 (NUM_PROBES * sizeof(struct analog_probe));
359 /* Avoid compiler warnings. */
364 g_io_channel_read_chars(channels[0],
365 (gchar *) &c, BUFSIZE, (gsize *) &z, NULL);
369 packet.type = DF_ANALOG;
371 packet.length = (z / 3) * sample_size;
372 packet.unitsize = sample_size;
374 buf = malloc(sample_size * packet.length);
378 /* Craft our packet. */
379 for (i = 0; i < z / 3; i++) {
380 sample = (struct analog_sample *) (buf + (i * sample_size));
381 sample->num_probes = NUM_PROBES;
384 for (x = 0; x < NUM_PROBES - 1; x++) {
385 sample->probes[x].val =
387 sample->probes[x].res = 1;
390 /* 1 Analog probe, 16 bit adc */
391 for (; x < NUM_PROBES; x++) {
392 sample->probes[x].val =
393 *(uint16_t *) (c + i * 3 + 1);
394 sample->probes[x].val &= ((1 << 16) - 1);
395 sample->probes[x].res = 16;
400 packet.payload = buf;
401 session_bus(user_data, &packet);
404 packet.type = DF_LOGIC;
408 session_bus(user_data, &packet);
413 if (!thread_running && z <= 0)
415 /* Make sure we don't receive more packets */
416 g_io_channel_close(channels[0]);
418 /* Send last packet. */
419 packet.type = DF_END;
420 session_bus(user_data, &packet);
428 static int hw_start_acquisition(int device_index, gpointer session_device_id)
430 struct datafeed_packet *packet;
431 struct datafeed_header *header;
432 struct databag *mydata;
434 mydata = malloc(sizeof(struct databag));
436 return SIGROK_ERR_MALLOC;
438 mydata->sample_generator = default_genmode;
439 mydata->session_device_id = session_device_id;
440 mydata->device_index = device_index;
441 mydata->samples_counter = 0;
443 if (pipe(mydata->pipe_fds))
446 channels[0] = g_io_channel_unix_new(mydata->pipe_fds[0]);
447 channels[1] = g_io_channel_unix_new(mydata->pipe_fds[1]);
449 /* Set channel encoding to binary (default is UTF-8). */
450 g_io_channel_set_encoding(channels[0], NULL, NULL);
451 g_io_channel_set_encoding(channels[1], NULL, NULL);
453 /* Make channels to unbuffered. */
454 g_io_channel_set_buffered(channels[0], FALSE);
455 g_io_channel_set_buffered(channels[1], FALSE);
457 source_add(mydata->pipe_fds[0], G_IO_IN | G_IO_ERR, 40, receive_data,
460 /* Run the demo thread. */
462 mydata->timer = g_timer_new();
465 g_thread_create((GThreadFunc)thread_func, mydata, TRUE, NULL);
469 packet = malloc(sizeof(struct datafeed_packet));
470 header = malloc(sizeof(struct datafeed_header));
471 if (!packet || !header)
472 return SIGROK_ERR_MALLOC;
474 packet->type = DF_HEADER;
475 packet->length = sizeof(struct datafeed_header);
476 packet->payload = (unsigned char *)header;
478 packet->unitsize = sizeof(struct analog_sample) +
479 (NUM_PROBES * sizeof(struct analog_probe));
481 header->feed_version = 1;
482 gettimeofday(&header->starttime, NULL);
483 header->samplerate = cur_samplerate;
484 header->protocol_id = PROTO_RAW;
485 header->num_logic_probes = NUM_PROBES;
486 header->num_analog_probes = 0;
487 session_bus(session_device_id, packet);
494 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
496 /* Avoid compiler warnings. */
497 device_index = device_index;
498 session_device_id = session_device_id;
500 /* Stop generate thread. */
504 struct device_plugin demo_plugin_info = {
514 hw_set_configuration,
515 hw_start_acquisition,