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