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