]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2010 Bert Vermeulen <bert@biot.com> | |
5 | * | |
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 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
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. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <stdio.h> | |
21 | #include <stdint.h> | |
22 | #include <stdlib.h> | |
23 | #include <sys/types.h> | |
24 | #include <sys/stat.h> | |
25 | #include <fcntl.h> | |
26 | #include <unistd.h> | |
27 | #include <termios.h> | |
28 | #include <string.h> | |
29 | #include <sys/time.h> | |
30 | #include <inttypes.h> | |
31 | #include <arpa/inet.h> | |
32 | #include <glib.h> | |
33 | #include <sigrok.h> | |
34 | ||
35 | #define NUM_PROBES 32 | |
36 | #define NUM_TRIGGER_STAGES 4 | |
37 | #define TRIGGER_TYPES "01" | |
38 | #define SERIAL_SPEED B115200 | |
39 | /* TODO: SERIAL_ bits, parity, stop bit */ | |
40 | #define CLOCK_RATE 100000000 | |
41 | ||
42 | /* Command opcodes */ | |
43 | #define CMD_RESET 0x00 | |
44 | #define CMD_ID 0x02 | |
45 | #define CMD_SET_FLAGS 0x82 | |
46 | #define CMD_SET_DIVIDER 0x80 | |
47 | #define CMD_RUN 0x01 | |
48 | #define CMD_CAPTURE_SIZE 0x81 | |
49 | #define CMD_SET_TRIGGER_MASK_0 0xc0 | |
50 | #define CMD_SET_TRIGGER_MASK_1 0xc4 | |
51 | #define CMD_SET_TRIGGER_MASK_2 0xc8 | |
52 | #define CMD_SET_TRIGGER_MASK_3 0xcc | |
53 | #define CMD_SET_TRIGGER_VALUE_0 0xc1 | |
54 | #define CMD_SET_TRIGGER_VALUE_1 0xc5 | |
55 | #define CMD_SET_TRIGGER_VALUE_2 0xc9 | |
56 | #define CMD_SET_TRIGGER_VALUE_3 0xcd | |
57 | #define CMD_SET_TRIGGER_CONFIG_0 0xc2 | |
58 | #define CMD_SET_TRIGGER_CONFIG_1 0xc6 | |
59 | #define CMD_SET_TRIGGER_CONFIG_2 0xca | |
60 | #define CMD_SET_TRIGGER_CONFIG_3 0xce | |
61 | ||
62 | /* Bitmasks for CMD_FLAGS */ | |
63 | #define FLAG_DEMUX 0x01 | |
64 | #define FLAG_FILTER 0x02 | |
65 | #define FLAG_CHANNELGROUP_1 0x04 | |
66 | #define FLAG_CHANNELGROUP_2 0x08 | |
67 | #define FLAG_CHANNELGROUP_3 0x10 | |
68 | #define FLAG_CHANNELGROUP_4 0x20 | |
69 | #define FLAG_CLOCK_EXTERNAL 0x40 | |
70 | #define FLAG_CLOCK_INVERTED 0x80 | |
71 | #define FLAG_RLE 0x0100 | |
72 | ||
73 | static int capabilities[] = { | |
74 | HWCAP_LOGIC_ANALYZER, | |
75 | HWCAP_SAMPLERATE, | |
76 | HWCAP_CAPTURE_RATIO, | |
77 | HWCAP_LIMIT_SAMPLES, | |
78 | 0, | |
79 | }; | |
80 | ||
81 | static struct samplerates samplerates = { | |
82 | 10, | |
83 | MHZ(200), | |
84 | 1, | |
85 | 0, | |
86 | }; | |
87 | ||
88 | /* List of struct serial_device_instance */ | |
89 | static GSList *device_instances = NULL; | |
90 | ||
91 | /* Current state of the flag register */ | |
92 | static uint32_t flag_reg = 0; | |
93 | ||
94 | static uint64_t cur_samplerate = 0; | |
95 | static uint64_t limit_samples = 0; | |
96 | /* | |
97 | * Pre/post trigger capture ratio, in percentage. | |
98 | * 0 means no pre-trigger data. | |
99 | */ | |
100 | static int capture_ratio = 0; | |
101 | static uint32_t probe_mask = 0xffffffff; | |
102 | static uint32_t trigger_mask[4] = { 0, 0, 0, 0 }; | |
103 | static uint32_t trigger_value[4] = { 0, 0, 0, 0 }; | |
104 | ||
105 | static int send_shortcommand(int fd, uint8_t command) | |
106 | { | |
107 | char buf[1]; | |
108 | ||
109 | g_message("ols: sending cmd 0x%.2x", command); | |
110 | buf[0] = command; | |
111 | if (write(fd, buf, 1) != 1) | |
112 | return SIGROK_ERR; | |
113 | ||
114 | return SIGROK_OK; | |
115 | } | |
116 | ||
117 | static int send_longcommand(int fd, uint8_t command, uint32_t data) | |
118 | { | |
119 | char buf[5]; | |
120 | ||
121 | g_message("ols: sending cmd 0x%.2x data 0x%.8x", command, data); | |
122 | buf[0] = command; | |
123 | buf[1] = (data & 0xff000000) >> 24; | |
124 | buf[2] = (data & 0xff0000) >> 16; | |
125 | buf[3] = (data & 0xff00) >> 8; | |
126 | buf[4] = data & 0xff; | |
127 | if (write(fd, buf, 5) != 5) | |
128 | return SIGROK_ERR; | |
129 | ||
130 | return SIGROK_OK; | |
131 | } | |
132 | ||
133 | static int configure_probes(GSList *probes) | |
134 | { | |
135 | struct probe *probe; | |
136 | GSList *l; | |
137 | int probe_bit, stage, i; | |
138 | char *tc; | |
139 | ||
140 | probe_mask = 0; | |
141 | for (i = 0; i < NUM_TRIGGER_STAGES; i++) { | |
142 | trigger_mask[i] = 0; | |
143 | trigger_value[i] = 0; | |
144 | } | |
145 | ||
146 | for (l = probes; l; l = l->next) { | |
147 | probe = (struct probe *)l->data; | |
148 | if (!probe->enabled) | |
149 | continue; | |
150 | ||
151 | /* | |
152 | * Set up the probe mask for later configuration into the | |
153 | * flag register. | |
154 | */ | |
155 | probe_bit = 1 << (probe->index - 1); | |
156 | probe_mask |= probe_bit; | |
157 | ||
158 | if (probe->trigger) | |
159 | continue; | |
160 | ||
161 | /* Configure trigger mask and value. */ | |
162 | stage = 0; | |
163 | for (tc = probe->trigger; tc && *tc; tc++) { | |
164 | trigger_mask[stage] |= probe_bit; | |
165 | if (*tc == '1') | |
166 | trigger_value[stage] |= probe_bit; | |
167 | stage++; | |
168 | if (stage > 3) | |
169 | /* | |
170 | * TODO: Only supporting parallel mode, with | |
171 | * up to 4 stages. | |
172 | */ | |
173 | return SIGROK_ERR; | |
174 | } | |
175 | } | |
176 | ||
177 | return SIGROK_OK; | |
178 | } | |
179 | ||
180 | static void byteswap(uint32_t * in) | |
181 | { | |
182 | uint32_t out; | |
183 | ||
184 | out = (*in & 0xff) << 8; | |
185 | out |= (*in & 0xff00) >> 8; | |
186 | out |= (*in & 0xff0000) << 8; | |
187 | out |= (*in & 0xff000000) >> 8; | |
188 | *in = out; | |
189 | } | |
190 | ||
191 | static int hw_init(char *deviceinfo) | |
192 | { | |
193 | struct sigrok_device_instance *sdi; | |
194 | GSList *ports, *l; | |
195 | GPollFD *fds; | |
196 | int devcnt, final_devcnt, num_ports, fd, ret, i; | |
197 | char buf[8], **device_names, **serial_params; | |
198 | ||
199 | if (deviceinfo) | |
200 | ports = g_slist_append(NULL, strdup(deviceinfo)); | |
201 | else | |
202 | /* No specific device given, so scan all serial ports. */ | |
203 | ports = list_serial_ports(); | |
204 | ||
205 | num_ports = g_slist_length(ports); | |
206 | fds = calloc(1, num_ports * sizeof(GPollFD)); | |
207 | device_names = malloc(num_ports * sizeof(char *)); | |
208 | serial_params = malloc(num_ports * sizeof(char *)); | |
209 | devcnt = 0; | |
210 | for (l = ports; l; l = l->next) { | |
211 | /* The discovery procedure is like this: first send the Reset | |
212 | * command (0x00) 5 times, since the device could be anywhere | |
213 | * in a 5-byte command. Then send the ID command (0x02). | |
214 | * If the device responds with 4 bytes ("OLS1" or "SLA1"), we | |
215 | * have a match. | |
216 | * | |
217 | * Since it may take the device a while to respond at 115Kb/s, | |
218 | * we do all the sending first, then wait for all of them to | |
219 | * respond with g_poll(). | |
220 | */ | |
221 | g_message("probing %s...", (char *)l->data); | |
222 | fd = serial_open(l->data, O_RDWR | O_NONBLOCK); | |
223 | if (fd != -1) { | |
224 | serial_params[devcnt] = serial_backup_params(fd); | |
225 | serial_set_params(fd, 115200, 8, 0, 1, 2); | |
226 | ret = SIGROK_OK; | |
227 | for (i = 0; i < 5; i++) { | |
228 | if ((ret = send_shortcommand(fd, | |
229 | CMD_RESET)) != SIGROK_OK) { | |
230 | /* Serial port is not writable. */ | |
231 | break; | |
232 | } | |
233 | } | |
234 | if (ret != SIGROK_OK) { | |
235 | serial_restore_params(fd, | |
236 | serial_params[devcnt]); | |
237 | serial_close(fd); | |
238 | continue; | |
239 | } | |
240 | send_shortcommand(fd, CMD_ID); | |
241 | fds[devcnt].fd = fd; | |
242 | fds[devcnt].events = G_IO_IN; | |
243 | device_names[devcnt] = strdup(l->data); | |
244 | devcnt++; | |
245 | } | |
246 | free(l->data); | |
247 | } | |
248 | ||
249 | /* 2ms should do (enough time for 28 bytes to go over the bus). */ | |
250 | usleep(2000); | |
251 | ||
252 | final_devcnt = 0; | |
253 | g_poll(fds, devcnt, 1); | |
254 | for (i = 0; i < devcnt; i++) { | |
255 | if (fds[i].revents == G_IO_IN) { | |
256 | if (read(fds[i].fd, buf, 4) == 4) { | |
257 | if (!strncmp(buf, "1SLO", 4) | |
258 | || !strncmp(buf, "1ALS", 4)) { | |
259 | if (!strncmp(buf, "1SLO", 4)) | |
260 | sdi = sigrok_device_instance_new | |
261 | (final_devcnt, ST_INACTIVE, | |
262 | "Openbench", | |
263 | "Logic Sniffer", "v1.0"); | |
264 | else | |
265 | sdi = sigrok_device_instance_new | |
266 | (final_devcnt, ST_INACTIVE, | |
267 | "Sump", "Logic Analyzer", | |
268 | "v1.0"); | |
269 | sdi->serial = serial_device_instance_new | |
270 | (device_names[i], -1); | |
271 | device_instances = | |
272 | g_slist_append(device_instances, sdi); | |
273 | final_devcnt++; | |
274 | serial_close(fds[i].fd); | |
275 | fds[i].fd = 0; | |
276 | } | |
277 | } | |
278 | free(device_names[i]); | |
279 | } | |
280 | ||
281 | if (fds[i].fd != 0) { | |
282 | serial_restore_params(fds[i].fd, serial_params[i]); | |
283 | serial_close(fds[i].fd); | |
284 | } | |
285 | free(serial_params[i]); | |
286 | } | |
287 | ||
288 | free(fds); | |
289 | free(device_names); | |
290 | free(serial_params); | |
291 | g_slist_free(ports); | |
292 | ||
293 | cur_samplerate = samplerates.low; | |
294 | ||
295 | return final_devcnt; | |
296 | } | |
297 | ||
298 | static int hw_opendev(int device_index) | |
299 | { | |
300 | struct sigrok_device_instance *sdi; | |
301 | ||
302 | if (!(sdi = get_sigrok_device_instance(device_instances, device_index))) | |
303 | return SIGROK_ERR; | |
304 | ||
305 | sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR); | |
306 | if (sdi->serial->fd == -1) | |
307 | return SIGROK_ERR; | |
308 | ||
309 | sdi->status = ST_ACTIVE; | |
310 | ||
311 | return SIGROK_OK; | |
312 | } | |
313 | ||
314 | static void hw_closedev(int device_index) | |
315 | { | |
316 | struct sigrok_device_instance *sdi; | |
317 | ||
318 | if (!(sdi = get_sigrok_device_instance(device_instances, device_index))) | |
319 | return; | |
320 | ||
321 | if (sdi->serial->fd != -1) { | |
322 | serial_close(sdi->serial->fd); | |
323 | sdi->serial->fd = -1; | |
324 | sdi->status = ST_INACTIVE; | |
325 | } | |
326 | } | |
327 | ||
328 | static void hw_cleanup(void) | |
329 | { | |
330 | GSList *l; | |
331 | struct sigrok_device_instance *sdi; | |
332 | ||
333 | /* Properly close all devices. */ | |
334 | for (l = device_instances; l; l = l->next) { | |
335 | sdi = l->data; | |
336 | if (sdi->serial->fd != -1) | |
337 | serial_close(sdi->serial->fd); | |
338 | sigrok_device_instance_free(sdi); | |
339 | } | |
340 | g_slist_free(device_instances); | |
341 | device_instances = NULL; | |
342 | } | |
343 | ||
344 | static void *hw_get_device_info(int device_index, int device_info_id) | |
345 | { | |
346 | struct sigrok_device_instance *sdi; | |
347 | void *info; | |
348 | ||
349 | if (!(sdi = get_sigrok_device_instance(device_instances, device_index))) | |
350 | return NULL; | |
351 | ||
352 | info = NULL; | |
353 | switch (device_info_id) { | |
354 | case DI_INSTANCE: | |
355 | info = sdi; | |
356 | break; | |
357 | case DI_NUM_PROBES: | |
358 | info = GINT_TO_POINTER(NUM_PROBES); | |
359 | break; | |
360 | case DI_SAMPLERATES: | |
361 | info = &samplerates; | |
362 | break; | |
363 | case DI_TRIGGER_TYPES: | |
364 | info = (char *)TRIGGER_TYPES; | |
365 | break; | |
366 | case DI_CUR_SAMPLERATE: | |
367 | info = &cur_samplerate; | |
368 | break; | |
369 | } | |
370 | ||
371 | return info; | |
372 | } | |
373 | ||
374 | static int hw_get_status(int device_index) | |
375 | { | |
376 | struct sigrok_device_instance *sdi; | |
377 | ||
378 | if (!(sdi = get_sigrok_device_instance(device_instances, device_index))) | |
379 | return ST_NOT_FOUND; | |
380 | ||
381 | return sdi->status; | |
382 | } | |
383 | ||
384 | static int *hw_get_capabilities(void) | |
385 | { | |
386 | return capabilities; | |
387 | } | |
388 | ||
389 | static int set_configuration_samplerate(struct sigrok_device_instance *sdi, | |
390 | uint64_t samplerate) | |
391 | { | |
392 | uint32_t divider; | |
393 | ||
394 | if (samplerate < samplerates.low || samplerate > samplerates.high) | |
395 | return SIGROK_ERR_SAMPLERATE; | |
396 | ||
397 | if (samplerate > CLOCK_RATE) { | |
398 | flag_reg |= FLAG_DEMUX; | |
399 | divider = (CLOCK_RATE * 2 / samplerate) - 1; | |
400 | } else { | |
401 | flag_reg &= ~FLAG_DEMUX; | |
402 | divider = (CLOCK_RATE / samplerate) - 1; | |
403 | } | |
404 | divider = htonl(divider); | |
405 | ||
406 | g_message("setting samplerate to %" PRIu64 " Hz (divider %u, demux %s)", | |
407 | samplerate, divider, flag_reg & FLAG_DEMUX ? "on" : "off"); | |
408 | ||
409 | if (send_longcommand(sdi->serial->fd, CMD_SET_DIVIDER, | |
410 | divider) != SIGROK_OK) | |
411 | return SIGROK_ERR; | |
412 | cur_samplerate = samplerate; | |
413 | ||
414 | return SIGROK_OK; | |
415 | } | |
416 | ||
417 | static int hw_set_configuration(int device_index, int capability, void *value) | |
418 | { | |
419 | struct sigrok_device_instance *sdi; | |
420 | int ret; | |
421 | uint64_t *tmp_u64; | |
422 | ||
423 | if (!(sdi = get_sigrok_device_instance(device_instances, device_index))) | |
424 | return SIGROK_ERR; | |
425 | ||
426 | if (sdi->status != ST_ACTIVE) | |
427 | return SIGROK_ERR; | |
428 | ||
429 | if (capability == HWCAP_SAMPLERATE) { | |
430 | tmp_u64 = value; | |
431 | ret = set_configuration_samplerate(sdi, *tmp_u64); | |
432 | } else if (capability == HWCAP_PROBECONFIG) { | |
433 | ret = configure_probes((GSList *) value); | |
434 | } else if (capability == HWCAP_LIMIT_SAMPLES) { | |
435 | limit_samples = strtoull(value, NULL, 10); | |
436 | ret = SIGROK_OK; | |
437 | } else if (capability == HWCAP_CAPTURE_RATIO) { | |
438 | capture_ratio = strtol(value, NULL, 10); | |
439 | if (capture_ratio < 0 || capture_ratio > 100) { | |
440 | capture_ratio = 0; | |
441 | ret = SIGROK_ERR; | |
442 | } else | |
443 | ret = SIGROK_OK; | |
444 | } else { | |
445 | ret = SIGROK_ERR; | |
446 | } | |
447 | ||
448 | return ret; | |
449 | } | |
450 | ||
451 | static int receive_data(int fd, int revents, void *user_data) | |
452 | { | |
453 | static unsigned int num_transfers = 0; | |
454 | static int num_bytes = 0; | |
455 | static char last_sample[4] = { 0xff, 0xff, 0xff, 0xff }; | |
456 | static unsigned char sample[4] = { 0, 0, 0, 0 }; | |
457 | static unsigned char tmp_sample[4]; | |
458 | int count, buflen, num_channels, i, j; | |
459 | struct datafeed_packet packet; | |
460 | unsigned char byte, *buffer; | |
461 | ||
462 | if (num_transfers++ == 0) { | |
463 | /* | |
464 | * First time round, means the device started sending data, | |
465 | * and will not stop until done. If it stops sending for | |
466 | * longer than it takes to send a byte, that means it's | |
467 | * finished. We'll double that to 30ms to be sure... | |
468 | */ | |
469 | source_remove(fd); | |
470 | source_add(fd, G_IO_IN, 100, receive_data, user_data); | |
471 | } | |
472 | ||
473 | num_channels = 0; | |
474 | for (i = 0x20; i > 0x02; i /= 2) { | |
475 | if ((flag_reg & i) == 0) | |
476 | num_channels++; | |
477 | } | |
478 | ||
479 | if (revents == G_IO_IN | |
480 | && num_transfers / num_channels <= limit_samples) { | |
481 | if (read(fd, &byte, 1) != 1) | |
482 | return FALSE; | |
483 | ||
484 | sample[num_bytes++] = byte; | |
485 | if (num_bytes == num_channels) { | |
486 | /* Got a full sample. */ | |
487 | if (flag_reg & FLAG_RLE) { | |
488 | /* | |
489 | * In RLE mode -1 should never come in as a | |
490 | * sample, because bit 31 is the "count" flag. | |
491 | * TODO: Endianness may be wrong here, could be | |
492 | * sample[3]. | |
493 | */ | |
494 | if (sample[0] & 0x80 | |
495 | && !(last_sample[0] & 0x80)) { | |
496 | count = (int)(*sample) & 0x7fffffff; | |
497 | buffer = g_malloc(count); | |
498 | buflen = 0; | |
499 | for (i = 0; i < count; i++) { | |
500 | memcpy(buffer + buflen, | |
501 | last_sample, 4); | |
502 | buflen += 4; | |
503 | } | |
504 | } else { | |
505 | /* | |
506 | * Just a single sample, next sample | |
507 | * will probably be a count referring | |
508 | * to this -- but this one is still a | |
509 | * part of the stream. | |
510 | */ | |
511 | buffer = sample; | |
512 | buflen = 4; | |
513 | } | |
514 | } else { | |
515 | /* No compression. */ | |
516 | buffer = sample; | |
517 | buflen = 4; | |
518 | } | |
519 | ||
520 | if (num_channels < 4) { | |
521 | /* | |
522 | * Some channel groups may have been turned | |
523 | * off, to speed up transfer between the | |
524 | * hardware and the PC. Expand that here before | |
525 | * submitting it over the session bus -- | |
526 | * whatever is listening on the bus will be | |
527 | * expecting a full 32-bit sample, based on | |
528 | * the number of probes. | |
529 | */ | |
530 | j = 0; | |
531 | memset(tmp_sample, 0, 4); | |
532 | for (i = 0; i < 4; i++) { | |
533 | if ((flag_reg & (8 >> i)) == 0) { | |
534 | /* | |
535 | * This channel group was | |
536 | * enabled, copy from received | |
537 | * sample. | |
538 | */ | |
539 | tmp_sample[i] = sample[j++]; | |
540 | } | |
541 | } | |
542 | memcpy(sample, tmp_sample, 4); | |
543 | } | |
544 | ||
545 | /* Send it all to the session bus. */ | |
546 | packet.type = DF_LOGIC32; | |
547 | packet.length = buflen; | |
548 | packet.payload = buffer; | |
549 | session_bus(user_data, &packet); | |
550 | if (buffer == sample) | |
551 | memcpy(last_sample, buffer, num_channels); | |
552 | else | |
553 | g_free(buffer); | |
554 | ||
555 | memset(sample, 0, 4); | |
556 | num_bytes = 0; | |
557 | } | |
558 | } else { | |
559 | /* | |
560 | * This is the main loop telling us a timeout was reached, or | |
561 | * we've acquired all the samples we asked for -- we're done. | |
562 | */ | |
563 | tcflush(fd, TCIOFLUSH); | |
564 | serial_close(fd); | |
565 | packet.type = DF_END; | |
566 | packet.length = 0; | |
567 | session_bus(user_data, &packet); | |
568 | } | |
569 | ||
570 | return TRUE; | |
571 | } | |
572 | ||
573 | static int hw_start_acquisition(int device_index, gpointer session_device_id) | |
574 | { | |
575 | int i; | |
576 | struct datafeed_packet *packet; | |
577 | struct datafeed_header *header; | |
578 | struct sigrok_device_instance *sdi; | |
579 | uint32_t data; | |
580 | uint16_t readcount, delaycount; | |
581 | uint8_t changrp_mask; | |
582 | ||
583 | if (!(sdi = get_sigrok_device_instance(device_instances, device_index))) | |
584 | return SIGROK_ERR; | |
585 | ||
586 | if (sdi->status != ST_ACTIVE) | |
587 | return SIGROK_ERR; | |
588 | ||
589 | if (trigger_mask[0]) { | |
590 | /* Trigger masks */ | |
591 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0, | |
592 | trigger_mask[0]) != SIGROK_OK) | |
593 | return SIGROK_ERR; | |
594 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_1, | |
595 | trigger_mask[1]) != SIGROK_OK) | |
596 | return SIGROK_ERR; | |
597 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_2, | |
598 | trigger_mask[2]) != SIGROK_OK) | |
599 | return SIGROK_ERR; | |
600 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_3, | |
601 | trigger_mask[3]) != SIGROK_OK) | |
602 | return SIGROK_ERR; | |
603 | ||
604 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0, | |
605 | trigger_value[0]) != SIGROK_OK) | |
606 | return SIGROK_ERR; | |
607 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_1, | |
608 | trigger_value[1]) != SIGROK_OK) | |
609 | return SIGROK_ERR; | |
610 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_2, | |
611 | trigger_value[2]) != SIGROK_OK) | |
612 | return SIGROK_ERR; | |
613 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_3, | |
614 | trigger_value[3]) != SIGROK_OK) | |
615 | return SIGROK_ERR; | |
616 | ||
617 | /* Trigger configuration */ | |
618 | /* | |
619 | * TODO: The start flag should only be on the last used | |
620 | * stage I think... | |
621 | */ | |
622 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0, | |
623 | 0x00000008) != SIGROK_OK) | |
624 | return SIGROK_ERR; | |
625 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_1, | |
626 | 0x00000000) != SIGROK_OK) | |
627 | return SIGROK_ERR; | |
628 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_2, | |
629 | 0x00000000) != SIGROK_OK) | |
630 | return SIGROK_ERR; | |
631 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_3, | |
632 | 0x00000000) != SIGROK_OK) | |
633 | return SIGROK_ERR; | |
634 | delaycount = limit_samples / 4 * (capture_ratio / 100); | |
635 | } else { | |
636 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0, | |
637 | trigger_mask[0]) != SIGROK_OK) | |
638 | return SIGROK_ERR; | |
639 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0, | |
640 | trigger_value[0]) != SIGROK_OK) | |
641 | return SIGROK_ERR; | |
642 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0, | |
643 | 0x00000008) != SIGROK_OK) | |
644 | return SIGROK_ERR; | |
645 | delaycount = limit_samples / 4; | |
646 | } | |
647 | ||
648 | set_configuration_samplerate(sdi, cur_samplerate); | |
649 | ||
650 | /* Send sample limit and pre/post-trigger capture ratio. */ | |
651 | readcount = limit_samples / 4; | |
652 | if (flag_reg & FLAG_DEMUX) { | |
653 | data = (delaycount - 8) & 0xfff8 << 13; | |
654 | data |= (readcount - 4) & 0xffff; | |
655 | } else { | |
656 | flag_reg |= FLAG_FILTER; | |
657 | data = (readcount - 1) << 16; | |
658 | data |= (delaycount - 1); | |
659 | } | |
660 | /* TODO: htonl()? */ | |
661 | byteswap(&data); | |
662 | if (send_longcommand(sdi->serial->fd, CMD_CAPTURE_SIZE, | |
663 | data) != SIGROK_OK) | |
664 | return SIGROK_ERR; | |
665 | ||
666 | /* | |
667 | * Enable/disable channel groups in the flag register according to the | |
668 | * probe mask. The register stores them backwards, hence shift right | |
669 | * from 1000. | |
670 | */ | |
671 | changrp_mask = 0; | |
672 | for (i = 0; i < 4; i++) { | |
673 | if (probe_mask & (0xff << (i * 8))) | |
674 | changrp_mask |= (8 >> i); | |
675 | } | |
676 | ||
677 | /* The flag register wants them here, and 1 means "disable channel". */ | |
678 | flag_reg |= ~(changrp_mask << 2) & 0x3c; | |
679 | ||
680 | data = flag_reg << 24; | |
681 | if (send_longcommand(sdi->serial->fd, CMD_SET_FLAGS, data) != SIGROK_OK) | |
682 | return SIGROK_ERR; | |
683 | ||
684 | /* Start acquisition on the device. */ | |
685 | if (send_shortcommand(sdi->serial->fd, CMD_RUN) != SIGROK_OK) | |
686 | return SIGROK_ERR; | |
687 | ||
688 | source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, | |
689 | session_device_id); | |
690 | ||
691 | /* Send header packet to the session bus. */ | |
692 | packet = g_malloc(sizeof(struct datafeed_packet)); | |
693 | header = g_malloc(sizeof(struct datafeed_header)); | |
694 | if (!packet || !header) | |
695 | return SIGROK_ERR; | |
696 | packet->type = DF_HEADER; | |
697 | packet->length = sizeof(struct datafeed_header); | |
698 | packet->payload = (unsigned char *)header; | |
699 | header->feed_version = 1; | |
700 | gettimeofday(&header->starttime, NULL); | |
701 | header->samplerate = cur_samplerate; | |
702 | header->protocol_id = PROTO_RAW; | |
703 | header->num_probes = NUM_PROBES; | |
704 | session_bus(session_device_id, packet); | |
705 | g_free(header); | |
706 | g_free(packet); | |
707 | ||
708 | return SIGROK_OK; | |
709 | } | |
710 | ||
711 | static void hw_stop_acquisition(int device_index, gpointer session_device_id) | |
712 | { | |
713 | struct datafeed_packet packet; | |
714 | ||
715 | /* QUICK HACK */ | |
716 | device_index = device_index; | |
717 | ||
718 | packet.type = DF_END; | |
719 | packet.length = 0; | |
720 | session_bus(session_device_id, &packet); | |
721 | } | |
722 | ||
723 | struct device_plugin ols_plugin_info = { | |
724 | "sump", | |
725 | 1, | |
726 | hw_init, | |
727 | hw_cleanup, | |
728 | hw_opendev, | |
729 | hw_closedev, | |
730 | hw_get_device_info, | |
731 | hw_get_status, | |
732 | hw_get_capabilities, | |
733 | hw_set_configuration, | |
734 | hw_start_acquisition, | |
735 | hw_stop_acquisition, | |
736 | }; |