]>
Commit | Line | Data |
---|---|---|
6239c175 UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de> | |
fc96e6f8 | 5 | * Copyright (C) 2011 Olivier Fauchon <olivier@aixmarseille.com> |
6239c175 UH |
6 | * |
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. | |
11 | * | |
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. | |
16 | * | |
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 | |
20 | */ | |
21 | ||
22 | #include <stdlib.h> | |
85b5af06 | 23 | #include <unistd.h> |
6239c175 UH |
24 | #include <string.h> |
25 | #include <sigrok.h> | |
26 | #include "config.h" | |
27 | ||
e15f48c2 BV |
28 | #define NUM_PROBES 8 |
29 | #define DEMONAME "Demo device" | |
30 | /* size of chunks to send through the session bus */ | |
31 | #define BUFSIZE 4096 | |
85b5af06 | 32 | |
e15f48c2 | 33 | enum { |
917e0e71 | 34 | GENMODE_DEFAULT, |
e15f48c2 BV |
35 | GENMODE_RANDOM, |
36 | GENMODE_INC, | |
37 | }; | |
85b5af06 | 38 | |
e15f48c2 BV |
39 | struct databag { |
40 | int pipe_fds[2]; | |
41 | uint8_t sample_generator; | |
42 | uint8_t thread_running; | |
43 | uint64_t samples_counter; | |
44 | int device_index; | |
45 | int loop_sleep; | |
46 | gpointer session_device_id; | |
47 | }; | |
85b5af06 UH |
48 | |
49 | static GThread *my_thread; | |
50 | static int thread_running; | |
51 | ||
6239c175 UH |
52 | static int capabilities[] = { |
53 | HWCAP_LOGIC_ANALYZER, | |
e15f48c2 | 54 | HWCAP_PATTERN_MODE, |
6239c175 | 55 | HWCAP_LIMIT_SAMPLES, |
ba3d481b | 56 | HWCAP_CONTINUOUS |
6239c175 UH |
57 | }; |
58 | ||
02440dd8 | 59 | static const char *patternmodes[] = { |
e15f48c2 BV |
60 | "random", |
61 | "incremental", | |
02440dd8 | 62 | NULL, |
85b5af06 UH |
63 | }; |
64 | ||
917e0e71 BV |
65 | static uint8_t genmode_default[] = { |
66 | 0x4c, 0x92, 0x92, 0x92, 0x64, 0x00, 0x00, 0x00, | |
67 | 0x82, 0xfe, 0xfe, 0x82, 0x00, 0x00, 0x00, 0x00, | |
68 | 0x7c, 0x82, 0x82, 0x92, 0x74, 0x00, 0x00, 0x00, | |
69 | 0xfe, 0x12, 0x12, 0x32, 0xcc, 0x00, 0x00, 0x00, | |
70 | 0x7c, 0x82, 0x82, 0x82, 0x7c, 0x00, 0x00, 0x00, | |
71 | 0xfe, 0x10, 0x28, 0x44, 0x82, 0x00, 0x00, 0x00, | |
72 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
73 | 0xbe, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
74 | }; | |
75 | ||
85b5af06 UH |
76 | /* List of struct sigrok_device_instance, maintained by opendev()/closedev(). */ |
77 | static GSList *device_instances = NULL; | |
6239c175 | 78 | static uint64_t cur_samplerate = 0; |
ba3d481b | 79 | static uint64_t limit_samples = -1; |
917e0e71 | 80 | static int default_genmode = GENMODE_DEFAULT; |
6239c175 | 81 | |
6239c175 UH |
82 | static void hw_stop_acquisition(int device_index, gpointer session_device_id); |
83 | ||
6239c175 UH |
84 | static int hw_init(char *deviceinfo) |
85 | { | |
85b5af06 UH |
86 | struct sigrok_device_instance *sdi; |
87 | ||
e15f48c2 BV |
88 | /* Avoid compiler warnings. */ |
89 | deviceinfo = deviceinfo; | |
85b5af06 | 90 | |
e15f48c2 | 91 | sdi = sigrok_device_instance_new(0, ST_ACTIVE, DEMONAME, NULL, NULL); |
85b5af06 UH |
92 | if (!sdi) |
93 | return 0; | |
e15f48c2 | 94 | |
85b5af06 UH |
95 | device_instances = g_slist_append(device_instances, sdi); |
96 | ||
97 | return 1; | |
6239c175 UH |
98 | } |
99 | ||
100 | static int hw_opendev(int device_index) | |
101 | { | |
17e1afcb | 102 | /* Avoid compiler warnings. */ |
6239c175 UH |
103 | device_index = device_index; |
104 | ||
105 | /* Nothing needed so far. */ | |
106 | return SIGROK_OK; | |
107 | } | |
108 | ||
109 | static void hw_closedev(int device_index) | |
110 | { | |
17e1afcb | 111 | /* Avoid compiler warnings. */ |
6239c175 UH |
112 | device_index = device_index; |
113 | ||
114 | /* Nothing needed so far. */ | |
115 | } | |
116 | ||
117 | static void hw_cleanup(void) | |
118 | { | |
119 | /* Nothing needed so far. */ | |
120 | } | |
121 | ||
122 | static void *hw_get_device_info(int device_index, int device_info_id) | |
123 | { | |
85b5af06 UH |
124 | struct sigrok_device_instance *sdi; |
125 | void *info = NULL; | |
126 | ||
127 | if (!(sdi = get_sigrok_device_instance(device_instances, device_index))) | |
128 | return NULL; | |
129 | ||
6239c175 UH |
130 | switch (device_info_id) { |
131 | case DI_INSTANCE: | |
85b5af06 | 132 | info = sdi; |
6239c175 UH |
133 | break; |
134 | case DI_NUM_PROBES: | |
135 | info = GINT_TO_POINTER(NUM_PROBES); | |
136 | break; | |
6239c175 UH |
137 | case DI_CUR_SAMPLERATE: |
138 | info = &cur_samplerate; | |
139 | break; | |
e15f48c2 BV |
140 | case DI_PATTERNMODES: |
141 | info = &patternmodes; | |
142 | break; | |
6239c175 UH |
143 | } |
144 | ||
145 | return info; | |
146 | } | |
147 | ||
148 | static int hw_get_status(int device_index) | |
149 | { | |
17e1afcb | 150 | /* Avoid compiler warnings. */ |
6239c175 | 151 | device_index = device_index; |
5096c6a6 | 152 | |
e15f48c2 | 153 | return ST_ACTIVE; |
6239c175 UH |
154 | } |
155 | ||
156 | static int *hw_get_capabilities(void) | |
157 | { | |
158 | return capabilities; | |
159 | } | |
160 | ||
161 | static int hw_set_configuration(int device_index, int capability, void *value) | |
162 | { | |
163 | int ret; | |
85b5af06 | 164 | uint64_t *tmp_u64; |
e15f48c2 | 165 | char *stropt; |
6239c175 | 166 | |
17e1afcb | 167 | /* Avoid compiler warnings. */ |
6239c175 UH |
168 | device_index = device_index; |
169 | ||
e15f48c2 | 170 | if (capability == HWCAP_PROBECONFIG) { |
02440dd8 | 171 | /* Nothing to do. */ |
85b5af06 | 172 | ret = SIGROK_OK; |
6239c175 | 173 | } else if (capability == HWCAP_LIMIT_SAMPLES) { |
85b5af06 UH |
174 | tmp_u64 = value; |
175 | limit_samples = *tmp_u64; | |
6239c175 | 176 | ret = SIGROK_OK; |
e15f48c2 BV |
177 | } else if (capability == HWCAP_PATTERN_MODE) { |
178 | stropt = value; | |
179 | if (!strcmp(stropt, "random")) { | |
180 | default_genmode = GENMODE_RANDOM; | |
181 | ret = SIGROK_OK; | |
182 | } else if (!strcmp(stropt, "incremental")) { | |
183 | default_genmode = GENMODE_INC; | |
184 | ret = SIGROK_OK; | |
185 | } else { | |
186 | ret = SIGROK_ERR; | |
187 | } | |
6239c175 UH |
188 | } else { |
189 | ret = SIGROK_ERR; | |
190 | } | |
191 | ||
192 | return ret; | |
193 | } | |
194 | ||
5096c6a6 | 195 | static void samples_generator(uint8_t *buf, uint64_t size, void *data) |
85b5af06 UH |
196 | { |
197 | struct databag *mydata = data; | |
917e0e71 | 198 | uint64_t p, i; |
85b5af06 | 199 | |
5096c6a6 | 200 | memset(buf, 0, size); |
85b5af06 UH |
201 | |
202 | switch (mydata->sample_generator) { | |
917e0e71 BV |
203 | case GENMODE_DEFAULT: |
204 | p = 0; | |
205 | for (i = 0; i < size; i++) { | |
206 | *(buf + i) = ~(genmode_default[p] >> 1); | |
207 | if (++p == 64) | |
208 | p = 0; | |
209 | } | |
210 | break; | |
85b5af06 | 211 | case GENMODE_RANDOM: /* Random */ |
5096c6a6 UH |
212 | for (i = 0; i < size; i++) |
213 | *(buf + i) = (uint8_t)(rand() & 0xff); | |
85b5af06 UH |
214 | break; |
215 | case GENMODE_INC: /* Simple increment */ | |
5096c6a6 | 216 | for (i = 0; i < size; i++) |
85b5af06 UH |
217 | *(buf + i) = i; |
218 | break; | |
219 | } | |
220 | } | |
221 | ||
222 | /* Thread function */ | |
223 | static void thread_func(void *data) | |
224 | { | |
225 | struct databag *mydata = data; | |
226 | uint8_t buf[BUFSIZE]; | |
227 | uint64_t nb_to_send = 0; | |
228 | ||
229 | while (thread_running) { | |
ba3d481b OF |
230 | if (limit_samples) |
231 | nb_to_send = limit_samples - mydata->samples_counter; | |
232 | else | |
02440dd8 | 233 | nb_to_send = BUFSIZE; /* Continuous mode */ |
85b5af06 UH |
234 | |
235 | if (nb_to_send == 0) { | |
236 | close(mydata->pipe_fds[1]); | |
237 | thread_running = 0; | |
238 | hw_stop_acquisition(mydata->device_index, | |
239 | mydata->session_device_id); | |
240 | } else if (nb_to_send > BUFSIZE) { | |
241 | nb_to_send = BUFSIZE; | |
242 | } | |
243 | ||
244 | samples_generator(buf, nb_to_send, data); | |
245 | mydata->samples_counter += nb_to_send; | |
246 | ||
247 | write(mydata->pipe_fds[1], &buf, nb_to_send); | |
248 | g_usleep(mydata->loop_sleep); | |
249 | } | |
250 | } | |
251 | ||
252 | /* Callback handling data */ | |
253 | static int receive_data(int fd, int revents, void *user_data) | |
254 | { | |
255 | struct datafeed_packet packet; | |
85b5af06 UH |
256 | char c[BUFSIZE]; |
257 | uint64_t z; | |
258 | ||
259 | /* Avoid compiler warnings. */ | |
260 | revents = revents; | |
261 | ||
262 | z = read(fd, &c, BUFSIZE); | |
263 | if (z > 0) { | |
4c046c6b | 264 | packet.type = DF_LOGIC; |
85b5af06 | 265 | packet.length = z; |
4c046c6b | 266 | packet.unitsize = 1; |
85b5af06 UH |
267 | packet.payload = c; |
268 | session_bus(user_data, &packet); | |
269 | } | |
270 | return TRUE; | |
271 | } | |
272 | ||
6239c175 UH |
273 | static int hw_start_acquisition(int device_index, gpointer session_device_id) |
274 | { | |
275 | struct datafeed_packet *packet; | |
276 | struct datafeed_header *header; | |
85b5af06 | 277 | struct databag *mydata; |
6239c175 | 278 | |
85b5af06 | 279 | mydata = malloc(sizeof(struct databag)); |
5096c6a6 UH |
280 | if (!mydata) |
281 | return SIGROK_ERR_MALLOC; | |
85b5af06 | 282 | |
e15f48c2 | 283 | mydata->sample_generator = default_genmode; |
85b5af06 UH |
284 | mydata->session_device_id = session_device_id; |
285 | mydata->device_index = device_index; | |
286 | mydata->samples_counter = 0; | |
287 | mydata->loop_sleep = 100000; | |
288 | ||
e15f48c2 BV |
289 | if (pipe(mydata->pipe_fds)) |
290 | return SIGROK_ERR; | |
85b5af06 | 291 | |
85b5af06 UH |
292 | source_add(mydata->pipe_fds[0], G_IO_IN | G_IO_ERR, 40, receive_data, |
293 | session_device_id); | |
294 | ||
295 | /* Run the demo thread. */ | |
296 | g_thread_init(NULL); | |
297 | thread_running = 1; | |
298 | my_thread = | |
299 | g_thread_create((GThreadFunc)thread_func, mydata, TRUE, NULL); | |
e15f48c2 BV |
300 | if (!my_thread) |
301 | return SIGROK_ERR; | |
6239c175 UH |
302 | |
303 | packet = malloc(sizeof(struct datafeed_packet)); | |
304 | header = malloc(sizeof(struct datafeed_header)); | |
e15f48c2 | 305 | if (!packet || !header) |
6239c175 UH |
306 | return SIGROK_ERR_MALLOC; |
307 | ||
6239c175 UH |
308 | packet->type = DF_HEADER; |
309 | packet->length = sizeof(struct datafeed_header); | |
310 | packet->payload = (unsigned char *)header; | |
311 | header->feed_version = 1; | |
312 | gettimeofday(&header->starttime, NULL); | |
313 | header->samplerate = cur_samplerate; | |
314 | header->protocol_id = PROTO_RAW; | |
c2616fb9 DR |
315 | header->num_logic_probes = NUM_PROBES; |
316 | header->num_analog_probes = 0; | |
6239c175 UH |
317 | session_bus(session_device_id, packet); |
318 | free(header); | |
319 | free(packet); | |
320 | ||
321 | return SIGROK_OK; | |
322 | } | |
323 | ||
6239c175 UH |
324 | static void hw_stop_acquisition(int device_index, gpointer session_device_id) |
325 | { | |
326 | struct datafeed_packet packet; | |
327 | ||
17e1afcb | 328 | /* Avoid compiler warnings. */ |
6239c175 UH |
329 | device_index = device_index; |
330 | ||
85b5af06 | 331 | /* Send last packet. */ |
6239c175 UH |
332 | packet.type = DF_END; |
333 | session_bus(session_device_id, &packet); | |
6239c175 UH |
334 | } |
335 | ||
336 | struct device_plugin demo_plugin_info = { | |
337 | "demo", | |
338 | 1, | |
339 | hw_init, | |
340 | hw_cleanup, | |
341 | hw_opendev, | |
342 | hw_closedev, | |
343 | hw_get_device_info, | |
344 | hw_get_status, | |
345 | hw_get_capabilities, | |
346 | hw_set_configuration, | |
347 | hw_start_acquisition, | |
348 | hw_stop_acquisition, | |
349 | }; |