]>
Commit | Line | Data |
---|---|---|
a1bb33af UH |
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> | |
6937bb75 | 31 | #include <arpa/inet.h> |
a1bb33af UH |
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 = { | |
6937bb75 | 84 | 10, |
a1bb33af UH |
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 */ | |
6937bb75 | 94 | static uint32_t flag_reg = 0; |
a1bb33af UH |
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. */ | |
6937bb75 BV |
99 | static int capture_ratio = 0; |
100 | static uint32_t probe_mask = 0xffffffff, trigger_mask[4] = {0}, trigger_value[4] = {0}; | |
a1bb33af UH |
101 | |
102 | ||
103 | ||
6937bb75 | 104 | static int send_shortcommand(int fd, uint8_t command) |
a1bb33af UH |
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) | |
e31b636d | 111 | return SIGROK_ERR; |
a1bb33af UH |
112 | |
113 | return SIGROK_OK; | |
114 | } | |
115 | ||
116 | ||
6937bb75 | 117 | static int send_longcommand(int fd, uint8_t command, uint32_t data) |
a1bb33af UH |
118 | { |
119 | char buf[5]; | |
120 | ||
121 | g_message("ols: sending cmd 0x%.2x data 0x%.8x", command, data); | |
122 | buf[0] = command; | |
6937bb75 BV |
123 | buf[1] = (data & 0xff000000) >> 24; |
124 | buf[2] = (data & 0xff0000) >> 16; | |
125 | buf[3] = (data & 0xff00) >> 8; | |
126 | buf[4] = data & 0xff; | |
a1bb33af | 127 | if(write(fd, buf, 5) != 5) |
e31b636d | 128 | return SIGROK_ERR; |
a1bb33af UH |
129 | |
130 | return SIGROK_OK; | |
131 | } | |
132 | ||
133 | static int configure_probes(GSList *probes) | |
134 | { | |
135 | struct probe *probe; | |
136 | GSList *l; | |
6937bb75 | 137 | int probe_bit, stage, i; |
a1bb33af UH |
138 | char *tc; |
139 | ||
140 | probe_mask = 0; | |
6937bb75 | 141 | for(i = 0; i < NUM_TRIGGER_STAGES; i++) { |
a1bb33af UH |
142 | trigger_mask[i] = 0; |
143 | trigger_value[i] = 0; | |
144 | } | |
145 | ||
6937bb75 | 146 | for(l = probes; l; l = l->next) { |
a1bb33af | 147 | probe = (struct probe *) l->data; |
6937bb75 BV |
148 | if(!probe->enabled) |
149 | continue; | |
150 | ||
151 | /* set up the probe mask for later configuration into the flag register */ | |
a1bb33af UH |
152 | probe_bit = 1 << (probe->index - 1); |
153 | probe_mask |= probe_bit; | |
6937bb75 | 154 | |
a1bb33af | 155 | if(probe->trigger) |
6937bb75 BV |
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 */ | |
54dc4bc7 | 167 | return SIGROK_ERR; |
a1bb33af UH |
168 | } |
169 | } | |
170 | ||
a1bb33af UH |
171 | return SIGROK_OK; |
172 | } | |
173 | ||
174 | ||
6937bb75 BV |
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 | ||
a1bb33af UH |
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; | |
6937bb75 | 194 | int devcnt, final_devcnt, num_ports, fd, ret, i; |
a1bb33af UH |
195 | char buf[8], **device_names; |
196 | ||
197 | if(deviceinfo) | |
6937bb75 | 198 | ports = g_slist_append(NULL, strdup(deviceinfo)); |
a1bb33af UH |
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); | |
6937bb75 BV |
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)); | |
a1bb33af UH |
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 | */ | |
6937bb75 | 215 | g_message("probing %s...", (char *) l->data); |
a1bb33af UH |
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); | |
6937bb75 BV |
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; | |
a1bb33af | 226 | tcsetattr(fd, TCSADRAIN, &term); |
6937bb75 BV |
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 | } | |
a1bb33af | 235 | } |
6937bb75 BV |
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++; | |
a1bb33af | 243 | } |
6937bb75 | 244 | free(l->data); |
a1bb33af UH |
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++; | |
6937bb75 | 265 | close(fds[i].fd); |
a1bb33af UH |
266 | fds[i].fd = 0; |
267 | } | |
268 | } | |
6937bb75 | 269 | free(device_names[i]); |
a1bb33af UH |
270 | } |
271 | ||
6937bb75 | 272 | if(fds[i].fd != 0) { |
a1bb33af | 273 | tcsetattr(fds[i].fd, TCSADRAIN, &prev_termios[i]); |
6937bb75 BV |
274 | close(fds[i].fd); |
275 | } | |
a1bb33af UH |
276 | } |
277 | ||
6937bb75 BV |
278 | free(fds); |
279 | free(device_names); | |
280 | free(prev_termios); | |
a1bb33af UH |
281 | g_slist_free(ports); |
282 | ||
6937bb75 BV |
283 | cur_samplerate = samplerates.low; |
284 | ||
a1bb33af UH |
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))) | |
e31b636d | 294 | return SIGROK_ERR; |
a1bb33af UH |
295 | |
296 | sdi->serial->fd = open(sdi->serial->port, O_RDWR); | |
297 | if(sdi->serial->fd == -1) | |
e31b636d | 298 | return SIGROK_ERR; |
a1bb33af UH |
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; | |
4c100f32 | 363 | case DI_CUR_SAMPLERATE: |
a1bb33af UH |
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) | |
e31b636d | 395 | return SIGROK_ERR_SAMPLERATE; |
a1bb33af UH |
396 | |
397 | if(samplerate > CLOCK_RATE) { | |
398 | flag_reg |= FLAG_DEMUX; | |
6937bb75 | 399 | divider = (CLOCK_RATE * 2 / samplerate) - 1; |
a1bb33af UH |
400 | } |
401 | else { | |
6937bb75 BV |
402 | flag_reg &= ~FLAG_DEMUX; |
403 | divider = (CLOCK_RATE / samplerate) - 1; | |
a1bb33af | 404 | } |
6937bb75 | 405 | divider = htonl(divider); |
a1bb33af UH |
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) | |
e31b636d | 410 | return SIGROK_ERR; |
a1bb33af UH |
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))) | |
e31b636d | 424 | return SIGROK_ERR; |
a1bb33af UH |
425 | |
426 | if(sdi->status != ST_ACTIVE) | |
e31b636d | 427 | return SIGROK_ERR; |
a1bb33af UH |
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; | |
e31b636d | 443 | ret = SIGROK_ERR; |
a1bb33af UH |
444 | } |
445 | else | |
446 | ret = SIGROK_OK; | |
447 | } | |
448 | else | |
e31b636d | 449 | ret = SIGROK_ERR; |
a1bb33af UH |
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}; | |
6937bb75 BV |
460 | static unsigned char sample[4] = {0}, tmp_sample[4]; |
461 | int count, buflen, num_channels, i, j; | |
a1bb33af UH |
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); | |
6937bb75 | 471 | source_add(fd, G_IO_IN, 100, receive_data, user_data); |
a1bb33af UH |
472 | } |
473 | ||
6937bb75 BV |
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) { | |
a1bb33af UH |
480 | if(read(fd, &byte, 1) != 1) |
481 | return FALSE; | |
482 | ||
483 | sample[num_bytes++] = byte; | |
6937bb75 | 484 | if(num_bytes == num_channels) { |
a1bb33af UH |
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 | ||
6937bb75 BV |
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 | ||
a1bb33af UH |
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) | |
6937bb75 | 537 | memcpy(last_sample, buffer, num_channels); |
a1bb33af UH |
538 | else |
539 | g_free(buffer); | |
540 | ||
6937bb75 | 541 | memset(sample, 0, 4); |
a1bb33af UH |
542 | num_bytes = 0; |
543 | } | |
544 | } | |
545 | else { | |
6937bb75 BV |
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 */ | |
a1bb33af UH |
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; | |
6937bb75 | 564 | int i; |
a1bb33af | 565 | uint32_t data; |
6937bb75 BV |
566 | uint16_t readcount, delaycount; |
567 | uint8_t changrp_mask; | |
a1bb33af UH |
568 | |
569 | if(!(sdi = get_sigrok_device_instance(device_instances, device_index))) | |
e31b636d | 570 | return SIGROK_ERR; |
a1bb33af UH |
571 | |
572 | if(sdi->status != ST_ACTIVE) | |
e31b636d | 573 | return SIGROK_ERR; |
a1bb33af | 574 | |
6937bb75 BV |
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) | |
54dc4bc7 | 578 | return SIGROK_ERR; |
6937bb75 | 579 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_1, trigger_mask[1]) != SIGROK_OK) |
54dc4bc7 | 580 | return SIGROK_ERR; |
6937bb75 | 581 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_2, trigger_mask[2]) != SIGROK_OK) |
54dc4bc7 | 582 | return SIGROK_ERR; |
6937bb75 | 583 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_3, trigger_mask[3]) != SIGROK_OK) |
54dc4bc7 | 584 | return SIGROK_ERR; |
6937bb75 BV |
585 | |
586 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0, trigger_value[0]) != SIGROK_OK) | |
54dc4bc7 | 587 | return SIGROK_ERR; |
6937bb75 | 588 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_1, trigger_value[1]) != SIGROK_OK) |
54dc4bc7 | 589 | return SIGROK_ERR; |
6937bb75 | 590 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_2, trigger_value[2]) != SIGROK_OK) |
54dc4bc7 | 591 | return SIGROK_ERR; |
6937bb75 | 592 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_3, trigger_value[3]) != SIGROK_OK) |
54dc4bc7 | 593 | return SIGROK_ERR; |
6937bb75 BV |
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) | |
54dc4bc7 | 598 | return SIGROK_ERR; |
6937bb75 | 599 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_1, 0x00000000) != SIGROK_OK) |
54dc4bc7 | 600 | return SIGROK_ERR; |
6937bb75 | 601 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_2, 0x00000000) != SIGROK_OK) |
54dc4bc7 | 602 | return SIGROK_ERR; |
6937bb75 | 603 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_3, 0x00000000) != SIGROK_OK) |
54dc4bc7 | 604 | return SIGROK_ERR; |
6937bb75 BV |
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) | |
54dc4bc7 | 608 | return SIGROK_ERR; |
6937bb75 | 609 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0, trigger_value[0]) != SIGROK_OK) |
54dc4bc7 | 610 | return SIGROK_ERR; |
6937bb75 | 611 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0, 0x00000008) != SIGROK_OK) |
54dc4bc7 | 612 | return SIGROK_ERR; |
6937bb75 BV |
613 | delaycount = limit_samples / 4; |
614 | } | |
a1bb33af | 615 | |
6937bb75 | 616 | set_configuration_samplerate(sdi, cur_samplerate); |
a1bb33af UH |
617 | |
618 | /* send sample limit and pre/post-trigger capture ratio */ | |
6937bb75 BV |
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); | |
a1bb33af | 630 | if(send_longcommand(sdi->serial->fd, CMD_CAPTURE_SIZE, data) != SIGROK_OK) |
e31b636d | 631 | return SIGROK_ERR; |
a1bb33af | 632 | |
6937bb75 BV |
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; | |
a1bb33af | 645 | |
6937bb75 BV |
646 | data = flag_reg << 24; |
647 | if(send_longcommand(sdi->serial->fd, CMD_SET_FLAGS, data) != SIGROK_OK) | |
54dc4bc7 | 648 | return SIGROK_ERR; |
a1bb33af UH |
649 | |
650 | /* start acquisition on the device */ | |
651 | if(send_shortcommand(sdi->serial->fd, CMD_RUN) != SIGROK_OK) | |
e31b636d | 652 | return SIGROK_ERR; |
a1bb33af UH |
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) | |
e31b636d | 660 | return SIGROK_ERR; |
a1bb33af UH |
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); | |
4c100f32 | 666 | header->samplerate = cur_samplerate; |
a1bb33af UH |
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 |