]>
Commit | Line | Data |
---|---|---|
0aba65da UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2010-2012 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 "protocol.h" | |
21 | ||
22 | #define SERIALCOMM "115200/8n1" | |
23 | ||
24 | static const int hwcaps[] = { | |
25 | SR_HWCAP_LOGIC_ANALYZER, | |
26 | SR_HWCAP_SAMPLERATE, | |
27 | SR_HWCAP_CAPTURE_RATIO, | |
28 | SR_HWCAP_LIMIT_SAMPLES, | |
29 | SR_HWCAP_RLE, | |
30 | 0, | |
31 | }; | |
32 | ||
33 | /* Probes are numbered 0-31 (on the PCB silkscreen). */ | |
34 | SR_PRIV const char *ols_probe_names[NUM_PROBES + 1] = { | |
35 | "0", | |
36 | "1", | |
37 | "2", | |
38 | "3", | |
39 | "4", | |
40 | "5", | |
41 | "6", | |
42 | "7", | |
43 | "8", | |
44 | "9", | |
45 | "10", | |
46 | "11", | |
47 | "12", | |
48 | "13", | |
49 | "14", | |
50 | "15", | |
51 | "16", | |
52 | "17", | |
53 | "18", | |
54 | "19", | |
55 | "20", | |
56 | "21", | |
57 | "22", | |
58 | "23", | |
59 | "24", | |
60 | "25", | |
61 | "26", | |
62 | "27", | |
63 | "28", | |
64 | "29", | |
65 | "30", | |
66 | "31", | |
67 | NULL, | |
68 | }; | |
69 | ||
70 | /* default supported samplerates, can be overridden by device metadata */ | |
71 | static const struct sr_samplerates samplerates = { | |
72 | SR_HZ(10), | |
73 | SR_MHZ(200), | |
74 | SR_HZ(1), | |
75 | NULL, | |
76 | }; | |
77 | ||
78 | SR_PRIV struct sr_dev_driver ols_driver_info; | |
79 | static struct sr_dev_driver *di = &ols_driver_info; | |
80 | ||
81 | static int hw_init(struct sr_context *sr_ctx) | |
82 | { | |
83 | struct drv_context *drvc; | |
84 | ||
85 | if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { | |
86 | sr_err("Driver context malloc failed."); | |
87 | return SR_ERR_MALLOC; | |
88 | } | |
89 | drvc->sr_ctx = sr_ctx; | |
90 | di->priv = drvc; | |
91 | ||
92 | return SR_OK; | |
93 | } | |
94 | ||
95 | static GSList *hw_scan(GSList *options) | |
96 | { | |
97 | struct sr_hwopt *opt; | |
98 | struct sr_dev_inst *sdi; | |
99 | struct drv_context *drvc; | |
100 | struct dev_context *devc; | |
101 | struct sr_probe *probe; | |
102 | struct sr_serial_dev_inst *serial; | |
103 | GPollFD probefd; | |
104 | GSList *l, *devices; | |
105 | int ret, i; | |
106 | const char *conn, *serialcomm; | |
107 | char buf[8]; | |
108 | ||
109 | (void)options; | |
110 | drvc = di->priv; | |
111 | devices = NULL; | |
112 | ||
113 | conn = serialcomm = NULL; | |
114 | for (l = options; l; l = l->next) { | |
115 | opt = l->data; | |
116 | switch (opt->hwopt) { | |
117 | case SR_HWOPT_CONN: | |
118 | conn = opt->value; | |
119 | break; | |
120 | case SR_HWOPT_SERIALCOMM: | |
121 | serialcomm = opt->value; | |
122 | break; | |
123 | } | |
124 | } | |
125 | if (!conn) | |
126 | return NULL; | |
127 | ||
128 | if (serialcomm == NULL) | |
129 | serialcomm = SERIALCOMM; | |
130 | ||
131 | if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) | |
132 | return NULL; | |
133 | ||
134 | /* The discovery procedure is like this: first send the Reset | |
135 | * command (0x00) 5 times, since the device could be anywhere | |
136 | * in a 5-byte command. Then send the ID command (0x02). | |
137 | * If the device responds with 4 bytes ("OLS1" or "SLA1"), we | |
138 | * have a match. | |
139 | */ | |
140 | sr_info("Probing %s.", conn); | |
141 | if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) | |
142 | return NULL; | |
143 | ||
144 | ret = SR_OK; | |
145 | for (i = 0; i < 5; i++) { | |
146 | if ((ret = send_shortcommand(serial, CMD_RESET)) != SR_OK) { | |
147 | sr_err("Port %s is not writable.", conn); | |
148 | break; | |
149 | } | |
150 | } | |
151 | if (ret != SR_OK) { | |
152 | serial_close(serial); | |
153 | sr_err("Could not use port %s. Quitting.", conn); | |
154 | return NULL; | |
155 | } | |
156 | send_shortcommand(serial, CMD_ID); | |
157 | ||
158 | /* Wait 10ms for a response. */ | |
159 | g_usleep(10000); | |
160 | ||
161 | probefd.fd = serial->fd; | |
162 | probefd.events = G_IO_IN; | |
163 | g_poll(&probefd, 1, 1); | |
164 | ||
165 | if (probefd.revents != G_IO_IN) | |
166 | return NULL; | |
167 | if (serial_read(serial, buf, 4) != 4) | |
168 | return NULL; | |
169 | if (strncmp(buf, "1SLO", 4) && strncmp(buf, "1ALS", 4)) | |
170 | return NULL; | |
171 | ||
172 | /* Definitely using the OLS protocol, check if it supports | |
173 | * the metadata command. | |
174 | */ | |
175 | send_shortcommand(serial, CMD_METADATA); | |
176 | if (g_poll(&probefd, 1, 10) > 0) { | |
177 | /* Got metadata. */ | |
178 | sdi = get_metadata(serial); | |
179 | sdi->index = 0; | |
180 | devc = sdi->priv; | |
181 | } else { | |
182 | /* Not an OLS -- some other board that uses the sump protocol. */ | |
183 | sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, | |
184 | "Sump", "Logic Analyzer", "v1.0"); | |
185 | sdi->driver = di; | |
186 | devc = ols_dev_new(); | |
187 | for (i = 0; i < 32; i++) { | |
188 | if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, | |
189 | ols_probe_names[i]))) | |
190 | return 0; | |
191 | sdi->probes = g_slist_append(sdi->probes, probe); | |
192 | } | |
193 | sdi->priv = devc; | |
194 | } | |
195 | devc->serial = serial; | |
196 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
197 | devices = g_slist_append(devices, sdi); | |
198 | ||
199 | serial_close(serial); | |
200 | ||
201 | return devices; | |
202 | } | |
203 | ||
204 | static GSList *hw_dev_list(void) | |
205 | { | |
206 | struct drv_context *drvc; | |
207 | ||
208 | drvc = di->priv; | |
209 | ||
210 | return drvc->instances; | |
211 | } | |
212 | ||
213 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
214 | { | |
215 | struct dev_context *devc; | |
216 | ||
217 | devc = sdi->priv; | |
218 | ||
219 | if (serial_open(devc->serial, SERIAL_RDWR) != SR_OK) | |
220 | return SR_ERR; | |
221 | ||
222 | sdi->status = SR_ST_ACTIVE; | |
223 | ||
224 | return SR_OK; | |
225 | } | |
226 | ||
227 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
228 | { | |
229 | struct dev_context *devc; | |
230 | ||
231 | devc = sdi->priv; | |
232 | ||
233 | if (devc->serial && devc->serial->fd != -1) { | |
234 | serial_close(devc->serial); | |
235 | sdi->status = SR_ST_INACTIVE; | |
236 | } | |
237 | ||
238 | return SR_OK; | |
239 | } | |
240 | ||
241 | static int hw_cleanup(void) | |
242 | { | |
243 | GSList *l; | |
244 | struct sr_dev_inst *sdi; | |
245 | struct drv_context *drvc; | |
246 | struct dev_context *devc; | |
247 | int ret = SR_OK; | |
248 | ||
249 | if (!(drvc = di->priv)) | |
250 | return SR_OK; | |
251 | ||
252 | /* Properly close and free all devices. */ | |
253 | for (l = drvc->instances; l; l = l->next) { | |
254 | if (!(sdi = l->data)) { | |
255 | /* Log error, but continue cleaning up the rest. */ | |
256 | sr_err("%s: sdi was NULL, continuing", __func__); | |
257 | ret = SR_ERR_BUG; | |
258 | continue; | |
259 | } | |
260 | if (!(devc = sdi->priv)) { | |
261 | /* Log error, but continue cleaning up the rest. */ | |
262 | sr_err("%s: sdi->priv was NULL, continuing", __func__); | |
263 | ret = SR_ERR_BUG; | |
264 | continue; | |
265 | } | |
266 | hw_dev_close(sdi); | |
267 | sr_serial_dev_inst_free(devc->serial); | |
268 | sr_dev_inst_free(sdi); | |
269 | } | |
270 | g_slist_free(drvc->instances); | |
271 | drvc->instances = NULL; | |
272 | ||
273 | return ret; | |
274 | } | |
275 | ||
276 | static int hw_info_get(int info_id, const void **data, | |
277 | const struct sr_dev_inst *sdi) | |
278 | { | |
279 | struct dev_context *devc; | |
280 | ||
281 | switch (info_id) { | |
282 | case SR_DI_HWCAPS: | |
283 | *data = hwcaps; | |
284 | break; | |
285 | case SR_DI_NUM_PROBES: | |
286 | *data = GINT_TO_POINTER(1); | |
287 | break; | |
288 | case SR_DI_PROBE_NAMES: | |
289 | *data = ols_probe_names; | |
290 | break; | |
291 | case SR_DI_SAMPLERATES: | |
292 | *data = &samplerates; | |
293 | break; | |
294 | case SR_DI_TRIGGER_TYPES: | |
295 | *data = (char *)TRIGGER_TYPES; | |
296 | break; | |
297 | case SR_DI_CUR_SAMPLERATE: | |
298 | if (sdi) { | |
299 | devc = sdi->priv; | |
300 | *data = &devc->cur_samplerate; | |
301 | } else | |
302 | return SR_ERR; | |
303 | break; | |
304 | default: | |
305 | return SR_ERR_ARG; | |
306 | } | |
307 | ||
308 | return SR_OK; | |
309 | } | |
310 | ||
311 | static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, | |
312 | const void *value) | |
313 | { | |
314 | struct dev_context *devc; | |
315 | int ret; | |
316 | const uint64_t *tmp_u64; | |
317 | ||
318 | devc = sdi->priv; | |
319 | ||
320 | if (sdi->status != SR_ST_ACTIVE) | |
321 | return SR_ERR; | |
322 | ||
323 | switch (hwcap) { | |
324 | case SR_HWCAP_SAMPLERATE: | |
325 | ret = ols_set_samplerate(sdi, *(const uint64_t *)value, | |
326 | &samplerates); | |
327 | break; | |
328 | case SR_HWCAP_LIMIT_SAMPLES: | |
329 | tmp_u64 = value; | |
330 | if (*tmp_u64 < MIN_NUM_SAMPLES) | |
331 | return SR_ERR; | |
332 | if (*tmp_u64 > devc->max_samples) | |
333 | sr_err("Sample limit exceeds hardware maximum."); | |
334 | devc->limit_samples = *tmp_u64; | |
335 | sr_info("Sample limit is %" PRIu64 ".", devc->limit_samples); | |
336 | ret = SR_OK; | |
337 | break; | |
338 | case SR_HWCAP_CAPTURE_RATIO: | |
339 | devc->capture_ratio = *(const uint64_t *)value; | |
340 | if (devc->capture_ratio < 0 || devc->capture_ratio > 100) { | |
341 | devc->capture_ratio = 0; | |
342 | ret = SR_ERR; | |
343 | } else | |
344 | ret = SR_OK; | |
345 | break; | |
346 | case SR_HWCAP_RLE: | |
347 | if (GPOINTER_TO_INT(value)) { | |
348 | sr_info("Enabling RLE."); | |
349 | devc->flag_reg |= FLAG_RLE; | |
350 | } | |
351 | ret = SR_OK; | |
352 | break; | |
353 | default: | |
354 | ret = SR_ERR; | |
355 | } | |
356 | ||
357 | return ret; | |
358 | } | |
359 | ||
360 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, | |
361 | void *cb_data) | |
362 | { | |
363 | struct sr_datafeed_packet *packet; | |
364 | struct sr_datafeed_header *header; | |
365 | struct sr_datafeed_meta_logic meta; | |
366 | struct dev_context *devc; | |
367 | uint32_t trigger_config[4]; | |
368 | uint32_t data; | |
369 | uint16_t readcount, delaycount; | |
370 | uint8_t changrp_mask; | |
371 | int num_channels; | |
372 | int i; | |
373 | ||
374 | devc = sdi->priv; | |
375 | ||
376 | if (sdi->status != SR_ST_ACTIVE) | |
377 | return SR_ERR; | |
378 | ||
379 | if (ols_configure_probes(sdi) != SR_OK) { | |
380 | sr_err("Failed to configure probes."); | |
381 | return SR_ERR; | |
382 | } | |
383 | ||
384 | /* | |
385 | * Enable/disable channel groups in the flag register according to the | |
386 | * probe mask. Calculate this here, because num_channels is needed | |
387 | * to limit readcount. | |
388 | */ | |
389 | changrp_mask = 0; | |
390 | num_channels = 0; | |
391 | for (i = 0; i < 4; i++) { | |
392 | if (devc->probe_mask & (0xff << (i * 8))) { | |
393 | changrp_mask |= (1 << i); | |
394 | num_channels++; | |
395 | } | |
396 | } | |
397 | ||
398 | /* | |
399 | * Limit readcount to prevent reading past the end of the hardware | |
400 | * buffer. | |
401 | */ | |
402 | readcount = MIN(devc->max_samples / num_channels, devc->limit_samples) / 4; | |
403 | ||
404 | memset(trigger_config, 0, 16); | |
405 | trigger_config[devc->num_stages - 1] |= 0x08; | |
406 | if (devc->trigger_mask[0]) { | |
407 | delaycount = readcount * (1 - devc->capture_ratio / 100.0); | |
408 | devc->trigger_at = (readcount - delaycount) * 4 - devc->num_stages; | |
409 | ||
410 | if (send_longcommand(devc->serial, CMD_SET_TRIGGER_MASK_0, | |
411 | reverse32(devc->trigger_mask[0])) != SR_OK) | |
412 | return SR_ERR; | |
413 | if (send_longcommand(devc->serial, CMD_SET_TRIGGER_VALUE_0, | |
414 | reverse32(devc->trigger_value[0])) != SR_OK) | |
415 | return SR_ERR; | |
416 | if (send_longcommand(devc->serial, CMD_SET_TRIGGER_CONFIG_0, | |
417 | trigger_config[0]) != SR_OK) | |
418 | return SR_ERR; | |
419 | ||
420 | if (send_longcommand(devc->serial, CMD_SET_TRIGGER_MASK_1, | |
421 | reverse32(devc->trigger_mask[1])) != SR_OK) | |
422 | return SR_ERR; | |
423 | if (send_longcommand(devc->serial, CMD_SET_TRIGGER_VALUE_1, | |
424 | reverse32(devc->trigger_value[1])) != SR_OK) | |
425 | return SR_ERR; | |
426 | if (send_longcommand(devc->serial, CMD_SET_TRIGGER_CONFIG_1, | |
427 | trigger_config[1]) != SR_OK) | |
428 | return SR_ERR; | |
429 | ||
430 | if (send_longcommand(devc->serial, CMD_SET_TRIGGER_MASK_2, | |
431 | reverse32(devc->trigger_mask[2])) != SR_OK) | |
432 | return SR_ERR; | |
433 | if (send_longcommand(devc->serial, CMD_SET_TRIGGER_VALUE_2, | |
434 | reverse32(devc->trigger_value[2])) != SR_OK) | |
435 | return SR_ERR; | |
436 | if (send_longcommand(devc->serial, CMD_SET_TRIGGER_CONFIG_2, | |
437 | trigger_config[2]) != SR_OK) | |
438 | return SR_ERR; | |
439 | ||
440 | if (send_longcommand(devc->serial, CMD_SET_TRIGGER_MASK_3, | |
441 | reverse32(devc->trigger_mask[3])) != SR_OK) | |
442 | return SR_ERR; | |
443 | if (send_longcommand(devc->serial, CMD_SET_TRIGGER_VALUE_3, | |
444 | reverse32(devc->trigger_value[3])) != SR_OK) | |
445 | return SR_ERR; | |
446 | if (send_longcommand(devc->serial, CMD_SET_TRIGGER_CONFIG_3, | |
447 | trigger_config[3]) != SR_OK) | |
448 | return SR_ERR; | |
449 | } else { | |
450 | if (send_longcommand(devc->serial, CMD_SET_TRIGGER_MASK_0, | |
451 | devc->trigger_mask[0]) != SR_OK) | |
452 | return SR_ERR; | |
453 | if (send_longcommand(devc->serial, CMD_SET_TRIGGER_VALUE_0, | |
454 | devc->trigger_value[0]) != SR_OK) | |
455 | return SR_ERR; | |
456 | if (send_longcommand(devc->serial, CMD_SET_TRIGGER_CONFIG_0, | |
457 | 0x00000008) != SR_OK) | |
458 | return SR_ERR; | |
459 | delaycount = readcount; | |
460 | } | |
461 | ||
462 | sr_info("Setting samplerate to %" PRIu64 "Hz (divider %u, " | |
463 | "demux %s)", devc->cur_samplerate, devc->cur_samplerate_divider, | |
464 | devc->flag_reg & FLAG_DEMUX ? "on" : "off"); | |
465 | if (send_longcommand(devc->serial, CMD_SET_DIVIDER, | |
466 | reverse32(devc->cur_samplerate_divider)) != SR_OK) | |
467 | return SR_ERR; | |
468 | ||
469 | /* Send sample limit and pre/post-trigger capture ratio. */ | |
470 | data = ((readcount - 1) & 0xffff) << 16; | |
471 | data |= (delaycount - 1) & 0xffff; | |
472 | if (send_longcommand(devc->serial, CMD_CAPTURE_SIZE, reverse16(data)) != SR_OK) | |
473 | return SR_ERR; | |
474 | ||
475 | /* The flag register wants them here, and 1 means "disable channel". */ | |
476 | devc->flag_reg |= ~(changrp_mask << 2) & 0x3c; | |
477 | devc->flag_reg |= FLAG_FILTER; | |
478 | devc->rle_count = 0; | |
479 | data = (devc->flag_reg << 24) | ((devc->flag_reg << 8) & 0xff0000); | |
480 | if (send_longcommand(devc->serial, CMD_SET_FLAGS, data) != SR_OK) | |
481 | return SR_ERR; | |
482 | ||
483 | /* Start acquisition on the device. */ | |
484 | if (send_shortcommand(devc->serial, CMD_RUN) != SR_OK) | |
485 | return SR_ERR; | |
486 | ||
487 | sr_source_add(devc->serial->fd, G_IO_IN, -1, ols_receive_data, | |
488 | cb_data); | |
489 | ||
490 | if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) { | |
491 | sr_err("Datafeed packet malloc failed."); | |
492 | return SR_ERR_MALLOC; | |
493 | } | |
494 | ||
495 | if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) { | |
496 | sr_err("Datafeed header malloc failed."); | |
497 | g_free(packet); | |
498 | return SR_ERR_MALLOC; | |
499 | } | |
500 | ||
501 | /* Send header packet to the session bus. */ | |
502 | packet->type = SR_DF_HEADER; | |
503 | packet->payload = (unsigned char *)header; | |
504 | header->feed_version = 1; | |
505 | gettimeofday(&header->starttime, NULL); | |
506 | sr_session_send(cb_data, packet); | |
507 | ||
508 | /* Send metadata about the SR_DF_LOGIC packets to come. */ | |
509 | packet->type = SR_DF_META_LOGIC; | |
510 | packet->payload = &meta; | |
511 | meta.samplerate = devc->cur_samplerate; | |
512 | meta.num_probes = NUM_PROBES; | |
513 | sr_session_send(cb_data, packet); | |
514 | ||
515 | g_free(header); | |
516 | g_free(packet); | |
517 | ||
518 | return SR_OK; | |
519 | } | |
520 | ||
521 | /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */ | |
522 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
523 | { | |
524 | /* Avoid compiler warnings. */ | |
525 | (void)cb_data; | |
526 | ||
527 | abort_acquisition(sdi); | |
528 | ||
529 | return SR_OK; | |
530 | } | |
531 | ||
532 | SR_PRIV struct sr_dev_driver ols_driver_info = { | |
533 | .name = "ols", | |
534 | .longname = "Openbench Logic Sniffer", | |
535 | .api_version = 1, | |
536 | .init = hw_init, | |
537 | .cleanup = hw_cleanup, | |
538 | .scan = hw_scan, | |
539 | .dev_list = hw_dev_list, | |
540 | .dev_clear = hw_cleanup, | |
541 | .dev_open = hw_dev_open, | |
542 | .dev_close = hw_dev_close, | |
543 | .info_get = hw_info_get, | |
544 | .dev_config_set = hw_dev_config_set, | |
545 | .dev_acquisition_start = hw_dev_acquisition_start, | |
546 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
547 | .priv = NULL, | |
548 | }; |