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 NUM_TRIGGER_STAGES 4
32 #define TRIGGER_TYPES "01"
34 /* Software trigger implementation: positive values indicate trigger stage. */
35 #define TRIGGER_FIRED -1
37 #define USB_MODEL_NAME "demodevice"
38 #define USB_VENDOR_NAME "sigrok"
39 #define USB_MODEL_VERSION "v1.0"
41 #define GENMODE_RANDOM 1
44 static GThread *my_thread;
45 static int thread_running;
47 static int capabilities[] = {
54 /* Random selection of samplerates this "device" shall support. */
55 static uint64_t supported_samplerates[] = {
65 static struct samplerates samplerates = {
69 supported_samplerates,
74 uint8_t sample_generator;
75 uint8_t thread_running;
76 uint64_t samples_counter;
79 gpointer session_device_id;
82 /* List of struct sigrok_device_instance, maintained by opendev()/closedev(). */
83 static GSList *device_instances = NULL;
85 /* TODO: All of these should go in a device-specific struct. */
86 static uint64_t cur_samplerate = 0;
87 static uint64_t limit_samples = -1;
88 // static uint8_t probe_mask = 0;
89 // static uint8_t trigger_mask[NUM_TRIGGER_STAGES] = { 0 };
90 // static uint8_t trigger_value[NUM_TRIGGER_STAGES] = { 0 };
91 // static uint8_t trigger_buffer[NUM_TRIGGER_STAGES] = { 0 };
93 // static int trigger_stage = TRIGGER_FIRED;
95 static int hw_set_configuration(int device_index, int capability, void *value);
96 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
98 static int hw_init(char *deviceinfo)
100 /* Avoid compiler warnings. */
101 deviceinfo = deviceinfo;
103 struct sigrok_device_instance *sdi;
105 sdi = sigrok_device_instance_new(0, ST_ACTIVE,
106 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
110 device_instances = g_slist_append(device_instances, sdi);
115 static int hw_opendev(int device_index)
117 /* Avoid compiler warnings. */
118 device_index = device_index;
120 /* Nothing needed so far. */
124 static void hw_closedev(int device_index)
126 /* Avoid compiler warnings. */
127 device_index = device_index;
129 /* Nothing needed so far. */
132 static void hw_cleanup(void)
134 /* Nothing needed so far. */
137 static void *hw_get_device_info(int device_index, int device_info_id)
139 struct sigrok_device_instance *sdi;
142 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
145 switch (device_info_id) {
150 info = GINT_TO_POINTER(NUM_PROBES);
155 case DI_TRIGGER_TYPES:
156 info = TRIGGER_TYPES;
158 case DI_CUR_SAMPLERATE:
159 info = &cur_samplerate;
166 static int hw_get_status(int device_index)
168 /* Avoid compiler warnings. */
169 device_index = device_index;
171 return 0; /* FIXME */
174 static int *hw_get_capabilities(void)
179 static int hw_set_configuration(int device_index, int capability, void *value)
184 /* Avoid compiler warnings. */
185 device_index = device_index;
187 if (capability == HWCAP_SAMPLERATE) {
188 cur_samplerate = *(uint64_t *) value;
190 } else if (capability == HWCAP_PROBECONFIG) {
191 // ret = configure_probes((GSList *) value); FIXME
193 } else if (capability == HWCAP_LIMIT_SAMPLES) {
195 limit_samples = *tmp_u64;
204 static void samples_generator(uint8_t *buf, uint64_t size, void *data)
206 struct databag *mydata = data;
209 memset(buf, 0, size);
211 switch (mydata->sample_generator) {
212 case GENMODE_RANDOM: /* Random */
213 for (i = 0; i < size; i++)
214 *(buf + i) = (uint8_t)(rand() & 0xff);
216 case GENMODE_INC: /* Simple increment */
217 for (i = 0; i < size; i++)
223 /* Thread function */
224 static void thread_func(void *data)
226 struct databag *mydata = data;
227 uint8_t buf[BUFSIZE];
228 uint64_t nb_to_send = 0;
230 while (thread_running) {
232 nb_to_send = limit_samples - mydata->samples_counter;
234 nb_to_send = BUFSIZE; // CONTINUOUS MODE
236 if (nb_to_send == 0) {
237 close(mydata->pipe_fds[1]);
239 hw_stop_acquisition(mydata->device_index,
240 mydata->session_device_id);
241 } else if (nb_to_send > BUFSIZE) {
242 nb_to_send = BUFSIZE;
245 samples_generator(buf, nb_to_send, data);
246 mydata->samples_counter += nb_to_send;
248 write(mydata->pipe_fds[1], &buf, nb_to_send);
249 g_usleep(mydata->loop_sleep);
253 /* Callback handling data */
254 static int receive_data(int fd, int revents, void *user_data)
256 struct datafeed_packet packet;
257 /* uint16_t samples[1000]; */
261 /* Avoid compiler warnings. */
264 z = read(fd, &c, BUFSIZE);
266 packet.type = DF_LOGIC;
270 session_bus(user_data, &packet);
275 static int hw_start_acquisition(int device_index, gpointer session_device_id)
277 struct datafeed_packet *packet;
278 struct datafeed_header *header;
280 struct databag *mydata;
282 mydata = malloc(sizeof(struct databag));
284 return SIGROK_ERR_MALLOC;
286 mydata->sample_generator = GENMODE_RANDOM;
287 mydata->session_device_id = session_device_id;
288 mydata->device_index = device_index;
289 mydata->samples_counter = 0;
290 mydata->loop_sleep = 100000;
292 if (pipe(mydata->pipe_fds)) {
293 fprintf(stderr, "Pipe failed.\n");
294 return SIGROK_ERR_MALLOC; /* FIXME */
297 source_add(mydata->pipe_fds[0], G_IO_IN | G_IO_ERR, 40, receive_data,
300 /* Run the demo thread. */
304 g_thread_create((GThreadFunc)thread_func, mydata, TRUE, NULL);
306 fprintf(stderr, "demo: Thread creation failed.\n");
307 return SIGROK_ERR_MALLOC; /* FIXME */
310 packet = malloc(sizeof(struct datafeed_packet));
311 header = malloc(sizeof(struct datafeed_header));
313 if (!packet || !header || !buf)
314 return SIGROK_ERR_MALLOC;
317 memset(buf, 0x55, 2048);
319 packet->type = DF_HEADER;
320 packet->length = sizeof(struct datafeed_header);
321 packet->payload = (unsigned char *)header;
322 header->feed_version = 1;
323 gettimeofday(&header->starttime, NULL);
324 header->samplerate = cur_samplerate;
325 header->protocol_id = PROTO_RAW;
326 header->num_logic_probes = NUM_PROBES;
327 header->num_analog_probes = 0;
328 session_bus(session_device_id, packet);
335 /* This stops acquisition on ALL devices, ignoring device_index. */
336 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
338 struct datafeed_packet packet;
340 /* Avoid compiler warnings. */
341 device_index = device_index;
343 /* Send last packet. */
344 packet.type = DF_END;
345 session_bus(session_device_id, &packet);
348 struct device_plugin demo_plugin_info = {
358 hw_set_configuration,
359 hw_start_acquisition,