]>
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; | |
6937bb75 | 193 | int devcnt, final_devcnt, num_ports, fd, ret, i; |
d02a535e | 194 | char buf[8], **device_names, **serial_params; |
a1bb33af UH |
195 | |
196 | if(deviceinfo) | |
6937bb75 | 197 | ports = g_slist_append(NULL, strdup(deviceinfo)); |
a1bb33af UH |
198 | else |
199 | /* no specific device given, so scan all serial ports */ | |
200 | ports = list_serial_ports(); | |
201 | ||
202 | num_ports = g_slist_length(ports); | |
6937bb75 BV |
203 | fds = calloc(1, num_ports * sizeof(GPollFD)); |
204 | device_names = malloc(num_ports * (sizeof(char *))); | |
d02a535e | 205 | serial_params = malloc(num_ports * (sizeof(char *))); |
a1bb33af UH |
206 | devcnt = 0; |
207 | for(l = ports; l; l = l->next) { | |
208 | /* The discovery procedure is like this: first send the Reset command (0x00) 5 times, | |
209 | * since the device could be anywhere in a 5-byte command. Then send the ID command | |
210 | * (0x02). If the device responds with 4 bytes ("OLS1" or "SLA1"), we have a match. | |
211 | * Since it may take the device a while to respond at 115Kb/s, we do all the sending | |
212 | * first, then wait for all of them to respond with g_poll(). | |
213 | */ | |
6937bb75 | 214 | g_message("probing %s...", (char *) l->data); |
d02a535e | 215 | fd = serial_open(l->data, O_RDWR | O_NONBLOCK); |
a1bb33af | 216 | if(fd != -1) { |
d02a535e BV |
217 | serial_params[devcnt] = serial_backup_params(fd); |
218 | serial_set_params(fd, 115200, 8, 0, 1, 2); | |
6937bb75 BV |
219 | ret = SIGROK_OK; |
220 | for(i = 0; i < 5; i++) { | |
221 | if( (ret = send_shortcommand(fd, CMD_RESET)) != SIGROK_OK) { | |
d02a535e | 222 | /* serial port is not writable */ |
6937bb75 BV |
223 | break; |
224 | } | |
a1bb33af | 225 | } |
d02a535e BV |
226 | if(ret != SIGROK_OK) { |
227 | serial_restore_params(fd, serial_params[devcnt]); | |
228 | serial_close(fd); | |
6937bb75 | 229 | continue; |
d02a535e | 230 | } |
6937bb75 BV |
231 | send_shortcommand(fd, CMD_ID); |
232 | fds[devcnt].fd = fd; | |
233 | fds[devcnt].events = G_IO_IN; | |
234 | device_names[devcnt] = strdup(l->data); | |
235 | devcnt++; | |
a1bb33af | 236 | } |
6937bb75 | 237 | free(l->data); |
a1bb33af UH |
238 | } |
239 | ||
240 | /* 2ms should do it, that's enough time for 28 bytes to go over the bus */ | |
241 | usleep(2000); | |
242 | ||
243 | final_devcnt = 0; | |
244 | g_poll(fds, devcnt, 1); | |
245 | for(i = 0; i < devcnt; i++) { | |
246 | if(fds[i].revents == G_IO_IN) { | |
247 | if(read(fds[i].fd, buf, 4) == 4) { | |
248 | if(!strncmp(buf, "1SLO", 4) || !strncmp(buf, "1ALS", 4)) { | |
249 | if(!strncmp(buf, "1SLO", 4)) | |
250 | sdi = sigrok_device_instance_new(final_devcnt, ST_INACTIVE, | |
251 | "Openbench", "Logic Sniffer", "v1.0"); | |
252 | else | |
253 | sdi = sigrok_device_instance_new(final_devcnt, ST_INACTIVE, | |
254 | "Sump", "Logic Analyzer", "v1.0"); | |
255 | sdi->serial = serial_device_instance_new(device_names[i], -1); | |
256 | device_instances = g_slist_append(device_instances, sdi); | |
257 | final_devcnt++; | |
d02a535e | 258 | serial_close(fds[i].fd); |
a1bb33af UH |
259 | fds[i].fd = 0; |
260 | } | |
261 | } | |
6937bb75 | 262 | free(device_names[i]); |
a1bb33af UH |
263 | } |
264 | ||
6937bb75 | 265 | if(fds[i].fd != 0) { |
d02a535e BV |
266 | serial_restore_params(fds[i].fd, serial_params[i]); |
267 | serial_close(fds[i].fd); | |
6937bb75 | 268 | } |
d02a535e | 269 | free(serial_params[i]); |
a1bb33af UH |
270 | } |
271 | ||
6937bb75 BV |
272 | free(fds); |
273 | free(device_names); | |
d02a535e | 274 | free(serial_params); |
a1bb33af UH |
275 | g_slist_free(ports); |
276 | ||
6937bb75 BV |
277 | cur_samplerate = samplerates.low; |
278 | ||
a1bb33af UH |
279 | return final_devcnt; |
280 | } | |
281 | ||
282 | ||
283 | static int hw_opendev(int device_index) | |
284 | { | |
285 | struct sigrok_device_instance *sdi; | |
286 | ||
287 | if(!(sdi = get_sigrok_device_instance(device_instances, device_index))) | |
e31b636d | 288 | return SIGROK_ERR; |
a1bb33af | 289 | |
d02a535e | 290 | sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR); |
a1bb33af | 291 | if(sdi->serial->fd == -1) |
e31b636d | 292 | return SIGROK_ERR; |
a1bb33af UH |
293 | |
294 | sdi->status = ST_ACTIVE; | |
295 | ||
296 | return SIGROK_OK; | |
297 | } | |
298 | ||
299 | ||
300 | static void hw_closedev(int device_index) | |
301 | { | |
302 | struct sigrok_device_instance *sdi; | |
303 | ||
304 | if(!(sdi = get_sigrok_device_instance(device_instances, device_index))) | |
305 | return; | |
306 | ||
307 | if(sdi->serial->fd != -1) { | |
d02a535e | 308 | serial_close(sdi->serial->fd); |
a1bb33af UH |
309 | sdi->serial->fd = -1; |
310 | sdi->status = ST_INACTIVE; | |
311 | } | |
312 | ||
313 | } | |
314 | ||
315 | ||
316 | static void hw_cleanup(void) | |
317 | { | |
318 | GSList *l; | |
319 | struct sigrok_device_instance *sdi; | |
320 | ||
321 | /* properly close all devices */ | |
322 | for(l = device_instances; l; l = l->next) { | |
323 | sdi = l->data; | |
324 | if(sdi->serial->fd != -1) | |
d02a535e | 325 | serial_close(sdi->serial->fd); |
a1bb33af UH |
326 | sigrok_device_instance_free(sdi); |
327 | } | |
328 | g_slist_free(device_instances); | |
329 | device_instances = NULL; | |
330 | ||
331 | } | |
332 | ||
333 | ||
334 | static void *hw_get_device_info(int device_index, int device_info_id) | |
335 | { | |
336 | struct sigrok_device_instance *sdi; | |
337 | void *info; | |
338 | ||
339 | if( !(sdi = get_sigrok_device_instance(device_instances, device_index)) ) | |
340 | return NULL; | |
341 | ||
342 | info = NULL; | |
343 | switch(device_info_id) | |
344 | { | |
345 | case DI_INSTANCE: | |
346 | info = sdi; | |
347 | break; | |
348 | case DI_NUM_PROBES: | |
349 | info = GINT_TO_POINTER(NUM_PROBES); | |
350 | break; | |
351 | case DI_SAMPLERATES: | |
352 | info = &samplerates; | |
353 | break; | |
354 | case DI_TRIGGER_TYPES: | |
355 | info = (char *) TRIGGER_TYPES; | |
356 | break; | |
4c100f32 | 357 | case DI_CUR_SAMPLERATE: |
a1bb33af UH |
358 | info = &cur_samplerate; |
359 | break; | |
360 | } | |
361 | ||
362 | return info; | |
363 | } | |
364 | ||
365 | ||
366 | static int hw_get_status(int device_index) | |
367 | { | |
368 | struct sigrok_device_instance *sdi; | |
369 | ||
370 | if(!(sdi = get_sigrok_device_instance(device_instances, device_index))) | |
371 | return ST_NOT_FOUND; | |
372 | ||
373 | return sdi->status; | |
374 | } | |
375 | ||
376 | ||
377 | static int *hw_get_capabilities(void) | |
378 | { | |
379 | ||
380 | return capabilities; | |
381 | } | |
382 | ||
383 | ||
384 | static int set_configuration_samplerate(struct sigrok_device_instance *sdi, uint64_t samplerate) | |
385 | { | |
386 | uint32_t divider; | |
387 | ||
388 | if(samplerate < samplerates.low || samplerate > samplerates.high) | |
e31b636d | 389 | return SIGROK_ERR_SAMPLERATE; |
a1bb33af UH |
390 | |
391 | if(samplerate > CLOCK_RATE) { | |
392 | flag_reg |= FLAG_DEMUX; | |
6937bb75 | 393 | divider = (CLOCK_RATE * 2 / samplerate) - 1; |
a1bb33af UH |
394 | } |
395 | else { | |
6937bb75 BV |
396 | flag_reg &= ~FLAG_DEMUX; |
397 | divider = (CLOCK_RATE / samplerate) - 1; | |
a1bb33af | 398 | } |
6937bb75 | 399 | divider = htonl(divider); |
a1bb33af UH |
400 | |
401 | g_message("setting samplerate to %"PRIu64" Hz (divider %u, demux %s)", samplerate, divider, | |
402 | flag_reg & FLAG_DEMUX ? "on" : "off"); | |
403 | if(send_longcommand(sdi->serial->fd, CMD_SET_DIVIDER, divider) != SIGROK_OK) | |
e31b636d | 404 | return SIGROK_ERR; |
a1bb33af UH |
405 | cur_samplerate = samplerate; |
406 | ||
407 | return SIGROK_OK; | |
408 | } | |
409 | ||
410 | ||
411 | static int hw_set_configuration(int device_index, int capability, void *value) | |
412 | { | |
413 | struct sigrok_device_instance *sdi; | |
414 | int ret; | |
415 | uint64_t *tmp_u64; | |
416 | ||
417 | if(!(sdi = get_sigrok_device_instance(device_instances, device_index))) | |
e31b636d | 418 | return SIGROK_ERR; |
a1bb33af UH |
419 | |
420 | if(sdi->status != ST_ACTIVE) | |
e31b636d | 421 | return SIGROK_ERR; |
a1bb33af UH |
422 | |
423 | if(capability == HWCAP_SAMPLERATE) { | |
424 | tmp_u64 = value; | |
425 | ret = set_configuration_samplerate(sdi, *tmp_u64); | |
426 | } | |
427 | else if(capability == HWCAP_PROBECONFIG) | |
428 | ret = configure_probes( (GSList *) value); | |
429 | else if(capability == HWCAP_LIMIT_SAMPLES) { | |
430 | limit_samples = strtoull(value, NULL, 10); | |
431 | ret = SIGROK_OK; | |
432 | } | |
433 | else if(capability == HWCAP_CAPTURE_RATIO) { | |
434 | capture_ratio = strtol(value, NULL, 10); | |
435 | if(capture_ratio < 0 || capture_ratio > 100) { | |
436 | capture_ratio = 0; | |
e31b636d | 437 | ret = SIGROK_ERR; |
a1bb33af UH |
438 | } |
439 | else | |
440 | ret = SIGROK_OK; | |
441 | } | |
442 | else | |
e31b636d | 443 | ret = SIGROK_ERR; |
a1bb33af UH |
444 | |
445 | return ret; | |
446 | } | |
447 | ||
448 | ||
449 | static int receive_data(int fd, int revents, void *user_data) | |
450 | { | |
451 | static int num_transfers = 0; | |
452 | static int num_bytes = 0; | |
453 | static char last_sample[4] = {0xff}; | |
6937bb75 BV |
454 | static unsigned char sample[4] = {0}, tmp_sample[4]; |
455 | int count, buflen, num_channels, i, j; | |
a1bb33af UH |
456 | struct datafeed_packet packet; |
457 | unsigned char byte, *buffer; | |
458 | ||
459 | if(num_transfers++ == 0) { | |
460 | /* first time round, means the device started sending data, and will not | |
461 | * stop until done. if it stops sending for longer than it takes to send | |
462 | * a byte, that means it's finished. we'll double that to 30ms to be sure... | |
463 | */ | |
464 | source_remove(fd); | |
6937bb75 | 465 | source_add(fd, G_IO_IN, 100, receive_data, user_data); |
a1bb33af UH |
466 | } |
467 | ||
6937bb75 BV |
468 | num_channels = 0; |
469 | for(i = 0x20; i > 0x02; i /= 2) | |
470 | if((flag_reg & i) == 0) | |
471 | num_channels++; | |
472 | ||
473 | if(revents == G_IO_IN && num_transfers / num_channels <= limit_samples) { | |
a1bb33af UH |
474 | if(read(fd, &byte, 1) != 1) |
475 | return FALSE; | |
476 | ||
477 | sample[num_bytes++] = byte; | |
6937bb75 | 478 | if(num_bytes == num_channels) { |
a1bb33af UH |
479 | /* got a full sample */ |
480 | if(flag_reg & FLAG_RLE) { | |
481 | /* in RLE mode -1 should never come in as a sample, because | |
482 | * bit 31 is the "count" flag */ | |
483 | /* TODO: endianness may be wrong here, could be sample[3] */ | |
484 | if(sample[0] & 0x80 && !(last_sample[0] & 0x80)) { | |
485 | count = (int) (*sample) & 0x7fffffff; | |
486 | buffer = g_malloc(count); | |
487 | buflen = 0; | |
488 | for(i = 0; i < count; i++) | |
489 | { | |
490 | memcpy(buffer + buflen , last_sample, 4); | |
491 | buflen += 4; | |
492 | } | |
493 | } | |
494 | else { | |
495 | /* just a single sample, next sample will probably be a count | |
496 | * referring to this -- but this one is still a part of the stream | |
497 | */ | |
498 | buffer = sample; | |
499 | buflen = 4; | |
500 | } | |
501 | } | |
502 | else { | |
503 | /* no compression */ | |
504 | buffer = sample; | |
505 | buflen = 4; | |
506 | } | |
507 | ||
6937bb75 BV |
508 | if(num_channels < 4) { |
509 | /* some channel groups may have been turned off, to speed up transfer | |
510 | * between the hardware and the PC. expand that here before submitting | |
511 | * it over the session bus -- whatever is listening on the bus will be | |
512 | * expecting a full 32-bit sample, based on the number of probes. | |
513 | */ | |
514 | j = 0; | |
515 | memset(tmp_sample, 0, 4); | |
516 | for(i = 0; i < 4; i++) { | |
517 | if((flag_reg & (8 >> i)) == 0) { | |
518 | /* this channel group was enabled, copy from received sample */ | |
519 | tmp_sample[i] = sample[j++]; | |
520 | } | |
521 | } | |
522 | memcpy(sample, tmp_sample, 4); | |
523 | } | |
524 | ||
a1bb33af UH |
525 | /* send it all to the session bus */ |
526 | packet.type = DF_LOGIC32; | |
527 | packet.length = buflen; | |
528 | packet.payload = buffer; | |
529 | session_bus(user_data, &packet); | |
530 | if(buffer == sample) | |
6937bb75 | 531 | memcpy(last_sample, buffer, num_channels); |
a1bb33af UH |
532 | else |
533 | g_free(buffer); | |
534 | ||
6937bb75 | 535 | memset(sample, 0, 4); |
a1bb33af UH |
536 | num_bytes = 0; |
537 | } | |
538 | } | |
539 | else { | |
6937bb75 BV |
540 | /* this is the main loop telling us a timeout was reached, or we've |
541 | * acquired all the samples we asked for -- we're done */ | |
a1bb33af | 542 | tcflush(fd, TCIOFLUSH); |
d02a535e | 543 | serial_close(fd); |
a1bb33af UH |
544 | packet.type = DF_END; |
545 | packet.length = 0; | |
546 | session_bus(user_data, &packet); | |
547 | } | |
548 | ||
549 | return TRUE; | |
550 | } | |
551 | ||
552 | ||
553 | static int hw_start_acquisition(int device_index, gpointer session_device_id) | |
554 | { | |
555 | struct datafeed_packet *packet; | |
556 | struct datafeed_header *header; | |
557 | struct sigrok_device_instance *sdi; | |
6937bb75 | 558 | int i; |
a1bb33af | 559 | uint32_t data; |
6937bb75 BV |
560 | uint16_t readcount, delaycount; |
561 | uint8_t changrp_mask; | |
a1bb33af UH |
562 | |
563 | if(!(sdi = get_sigrok_device_instance(device_instances, device_index))) | |
e31b636d | 564 | return SIGROK_ERR; |
a1bb33af UH |
565 | |
566 | if(sdi->status != ST_ACTIVE) | |
e31b636d | 567 | return SIGROK_ERR; |
a1bb33af | 568 | |
6937bb75 BV |
569 | if(trigger_mask[0]) { |
570 | /* trigger masks */ | |
571 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0, trigger_mask[0]) != SIGROK_OK) | |
54dc4bc7 | 572 | return SIGROK_ERR; |
6937bb75 | 573 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_1, trigger_mask[1]) != SIGROK_OK) |
54dc4bc7 | 574 | return SIGROK_ERR; |
6937bb75 | 575 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_2, trigger_mask[2]) != SIGROK_OK) |
54dc4bc7 | 576 | return SIGROK_ERR; |
6937bb75 | 577 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_3, trigger_mask[3]) != SIGROK_OK) |
54dc4bc7 | 578 | return SIGROK_ERR; |
6937bb75 BV |
579 | |
580 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0, trigger_value[0]) != SIGROK_OK) | |
54dc4bc7 | 581 | return SIGROK_ERR; |
6937bb75 | 582 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_1, trigger_value[1]) != SIGROK_OK) |
54dc4bc7 | 583 | return SIGROK_ERR; |
6937bb75 | 584 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_2, trigger_value[2]) != SIGROK_OK) |
54dc4bc7 | 585 | return SIGROK_ERR; |
6937bb75 | 586 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_3, trigger_value[3]) != SIGROK_OK) |
54dc4bc7 | 587 | return SIGROK_ERR; |
6937bb75 BV |
588 | |
589 | /* trigger configuration */ | |
590 | /* TODO: the start flag should only be on the last used stage I think... */ | |
591 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0, 0x00000008) != SIGROK_OK) | |
54dc4bc7 | 592 | return SIGROK_ERR; |
6937bb75 | 593 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_1, 0x00000000) != SIGROK_OK) |
54dc4bc7 | 594 | return SIGROK_ERR; |
6937bb75 | 595 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_2, 0x00000000) != SIGROK_OK) |
54dc4bc7 | 596 | return SIGROK_ERR; |
6937bb75 | 597 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_3, 0x00000000) != SIGROK_OK) |
54dc4bc7 | 598 | return SIGROK_ERR; |
6937bb75 BV |
599 | delaycount = limit_samples / 4 * (capture_ratio / 100); |
600 | } else { | |
601 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0, trigger_mask[0]) != SIGROK_OK) | |
54dc4bc7 | 602 | return SIGROK_ERR; |
6937bb75 | 603 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0, trigger_value[0]) != SIGROK_OK) |
54dc4bc7 | 604 | return SIGROK_ERR; |
6937bb75 | 605 | if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0, 0x00000008) != SIGROK_OK) |
54dc4bc7 | 606 | return SIGROK_ERR; |
6937bb75 BV |
607 | delaycount = limit_samples / 4; |
608 | } | |
a1bb33af | 609 | |
6937bb75 | 610 | set_configuration_samplerate(sdi, cur_samplerate); |
a1bb33af UH |
611 | |
612 | /* send sample limit and pre/post-trigger capture ratio */ | |
6937bb75 BV |
613 | readcount = limit_samples / 4; |
614 | if(flag_reg & FLAG_DEMUX) { | |
615 | data = (delaycount - 8) & 0xfff8 << 13; | |
616 | data |= (readcount - 4) & 0xffff; | |
617 | } else { | |
618 | flag_reg |= FLAG_FILTER; | |
619 | data = (readcount - 1) << 16; | |
620 | data |= (delaycount - 1); | |
621 | } | |
622 | /* TODO: htonl()? */ | |
623 | byteswap(&data); | |
a1bb33af | 624 | if(send_longcommand(sdi->serial->fd, CMD_CAPTURE_SIZE, data) != SIGROK_OK) |
e31b636d | 625 | return SIGROK_ERR; |
a1bb33af | 626 | |
6937bb75 BV |
627 | /* flag register */ |
628 | /* enable/disable channel groups in the flag register according to the | |
629 | * probe mask. The register stores them backwards, hence shift right from 1000. | |
630 | */ | |
631 | changrp_mask = 0; | |
632 | for(i = 0; i < 4; i++) | |
633 | { | |
634 | if(probe_mask & (0xff << (i * 8))) | |
635 | changrp_mask |= (8 >> i); | |
636 | } | |
637 | /* but the flag register wants them here, with 1 meaning "disable channel" */ | |
638 | flag_reg |= ~(changrp_mask << 2) & 0x3c; | |
a1bb33af | 639 | |
6937bb75 BV |
640 | data = flag_reg << 24; |
641 | if(send_longcommand(sdi->serial->fd, CMD_SET_FLAGS, data) != SIGROK_OK) | |
54dc4bc7 | 642 | return SIGROK_ERR; |
a1bb33af UH |
643 | |
644 | /* start acquisition on the device */ | |
645 | if(send_shortcommand(sdi->serial->fd, CMD_RUN) != SIGROK_OK) | |
e31b636d | 646 | return SIGROK_ERR; |
a1bb33af UH |
647 | |
648 | source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, session_device_id); | |
649 | ||
650 | /* send header packet to the session bus */ | |
651 | packet = g_malloc(sizeof(struct datafeed_packet)); | |
652 | header = g_malloc(sizeof(struct datafeed_header)); | |
653 | if(!packet || !header) | |
e31b636d | 654 | return SIGROK_ERR; |
a1bb33af UH |
655 | packet->type = DF_HEADER; |
656 | packet->length = sizeof(struct datafeed_header); | |
657 | packet->payload = (unsigned char *) header; | |
658 | header->feed_version = 1; | |
659 | gettimeofday(&header->starttime, NULL); | |
4c100f32 | 660 | header->samplerate = cur_samplerate; |
a1bb33af UH |
661 | header->protocol_id = PROTO_RAW; |
662 | header->num_probes = NUM_PROBES; | |
663 | session_bus(session_device_id, packet); | |
664 | g_free(header); | |
665 | g_free(packet); | |
666 | ||
667 | return SIGROK_OK; | |
668 | } | |
669 | ||
670 | ||
671 | static void hw_stop_acquisition(int device_index, gpointer session_device_id) | |
672 | { | |
673 | struct datafeed_packet packet; | |
674 | ||
675 | packet.type = DF_END; | |
676 | packet.length = 0; | |
677 | session_bus(session_device_id, &packet); | |
678 | ||
679 | } | |
680 | ||
681 | ||
682 | ||
683 | struct device_plugin ols_plugin_info = { | |
684 | "sump", | |
685 | 1, | |
686 | hw_init, | |
687 | hw_cleanup, | |
688 | ||
689 | hw_opendev, | |
690 | hw_closedev, | |
691 | hw_get_device_info, | |
692 | hw_get_status, | |
693 | hw_get_capabilities, | |
694 | hw_set_configuration, | |
695 | hw_start_acquisition, | |
696 | hw_stop_acquisition | |
697 | }; | |
698 |