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