]>
Commit | Line | Data |
---|---|---|
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 | #ifdef _WIN32 | |
28 | #include <windows.h> | |
29 | #else | |
30 | #include <termios.h> | |
31 | #endif | |
32 | #include <string.h> | |
33 | #include <sys/time.h> | |
34 | #include <inttypes.h> | |
35 | #ifdef _WIN32 | |
36 | /* TODO */ | |
37 | #else | |
38 | #include <arpa/inet.h> | |
39 | #endif | |
40 | #include <glib.h> | |
41 | #include <sigrok.h> | |
42 | #include <sigrok-internal.h> | |
43 | #include "ols.h" | |
44 | ||
45 | #ifdef _WIN32 | |
46 | #define O_NONBLOCK FIONBIO | |
47 | #endif | |
48 | ||
49 | static int capabilities[] = { | |
50 | SR_HWCAP_LOGIC_ANALYZER, | |
51 | SR_HWCAP_SAMPLERATE, | |
52 | SR_HWCAP_CAPTURE_RATIO, | |
53 | SR_HWCAP_LIMIT_SAMPLES, | |
54 | 0, | |
55 | }; | |
56 | ||
57 | /* default supported samplerates, can be overridden by device metadata */ | |
58 | static struct sr_samplerates samplerates = { | |
59 | SR_HZ(10), | |
60 | SR_MHZ(200), | |
61 | SR_HZ(1), | |
62 | NULL, | |
63 | }; | |
64 | ||
65 | /* List of struct sr_serial_device_instance */ | |
66 | static GSList *device_instances = NULL; | |
67 | ||
68 | static int send_shortcommand(int fd, uint8_t command) | |
69 | { | |
70 | char buf[1]; | |
71 | ||
72 | sr_dbg("ols: sending cmd 0x%.2x", command); | |
73 | buf[0] = command; | |
74 | if (serial_write(fd, buf, 1) != 1) | |
75 | return SR_ERR; | |
76 | ||
77 | return SR_OK; | |
78 | } | |
79 | ||
80 | static int send_longcommand(int fd, uint8_t command, uint32_t data) | |
81 | { | |
82 | char buf[5]; | |
83 | ||
84 | sr_dbg("ols: sending cmd 0x%.2x data 0x%.8x", command, data); | |
85 | buf[0] = command; | |
86 | buf[1] = (data & 0xff000000) >> 24; | |
87 | buf[2] = (data & 0xff0000) >> 16; | |
88 | buf[3] = (data & 0xff00) >> 8; | |
89 | buf[4] = data & 0xff; | |
90 | if (serial_write(fd, buf, 5) != 5) | |
91 | return SR_ERR; | |
92 | ||
93 | return SR_OK; | |
94 | } | |
95 | ||
96 | static int configure_probes(struct ols_device *ols, GSList *probes) | |
97 | { | |
98 | struct sr_probe *probe; | |
99 | GSList *l; | |
100 | int probe_bit, stage, i; | |
101 | char *tc; | |
102 | ||
103 | ols->probe_mask = 0; | |
104 | for (i = 0; i < NUM_TRIGGER_STAGES; i++) { | |
105 | ols->trigger_mask[i] = 0; | |
106 | ols->trigger_value[i] = 0; | |
107 | } | |
108 | ||
109 | ols->num_stages = 0; | |
110 | for (l = probes; l; l = l->next) { | |
111 | probe = (struct sr_probe *)l->data; | |
112 | if (!probe->enabled) | |
113 | continue; | |
114 | ||
115 | /* | |
116 | * Set up the probe mask for later configuration into the | |
117 | * flag register. | |
118 | */ | |
119 | probe_bit = 1 << (probe->index - 1); | |
120 | ols->probe_mask |= probe_bit; | |
121 | ||
122 | if (!probe->trigger) | |
123 | continue; | |
124 | ||
125 | /* Configure trigger mask and value. */ | |
126 | stage = 0; | |
127 | for (tc = probe->trigger; tc && *tc; tc++) { | |
128 | ols->trigger_mask[stage] |= probe_bit; | |
129 | if (*tc == '1') | |
130 | ols->trigger_value[stage] |= probe_bit; | |
131 | stage++; | |
132 | if (stage > 3) | |
133 | /* | |
134 | * TODO: Only supporting parallel mode, with | |
135 | * up to 4 stages. | |
136 | */ | |
137 | return SR_ERR; | |
138 | } | |
139 | if (stage > ols->num_stages) | |
140 | ols->num_stages = stage; | |
141 | } | |
142 | ||
143 | return SR_OK; | |
144 | } | |
145 | ||
146 | static uint32_t reverse16(uint32_t in) | |
147 | { | |
148 | uint32_t out; | |
149 | ||
150 | out = (in & 0xff) << 8; | |
151 | out |= (in & 0xff00) >> 8; | |
152 | out |= (in & 0xff0000) << 8; | |
153 | out |= (in & 0xff000000) >> 8; | |
154 | ||
155 | return out; | |
156 | } | |
157 | ||
158 | static uint32_t reverse32(uint32_t in) | |
159 | { | |
160 | uint32_t out; | |
161 | ||
162 | out = (in & 0xff) << 24; | |
163 | out |= (in & 0xff00) << 8; | |
164 | out |= (in & 0xff0000) >> 8; | |
165 | out |= (in & 0xff000000) >> 24; | |
166 | ||
167 | return out; | |
168 | } | |
169 | ||
170 | static struct ols_device *ols_device_new(void) | |
171 | { | |
172 | struct ols_device *ols; | |
173 | ||
174 | /* TODO: Is 'ols' ever g_free()'d? */ | |
175 | if (!(ols = g_try_malloc0(sizeof(struct ols_device)))) { | |
176 | sr_err("ols: %s: ols malloc failed", __func__); | |
177 | return NULL; | |
178 | } | |
179 | ||
180 | ols->trigger_at = -1; | |
181 | ols->probe_mask = 0xffffffff; | |
182 | ols->cur_samplerate = SR_KHZ(200); | |
183 | ||
184 | return ols; | |
185 | } | |
186 | ||
187 | static struct sr_device_instance *get_metadata(int fd) | |
188 | { | |
189 | struct sr_device_instance *sdi; | |
190 | struct ols_device *ols; | |
191 | uint32_t tmp_int; | |
192 | uint8_t key, type, token; | |
193 | GString *tmp_str, *devicename, *version; | |
194 | gchar tmp_c; | |
195 | ||
196 | sdi = sr_device_instance_new(0, SR_ST_INACTIVE, NULL, NULL, NULL); | |
197 | ols = ols_device_new(); | |
198 | sdi->priv = ols; | |
199 | ||
200 | devicename = g_string_new(""); | |
201 | version = g_string_new(""); | |
202 | ||
203 | key = 0xff; | |
204 | while (key) { | |
205 | if (serial_read(fd, &key, 1) != 1 || key == 0x00) | |
206 | break; | |
207 | type = key >> 5; | |
208 | token = key & 0x1f; | |
209 | switch (type) { | |
210 | case 0: | |
211 | /* NULL-terminated string */ | |
212 | tmp_str = g_string_new(""); | |
213 | while (serial_read(fd, &tmp_c, 1) == 1 && tmp_c != '\0') | |
214 | g_string_append_c(tmp_str, tmp_c); | |
215 | sr_dbg("ols: got metadata key 0x%.2x value '%s'", | |
216 | key, tmp_str->str); | |
217 | switch (token) { | |
218 | case 0x01: | |
219 | /* Device name */ | |
220 | devicename = g_string_append(devicename, tmp_str->str); | |
221 | break; | |
222 | case 0x02: | |
223 | /* FPGA firmware version */ | |
224 | if (version->len) | |
225 | g_string_append(version, ", "); | |
226 | g_string_append(version, "FPGA version "); | |
227 | g_string_append(version, tmp_str->str); | |
228 | break; | |
229 | case 0x03: | |
230 | /* Ancillary version */ | |
231 | if (version->len) | |
232 | g_string_append(version, ", "); | |
233 | g_string_append(version, "Ancillary version "); | |
234 | g_string_append(version, tmp_str->str); | |
235 | break; | |
236 | default: | |
237 | sr_info("ols: unknown token 0x%.2x: '%s'", | |
238 | token, tmp_str->str); | |
239 | break; | |
240 | } | |
241 | g_string_free(tmp_str, TRUE); | |
242 | break; | |
243 | case 1: | |
244 | /* 32-bit unsigned integer */ | |
245 | if (serial_read(fd, &tmp_int, 4) != 4) | |
246 | break; | |
247 | tmp_int = reverse32(tmp_int); | |
248 | sr_dbg("ols: got metadata key 0x%.2x value 0x%.8x", | |
249 | key, tmp_int); | |
250 | switch (token) { | |
251 | case 0x00: | |
252 | /* Number of usable probes */ | |
253 | ols->num_probes = tmp_int; | |
254 | break; | |
255 | case 0x01: | |
256 | /* Amount of sample memory available (bytes) */ | |
257 | ols->max_samples = tmp_int; | |
258 | break; | |
259 | case 0x02: | |
260 | /* Amount of dynamic memory available (bytes) */ | |
261 | /* what is this for? */ | |
262 | break; | |
263 | case 0x03: | |
264 | /* Maximum sample rate (hz) */ | |
265 | ols->max_samplerate = tmp_int; | |
266 | break; | |
267 | case 0x04: | |
268 | /* protocol version */ | |
269 | ols->protocol_version = tmp_int; | |
270 | break; | |
271 | default: | |
272 | sr_info("ols: unknown token 0x%.2x: 0x%.8x", | |
273 | token, tmp_int); | |
274 | break; | |
275 | } | |
276 | break; | |
277 | case 2: | |
278 | /* 8-bit unsigned integer */ | |
279 | if (serial_read(fd, &tmp_c, 1) != 1) | |
280 | break; | |
281 | sr_dbg("ols: got metadata key 0x%.2x value 0x%.2x", | |
282 | key, tmp_c); | |
283 | switch (token) { | |
284 | case 0x00: | |
285 | /* Number of usable probes */ | |
286 | ols->num_probes = tmp_c; | |
287 | break; | |
288 | case 0x01: | |
289 | /* protocol version */ | |
290 | ols->protocol_version = tmp_c; | |
291 | break; | |
292 | default: | |
293 | sr_info("ols: unknown token 0x%.2x: 0x%.2x", | |
294 | token, tmp_c); | |
295 | break; | |
296 | } | |
297 | break; | |
298 | default: | |
299 | /* unknown type */ | |
300 | break; | |
301 | } | |
302 | } | |
303 | ||
304 | sdi->model = devicename->str; | |
305 | sdi->version = version->str; | |
306 | g_string_free(devicename, FALSE); | |
307 | g_string_free(version, FALSE); | |
308 | ||
309 | return sdi; | |
310 | } | |
311 | ||
312 | static int hw_init(const char *deviceinfo) | |
313 | { | |
314 | struct sr_device_instance *sdi; | |
315 | struct ols_device *ols; | |
316 | GSList *ports, *l; | |
317 | GPollFD *fds, probefd; | |
318 | int devcnt, final_devcnt, num_ports, fd, ret, i; | |
319 | char buf[8], **device_names, **serial_params; | |
320 | ||
321 | final_devcnt = 0; | |
322 | ||
323 | if (deviceinfo) | |
324 | ports = g_slist_append(NULL, strdup(deviceinfo)); | |
325 | else | |
326 | /* No specific device given, so scan all serial ports. */ | |
327 | ports = list_serial_ports(); | |
328 | ||
329 | num_ports = g_slist_length(ports); | |
330 | ||
331 | if (!(fds = g_try_malloc0(num_ports * sizeof(GPollFD)))) { | |
332 | sr_err("ols: %s: fds malloc failed", __func__); | |
333 | goto hw_init_free_ports; /* TODO: SR_ERR_MALLOC. */ | |
334 | } | |
335 | ||
336 | if (!(device_names = g_try_malloc(num_ports * sizeof(char *)))) { | |
337 | sr_err("ols: %s: device_names malloc failed", __func__); | |
338 | goto hw_init_free_fds; /* TODO: SR_ERR_MALLOC. */ | |
339 | } | |
340 | ||
341 | if (!(serial_params = g_try_malloc(num_ports * sizeof(char *)))) { | |
342 | sr_err("ols: %s: serial_params malloc failed", __func__); | |
343 | goto hw_init_free_device_names; /* TODO: SR_ERR_MALLOC. */ | |
344 | } | |
345 | ||
346 | devcnt = 0; | |
347 | for (l = ports; l; l = l->next) { | |
348 | /* The discovery procedure is like this: first send the Reset | |
349 | * command (0x00) 5 times, since the device could be anywhere | |
350 | * in a 5-byte command. Then send the ID command (0x02). | |
351 | * If the device responds with 4 bytes ("OLS1" or "SLA1"), we | |
352 | * have a match. | |
353 | * | |
354 | * Since it may take the device a while to respond at 115Kb/s, | |
355 | * we do all the sending first, then wait for all of them to | |
356 | * respond with g_poll(). | |
357 | */ | |
358 | sr_info("ols: probing %s...", (char *)l->data); | |
359 | fd = serial_open(l->data, O_RDWR | O_NONBLOCK); | |
360 | if (fd != -1) { | |
361 | serial_params[devcnt] = serial_backup_params(fd); | |
362 | serial_set_params(fd, 115200, 8, 0, 1, 2); | |
363 | ret = SR_OK; | |
364 | for (i = 0; i < 5; i++) { | |
365 | if ((ret = send_shortcommand(fd, | |
366 | CMD_RESET)) != SR_OK) { | |
367 | /* Serial port is not writable. */ | |
368 | break; | |
369 | } | |
370 | } | |
371 | if (ret != SR_OK) { | |
372 | serial_restore_params(fd, | |
373 | serial_params[devcnt]); | |
374 | serial_close(fd); | |
375 | continue; | |
376 | } | |
377 | send_shortcommand(fd, CMD_ID); | |
378 | fds[devcnt].fd = fd; | |
379 | fds[devcnt].events = G_IO_IN; | |
380 | device_names[devcnt] = strdup(l->data); | |
381 | devcnt++; | |
382 | } | |
383 | free(l->data); | |
384 | } | |
385 | ||
386 | /* 2ms isn't enough for reliable transfer with pl2303, let's try 10 */ | |
387 | usleep(10000); | |
388 | ||
389 | g_poll(fds, devcnt, 1); | |
390 | ||
391 | for (i = 0; i < devcnt; i++) { | |
392 | if (fds[i].revents != G_IO_IN) | |
393 | continue; | |
394 | if (serial_read(fds[i].fd, buf, 4) != 4) | |
395 | continue; | |
396 | if (strncmp(buf, "1SLO", 4) && strncmp(buf, "1ALS", 4)) | |
397 | continue; | |
398 | ||
399 | /* definitely using the OLS protocol, check if it supports | |
400 | * the metadata command | |
401 | */ | |
402 | send_shortcommand(fds[i].fd, CMD_METADATA); | |
403 | probefd.fd = fds[i].fd; | |
404 | probefd.events = G_IO_IN; | |
405 | if (g_poll(&probefd, 1, 10) > 0) { | |
406 | /* got metadata */ | |
407 | sdi = get_metadata(fds[i].fd); | |
408 | sdi->index = final_devcnt; | |
409 | } else { | |
410 | /* not an OLS -- some other board that uses the sump protocol */ | |
411 | sdi = sr_device_instance_new(final_devcnt, SR_ST_INACTIVE, | |
412 | "Sump", "Logic Analyzer", "v1.0"); | |
413 | ols = ols_device_new(); | |
414 | ols->num_probes = 32; | |
415 | sdi->priv = ols; | |
416 | } | |
417 | sdi->serial = sr_serial_device_instance_new(device_names[i], -1); | |
418 | device_instances = g_slist_append(device_instances, sdi); | |
419 | final_devcnt++; | |
420 | serial_close(fds[i].fd); | |
421 | fds[i].fd = 0; | |
422 | } | |
423 | ||
424 | /* clean up after all the probing */ | |
425 | for (i = 0; i < devcnt; i++) { | |
426 | if (fds[i].fd != 0) { | |
427 | serial_restore_params(fds[i].fd, serial_params[i]); | |
428 | serial_close(fds[i].fd); | |
429 | } | |
430 | free(serial_params[i]); | |
431 | free(device_names[i]); | |
432 | } | |
433 | ||
434 | g_free(serial_params); | |
435 | hw_init_free_device_names: | |
436 | g_free(device_names); | |
437 | hw_init_free_fds: | |
438 | g_free(fds); | |
439 | hw_init_free_ports: | |
440 | g_slist_free(ports); | |
441 | ||
442 | return final_devcnt; | |
443 | } | |
444 | ||
445 | static int hw_opendev(int device_index) | |
446 | { | |
447 | struct sr_device_instance *sdi; | |
448 | ||
449 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) | |
450 | return SR_ERR; | |
451 | ||
452 | sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR); | |
453 | if (sdi->serial->fd == -1) | |
454 | return SR_ERR; | |
455 | ||
456 | sdi->status = SR_ST_ACTIVE; | |
457 | ||
458 | return SR_OK; | |
459 | } | |
460 | ||
461 | static int hw_closedev(int device_index) | |
462 | { | |
463 | struct sr_device_instance *sdi; | |
464 | ||
465 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) { | |
466 | sr_err("ols: %s: sdi was NULL", __func__); | |
467 | return SR_ERR; /* TODO: SR_ERR_ARG? */ | |
468 | } | |
469 | ||
470 | /* TODO */ | |
471 | if (sdi->serial->fd != -1) { | |
472 | serial_close(sdi->serial->fd); | |
473 | sdi->serial->fd = -1; | |
474 | sdi->status = SR_ST_INACTIVE; | |
475 | } | |
476 | ||
477 | return SR_OK; | |
478 | } | |
479 | ||
480 | static void hw_cleanup(void) | |
481 | { | |
482 | GSList *l; | |
483 | struct sr_device_instance *sdi; | |
484 | ||
485 | /* Properly close all devices. */ | |
486 | for (l = device_instances; l; l = l->next) { | |
487 | sdi = l->data; | |
488 | if (sdi->serial->fd != -1) | |
489 | serial_close(sdi->serial->fd); | |
490 | sr_device_instance_free(sdi); | |
491 | } | |
492 | g_slist_free(device_instances); | |
493 | device_instances = NULL; | |
494 | } | |
495 | ||
496 | static void *hw_get_device_info(int device_index, int device_info_id) | |
497 | { | |
498 | struct sr_device_instance *sdi; | |
499 | struct ols_device *ols; | |
500 | void *info; | |
501 | ||
502 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) | |
503 | return NULL; | |
504 | ols = sdi->priv; | |
505 | ||
506 | info = NULL; | |
507 | switch (device_info_id) { | |
508 | case SR_DI_INSTANCE: | |
509 | info = sdi; | |
510 | break; | |
511 | case SR_DI_NUM_PROBES: | |
512 | info = GINT_TO_POINTER(NUM_PROBES); | |
513 | break; | |
514 | case SR_DI_SAMPLERATES: | |
515 | info = &samplerates; | |
516 | break; | |
517 | case SR_DI_TRIGGER_TYPES: | |
518 | info = (char *)TRIGGER_TYPES; | |
519 | break; | |
520 | case SR_DI_CUR_SAMPLERATE: | |
521 | info = &ols->cur_samplerate; | |
522 | break; | |
523 | } | |
524 | ||
525 | return info; | |
526 | } | |
527 | ||
528 | static int hw_get_status(int device_index) | |
529 | { | |
530 | struct sr_device_instance *sdi; | |
531 | ||
532 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) | |
533 | return SR_ST_NOT_FOUND; | |
534 | ||
535 | return sdi->status; | |
536 | } | |
537 | ||
538 | static int *hw_get_capabilities(void) | |
539 | { | |
540 | return capabilities; | |
541 | } | |
542 | ||
543 | static int set_configuration_samplerate(struct sr_device_instance *sdi, | |
544 | uint64_t samplerate) | |
545 | { | |
546 | struct ols_device *ols; | |
547 | ||
548 | ols = sdi->priv; | |
549 | if (ols->max_samplerate) { | |
550 | if (samplerate > ols->max_samplerate) | |
551 | return SR_ERR_SAMPLERATE; | |
552 | } else if (samplerate < samplerates.low || samplerate > samplerates.high) | |
553 | return SR_ERR_SAMPLERATE; | |
554 | ||
555 | ols->cur_samplerate = samplerate; | |
556 | if (samplerate > CLOCK_RATE) { | |
557 | ols->flag_reg |= FLAG_DEMUX; | |
558 | ols->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1; | |
559 | } else { | |
560 | ols->flag_reg &= ~FLAG_DEMUX; | |
561 | ols->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1; | |
562 | } | |
563 | ||
564 | return SR_OK; | |
565 | } | |
566 | ||
567 | static int hw_set_configuration(int device_index, int capability, void *value) | |
568 | { | |
569 | struct sr_device_instance *sdi; | |
570 | struct ols_device *ols; | |
571 | int ret; | |
572 | uint64_t *tmp_u64; | |
573 | ||
574 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) | |
575 | return SR_ERR; | |
576 | ols = sdi->priv; | |
577 | ||
578 | if (sdi->status != SR_ST_ACTIVE) | |
579 | return SR_ERR; | |
580 | ||
581 | switch (capability) { | |
582 | case SR_HWCAP_SAMPLERATE: | |
583 | tmp_u64 = value; | |
584 | ret = set_configuration_samplerate(sdi, *tmp_u64); | |
585 | break; | |
586 | case SR_HWCAP_PROBECONFIG: | |
587 | ret = configure_probes(ols, (GSList *) value); | |
588 | break; | |
589 | case SR_HWCAP_LIMIT_SAMPLES: | |
590 | tmp_u64 = value; | |
591 | if (*tmp_u64 < MIN_NUM_SAMPLES) | |
592 | return SR_ERR; | |
593 | ols->limit_samples = *tmp_u64; | |
594 | sr_info("ols: sample limit %" PRIu64, ols->limit_samples); | |
595 | ret = SR_OK; | |
596 | break; | |
597 | case SR_HWCAP_CAPTURE_RATIO: | |
598 | tmp_u64 = value; | |
599 | ols->capture_ratio = *tmp_u64; | |
600 | if (ols->capture_ratio < 0 || ols->capture_ratio > 100) { | |
601 | ols->capture_ratio = 0; | |
602 | ret = SR_ERR; | |
603 | } else | |
604 | ret = SR_OK; | |
605 | break; | |
606 | default: | |
607 | ret = SR_ERR; | |
608 | } | |
609 | ||
610 | return ret; | |
611 | } | |
612 | ||
613 | static int receive_data(int fd, int revents, void *user_data) | |
614 | { | |
615 | struct sr_datafeed_packet packet; | |
616 | struct sr_device_instance *sdi; | |
617 | struct ols_device *ols; | |
618 | GSList *l; | |
619 | int count, buflen, num_channels, offset, i, j; | |
620 | unsigned char byte, *buffer; | |
621 | ||
622 | /* find this device's ols_device struct by its fd */ | |
623 | ols = NULL; | |
624 | for (l = device_instances; l; l = l->next) { | |
625 | sdi = l->data; | |
626 | if (sdi->serial->fd == fd) { | |
627 | ols = sdi->priv; | |
628 | break; | |
629 | } | |
630 | } | |
631 | if (!ols) | |
632 | /* shouldn't happen */ | |
633 | return TRUE; | |
634 | ||
635 | if (ols->num_transfers++ == 0) { | |
636 | /* | |
637 | * First time round, means the device started sending data, | |
638 | * and will not stop until done. If it stops sending for | |
639 | * longer than it takes to send a byte, that means it's | |
640 | * finished. We'll double that to 30ms to be sure... | |
641 | */ | |
642 | sr_source_remove(fd); | |
643 | sr_source_add(fd, G_IO_IN, 30, receive_data, user_data); | |
644 | ols->raw_sample_buf = g_try_malloc(ols->limit_samples * 4); | |
645 | if (!ols->raw_sample_buf) { | |
646 | sr_err("ols: %s: ols->raw_sample_buf malloc failed", | |
647 | __func__); | |
648 | return FALSE; | |
649 | } | |
650 | /* fill with 1010... for debugging */ | |
651 | memset(ols->raw_sample_buf, 0x82, ols->limit_samples * 4); | |
652 | } | |
653 | ||
654 | num_channels = 0; | |
655 | for (i = 0x20; i > 0x02; i /= 2) { | |
656 | if ((ols->flag_reg & i) == 0) | |
657 | num_channels++; | |
658 | } | |
659 | ||
660 | if (revents == G_IO_IN | |
661 | && ols->num_transfers / num_channels <= ols->limit_samples) { | |
662 | if (serial_read(fd, &byte, 1) != 1) | |
663 | return FALSE; | |
664 | ||
665 | ols->sample[ols->num_bytes++] = byte; | |
666 | sr_dbg("ols: received byte 0x%.2x", byte); | |
667 | if (ols->num_bytes == num_channels) { | |
668 | /* Got a full sample. */ | |
669 | sr_dbg("ols: received sample 0x%.*x", | |
670 | ols->num_bytes * 2, (int) *ols->sample); | |
671 | if (ols->flag_reg & FLAG_RLE) { | |
672 | /* | |
673 | * In RLE mode -1 should never come in as a | |
674 | * sample, because bit 31 is the "count" flag. | |
675 | * TODO: Endianness may be wrong here, could be | |
676 | * sample[3]. | |
677 | */ | |
678 | if (ols->sample[0] & 0x80 | |
679 | && !(ols->last_sample[0] & 0x80)) { | |
680 | count = (int)(*ols->sample) & 0x7fffffff; | |
681 | if (!(buffer = g_try_malloc(count))) { | |
682 | sr_err("ols: %s: buffer malloc " | |
683 | "failed", __func__); | |
684 | return FALSE; | |
685 | } | |
686 | ||
687 | buflen = 0; | |
688 | for (i = 0; i < count; i++) { | |
689 | memcpy(buffer + buflen, ols->last_sample, 4); | |
690 | buflen += 4; | |
691 | } | |
692 | } else { | |
693 | /* | |
694 | * Just a single sample, next sample | |
695 | * will probably be a count referring | |
696 | * to this -- but this one is still a | |
697 | * part of the stream. | |
698 | */ | |
699 | buffer = ols->sample; | |
700 | buflen = 4; | |
701 | } | |
702 | } else { | |
703 | /* No compression. */ | |
704 | buffer = ols->sample; | |
705 | buflen = 4; | |
706 | } | |
707 | ||
708 | if (num_channels < 4) { | |
709 | /* | |
710 | * Some channel groups may have been turned | |
711 | * off, to speed up transfer between the | |
712 | * hardware and the PC. Expand that here before | |
713 | * submitting it over the session bus -- | |
714 | * whatever is listening on the bus will be | |
715 | * expecting a full 32-bit sample, based on | |
716 | * the number of probes. | |
717 | */ | |
718 | j = 0; | |
719 | memset(ols->tmp_sample, 0, 4); | |
720 | for (i = 0; i < 4; i++) { | |
721 | if (((ols->flag_reg >> 2) & (1 << i)) == 0) { | |
722 | /* | |
723 | * This channel group was | |
724 | * enabled, copy from received | |
725 | * sample. | |
726 | */ | |
727 | ols->tmp_sample[i] = ols->sample[j++]; | |
728 | } | |
729 | } | |
730 | memcpy(ols->sample, ols->tmp_sample, 4); | |
731 | sr_dbg("ols: full sample 0x%.8x", (int) *ols->sample); | |
732 | } | |
733 | ||
734 | /* the OLS sends its sample buffer backwards. | |
735 | * store it in reverse order here, so we can dump | |
736 | * this on the session bus later. | |
737 | */ | |
738 | offset = (ols->limit_samples - ols->num_transfers / num_channels) * 4; | |
739 | memcpy(ols->raw_sample_buf + offset, ols->sample, 4); | |
740 | ||
741 | if (buffer == ols->sample) | |
742 | memcpy(ols->last_sample, buffer, num_channels); | |
743 | else | |
744 | g_free(buffer); | |
745 | ||
746 | memset(ols->sample, 0, 4); | |
747 | ols->num_bytes = 0; | |
748 | } | |
749 | } else { | |
750 | /* | |
751 | * This is the main loop telling us a timeout was reached, or | |
752 | * we've acquired all the samples we asked for -- we're done. | |
753 | * Send the (properly-ordered) buffer to the frontend. | |
754 | */ | |
755 | if (ols->trigger_at != -1) { | |
756 | /* a trigger was set up, so we need to tell the frontend | |
757 | * about it. | |
758 | */ | |
759 | if (ols->trigger_at > 0) { | |
760 | /* there are pre-trigger samples, send those first */ | |
761 | packet.type = SR_DF_LOGIC; | |
762 | packet.length = ols->trigger_at * 4; | |
763 | packet.unitsize = 4; | |
764 | packet.payload = ols->raw_sample_buf; | |
765 | sr_session_bus(user_data, &packet); | |
766 | } | |
767 | ||
768 | packet.type = SR_DF_TRIGGER; | |
769 | packet.length = 0; | |
770 | sr_session_bus(user_data, &packet); | |
771 | ||
772 | packet.type = SR_DF_LOGIC; | |
773 | packet.length = (ols->limit_samples * 4) - (ols->trigger_at * 4); | |
774 | packet.unitsize = 4; | |
775 | packet.payload = ols->raw_sample_buf + ols->trigger_at * 4; | |
776 | sr_session_bus(user_data, &packet); | |
777 | } else { | |
778 | packet.type = SR_DF_LOGIC; | |
779 | packet.length = ols->limit_samples * 4; | |
780 | packet.unitsize = 4; | |
781 | packet.payload = ols->raw_sample_buf; | |
782 | sr_session_bus(user_data, &packet); | |
783 | } | |
784 | g_free(ols->raw_sample_buf); | |
785 | ||
786 | serial_flush(fd); | |
787 | serial_close(fd); | |
788 | packet.type = SR_DF_END; | |
789 | packet.length = 0; | |
790 | sr_session_bus(user_data, &packet); | |
791 | } | |
792 | ||
793 | return TRUE; | |
794 | } | |
795 | ||
796 | static int hw_start_acquisition(int device_index, gpointer session_device_id) | |
797 | { | |
798 | struct sr_datafeed_packet *packet; | |
799 | struct sr_datafeed_header *header; | |
800 | struct sr_device_instance *sdi; | |
801 | struct ols_device *ols; | |
802 | uint32_t trigger_config[4]; | |
803 | uint32_t data; | |
804 | uint16_t readcount, delaycount; | |
805 | uint8_t changrp_mask; | |
806 | int i; | |
807 | ||
808 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) | |
809 | return SR_ERR; | |
810 | ||
811 | ols = sdi->priv; | |
812 | ||
813 | if (sdi->status != SR_ST_ACTIVE) | |
814 | return SR_ERR; | |
815 | ||
816 | readcount = ols->limit_samples / 4; | |
817 | ||
818 | memset(trigger_config, 0, 16); | |
819 | trigger_config[ols->num_stages - 1] |= 0x08; | |
820 | if (ols->trigger_mask[0]) { | |
821 | delaycount = readcount * (1 - ols->capture_ratio / 100.0); | |
822 | ols->trigger_at = (readcount - delaycount) * 4 - ols->num_stages; | |
823 | ||
824 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0, | |
825 | reverse32(ols->trigger_mask[0])) != SR_OK) | |
826 | return SR_ERR; | |
827 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0, | |
828 | reverse32(ols->trigger_value[0])) != SR_OK) | |
829 | return SR_ERR; | |
830 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0, | |
831 | trigger_config[0]) != SR_OK) | |
832 | return SR_ERR; | |
833 | ||
834 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_1, | |
835 | reverse32(ols->trigger_mask[1])) != SR_OK) | |
836 | return SR_ERR; | |
837 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_1, | |
838 | reverse32(ols->trigger_value[1])) != SR_OK) | |
839 | return SR_ERR; | |
840 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_1, | |
841 | trigger_config[1]) != SR_OK) | |
842 | return SR_ERR; | |
843 | ||
844 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_2, | |
845 | reverse32(ols->trigger_mask[2])) != SR_OK) | |
846 | return SR_ERR; | |
847 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_2, | |
848 | reverse32(ols->trigger_value[2])) != SR_OK) | |
849 | return SR_ERR; | |
850 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_2, | |
851 | trigger_config[2]) != SR_OK) | |
852 | return SR_ERR; | |
853 | ||
854 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_3, | |
855 | reverse32(ols->trigger_mask[3])) != SR_OK) | |
856 | return SR_ERR; | |
857 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_3, | |
858 | reverse32(ols->trigger_value[3])) != SR_OK) | |
859 | return SR_ERR; | |
860 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_3, | |
861 | trigger_config[3]) != SR_OK) | |
862 | return SR_ERR; | |
863 | } else { | |
864 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0, | |
865 | ols->trigger_mask[0]) != SR_OK) | |
866 | return SR_ERR; | |
867 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0, | |
868 | ols->trigger_value[0]) != SR_OK) | |
869 | return SR_ERR; | |
870 | if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0, | |
871 | 0x00000008) != SR_OK) | |
872 | return SR_ERR; | |
873 | delaycount = readcount; | |
874 | } | |
875 | ||
876 | sr_info("ols: setting samplerate to %" PRIu64 " Hz (divider %u, " | |
877 | "demux %s)", ols->cur_samplerate, ols->cur_samplerate_divider, | |
878 | ols->flag_reg & FLAG_DEMUX ? "on" : "off"); | |
879 | if (send_longcommand(sdi->serial->fd, CMD_SET_DIVIDER, | |
880 | reverse32(ols->cur_samplerate_divider)) != SR_OK) | |
881 | return SR_ERR; | |
882 | ||
883 | /* Send sample limit and pre/post-trigger capture ratio. */ | |
884 | data = ((readcount - 1) & 0xffff) << 16; | |
885 | data |= (delaycount - 1) & 0xffff; | |
886 | if (send_longcommand(sdi->serial->fd, CMD_CAPTURE_SIZE, reverse16(data)) != SR_OK) | |
887 | return SR_ERR; | |
888 | ||
889 | /* | |
890 | * Enable/disable channel groups in the flag register according to the | |
891 | * probe mask. | |
892 | */ | |
893 | changrp_mask = 0; | |
894 | for (i = 0; i < 4; i++) { | |
895 | if (ols->probe_mask & (0xff << (i * 8))) | |
896 | changrp_mask |= (1 << i); | |
897 | } | |
898 | ||
899 | /* The flag register wants them here, and 1 means "disable channel". */ | |
900 | ols->flag_reg |= ~(changrp_mask << 2) & 0x3c; | |
901 | ols->flag_reg |= FLAG_FILTER; | |
902 | data = ols->flag_reg << 24; | |
903 | if (send_longcommand(sdi->serial->fd, CMD_SET_FLAGS, data) != SR_OK) | |
904 | return SR_ERR; | |
905 | ||
906 | /* Start acquisition on the device. */ | |
907 | if (send_shortcommand(sdi->serial->fd, CMD_RUN) != SR_OK) | |
908 | return SR_ERR; | |
909 | ||
910 | sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, | |
911 | session_device_id); | |
912 | ||
913 | if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) { | |
914 | sr_err("ols: %s: packet malloc failed", __func__); | |
915 | return SR_ERR_MALLOC; | |
916 | } | |
917 | ||
918 | if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) { | |
919 | sr_err("ols: %s: header malloc failed", __func__); | |
920 | g_free(packet); | |
921 | return SR_ERR_MALLOC; | |
922 | } | |
923 | ||
924 | /* Send header packet to the session bus. */ | |
925 | packet->type = SR_DF_HEADER; | |
926 | packet->length = sizeof(struct sr_datafeed_header); | |
927 | packet->payload = (unsigned char *)header; | |
928 | header->feed_version = 1; | |
929 | gettimeofday(&header->starttime, NULL); | |
930 | header->samplerate = ols->cur_samplerate; | |
931 | header->protocol_id = SR_PROTO_RAW; | |
932 | header->num_logic_probes = NUM_PROBES; | |
933 | header->num_analog_probes = 0; | |
934 | sr_session_bus(session_device_id, packet); | |
935 | ||
936 | g_free(header); | |
937 | g_free(packet); | |
938 | ||
939 | return SR_OK; | |
940 | } | |
941 | ||
942 | static void hw_stop_acquisition(int device_index, gpointer session_device_id) | |
943 | { | |
944 | struct sr_datafeed_packet packet; | |
945 | ||
946 | /* Avoid compiler warnings. */ | |
947 | device_index = device_index; | |
948 | ||
949 | packet.type = SR_DF_END; | |
950 | packet.length = 0; | |
951 | sr_session_bus(session_device_id, &packet); | |
952 | } | |
953 | ||
954 | struct sr_device_plugin ols_plugin_info = { | |
955 | .name = "ols", | |
956 | .longname = "Openbench Logic Sniffer", | |
957 | .api_version = 1, | |
958 | .init = hw_init, | |
959 | .cleanup = hw_cleanup, | |
960 | .opendev = hw_opendev, | |
961 | .closedev = hw_closedev, | |
962 | .get_device_info = hw_get_device_info, | |
963 | .get_status = hw_get_status, | |
964 | .get_capabilities = hw_get_capabilities, | |
965 | .set_configuration = hw_set_configuration, | |
966 | .start_acquisition = hw_start_acquisition, | |
967 | .stop_acquisition = hw_stop_acquisition, | |
968 | }; |