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
29 #define pipe(fds) _pipe(fds, 4096, _O_BINARY)
34 #define DEMONAME "Demo device"
35 /* size of chunks to send through the session bus */
44 GIOChannel *channels[2];
48 uint8_t sample_generator;
49 uint8_t thread_running;
50 uint64_t samples_counter;
53 gpointer session_device_id;
57 static int capabilities[] = {
65 static const char *patternmodes[] = {
71 static uint8_t genmode_default[] = {
72 0x4c, 0x92, 0x92, 0x92, 0x64, 0x00, 0x00, 0x00,
73 0x82, 0xfe, 0xfe, 0x82, 0x00, 0x00, 0x00, 0x00,
74 0x7c, 0x82, 0x82, 0x92, 0x74, 0x00, 0x00, 0x00,
75 0xfe, 0x12, 0x12, 0x32, 0xcc, 0x00, 0x00, 0x00,
76 0x7c, 0x82, 0x82, 0x82, 0x7c, 0x00, 0x00, 0x00,
77 0xfe, 0x10, 0x28, 0x44, 0x82, 0x00, 0x00, 0x00,
78 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
79 0xbe, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
82 /* List of struct sigrok_device_instance, maintained by opendev()/closedev(). */
83 static GSList *device_instances = NULL;
84 static uint64_t cur_samplerate = 0;
85 static uint64_t limit_samples = 0;
86 static uint64_t limit_msec = 0;
87 static int default_genmode = GENMODE_DEFAULT;
88 static GThread *my_thread;
89 static int thread_running;
91 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
93 static int hw_init(char *deviceinfo)
95 struct sigrok_device_instance *sdi;
97 /* Avoid compiler warnings. */
98 deviceinfo = deviceinfo;
100 sdi = sigrok_device_instance_new(0, ST_ACTIVE, DEMONAME, NULL, NULL);
104 device_instances = g_slist_append(device_instances, sdi);
109 static int hw_opendev(int device_index)
111 /* Avoid compiler warnings. */
112 device_index = device_index;
114 /* Nothing needed so far. */
118 static void hw_closedev(int device_index)
120 /* Avoid compiler warnings. */
121 device_index = device_index;
123 /* Nothing needed so far. */
126 static void hw_cleanup(void)
128 /* Nothing needed so far. */
131 static void *hw_get_device_info(int device_index, int device_info_id)
133 struct sigrok_device_instance *sdi;
136 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
139 switch (device_info_id) {
144 info = GINT_TO_POINTER(NUM_PROBES);
146 case DI_CUR_SAMPLERATE:
147 info = &cur_samplerate;
149 case DI_PATTERNMODES:
150 info = &patternmodes;
157 static int hw_get_status(int device_index)
159 /* Avoid compiler warnings. */
160 device_index = device_index;
165 static int *hw_get_capabilities(void)
170 static int hw_set_configuration(int device_index, int capability, void *value)
176 /* Avoid compiler warnings. */
177 device_index = device_index;
179 if (capability == HWCAP_PROBECONFIG) {
182 } else if (capability == HWCAP_LIMIT_SAMPLES) {
184 limit_samples = *tmp_u64;
186 } else if (capability == HWCAP_LIMIT_MSEC) {
188 limit_msec = *tmp_u64;
190 } else if (capability == HWCAP_PATTERN_MODE) {
192 if (!strcmp(stropt, "random")) {
193 default_genmode = GENMODE_RANDOM;
195 } else if (!strcmp(stropt, "incremental")) {
196 default_genmode = GENMODE_INC;
208 static void samples_generator(uint8_t *buf, uint64_t size, void *data)
210 struct databag *mydata = data;
213 memset(buf, 0, size);
215 switch (mydata->sample_generator) {
216 case GENMODE_DEFAULT:
218 for (i = 0; i < size; i++) {
219 *(buf + i) = ~(genmode_default[p] >> 1);
224 case GENMODE_RANDOM: /* Random */
225 for (i = 0; i < size; i++)
226 *(buf + i) = (uint8_t)(rand() & 0xff);
228 case GENMODE_INC: /* Simple increment */
229 for (i = 0; i < size; i++)
235 /* Thread function */
236 static void thread_func(void *data)
238 struct databag *mydata = data;
239 uint8_t buf[BUFSIZE];
240 uint64_t nb_to_send = 0;
242 unsigned int msec_elapsed;
244 while (thread_running) {
246 nb_to_send = limit_samples - mydata->samples_counter;
248 nb_to_send = BUFSIZE; /* Continuous mode */
251 msec_elapsed = g_timer_elapsed(mydata->timer, NULL) * 1000;
252 if (msec_elapsed > limit_msec)
256 if (nb_to_send == 0) {
257 close(mydata->pipe_fds[1]);
259 hw_stop_acquisition(mydata->device_index,
260 mydata->session_device_id);
261 } else if (nb_to_send > BUFSIZE) {
262 nb_to_send = BUFSIZE;
265 samples_generator(buf, nb_to_send, data);
266 mydata->samples_counter += nb_to_send;
268 g_io_channel_write_chars(channels[1], (gchar *)&buf,
269 nb_to_send, (gsize *)&bytes_written, NULL);
271 g_usleep(mydata->loop_sleep);
275 /* Callback handling data */
276 static int receive_data(int fd, int revents, void *user_data)
278 struct datafeed_packet packet;
282 /* Avoid compiler warnings. */
286 g_io_channel_read_chars(channels[0], (gchar *)&c, BUFSIZE, &z, NULL);
289 packet.type = DF_LOGIC;
293 session_bus(user_data, &packet);
298 static int hw_start_acquisition(int device_index, gpointer session_device_id)
300 struct datafeed_packet *packet;
301 struct datafeed_header *header;
302 struct databag *mydata;
304 mydata = malloc(sizeof(struct databag));
306 return SIGROK_ERR_MALLOC;
308 mydata->sample_generator = default_genmode;
309 mydata->session_device_id = session_device_id;
310 mydata->device_index = device_index;
311 mydata->samples_counter = 0;
312 mydata->loop_sleep = 100000;
314 if (pipe(mydata->pipe_fds))
317 channels[0] = g_io_channel_unix_new(mydata->pipe_fds[0]);
318 channels[1] = g_io_channel_unix_new(mydata->pipe_fds[1]);
320 /* Set channel encoding to binary (default is UTF-8). */
321 g_io_channel_set_encoding(channels[0], NULL, NULL);
322 g_io_channel_set_encoding(channels[1], NULL, NULL);
324 /* Make channels to unbuffered. */
325 g_io_channel_set_buffered(channels[0], FALSE);
326 g_io_channel_set_buffered(channels[1], FALSE);
328 source_add(mydata->pipe_fds[0], G_IO_IN | G_IO_ERR, 40, receive_data,
331 /* Run the demo thread. */
334 mydata->timer = g_timer_new();
337 g_thread_create((GThreadFunc)thread_func, mydata, TRUE, NULL);
341 packet = malloc(sizeof(struct datafeed_packet));
342 header = malloc(sizeof(struct datafeed_header));
343 if (!packet || !header)
344 return SIGROK_ERR_MALLOC;
346 packet->type = DF_HEADER;
347 packet->length = sizeof(struct datafeed_header);
348 packet->payload = (unsigned char *)header;
349 header->feed_version = 1;
350 gettimeofday(&header->starttime, NULL);
351 header->samplerate = cur_samplerate;
352 header->protocol_id = PROTO_RAW;
353 header->num_logic_probes = NUM_PROBES;
354 header->num_analog_probes = 0;
355 session_bus(session_device_id, packet);
362 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
364 struct datafeed_packet packet;
366 /* Avoid compiler warnings. */
367 device_index = device_index;
369 /* Send last packet. */
370 packet.type = DF_END;
371 session_bus(session_device_id, &packet);
374 struct device_plugin demo_plugin_info = {
384 hw_set_configuration,
385 hw_start_acquisition,