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;
56 static GThread *my_thread;
57 static int thread_running;
59 static int capabilities[] = {
66 static const char *patternmodes[] = {
72 static uint8_t genmode_default[] = {
73 0x4c, 0x92, 0x92, 0x92, 0x64, 0x00, 0x00, 0x00,
74 0x82, 0xfe, 0xfe, 0x82, 0x00, 0x00, 0x00, 0x00,
75 0x7c, 0x82, 0x82, 0x92, 0x74, 0x00, 0x00, 0x00,
76 0xfe, 0x12, 0x12, 0x32, 0xcc, 0x00, 0x00, 0x00,
77 0x7c, 0x82, 0x82, 0x82, 0x7c, 0x00, 0x00, 0x00,
78 0xfe, 0x10, 0x28, 0x44, 0x82, 0x00, 0x00, 0x00,
79 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
80 0xbe, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
83 /* List of struct sigrok_device_instance, maintained by opendev()/closedev(). */
84 static GSList *device_instances = NULL;
85 static uint64_t cur_samplerate = 0;
86 static uint64_t limit_samples = -1;
87 static int default_genmode = GENMODE_DEFAULT;
89 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
91 static int hw_init(char *deviceinfo)
93 struct sigrok_device_instance *sdi;
95 /* Avoid compiler warnings. */
96 deviceinfo = deviceinfo;
98 sdi = sigrok_device_instance_new(0, ST_ACTIVE, DEMONAME, NULL, NULL);
102 device_instances = g_slist_append(device_instances, sdi);
107 static int hw_opendev(int device_index)
109 /* Avoid compiler warnings. */
110 device_index = device_index;
112 /* Nothing needed so far. */
116 static void hw_closedev(int device_index)
118 /* Avoid compiler warnings. */
119 device_index = device_index;
121 /* Nothing needed so far. */
124 static void hw_cleanup(void)
126 /* Nothing needed so far. */
129 static void *hw_get_device_info(int device_index, int device_info_id)
131 struct sigrok_device_instance *sdi;
134 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
137 switch (device_info_id) {
142 info = GINT_TO_POINTER(NUM_PROBES);
144 case DI_CUR_SAMPLERATE:
145 info = &cur_samplerate;
147 case DI_PATTERNMODES:
148 info = &patternmodes;
155 static int hw_get_status(int device_index)
157 /* Avoid compiler warnings. */
158 device_index = device_index;
163 static int *hw_get_capabilities(void)
168 static int hw_set_configuration(int device_index, int capability, void *value)
174 /* Avoid compiler warnings. */
175 device_index = device_index;
177 if (capability == HWCAP_PROBECONFIG) {
180 } else if (capability == HWCAP_LIMIT_SAMPLES) {
182 limit_samples = *tmp_u64;
184 } else if (capability == HWCAP_PATTERN_MODE) {
186 if (!strcmp(stropt, "random")) {
187 default_genmode = GENMODE_RANDOM;
189 } else if (!strcmp(stropt, "incremental")) {
190 default_genmode = GENMODE_INC;
202 static void samples_generator(uint8_t *buf, uint64_t size, void *data)
204 struct databag *mydata = data;
207 memset(buf, 0, size);
209 switch (mydata->sample_generator) {
210 case GENMODE_DEFAULT:
212 for (i = 0; i < size; i++) {
213 *(buf + i) = ~(genmode_default[p] >> 1);
218 case GENMODE_RANDOM: /* Random */
219 for (i = 0; i < size; i++)
220 *(buf + i) = (uint8_t)(rand() & 0xff);
222 case GENMODE_INC: /* Simple increment */
223 for (i = 0; i < size; i++)
229 /* Thread function */
230 static void thread_func(void *data)
232 struct databag *mydata = data;
233 uint8_t buf[BUFSIZE];
234 uint64_t nb_to_send = 0;
237 while (thread_running) {
239 nb_to_send = limit_samples - mydata->samples_counter;
241 nb_to_send = BUFSIZE; /* Continuous mode */
243 if (nb_to_send == 0) {
244 close(mydata->pipe_fds[1]);
246 hw_stop_acquisition(mydata->device_index,
247 mydata->session_device_id);
248 } else if (nb_to_send > BUFSIZE) {
249 nb_to_send = BUFSIZE;
252 samples_generator(buf, nb_to_send, data);
253 mydata->samples_counter += nb_to_send;
255 g_io_channel_write_chars(channels[1], (gchar *)&buf,
256 nb_to_send, (gsize *)&bytes_written, NULL);
258 g_usleep(mydata->loop_sleep);
262 /* Callback handling data */
263 static int receive_data(int fd, int revents, void *user_data)
265 struct datafeed_packet packet;
269 /* Avoid compiler warnings. */
273 g_io_channel_read_chars(channels[0], (gchar *)&c, BUFSIZE, &z, NULL);
276 packet.type = DF_LOGIC;
280 session_bus(user_data, &packet);
285 static int hw_start_acquisition(int device_index, gpointer session_device_id)
287 struct datafeed_packet *packet;
288 struct datafeed_header *header;
289 struct databag *mydata;
291 mydata = malloc(sizeof(struct databag));
293 return SIGROK_ERR_MALLOC;
295 mydata->sample_generator = default_genmode;
296 mydata->session_device_id = session_device_id;
297 mydata->device_index = device_index;
298 mydata->samples_counter = 0;
299 mydata->loop_sleep = 100000;
301 if (pipe(mydata->pipe_fds))
304 channels[0] = g_io_channel_unix_new(mydata->pipe_fds[0]);
305 channels[1] = g_io_channel_unix_new(mydata->pipe_fds[1]);
307 /* Set channel encoding to binary (default is UTF-8). */
308 g_io_channel_set_encoding(channels[0], NULL, NULL);
309 g_io_channel_set_encoding(channels[1], NULL, NULL);
311 /* Make channels to unbuffered. */
312 g_io_channel_set_buffered(channels[0], FALSE);
313 g_io_channel_set_buffered(channels[1], FALSE);
315 source_add(mydata->pipe_fds[0], G_IO_IN | G_IO_ERR, 40, receive_data,
318 /* Run the demo thread. */
322 g_thread_create((GThreadFunc)thread_func, mydata, TRUE, NULL);
326 packet = malloc(sizeof(struct datafeed_packet));
327 header = malloc(sizeof(struct datafeed_header));
328 if (!packet || !header)
329 return SIGROK_ERR_MALLOC;
331 packet->type = DF_HEADER;
332 packet->length = sizeof(struct datafeed_header);
333 packet->payload = (unsigned char *)header;
334 header->feed_version = 1;
335 gettimeofday(&header->starttime, NULL);
336 header->samplerate = cur_samplerate;
337 header->protocol_id = PROTO_RAW;
338 header->num_logic_probes = NUM_PROBES;
339 header->num_analog_probes = 0;
340 session_bus(session_device_id, packet);
347 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
349 struct datafeed_packet packet;
351 /* Avoid compiler warnings. */
352 device_index = device_index;
354 /* Send last packet. */
355 packet.type = DF_END;
356 session_bus(session_device_id, &packet);
359 struct device_plugin demo_plugin_info = {
369 hw_set_configuration,
370 hw_start_acquisition,