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