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