]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 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 <config.h> | |
21 | #include "protocol.h" | |
22 | ||
23 | #define SERIALCOMM "115200/8n1" | |
24 | ||
25 | static const uint32_t scanopts[] = { | |
26 | SR_CONF_CONN, | |
27 | SR_CONF_SERIALCOMM, | |
28 | }; | |
29 | ||
30 | static const uint32_t drvopts[] = { | |
31 | SR_CONF_LOGIC_ANALYZER, | |
32 | }; | |
33 | ||
34 | static const uint32_t devopts[] = { | |
35 | SR_CONF_CONN | SR_CONF_GET, | |
36 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
37 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
38 | SR_CONF_TRIGGER_MATCH | SR_CONF_LIST, | |
39 | SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET, | |
40 | SR_CONF_EXTERNAL_CLOCK | SR_CONF_GET | SR_CONF_SET, | |
41 | SR_CONF_CLOCK_EDGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
42 | SR_CONF_PATTERN_MODE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
43 | SR_CONF_SWAP | SR_CONF_SET, | |
44 | SR_CONF_RLE | SR_CONF_GET | SR_CONF_SET, | |
45 | }; | |
46 | ||
47 | static const int32_t trigger_matches[] = { | |
48 | SR_TRIGGER_ZERO, | |
49 | SR_TRIGGER_ONE, | |
50 | }; | |
51 | ||
52 | static const char *external_clock_edges[] = { | |
53 | "rising", /* positive edge */ | |
54 | "falling" /* negative edge */ | |
55 | }; | |
56 | ||
57 | #define STR_PATTERN_NONE "None" | |
58 | #define STR_PATTERN_EXTERNAL "External" | |
59 | #define STR_PATTERN_INTERNAL "Internal" | |
60 | ||
61 | /* Supported methods of test pattern outputs */ | |
62 | enum { | |
63 | /** | |
64 | * Capture pins 31:16 (unbuffered wing) output a test pattern | |
65 | * that can captured on pins 0:15. | |
66 | */ | |
67 | PATTERN_EXTERNAL, | |
68 | ||
69 | /** Route test pattern internally to capture buffer. */ | |
70 | PATTERN_INTERNAL, | |
71 | }; | |
72 | ||
73 | static const char *patterns[] = { | |
74 | STR_PATTERN_NONE, | |
75 | STR_PATTERN_EXTERNAL, | |
76 | STR_PATTERN_INTERNAL, | |
77 | }; | |
78 | ||
79 | /* Channels are numbered 0-31 (on the PCB silkscreen). */ | |
80 | SR_PRIV const char *ols_channel_names[] = { | |
81 | "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", | |
82 | "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", | |
83 | "24", "25", "26", "27", "28", "29", "30", "31", | |
84 | }; | |
85 | ||
86 | /* Default supported samplerates, can be overridden by device metadata. */ | |
87 | static const uint64_t samplerates[] = { | |
88 | SR_HZ(10), | |
89 | SR_MHZ(200), | |
90 | SR_HZ(1), | |
91 | }; | |
92 | ||
93 | #define RESPONSE_DELAY_US (20 * 1000) | |
94 | ||
95 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
96 | { | |
97 | struct sr_config *src; | |
98 | struct sr_dev_inst *sdi; | |
99 | struct sr_serial_dev_inst *serial; | |
100 | GSList *l; | |
101 | int num_read; | |
102 | unsigned int i; | |
103 | const char *conn, *serialcomm; | |
104 | char buf[4] = { 0, 0, 0, 0 }; | |
105 | ||
106 | conn = serialcomm = NULL; | |
107 | for (l = options; l; l = l->next) { | |
108 | src = l->data; | |
109 | switch (src->key) { | |
110 | case SR_CONF_CONN: | |
111 | conn = g_variant_get_string(src->data, NULL); | |
112 | break; | |
113 | case SR_CONF_SERIALCOMM: | |
114 | serialcomm = g_variant_get_string(src->data, NULL); | |
115 | break; | |
116 | } | |
117 | } | |
118 | if (!conn) | |
119 | return NULL; | |
120 | ||
121 | if (!serialcomm) | |
122 | serialcomm = SERIALCOMM; | |
123 | ||
124 | serial = sr_serial_dev_inst_new(conn, serialcomm); | |
125 | ||
126 | /* The discovery procedure is like this: first send the Reset | |
127 | * command (0x00) 5 times, since the device could be anywhere | |
128 | * in a 5-byte command. Then send the ID command (0x02). | |
129 | * If the device responds with 4 bytes ("OLS1" or "SLA1"), we | |
130 | * have a match. | |
131 | */ | |
132 | sr_info("Probing %s.", conn); | |
133 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
134 | return NULL; | |
135 | ||
136 | if (ols_send_reset(serial) != SR_OK) { | |
137 | serial_close(serial); | |
138 | sr_err("Could not use port %s. Quitting.", conn); | |
139 | return NULL; | |
140 | } | |
141 | send_shortcommand(serial, CMD_ID); | |
142 | ||
143 | g_usleep(RESPONSE_DELAY_US); | |
144 | ||
145 | if (serial_has_receive_data(serial) == 0) { | |
146 | sr_dbg("Didn't get any ID reply."); | |
147 | return NULL; | |
148 | } | |
149 | ||
150 | num_read = | |
151 | serial_read_blocking(serial, buf, 4, serial_timeout(serial, 4)); | |
152 | if (num_read < 0) { | |
153 | sr_err("Getting ID reply failed (%d).", num_read); | |
154 | return NULL; | |
155 | } | |
156 | ||
157 | if (strncmp(buf, "1SLO", 4) && strncmp(buf, "1ALS", 4)) { | |
158 | GString *id = sr_hexdump_new((uint8_t *)buf, num_read); | |
159 | ||
160 | sr_err("Invalid ID reply (got %s).", id->str); | |
161 | ||
162 | sr_hexdump_free(id); | |
163 | return NULL; | |
164 | } | |
165 | ||
166 | /* Definitely using the OLS protocol, check if it supports | |
167 | * the metadata command. | |
168 | */ | |
169 | send_shortcommand(serial, CMD_METADATA); | |
170 | ||
171 | g_usleep(RESPONSE_DELAY_US); | |
172 | ||
173 | if (serial_has_receive_data(serial) != 0) { | |
174 | /* Got metadata. */ | |
175 | sdi = get_metadata(serial); | |
176 | } else { | |
177 | /* Not an OLS -- some other board that uses the sump protocol. */ | |
178 | sr_info("Device does not support metadata."); | |
179 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
180 | sdi->status = SR_ST_INACTIVE; | |
181 | sdi->vendor = g_strdup("Sump"); | |
182 | sdi->model = g_strdup("Logic Analyzer"); | |
183 | sdi->version = g_strdup("v1.0"); | |
184 | for (i = 0; i < ARRAY_SIZE(ols_channel_names); i++) | |
185 | sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, | |
186 | ols_channel_names[i]); | |
187 | sdi->priv = ols_dev_new(); | |
188 | } | |
189 | /* Configure samplerate and divider. */ | |
190 | if (ols_set_samplerate(sdi, DEFAULT_SAMPLERATE) != SR_OK) | |
191 | sr_dbg("Failed to set default samplerate (%" PRIu64 ").", | |
192 | DEFAULT_SAMPLERATE); | |
193 | sdi->inst_type = SR_INST_SERIAL; | |
194 | sdi->conn = serial; | |
195 | sdi->connection_id = g_strdup(serial->port); | |
196 | ||
197 | serial_close(serial); | |
198 | ||
199 | return std_scan_complete(di, g_slist_append(NULL, sdi)); | |
200 | } | |
201 | ||
202 | static int config_get(uint32_t key, GVariant **data, | |
203 | const struct sr_dev_inst *sdi, | |
204 | const struct sr_channel_group *cg) | |
205 | { | |
206 | struct dev_context *devc; | |
207 | ||
208 | (void)cg; | |
209 | ||
210 | if (!sdi) | |
211 | return SR_ERR_ARG; | |
212 | ||
213 | devc = sdi->priv; | |
214 | ||
215 | switch (key) { | |
216 | case SR_CONF_CONN: | |
217 | if (!sdi->conn || !sdi->connection_id) | |
218 | return SR_ERR_NA; | |
219 | *data = g_variant_new_string(sdi->connection_id); | |
220 | break; | |
221 | case SR_CONF_SAMPLERATE: | |
222 | *data = g_variant_new_uint64(devc->cur_samplerate); | |
223 | break; | |
224 | case SR_CONF_CAPTURE_RATIO: | |
225 | *data = g_variant_new_uint64(devc->capture_ratio); | |
226 | break; | |
227 | case SR_CONF_LIMIT_SAMPLES: | |
228 | *data = g_variant_new_uint64(devc->limit_samples); | |
229 | break; | |
230 | case SR_CONF_PATTERN_MODE: | |
231 | if (devc->capture_flags & CAPTURE_FLAG_EXTERNAL_TEST_MODE) | |
232 | *data = g_variant_new_string(STR_PATTERN_EXTERNAL); | |
233 | else if (devc->capture_flags & CAPTURE_FLAG_INTERNAL_TEST_MODE) | |
234 | *data = g_variant_new_string(STR_PATTERN_INTERNAL); | |
235 | else | |
236 | *data = g_variant_new_string(STR_PATTERN_NONE); | |
237 | break; | |
238 | case SR_CONF_RLE: | |
239 | *data = g_variant_new_boolean( | |
240 | devc->capture_flags & CAPTURE_FLAG_RLE ? TRUE : FALSE); | |
241 | break; | |
242 | case SR_CONF_EXTERNAL_CLOCK: | |
243 | *data = g_variant_new_boolean( | |
244 | devc->capture_flags & CAPTURE_FLAG_CLOCK_EXTERNAL | |
245 | ? TRUE : FALSE); | |
246 | break; | |
247 | case SR_CONF_CLOCK_EDGE: | |
248 | *data = g_variant_new_string(external_clock_edges[ | |
249 | devc->capture_flags & CAPTURE_FLAG_INVERT_EXT_CLOCK | |
250 | ? 1 : 0]); | |
251 | break; | |
252 | default: | |
253 | return SR_ERR_NA; | |
254 | } | |
255 | ||
256 | return SR_OK; | |
257 | } | |
258 | ||
259 | static int config_set(uint32_t key, GVariant *data, | |
260 | const struct sr_dev_inst *sdi, | |
261 | const struct sr_channel_group *cg) | |
262 | { | |
263 | struct dev_context *devc; | |
264 | uint16_t flag; | |
265 | uint64_t tmp_u64; | |
266 | const char *stropt; | |
267 | ||
268 | (void)cg; | |
269 | ||
270 | devc = sdi->priv; | |
271 | ||
272 | switch (key) { | |
273 | case SR_CONF_SAMPLERATE: | |
274 | tmp_u64 = g_variant_get_uint64(data); | |
275 | if (tmp_u64 < samplerates[0] || tmp_u64 > samplerates[1]) | |
276 | return SR_ERR_SAMPLERATE; | |
277 | return ols_set_samplerate(sdi, g_variant_get_uint64(data)); | |
278 | case SR_CONF_LIMIT_SAMPLES: | |
279 | tmp_u64 = g_variant_get_uint64(data); | |
280 | if (tmp_u64 < MIN_NUM_SAMPLES) | |
281 | return SR_ERR; | |
282 | devc->limit_samples = tmp_u64; | |
283 | break; | |
284 | case SR_CONF_CAPTURE_RATIO: | |
285 | devc->capture_ratio = g_variant_get_uint64(data); | |
286 | break; | |
287 | case SR_CONF_EXTERNAL_CLOCK: | |
288 | if (g_variant_get_boolean(data)) { | |
289 | sr_info("Enabling external clock."); | |
290 | devc->capture_flags |= CAPTURE_FLAG_CLOCK_EXTERNAL; | |
291 | } else { | |
292 | sr_info("Disabled external clock."); | |
293 | devc->capture_flags &= ~CAPTURE_FLAG_CLOCK_EXTERNAL; | |
294 | } | |
295 | break; | |
296 | case SR_CONF_CLOCK_EDGE: | |
297 | stropt = g_variant_get_string(data, NULL); | |
298 | if (!strcmp(stropt, external_clock_edges[1])) { | |
299 | sr_info("Triggering on falling edge of external clock."); | |
300 | devc->capture_flags |= CAPTURE_FLAG_INVERT_EXT_CLOCK; | |
301 | } else { | |
302 | sr_info("Triggering on rising edge of external clock."); | |
303 | devc->capture_flags &= ~CAPTURE_FLAG_INVERT_EXT_CLOCK; | |
304 | } | |
305 | break; | |
306 | case SR_CONF_PATTERN_MODE: | |
307 | stropt = g_variant_get_string(data, NULL); | |
308 | if (!strcmp(stropt, STR_PATTERN_NONE)) { | |
309 | sr_info("Disabling test modes."); | |
310 | flag = 0x0000; | |
311 | } else if (!strcmp(stropt, STR_PATTERN_INTERNAL)) { | |
312 | sr_info("Enabling internal test mode."); | |
313 | flag = CAPTURE_FLAG_INTERNAL_TEST_MODE; | |
314 | } else if (!strcmp(stropt, STR_PATTERN_EXTERNAL)) { | |
315 | sr_info("Enabling external test mode."); | |
316 | flag = CAPTURE_FLAG_EXTERNAL_TEST_MODE; | |
317 | } else { | |
318 | return SR_ERR; | |
319 | } | |
320 | devc->capture_flags &= ~CAPTURE_FLAG_INTERNAL_TEST_MODE; | |
321 | devc->capture_flags &= ~CAPTURE_FLAG_EXTERNAL_TEST_MODE; | |
322 | devc->capture_flags |= flag; | |
323 | break; | |
324 | case SR_CONF_SWAP: | |
325 | if (g_variant_get_boolean(data)) { | |
326 | sr_info("Enabling channel swapping."); | |
327 | devc->capture_flags |= CAPTURE_FLAG_SWAP_CHANNELS; | |
328 | } else { | |
329 | sr_info("Disabling channel swapping."); | |
330 | devc->capture_flags &= ~CAPTURE_FLAG_SWAP_CHANNELS; | |
331 | } | |
332 | break; | |
333 | case SR_CONF_RLE: | |
334 | if (g_variant_get_boolean(data)) { | |
335 | sr_info("Enabling RLE."); | |
336 | devc->capture_flags |= CAPTURE_FLAG_RLE; | |
337 | } else { | |
338 | sr_info("Disabling RLE."); | |
339 | devc->capture_flags &= ~CAPTURE_FLAG_RLE; | |
340 | } | |
341 | break; | |
342 | default: | |
343 | return SR_ERR_NA; | |
344 | } | |
345 | ||
346 | return SR_OK; | |
347 | } | |
348 | ||
349 | static int config_list(uint32_t key, GVariant **data, | |
350 | const struct sr_dev_inst *sdi, | |
351 | const struct sr_channel_group *cg) | |
352 | { | |
353 | struct dev_context *devc; | |
354 | int num_ols_changrp, i; | |
355 | ||
356 | switch (key) { | |
357 | case SR_CONF_SCAN_OPTIONS: | |
358 | case SR_CONF_DEVICE_OPTIONS: | |
359 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, | |
360 | devopts); | |
361 | case SR_CONF_SAMPLERATE: | |
362 | *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates)); | |
363 | break; | |
364 | case SR_CONF_TRIGGER_MATCH: | |
365 | *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches)); | |
366 | break; | |
367 | case SR_CONF_CLOCK_EDGE: | |
368 | *data = std_gvar_array_str(ARRAY_AND_SIZE(external_clock_edges)); | |
369 | break; | |
370 | case SR_CONF_PATTERN_MODE: | |
371 | *data = g_variant_new_strv(ARRAY_AND_SIZE(patterns)); | |
372 | break; | |
373 | case SR_CONF_LIMIT_SAMPLES: | |
374 | if (!sdi) | |
375 | return SR_ERR_ARG; | |
376 | devc = sdi->priv; | |
377 | if (devc->max_samples == 0) | |
378 | /* Device didn't specify sample memory size in metadata. */ | |
379 | return SR_ERR_NA; | |
380 | /* | |
381 | * Channel groups are turned off if no channels in that group are | |
382 | * enabled, making more room for samples for the enabled group. | |
383 | */ | |
384 | uint32_t channel_mask = ols_channel_mask(sdi); | |
385 | num_ols_changrp = 0; | |
386 | for (i = 0; i < 4; i++) { | |
387 | if (channel_mask & (0xff << (i * 8))) | |
388 | num_ols_changrp++; | |
389 | } | |
390 | ||
391 | *data = std_gvar_tuple_u64(MIN_NUM_SAMPLES, (num_ols_changrp) | |
392 | ? devc->max_samples / num_ols_changrp : MIN_NUM_SAMPLES); | |
393 | break; | |
394 | default: | |
395 | return SR_ERR_NA; | |
396 | } | |
397 | ||
398 | return SR_OK; | |
399 | } | |
400 | ||
401 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
402 | { | |
403 | int ret; | |
404 | struct dev_context *devc; | |
405 | struct sr_serial_dev_inst *serial; | |
406 | ||
407 | devc = sdi->priv; | |
408 | serial = sdi->conn; | |
409 | ||
410 | ret = ols_prepare_acquisition(sdi); | |
411 | if (ret != SR_OK) | |
412 | return ret; | |
413 | ||
414 | /* Start acquisition on the device. */ | |
415 | if (send_shortcommand(serial, CMD_ARM_BASIC_TRIGGER) != SR_OK) | |
416 | return SR_ERR; | |
417 | ||
418 | /* Reset all operational states. */ | |
419 | devc->rle_count = devc->num_transfers = 0; | |
420 | devc->num_samples = devc->num_bytes = 0; | |
421 | devc->cnt_bytes = devc->cnt_samples = devc->cnt_samples_rle = 0; | |
422 | memset(devc->sample, 0, 4); | |
423 | ||
424 | std_session_send_df_header(sdi); | |
425 | ||
426 | /* If the device stops sending for longer than it takes to send a byte, | |
427 | * that means it's finished. But wait at least 100 ms to be safe. | |
428 | */ | |
429 | return serial_source_add(sdi->session, serial, G_IO_IN, 100, | |
430 | ols_receive_data, (struct sr_dev_inst *)sdi); | |
431 | } | |
432 | ||
433 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
434 | { | |
435 | abort_acquisition(sdi); | |
436 | ||
437 | return SR_OK; | |
438 | } | |
439 | ||
440 | static struct sr_dev_driver ols_driver_info = { | |
441 | .name = "ols", | |
442 | .longname = "Openbench Logic Sniffer & SUMP compatibles", | |
443 | .api_version = 1, | |
444 | .init = std_init, | |
445 | .cleanup = std_cleanup, | |
446 | .scan = scan, | |
447 | .dev_list = std_dev_list, | |
448 | .dev_clear = std_dev_clear, | |
449 | .config_get = config_get, | |
450 | .config_set = config_set, | |
451 | .config_list = config_list, | |
452 | .dev_open = std_serial_dev_open, | |
453 | .dev_close = std_serial_dev_close, | |
454 | .dev_acquisition_start = dev_acquisition_start, | |
455 | .dev_acquisition_stop = dev_acquisition_stop, | |
456 | .context = NULL, | |
457 | }; | |
458 | SR_REGISTER_DEV_DRIVER(ols_driver_info); |