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