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