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