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