]>
Commit | Line | Data |
---|---|---|
0aba65da | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
0aba65da | 3 | * |
13d8e03c | 4 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> |
0aba65da UH |
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" | |
bf72f649 | 21 | #include <libserialport.h> |
0aba65da | 22 | |
2239728c | 23 | extern SR_PRIV struct sr_dev_driver ols_driver_info; |
0aba65da UH |
24 | static struct sr_dev_driver *di = &ols_driver_info; |
25 | ||
26 | SR_PRIV int send_shortcommand(struct sr_serial_dev_inst *serial, | |
27 | uint8_t command) | |
28 | { | |
29 | char buf[1]; | |
30 | ||
31 | sr_dbg("Sending cmd 0x%.2x.", command); | |
32 | buf[0] = command; | |
9f5d4c3c | 33 | if (serial_write_blocking(serial, buf, 1) != 1) |
0aba65da UH |
34 | return SR_ERR; |
35 | ||
36 | return SR_OK; | |
37 | } | |
38 | ||
39 | SR_PRIV int send_longcommand(struct sr_serial_dev_inst *serial, | |
40 | uint8_t command, uint32_t data) | |
41 | { | |
42 | char buf[5]; | |
43 | ||
44 | sr_dbg("Sending cmd 0x%.2x data 0x%.8x.", command, data); | |
45 | buf[0] = command; | |
46 | buf[1] = (data & 0xff000000) >> 24; | |
47 | buf[2] = (data & 0xff0000) >> 16; | |
48 | buf[3] = (data & 0xff00) >> 8; | |
49 | buf[4] = data & 0xff; | |
9f5d4c3c | 50 | if (serial_write_blocking(serial, buf, 5) != 5) |
0aba65da UH |
51 | return SR_ERR; |
52 | ||
53 | return SR_OK; | |
54 | } | |
55 | ||
56 | SR_PRIV int ols_configure_probes(const struct sr_dev_inst *sdi) | |
57 | { | |
58 | struct dev_context *devc; | |
59 | const struct sr_probe *probe; | |
60 | const GSList *l; | |
61 | int probe_bit, stage, i; | |
62 | char *tc; | |
63 | ||
64 | devc = sdi->priv; | |
65 | ||
66 | devc->probe_mask = 0; | |
67 | for (i = 0; i < NUM_TRIGGER_STAGES; i++) { | |
68 | devc->trigger_mask[i] = 0; | |
69 | devc->trigger_value[i] = 0; | |
70 | } | |
71 | ||
72 | devc->num_stages = 0; | |
73 | for (l = sdi->probes; l; l = l->next) { | |
74 | probe = (const struct sr_probe *)l->data; | |
75 | if (!probe->enabled) | |
76 | continue; | |
77 | ||
b1de0407 MR |
78 | if (probe->index >= devc->max_probes) { |
79 | sr_err("Channels over the limit of %d\n", devc->max_probes); | |
80 | return SR_ERR; | |
81 | } | |
82 | ||
0aba65da UH |
83 | /* |
84 | * Set up the probe mask for later configuration into the | |
85 | * flag register. | |
86 | */ | |
87 | probe_bit = 1 << (probe->index); | |
88 | devc->probe_mask |= probe_bit; | |
89 | ||
90 | if (!probe->trigger) | |
91 | continue; | |
92 | ||
93 | /* Configure trigger mask and value. */ | |
94 | stage = 0; | |
95 | for (tc = probe->trigger; tc && *tc; tc++) { | |
96 | devc->trigger_mask[stage] |= probe_bit; | |
97 | if (*tc == '1') | |
98 | devc->trigger_value[stage] |= probe_bit; | |
99 | stage++; | |
100 | if (stage > 3) | |
101 | /* | |
102 | * TODO: Only supporting parallel mode, with | |
103 | * up to 4 stages. | |
104 | */ | |
105 | return SR_ERR; | |
106 | } | |
107 | if (stage > devc->num_stages) | |
503133bb | 108 | devc->num_stages = stage - 1; |
0aba65da UH |
109 | } |
110 | ||
111 | return SR_OK; | |
112 | } | |
113 | ||
114 | SR_PRIV uint32_t reverse16(uint32_t in) | |
115 | { | |
116 | uint32_t out; | |
117 | ||
118 | out = (in & 0xff) << 8; | |
119 | out |= (in & 0xff00) >> 8; | |
120 | out |= (in & 0xff0000) << 8; | |
121 | out |= (in & 0xff000000) >> 8; | |
122 | ||
123 | return out; | |
124 | } | |
125 | ||
126 | SR_PRIV uint32_t reverse32(uint32_t in) | |
127 | { | |
128 | uint32_t out; | |
129 | ||
130 | out = (in & 0xff) << 24; | |
131 | out |= (in & 0xff00) << 8; | |
132 | out |= (in & 0xff0000) >> 8; | |
133 | out |= (in & 0xff000000) >> 24; | |
134 | ||
135 | return out; | |
136 | } | |
137 | ||
138 | SR_PRIV struct dev_context *ols_dev_new(void) | |
139 | { | |
140 | struct dev_context *devc; | |
141 | ||
bf256783 | 142 | if (!(devc = g_try_malloc(sizeof(struct dev_context)))) { |
0aba65da UH |
143 | sr_err("Device context malloc failed."); |
144 | return NULL; | |
145 | } | |
146 | ||
bf256783 BV |
147 | /* Device-specific settings */ |
148 | devc->max_samples = devc->max_samplerate = devc->protocol_version = 0; | |
149 | ||
150 | /* Acquisition settings */ | |
151 | devc->limit_samples = devc->capture_ratio = 0; | |
0aba65da UH |
152 | devc->trigger_at = -1; |
153 | devc->probe_mask = 0xffffffff; | |
bf256783 BV |
154 | devc->flag_reg = 0; |
155 | ||
0aba65da UH |
156 | return devc; |
157 | } | |
158 | ||
159 | SR_PRIV struct sr_dev_inst *get_metadata(struct sr_serial_dev_inst *serial) | |
160 | { | |
161 | struct sr_dev_inst *sdi; | |
162 | struct dev_context *devc; | |
163 | struct sr_probe *probe; | |
164 | uint32_t tmp_int, ui; | |
165 | uint8_t key, type, token; | |
166 | GString *tmp_str, *devname, *version; | |
167 | guchar tmp_c; | |
168 | ||
169 | sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, NULL, NULL, NULL); | |
170 | sdi->driver = di; | |
171 | devc = ols_dev_new(); | |
172 | sdi->priv = devc; | |
173 | ||
174 | devname = g_string_new(""); | |
175 | version = g_string_new(""); | |
176 | ||
177 | key = 0xff; | |
178 | while (key) { | |
9f5d4c3c | 179 | if (serial_read_blocking(serial, &key, 1) != 1 || key == 0x00) |
0aba65da UH |
180 | break; |
181 | type = key >> 5; | |
182 | token = key & 0x1f; | |
183 | switch (type) { | |
184 | case 0: | |
185 | /* NULL-terminated string */ | |
186 | tmp_str = g_string_new(""); | |
9f5d4c3c | 187 | while (serial_read_blocking(serial, &tmp_c, 1) == 1 && tmp_c != '\0') |
0aba65da UH |
188 | g_string_append_c(tmp_str, tmp_c); |
189 | sr_dbg("Got metadata key 0x%.2x value '%s'.", | |
190 | key, tmp_str->str); | |
191 | switch (token) { | |
192 | case 0x01: | |
193 | /* Device name */ | |
194 | devname = g_string_append(devname, tmp_str->str); | |
195 | break; | |
196 | case 0x02: | |
197 | /* FPGA firmware version */ | |
198 | if (version->len) | |
199 | g_string_append(version, ", "); | |
200 | g_string_append(version, "FPGA version "); | |
201 | g_string_append(version, tmp_str->str); | |
202 | break; | |
203 | case 0x03: | |
204 | /* Ancillary version */ | |
205 | if (version->len) | |
206 | g_string_append(version, ", "); | |
207 | g_string_append(version, "Ancillary version "); | |
208 | g_string_append(version, tmp_str->str); | |
209 | break; | |
210 | default: | |
211 | sr_info("ols: unknown token 0x%.2x: '%s'", | |
212 | token, tmp_str->str); | |
213 | break; | |
214 | } | |
215 | g_string_free(tmp_str, TRUE); | |
216 | break; | |
217 | case 1: | |
218 | /* 32-bit unsigned integer */ | |
9f5d4c3c | 219 | if (serial_read_blocking(serial, &tmp_int, 4) != 4) |
0aba65da UH |
220 | break; |
221 | tmp_int = reverse32(tmp_int); | |
222 | sr_dbg("Got metadata key 0x%.2x value 0x%.8x.", | |
223 | key, tmp_int); | |
224 | switch (token) { | |
225 | case 0x00: | |
226 | /* Number of usable probes */ | |
227 | for (ui = 0; ui < tmp_int; ui++) { | |
228 | if (!(probe = sr_probe_new(ui, SR_PROBE_LOGIC, TRUE, | |
229 | ols_probe_names[ui]))) | |
230 | return 0; | |
231 | sdi->probes = g_slist_append(sdi->probes, probe); | |
232 | } | |
233 | break; | |
234 | case 0x01: | |
235 | /* Amount of sample memory available (bytes) */ | |
236 | devc->max_samples = tmp_int; | |
237 | break; | |
238 | case 0x02: | |
239 | /* Amount of dynamic memory available (bytes) */ | |
240 | /* what is this for? */ | |
241 | break; | |
242 | case 0x03: | |
243 | /* Maximum sample rate (hz) */ | |
244 | devc->max_samplerate = tmp_int; | |
245 | break; | |
246 | case 0x04: | |
247 | /* protocol version */ | |
248 | devc->protocol_version = tmp_int; | |
249 | break; | |
250 | default: | |
251 | sr_info("Unknown token 0x%.2x: 0x%.8x.", | |
252 | token, tmp_int); | |
253 | break; | |
254 | } | |
255 | break; | |
256 | case 2: | |
257 | /* 8-bit unsigned integer */ | |
9f5d4c3c | 258 | if (serial_read_blocking(serial, &tmp_c, 1) != 1) |
0aba65da UH |
259 | break; |
260 | sr_dbg("Got metadata key 0x%.2x value 0x%.2x.", | |
261 | key, tmp_c); | |
262 | switch (token) { | |
263 | case 0x00: | |
264 | /* Number of usable probes */ | |
265 | for (ui = 0; ui < tmp_c; ui++) { | |
266 | if (!(probe = sr_probe_new(ui, SR_PROBE_LOGIC, TRUE, | |
267 | ols_probe_names[ui]))) | |
268 | return 0; | |
269 | sdi->probes = g_slist_append(sdi->probes, probe); | |
270 | } | |
271 | break; | |
272 | case 0x01: | |
273 | /* protocol version */ | |
274 | devc->protocol_version = tmp_c; | |
275 | break; | |
276 | default: | |
277 | sr_info("Unknown token 0x%.2x: 0x%.2x.", | |
278 | token, tmp_c); | |
279 | break; | |
280 | } | |
281 | break; | |
282 | default: | |
283 | /* unknown type */ | |
284 | break; | |
285 | } | |
286 | } | |
287 | ||
288 | sdi->model = devname->str; | |
289 | sdi->version = version->str; | |
290 | g_string_free(devname, FALSE); | |
291 | g_string_free(version, FALSE); | |
292 | ||
293 | return sdi; | |
294 | } | |
295 | ||
296 | SR_PRIV int ols_set_samplerate(const struct sr_dev_inst *sdi, | |
e46aa4f6 | 297 | const uint64_t samplerate) |
0aba65da UH |
298 | { |
299 | struct dev_context *devc; | |
300 | ||
301 | devc = sdi->priv; | |
e46aa4f6 | 302 | if (devc->max_samplerate && samplerate > devc->max_samplerate) |
0aba65da UH |
303 | return SR_ERR_SAMPLERATE; |
304 | ||
305 | if (samplerate > CLOCK_RATE) { | |
6ebe0039 | 306 | sr_info("Enabling demux mode."); |
0aba65da | 307 | devc->flag_reg |= FLAG_DEMUX; |
6ebe0039 | 308 | devc->flag_reg &= ~FLAG_FILTER; |
b1de0407 | 309 | devc->max_probes = NUM_PROBES / 2; |
0aba65da UH |
310 | devc->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1; |
311 | } else { | |
6ebe0039 | 312 | sr_info("Disabling demux mode."); |
0aba65da | 313 | devc->flag_reg &= ~FLAG_DEMUX; |
6a53bde6 | 314 | devc->flag_reg |= FLAG_FILTER; |
b1de0407 | 315 | devc->max_probes = NUM_PROBES; |
0aba65da UH |
316 | devc->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1; |
317 | } | |
318 | ||
319 | /* Calculate actual samplerate used and complain if it is different | |
320 | * from the requested. | |
321 | */ | |
322 | devc->cur_samplerate = CLOCK_RATE / (devc->cur_samplerate_divider + 1); | |
323 | if (devc->flag_reg & FLAG_DEMUX) | |
324 | devc->cur_samplerate *= 2; | |
325 | if (devc->cur_samplerate != samplerate) | |
e46aa4f6 | 326 | sr_info("Can't match samplerate %" PRIu64 ", using %" |
0aba65da UH |
327 | PRIu64 ".", samplerate, devc->cur_samplerate); |
328 | ||
329 | return SR_OK; | |
330 | } | |
331 | ||
332 | SR_PRIV void abort_acquisition(const struct sr_dev_inst *sdi) | |
333 | { | |
334 | struct sr_datafeed_packet packet; | |
459a0f26 | 335 | struct sr_serial_dev_inst *serial; |
0aba65da | 336 | |
459a0f26 | 337 | serial = sdi->conn; |
7faa3e88 | 338 | serial_source_remove(serial); |
0aba65da UH |
339 | |
340 | /* Terminate session */ | |
341 | packet.type = SR_DF_END; | |
342 | sr_session_send(sdi, &packet); | |
343 | } | |
344 | ||
345 | SR_PRIV int ols_receive_data(int fd, int revents, void *cb_data) | |
346 | { | |
459a0f26 BV |
347 | struct drv_context *drvc; |
348 | struct dev_context *devc; | |
349 | struct sr_serial_dev_inst *serial; | |
0aba65da UH |
350 | struct sr_datafeed_packet packet; |
351 | struct sr_datafeed_logic logic; | |
352 | struct sr_dev_inst *sdi; | |
0aba65da | 353 | GSList *l; |
fe9ac252 | 354 | uint32_t sample; |
00d04d3b | 355 | int num_channels, offset, j; |
b1de0407 | 356 | unsigned int i; |
0aba65da | 357 | unsigned char byte; |
bf72f649 | 358 | int serial_fd; |
0aba65da UH |
359 | |
360 | drvc = di->priv; | |
361 | ||
362 | /* Find this device's devc struct by its fd. */ | |
363 | devc = NULL; | |
364 | for (l = drvc->instances; l; l = l->next) { | |
365 | sdi = l->data; | |
366 | devc = sdi->priv; | |
459a0f26 | 367 | serial = sdi->conn; |
bf72f649 ML |
368 | sp_get_port_handle(serial->data, &serial_fd); |
369 | if (serial_fd == fd) | |
0aba65da UH |
370 | break; |
371 | devc = NULL; | |
372 | } | |
373 | if (!devc) | |
374 | /* Shouldn't happen. */ | |
375 | return TRUE; | |
376 | ||
377 | if (devc->num_transfers++ == 0) { | |
378 | /* | |
379 | * First time round, means the device started sending data, | |
380 | * and will not stop until done. If it stops sending for | |
381 | * longer than it takes to send a byte, that means it's | |
382 | * finished. We'll double that to 30ms to be sure... | |
383 | */ | |
264c99ed ML |
384 | serial_source_remove(serial); |
385 | serial_source_add(serial, G_IO_IN, 30, ols_receive_data, cb_data); | |
0aba65da UH |
386 | devc->raw_sample_buf = g_try_malloc(devc->limit_samples * 4); |
387 | if (!devc->raw_sample_buf) { | |
388 | sr_err("Sample buffer malloc failed."); | |
389 | return FALSE; | |
390 | } | |
391 | /* fill with 1010... for debugging */ | |
392 | memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4); | |
393 | } | |
394 | ||
395 | num_channels = 0; | |
f51acd69 MR |
396 | |
397 | for (i = NUM_PROBES; i > 0x02; i /= 2) { | |
398 | if ((devc->flag_reg & i) == 0) { | |
0aba65da | 399 | num_channels++; |
f51acd69 | 400 | } |
0aba65da UH |
401 | } |
402 | ||
faf72024 | 403 | if (revents == G_IO_IN && devc->num_samples < devc->limit_samples) { |
9f5d4c3c | 404 | if (serial_read_nonblocking(serial, &byte, 1) != 1) |
0aba65da UH |
405 | return FALSE; |
406 | ||
407 | /* Ignore it if we've read enough. */ | |
408 | if (devc->num_samples >= devc->limit_samples) | |
409 | return TRUE; | |
410 | ||
411 | devc->sample[devc->num_bytes++] = byte; | |
412 | sr_dbg("Received byte 0x%.2x.", byte); | |
413 | if (devc->num_bytes == num_channels) { | |
00d04d3b BV |
414 | /* Got a full sample. Convert from the OLS's little-endian |
415 | * sample to the local format. */ | |
fe9ac252 BV |
416 | sample = devc->sample[0] | (devc->sample[1] << 8) \ |
417 | | (devc->sample[2] << 16) | (devc->sample[3] << 24); | |
418 | sr_dbg("Received sample 0x%.*x.", devc->num_bytes * 2, sample); | |
0aba65da UH |
419 | if (devc->flag_reg & FLAG_RLE) { |
420 | /* | |
00d04d3b BV |
421 | * In RLE mode the high bit of the sample is the |
422 | * "count" flag, meaning this sample is the number | |
423 | * of times the previous sample occurred. | |
0aba65da UH |
424 | */ |
425 | if (devc->sample[devc->num_bytes - 1] & 0x80) { | |
00d04d3b BV |
426 | /* Clear the high bit. */ |
427 | sample &= ~(0x80 << (devc->num_bytes - 1) * 8); | |
fe9ac252 | 428 | devc->rle_count = sample; |
00d04d3b | 429 | sr_dbg("RLE count: %u.", devc->rle_count); |
0aba65da UH |
430 | devc->num_bytes = 0; |
431 | return TRUE; | |
432 | } | |
433 | } | |
434 | devc->num_samples += devc->rle_count + 1; | |
435 | if (devc->num_samples > devc->limit_samples) { | |
436 | /* Save us from overrunning the buffer. */ | |
437 | devc->rle_count -= devc->num_samples - devc->limit_samples; | |
438 | devc->num_samples = devc->limit_samples; | |
439 | } | |
440 | ||
441 | if (num_channels < 4) { | |
442 | /* | |
443 | * Some channel groups may have been turned | |
444 | * off, to speed up transfer between the | |
445 | * hardware and the PC. Expand that here before | |
446 | * submitting it over the session bus -- | |
447 | * whatever is listening on the bus will be | |
448 | * expecting a full 32-bit sample, based on | |
449 | * the number of probes. | |
450 | */ | |
451 | j = 0; | |
452 | memset(devc->tmp_sample, 0, 4); | |
453 | for (i = 0; i < 4; i++) { | |
454 | if (((devc->flag_reg >> 2) & (1 << i)) == 0) { | |
455 | /* | |
456 | * This channel group was | |
457 | * enabled, copy from received | |
458 | * sample. | |
459 | */ | |
460 | devc->tmp_sample[i] = devc->sample[j++]; | |
f51acd69 | 461 | } else if (devc->flag_reg & FLAG_DEMUX && (i > 2)) { |
b1de0407 | 462 | /* group 2 & 3 get added to 0 & 1 */ |
f51acd69 | 463 | devc->tmp_sample[i - 2] = devc->sample[j++]; |
0aba65da UH |
464 | } |
465 | } | |
466 | memcpy(devc->sample, devc->tmp_sample, 4); | |
fe9ac252 | 467 | sr_dbg("Full sample: 0x%.8x.", sample); |
0aba65da UH |
468 | } |
469 | ||
470 | /* the OLS sends its sample buffer backwards. | |
471 | * store it in reverse order here, so we can dump | |
472 | * this on the session bus later. | |
473 | */ | |
474 | offset = (devc->limit_samples - devc->num_samples) * 4; | |
475 | for (i = 0; i <= devc->rle_count; i++) { | |
476 | memcpy(devc->raw_sample_buf + offset + (i * 4), | |
477 | devc->sample, 4); | |
478 | } | |
479 | memset(devc->sample, 0, 4); | |
480 | devc->num_bytes = 0; | |
481 | devc->rle_count = 0; | |
482 | } | |
483 | } else { | |
484 | /* | |
485 | * This is the main loop telling us a timeout was reached, or | |
486 | * we've acquired all the samples we asked for -- we're done. | |
487 | * Send the (properly-ordered) buffer to the frontend. | |
488 | */ | |
489 | if (devc->trigger_at != -1) { | |
490 | /* a trigger was set up, so we need to tell the frontend | |
491 | * about it. | |
492 | */ | |
493 | if (devc->trigger_at > 0) { | |
494 | /* there are pre-trigger samples, send those first */ | |
495 | packet.type = SR_DF_LOGIC; | |
496 | packet.payload = &logic; | |
497 | logic.length = devc->trigger_at * 4; | |
498 | logic.unitsize = 4; | |
499 | logic.data = devc->raw_sample_buf + | |
500 | (devc->limit_samples - devc->num_samples) * 4; | |
501 | sr_session_send(cb_data, &packet); | |
502 | } | |
503 | ||
504 | /* send the trigger */ | |
505 | packet.type = SR_DF_TRIGGER; | |
506 | sr_session_send(cb_data, &packet); | |
507 | ||
508 | /* send post-trigger samples */ | |
509 | packet.type = SR_DF_LOGIC; | |
510 | packet.payload = &logic; | |
511 | logic.length = (devc->num_samples * 4) - (devc->trigger_at * 4); | |
512 | logic.unitsize = 4; | |
513 | logic.data = devc->raw_sample_buf + devc->trigger_at * 4 + | |
514 | (devc->limit_samples - devc->num_samples) * 4; | |
515 | sr_session_send(cb_data, &packet); | |
516 | } else { | |
517 | /* no trigger was used */ | |
518 | packet.type = SR_DF_LOGIC; | |
519 | packet.payload = &logic; | |
520 | logic.length = devc->num_samples * 4; | |
521 | logic.unitsize = 4; | |
522 | logic.data = devc->raw_sample_buf + | |
523 | (devc->limit_samples - devc->num_samples) * 4; | |
524 | sr_session_send(cb_data, &packet); | |
525 | } | |
526 | g_free(devc->raw_sample_buf); | |
527 | ||
459a0f26 | 528 | serial_flush(serial); |
0aba65da | 529 | abort_acquisition(sdi); |
0aba65da UH |
530 | } |
531 | ||
532 | return TRUE; | |
533 | } |