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