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
31 #define pipe(fds) _pipe(fds, 4096, _O_BINARY)
40 #define DEMONAME "Demo device"
41 /* size of chunks to send through the session bus */
54 GIOChannel *channels[2];
58 uint8_t sample_generator;
59 uint8_t thread_running;
60 uint64_t samples_counter;
62 gpointer session_device_id;
66 static int capabilities[] = {
74 static const char *patternmodes[] = {
81 static uint8_t genmode_default[] = {
82 0x4c, 0x92, 0x92, 0x92, 0x64, 0x00, 0x00, 0x00,
83 0x82, 0xfe, 0xfe, 0x82, 0x00, 0x00, 0x00, 0x00,
84 0x7c, 0x82, 0x82, 0x92, 0x74, 0x00, 0x00, 0x00,
85 0xfe, 0x12, 0x12, 0x32, 0xcc, 0x00, 0x00, 0x00,
86 0x7c, 0x82, 0x82, 0x82, 0x7c, 0x00, 0x00, 0x00,
87 0xfe, 0x10, 0x28, 0x44, 0x82, 0x00, 0x00, 0x00,
88 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
89 0xbe, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
93 /* List of struct sigrok_device_instance, maintained by opendev()/closedev(). */
94 static GSList *device_instances = NULL;
95 static uint64_t cur_samplerate = KHZ(200);
96 static uint64_t limit_samples = 0;
97 static uint64_t limit_msec = 0;
98 static int default_genmode = GENMODE_DEFAULT;
99 static GThread *my_thread;
100 static int thread_running;
102 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
104 static int hw_init(char *deviceinfo)
106 struct sigrok_device_instance *sdi;
108 /* Avoid compiler warnings. */
109 deviceinfo = deviceinfo;
111 sdi = sigrok_device_instance_new(0, ST_ACTIVE, DEMONAME, NULL, NULL);
115 device_instances = g_slist_append(device_instances, sdi);
120 static int hw_opendev(int device_index)
122 /* Avoid compiler warnings. */
123 device_index = device_index;
125 /* Nothing needed so far. */
129 static void hw_closedev(int device_index)
131 /* Avoid compiler warnings. */
132 device_index = device_index;
134 /* Nothing needed so far. */
137 static void hw_cleanup(void)
139 /* Nothing needed so far. */
142 static void *hw_get_device_info(int device_index, int device_info_id)
144 struct sigrok_device_instance *sdi;
147 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
150 switch (device_info_id) {
155 info = GINT_TO_POINTER(NUM_PROBES);
157 case DI_CUR_SAMPLERATE:
158 info = &cur_samplerate;
160 case DI_PATTERNMODES:
161 info = &patternmodes;
168 static int hw_get_status(int device_index)
170 /* Avoid compiler warnings. */
171 device_index = device_index;
176 static int *hw_get_capabilities(void)
181 static int hw_set_configuration(int device_index, int capability, void *value)
187 /* Avoid compiler warnings. */
188 device_index = device_index;
190 if (capability == HWCAP_PROBECONFIG) {
193 } else if (capability == HWCAP_LIMIT_SAMPLES) {
195 limit_samples = *tmp_u64;
197 } else if (capability == HWCAP_LIMIT_MSEC) {
199 limit_msec = *tmp_u64;
201 } else if (capability == HWCAP_PATTERN_MODE) {
203 if (!strcmp(stropt, "random")) {
204 default_genmode = GENMODE_RANDOM;
206 } else if (!strcmp(stropt, "incremental")) {
207 default_genmode = GENMODE_INC;
219 static void samples_generator(uint8_t *buf, uint64_t size, void *data)
221 struct databag *mydata = data;
225 * We will simulate a device with 8 logic probes and 1 analog probe.
226 * This fictional device sends the data packed: 8 bits for 8 logic
227 * probes and 16 bits for the analog probe, in this order.
229 * I could just generate a properly formatted DF_ANALOG packet here,
230 * but I will leave the formatting to receive_data() to make its code
231 * more like a real hardware driver.
233 memset(buf, 0, size * 3);
235 switch (mydata->sample_generator) {
237 case GENMODE_DEFAULT:
239 for (i = 0; i < size * 3; i += 3) {
240 *(buf + i) = (uint8_t)(rand() & 0xff);
241 *(uint16_t *) (buf + i + 1) = (uint16_t)(rand() & 0xffff);
245 for (i = 0; i < size * 3; i += 3) {
247 *(uint16_t *)(buf + i + 1) = i / 3 * 256 * 10;
253 memset(buf, 0, size);
255 switch (mydata->sample_generator) {
256 case GENMODE_DEFAULT:
258 for (i = 0; i < size; i++) {
259 *(buf + i) = ~(genmode_default[p] >> 1);
264 case GENMODE_RANDOM: /* Random */
265 for (i = 0; i < size; i++)
266 *(buf + i) = (uint8_t)(rand() & 0xff);
268 case GENMODE_INC: /* Simple increment */
269 for (i = 0; i < size; i++)
276 /* Thread function */
277 static void thread_func(void *data)
279 struct databag *mydata = data;
280 uint8_t buf[BUFSIZE];
281 uint64_t nb_to_send = 0;
284 double time_cur, time_last, time_diff;
286 time_last = g_timer_elapsed(mydata->timer, NULL);
288 while (thread_running) {
290 time_cur = g_timer_elapsed(mydata->timer, NULL);
292 time_diff = time_cur - time_last;
293 time_last = time_cur;
295 nb_to_send = cur_samplerate * time_diff;
298 nb_to_send = MIN(nb_to_send,
299 limit_samples - mydata->samples_counter);
301 /* Make sure we don't overflow. */
303 nb_to_send = MIN(nb_to_send, BUFSIZE / 3);
305 nb_to_send = MIN(nb_to_send, BUFSIZE);
309 samples_generator(buf, nb_to_send, data);
310 mydata->samples_counter += nb_to_send;
312 g_io_channel_write_chars(channels[1], (gchar *) &buf,
313 nb_to_send * 3, (gsize *) &bytes_written, NULL);
315 g_io_channel_write_chars(channels[1], (gchar *) &buf,
316 nb_to_send, (gsize *) &bytes_written, NULL);
320 /* Check if we're done. */
321 if ((limit_msec && time_cur * 1000 > limit_msec) ||
322 (limit_samples && mydata->samples_counter >= limit_samples))
324 close(mydata->pipe_fds[1]);
332 /* Callback handling data */
333 static int receive_data(int fd, int revents, void *user_data)
335 struct datafeed_packet packet;
339 struct analog_sample *sample;
341 int sample_size = sizeof(struct analog_sample) +
342 (NUM_PROBES * sizeof(struct analog_probe));
346 /* Avoid compiler warnings. */
351 g_io_channel_read_chars(channels[0],
352 (gchar *) &c, BUFSIZE, (gsize *) &z, NULL);
356 packet.type = DF_ANALOG;
358 packet.length = (z / 3) * sample_size;
359 packet.unitsize = sample_size;
361 buf = malloc(sample_size * packet.length);
365 /* Craft our packet. */
366 for (i = 0; i < z / 3; i++) {
367 sample = (struct analog_sample *) (buf + (i * sample_size));
368 sample->num_probes = NUM_PROBES;
371 for (x = 0; x < NUM_PROBES - 1; x++) {
372 sample->probes[x].val =
374 sample->probes[x].res = 1;
377 /* 1 Analog probe, 16 bit adc */
378 for (; x < NUM_PROBES; x++) {
379 sample->probes[x].val =
380 *(uint16_t *) (c + i * 3 + 1);
381 sample->probes[x].val &= ((1 << 16) - 1);
382 sample->probes[x].res = 16;
387 packet.payload = buf;
388 session_bus(user_data, &packet);
391 packet.type = DF_LOGIC;
395 session_bus(user_data, &packet);
400 if (!thread_running && z <= 0)
402 /* Make sure we don't receive more packets */
403 g_io_channel_close(channels[0]);
405 /* Send last packet. */
406 packet.type = DF_END;
407 session_bus(user_data, &packet);
415 static int hw_start_acquisition(int device_index, gpointer session_device_id)
417 struct datafeed_packet *packet;
418 struct datafeed_header *header;
419 struct databag *mydata;
421 mydata = malloc(sizeof(struct databag));
423 return SIGROK_ERR_MALLOC;
425 mydata->sample_generator = default_genmode;
426 mydata->session_device_id = session_device_id;
427 mydata->device_index = device_index;
428 mydata->samples_counter = 0;
430 if (pipe(mydata->pipe_fds))
433 channels[0] = g_io_channel_unix_new(mydata->pipe_fds[0]);
434 channels[1] = g_io_channel_unix_new(mydata->pipe_fds[1]);
436 /* Set channel encoding to binary (default is UTF-8). */
437 g_io_channel_set_encoding(channels[0], NULL, NULL);
438 g_io_channel_set_encoding(channels[1], NULL, NULL);
440 /* Make channels to unbuffered. */
441 g_io_channel_set_buffered(channels[0], FALSE);
442 g_io_channel_set_buffered(channels[1], FALSE);
444 source_add(mydata->pipe_fds[0], G_IO_IN | G_IO_ERR, 40, receive_data,
447 /* Run the demo thread. */
449 mydata->timer = g_timer_new();
452 g_thread_create((GThreadFunc)thread_func, mydata, TRUE, NULL);
456 packet = malloc(sizeof(struct datafeed_packet));
457 header = malloc(sizeof(struct datafeed_header));
458 if (!packet || !header)
459 return SIGROK_ERR_MALLOC;
461 packet->type = DF_HEADER;
462 packet->length = sizeof(struct datafeed_header);
463 packet->payload = (unsigned char *)header;
465 packet->unitsize = sizeof(struct analog_sample) +
466 (NUM_PROBES * sizeof(struct analog_probe));
468 header->feed_version = 1;
469 gettimeofday(&header->starttime, NULL);
470 header->samplerate = cur_samplerate;
471 header->protocol_id = PROTO_RAW;
472 header->num_logic_probes = NUM_PROBES;
473 header->num_analog_probes = 0;
474 session_bus(session_device_id, packet);
481 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
483 /* Avoid compiler warnings. */
484 device_index = device_index;
485 session_device_id = session_device_id;
487 /* Stop generate thread. */
491 struct device_plugin demo_plugin_info = {
501 hw_set_configuration,
502 hw_start_acquisition,