2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #define NUM_TRIGGER_STAGES 4
31 #define TRIGGER_TYPES "01"
33 /* Software trigger implementation: positive values indicate trigger stage. */
34 #define TRIGGER_FIRED -1
36 #define USB_MODEL_NAME "demodevice"
37 #define USB_VENDOR_NAME "demovendor"
38 #define USB_MODEL_VERSION "1.0"
40 #define GENMODE_RANDOM 1
43 static GThread *my_thread;
44 static int thread_running;
46 static int capabilities[] = {
52 /* Random selection of samplerates this "device" shall support. */
53 static uint64_t supported_samplerates[] = {
63 static struct samplerates samplerates = {
67 supported_samplerates,
72 uint8_t sample_generator;
73 uint8_t thread_running;
74 uint64_t samples_counter;
77 gpointer session_device_id;
80 /* List of struct sigrok_device_instance, maintained by opendev()/closedev(). */
81 static GSList *device_instances = NULL;
83 /* TODO: All of these should go in a device-specific struct. */
84 static uint64_t cur_samplerate = 0;
85 static uint64_t limit_samples = 0;
86 // static uint8_t probe_mask = 0;
87 // static uint8_t trigger_mask[NUM_TRIGGER_STAGES] = { 0 };
88 // static uint8_t trigger_value[NUM_TRIGGER_STAGES] = { 0 };
89 // static uint8_t trigger_buffer[NUM_TRIGGER_STAGES] = { 0 };
91 // static int trigger_stage = TRIGGER_FIRED;
93 static int hw_set_configuration(int device_index, int capability, void *value);
94 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
96 static int hw_init(char *deviceinfo)
98 /* Avoid compiler warning. */
99 deviceinfo = deviceinfo;
101 struct sigrok_device_instance *sdi;
103 sdi = sigrok_device_instance_new(0, ST_ACTIVE,
104 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
108 device_instances = g_slist_append(device_instances, sdi);
113 static int hw_opendev(int device_index)
115 /* Avoid compiler warning. */
116 device_index = device_index;
118 /* Nothing needed so far. */
122 static void hw_closedev(int device_index)
124 /* Avoid compiler warning. */
125 device_index = device_index;
127 /* Nothing needed so far. */
130 static void hw_cleanup(void)
132 /* Nothing needed so far. */
135 static void *hw_get_device_info(int device_index, int device_info_id)
137 struct sigrok_device_instance *sdi;
140 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
143 switch (device_info_id) {
148 info = GINT_TO_POINTER(NUM_PROBES);
153 case DI_TRIGGER_TYPES:
154 info = TRIGGER_TYPES;
156 case DI_CUR_SAMPLERATE:
157 info = &cur_samplerate;
164 static int hw_get_status(int device_index)
166 /* Avoid compiler warning. */
167 device_index = device_index;
169 return 0; /* FIXME */
172 static int *hw_get_capabilities(void)
177 static int hw_set_configuration(int device_index, int capability, void *value)
182 /* Avoid compiler warning. */
183 device_index = device_index;
185 if (capability == HWCAP_SAMPLERATE) {
186 cur_samplerate = *(uint64_t *) value;
188 } else if (capability == HWCAP_PROBECONFIG) {
189 // ret = configure_probes((GSList *) value); FIXME
191 } else if (capability == HWCAP_LIMIT_SAMPLES) {
193 limit_samples = *tmp_u64;
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_RANDOM: /* Random */
211 for (i = 0; i < size; i++)
212 *(buf + i) = (uint8_t)(rand() & 0xff);
214 case GENMODE_INC: /* Simple increment */
215 for (i = 0; i < size; i++)
221 /* Thread function */
222 static void thread_func(void *data)
224 struct databag *mydata = data;
225 uint8_t buf[BUFSIZE];
226 uint64_t nb_to_send = 0;
228 while (thread_running) {
229 nb_to_send = limit_samples - mydata->samples_counter;
231 if (nb_to_send == 0) {
232 close(mydata->pipe_fds[1]);
234 hw_stop_acquisition(mydata->device_index,
235 mydata->session_device_id);
236 } else if (nb_to_send > BUFSIZE) {
237 nb_to_send = BUFSIZE;
240 samples_generator(buf, nb_to_send, data);
241 mydata->samples_counter += nb_to_send;
243 write(mydata->pipe_fds[1], &buf, nb_to_send);
244 g_usleep(mydata->loop_sleep);
248 /* Callback handling data */
249 static int receive_data(int fd, int revents, void *user_data)
251 struct datafeed_packet packet;
252 /* uint16_t samples[1000]; */
256 /* Avoid compiler warnings. */
259 z = read(fd, &c, BUFSIZE);
261 packet.type = DF_LOGIC8;
264 session_bus(user_data, &packet);
269 static int hw_start_acquisition(int device_index, gpointer session_device_id)
271 struct datafeed_packet *packet;
272 struct datafeed_header *header;
274 struct databag *mydata;
276 mydata = malloc(sizeof(struct databag));
278 return SIGROK_ERR_MALLOC;
280 mydata->sample_generator = GENMODE_RANDOM;
281 mydata->session_device_id = session_device_id;
282 mydata->device_index = device_index;
283 mydata->samples_counter = 0;
284 mydata->loop_sleep = 100000;
286 if (pipe(mydata->pipe_fds)) {
287 fprintf(stderr, "Pipe failed.\n");
288 return SIGROK_ERR_MALLOC; /* FIXME */
291 source_add(mydata->pipe_fds[0], G_IO_IN | G_IO_ERR, 40, receive_data,
294 /* Run the demo thread. */
298 g_thread_create((GThreadFunc)thread_func, mydata, TRUE, NULL);
300 fprintf(stderr, "demo: Thread creation failed.\n");
301 return SIGROK_ERR_MALLOC; /* FIXME */
304 packet = malloc(sizeof(struct datafeed_packet));
305 header = malloc(sizeof(struct datafeed_header));
307 if (!packet || !header || !buf)
308 return SIGROK_ERR_MALLOC;
311 memset(buf, 0x55, 2048);
313 packet->type = DF_HEADER;
314 packet->length = sizeof(struct datafeed_header);
315 packet->payload = (unsigned char *)header;
316 header->feed_version = 1;
317 gettimeofday(&header->starttime, NULL);
318 header->samplerate = cur_samplerate;
319 header->protocol_id = PROTO_RAW;
320 header->num_probes = NUM_PROBES;
321 session_bus(session_device_id, packet);
328 /* This stops acquisition on ALL devices, ignoring device_index. */
329 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
331 struct datafeed_packet packet;
334 device_index = device_index;
336 /* Send last packet. */
337 packet.type = DF_END;
338 session_bus(session_device_id, &packet);
341 struct device_plugin demo_plugin_info = {
351 hw_set_configuration,
352 hw_start_acquisition,