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 "demovendor"
39 #define USB_MODEL_VERSION "1.0"
41 #define GENMODE_RANDOM 1
44 static GThread *my_thread;
45 static int thread_running;
47 static int capabilities[] = {
53 /* Random selection of samplerates this "device" shall support. */
54 static uint64_t supported_samplerates[] = {
64 static struct samplerates samplerates = {
68 supported_samplerates,
73 uint8_t sample_generator;
74 uint8_t thread_running;
75 uint64_t samples_counter;
78 gpointer session_device_id;
81 /* List of struct sigrok_device_instance, maintained by opendev()/closedev(). */
82 static GSList *device_instances = NULL;
84 /* TODO: All of these should go in a device-specific struct. */
85 static uint64_t cur_samplerate = 0;
86 static uint64_t limit_samples = 0;
87 // static uint8_t probe_mask = 0;
88 // static uint8_t trigger_mask[NUM_TRIGGER_STAGES] = { 0 };
89 // static uint8_t trigger_value[NUM_TRIGGER_STAGES] = { 0 };
90 // static uint8_t trigger_buffer[NUM_TRIGGER_STAGES] = { 0 };
92 // static int trigger_stage = TRIGGER_FIRED;
94 static int hw_set_configuration(int device_index, int capability, void *value);
95 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
97 static int hw_init(char *deviceinfo)
99 /* Avoid compiler warning. */
100 deviceinfo = deviceinfo;
102 struct sigrok_device_instance *sdi;
104 sdi = sigrok_device_instance_new(0, ST_ACTIVE,
105 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
109 device_instances = g_slist_append(device_instances, sdi);
114 static int hw_opendev(int device_index)
116 /* Avoid compiler warning. */
117 device_index = device_index;
119 /* Nothing needed so far. */
123 static void hw_closedev(int device_index)
125 /* Avoid compiler warning. */
126 device_index = device_index;
128 /* Nothing needed so far. */
131 static void hw_cleanup(void)
133 /* Nothing needed so far. */
136 static void *hw_get_device_info(int device_index, int device_info_id)
138 struct sigrok_device_instance *sdi;
141 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
144 switch (device_info_id) {
149 info = GINT_TO_POINTER(NUM_PROBES);
154 case DI_TRIGGER_TYPES:
155 info = TRIGGER_TYPES;
157 case DI_CUR_SAMPLERATE:
158 info = &cur_samplerate;
165 static int hw_get_status(int device_index)
167 /* Avoid compiler warning. */
168 device_index = device_index;
170 return 0; /* FIXME */
173 static int *hw_get_capabilities(void)
178 static int hw_set_configuration(int device_index, int capability, void *value)
183 /* Avoid compiler warning. */
184 device_index = device_index;
186 if (capability == HWCAP_SAMPLERATE) {
187 cur_samplerate = *(uint64_t *) value;
189 } else if (capability == HWCAP_PROBECONFIG) {
190 // ret = configure_probes((GSList *) value); FIXME
192 } else if (capability == HWCAP_LIMIT_SAMPLES) {
194 limit_samples = *tmp_u64;
203 static void samples_generator(uint8_t *buf, uint64_t size, void *data)
205 struct databag *mydata = data;
208 memset(buf, 0, size);
210 switch (mydata->sample_generator) {
211 case GENMODE_RANDOM: /* Random */
212 for (i = 0; i < size; i++)
213 *(buf + i) = (uint8_t)(rand() & 0xff);
215 case GENMODE_INC: /* Simple increment */
216 for (i = 0; i < size; i++)
222 /* Thread function */
223 static void thread_func(void *data)
225 struct databag *mydata = data;
226 uint8_t buf[BUFSIZE];
227 uint64_t nb_to_send = 0;
229 while (thread_running) {
230 nb_to_send = limit_samples - mydata->samples_counter;
232 if (nb_to_send == 0) {
233 close(mydata->pipe_fds[1]);
235 hw_stop_acquisition(mydata->device_index,
236 mydata->session_device_id);
237 } else if (nb_to_send > BUFSIZE) {
238 nb_to_send = BUFSIZE;
241 samples_generator(buf, nb_to_send, data);
242 mydata->samples_counter += nb_to_send;
244 write(mydata->pipe_fds[1], &buf, nb_to_send);
245 g_usleep(mydata->loop_sleep);
249 /* Callback handling data */
250 static int receive_data(int fd, int revents, void *user_data)
252 struct datafeed_packet packet;
253 /* uint16_t samples[1000]; */
257 /* Avoid compiler warnings. */
260 z = read(fd, &c, BUFSIZE);
262 packet.type = DF_LOGIC;
266 session_bus(user_data, &packet);
271 static int hw_start_acquisition(int device_index, gpointer session_device_id)
273 struct datafeed_packet *packet;
274 struct datafeed_header *header;
276 struct databag *mydata;
278 mydata = malloc(sizeof(struct databag));
280 return SIGROK_ERR_MALLOC;
282 mydata->sample_generator = GENMODE_RANDOM;
283 mydata->session_device_id = session_device_id;
284 mydata->device_index = device_index;
285 mydata->samples_counter = 0;
286 mydata->loop_sleep = 100000;
288 if (pipe(mydata->pipe_fds)) {
289 fprintf(stderr, "Pipe failed.\n");
290 return SIGROK_ERR_MALLOC; /* FIXME */
293 source_add(mydata->pipe_fds[0], G_IO_IN | G_IO_ERR, 40, receive_data,
296 /* Run the demo thread. */
300 g_thread_create((GThreadFunc)thread_func, mydata, TRUE, NULL);
302 fprintf(stderr, "demo: Thread creation failed.\n");
303 return SIGROK_ERR_MALLOC; /* FIXME */
306 packet = malloc(sizeof(struct datafeed_packet));
307 header = malloc(sizeof(struct datafeed_header));
309 if (!packet || !header || !buf)
310 return SIGROK_ERR_MALLOC;
313 memset(buf, 0x55, 2048);
315 packet->type = DF_HEADER;
316 packet->length = sizeof(struct datafeed_header);
317 packet->payload = (unsigned char *)header;
318 header->feed_version = 1;
319 gettimeofday(&header->starttime, NULL);
320 header->samplerate = cur_samplerate;
321 header->protocol_id = PROTO_RAW;
322 header->num_logic_probes = NUM_PROBES;
323 header->num_analog_probes = 0;
324 session_bus(session_device_id, packet);
331 /* This stops acquisition on ALL devices, ignoring device_index. */
332 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
334 struct datafeed_packet packet;
337 device_index = device_index;
339 /* Send last packet. */
340 packet.type = DF_END;
341 session_bus(session_device_id, &packet);
344 struct device_plugin demo_plugin_info = {
354 hw_set_configuration,
355 hw_start_acquisition,