2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
21 #include <libserialport.h>
23 extern SR_PRIV struct sr_dev_driver ols_driver_info;
24 static struct sr_dev_driver *di = &ols_driver_info;
26 SR_PRIV int send_shortcommand(struct sr_serial_dev_inst *serial,
31 sr_dbg("Sending cmd 0x%.2x.", command);
33 if (serial_write_blocking(serial, buf, 1) != 1)
39 SR_PRIV int send_longcommand(struct sr_serial_dev_inst *serial,
40 uint8_t command, uint8_t *data)
44 sr_dbg("Sending cmd 0x%.2x data 0x%.2x%.2x%.2x%.2x.", command,
45 data[0], data[1], data[2], data[3]);
51 if (serial_write_blocking(serial, buf, 5) != 5)
57 SR_PRIV int ols_configure_probes(const struct sr_dev_inst *sdi)
59 struct dev_context *devc;
60 const struct sr_channel *probe;
62 int probe_bit, stage, i;
68 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
69 devc->trigger_mask[i] = 0;
70 devc->trigger_value[i] = 0;
74 for (l = sdi->probes; l; l = l->next) {
75 probe = (const struct sr_channel *)l->data;
79 if (probe->index >= devc->max_probes) {
80 sr_err("Channels over the limit of %d\n", devc->max_probes);
85 * Set up the probe mask for later configuration into the
88 probe_bit = 1 << (probe->index);
89 devc->probe_mask |= probe_bit;
94 /* Configure trigger mask and value. */
96 for (tc = probe->trigger; tc && *tc; tc++) {
97 devc->trigger_mask[stage] |= probe_bit;
99 devc->trigger_value[stage] |= probe_bit;
101 /* Only supporting parallel mode, with up to 4 stages. */
105 if (stage > devc->num_stages)
106 devc->num_stages = stage - 1;
112 SR_PRIV struct dev_context *ols_dev_new(void)
114 struct dev_context *devc;
116 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
117 sr_err("Device context malloc failed.");
121 /* Device-specific settings */
122 devc->max_samples = devc->max_samplerate = devc->protocol_version = 0;
124 /* Acquisition settings */
125 devc->limit_samples = devc->capture_ratio = 0;
126 devc->trigger_at = -1;
127 devc->probe_mask = 0xffffffff;
133 SR_PRIV struct sr_dev_inst *get_metadata(struct sr_serial_dev_inst *serial)
135 struct sr_dev_inst *sdi;
136 struct dev_context *devc;
137 struct sr_channel *probe;
138 uint32_t tmp_int, ui;
139 uint8_t key, type, token;
140 GString *tmp_str, *devname, *version;
143 sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, NULL, NULL, NULL);
145 devc = ols_dev_new();
148 devname = g_string_new("");
149 version = g_string_new("");
153 if (serial_read_blocking(serial, &key, 1) != 1)
156 sr_dbg("Got metadata key 0x00, metadata ends.");
163 /* NULL-terminated string */
164 tmp_str = g_string_new("");
165 while (serial_read_blocking(serial, &tmp_c, 1) == 1 && tmp_c != '\0')
166 g_string_append_c(tmp_str, tmp_c);
167 sr_dbg("Got metadata key 0x%.2x value '%s'.",
172 devname = g_string_append(devname, tmp_str->str);
175 /* FPGA firmware version */
177 g_string_append(version, ", ");
178 g_string_append(version, "FPGA version ");
179 g_string_append(version, tmp_str->str);
182 /* Ancillary version */
184 g_string_append(version, ", ");
185 g_string_append(version, "Ancillary version ");
186 g_string_append(version, tmp_str->str);
189 sr_info("ols: unknown token 0x%.2x: '%s'",
190 token, tmp_str->str);
193 g_string_free(tmp_str, TRUE);
196 /* 32-bit unsigned integer */
197 if (serial_read_blocking(serial, &tmp_int, 4) != 4)
199 tmp_int = RB32(&tmp_int);
200 sr_dbg("Got metadata key 0x%.2x value 0x%.8x.",
204 /* Number of usable probes */
205 for (ui = 0; ui < tmp_int; ui++) {
206 if (!(probe = sr_probe_new(ui, SR_PROBE_LOGIC, TRUE,
207 ols_probe_names[ui])))
209 sdi->probes = g_slist_append(sdi->probes, probe);
213 /* Amount of sample memory available (bytes) */
214 devc->max_samples = tmp_int;
217 /* Amount of dynamic memory available (bytes) */
218 /* what is this for? */
221 /* Maximum sample rate (hz) */
222 devc->max_samplerate = tmp_int;
225 /* protocol version */
226 devc->protocol_version = tmp_int;
229 sr_info("Unknown token 0x%.2x: 0x%.8x.",
235 /* 8-bit unsigned integer */
236 if (serial_read_blocking(serial, &tmp_c, 1) != 1)
238 sr_dbg("Got metadata key 0x%.2x value 0x%.2x.",
242 /* Number of usable probes */
243 for (ui = 0; ui < tmp_c; ui++) {
244 if (!(probe = sr_probe_new(ui, SR_PROBE_LOGIC, TRUE,
245 ols_probe_names[ui])))
247 sdi->probes = g_slist_append(sdi->probes, probe);
251 /* protocol version */
252 devc->protocol_version = tmp_c;
255 sr_info("Unknown token 0x%.2x: 0x%.2x.",
266 sdi->model = devname->str;
267 sdi->version = version->str;
268 g_string_free(devname, FALSE);
269 g_string_free(version, FALSE);
274 SR_PRIV int ols_set_samplerate(const struct sr_dev_inst *sdi,
275 const uint64_t samplerate)
277 struct dev_context *devc;
280 if (devc->max_samplerate && samplerate > devc->max_samplerate)
281 return SR_ERR_SAMPLERATE;
283 if (samplerate > CLOCK_RATE) {
284 sr_info("Enabling demux mode.");
285 devc->flag_reg |= FLAG_DEMUX;
286 devc->flag_reg &= ~FLAG_FILTER;
287 devc->max_probes = NUM_PROBES / 2;
288 devc->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
290 sr_info("Disabling demux mode.");
291 devc->flag_reg &= ~FLAG_DEMUX;
292 devc->flag_reg |= FLAG_FILTER;
293 devc->max_probes = NUM_PROBES;
294 devc->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
297 /* Calculate actual samplerate used and complain if it is different
298 * from the requested.
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)
304 sr_info("Can't match samplerate %" PRIu64 ", using %"
305 PRIu64 ".", samplerate, devc->cur_samplerate);
310 SR_PRIV void abort_acquisition(const struct sr_dev_inst *sdi)
312 struct sr_datafeed_packet packet;
313 struct sr_serial_dev_inst *serial;
316 serial_source_remove(serial);
318 /* Terminate session */
319 packet.type = SR_DF_END;
320 sr_session_send(sdi, &packet);
323 SR_PRIV int ols_receive_data(int fd, int revents, void *cb_data)
325 struct dev_context *devc;
326 struct sr_dev_inst *sdi;
327 struct sr_serial_dev_inst *serial;
328 struct sr_datafeed_packet packet;
329 struct sr_datafeed_logic logic;
331 int num_channels, offset, j;
341 if (devc->num_transfers++ == 0) {
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...
348 serial_source_remove(serial);
349 serial_source_add(serial, G_IO_IN, 30, ols_receive_data, cb_data);
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.");
355 /* fill with 1010... for debugging */
356 memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4);
360 for (i = NUM_PROBES; i > 0x02; i /= 2) {
361 if ((devc->flag_reg & i) == 0) {
366 if (revents == G_IO_IN && devc->num_samples < devc->limit_samples) {
367 if (serial_read_nonblocking(serial, &byte, 1) != 1)
371 /* Ignore it if we've read enough. */
372 if (devc->num_samples >= devc->limit_samples)
375 devc->sample[devc->num_bytes++] = byte;
376 sr_spew("Received byte 0x%.2x.", byte);
377 if (devc->num_bytes == num_channels) {
379 devc->cnt_samples_rle++;
381 * Got a full sample. Convert from the OLS's little-endian
382 * sample to the local format.
384 sample = devc->sample[0] | (devc->sample[1] << 8) \
385 | (devc->sample[2] << 16) | (devc->sample[3] << 24);
386 sr_dbg("Received sample 0x%.*x.", devc->num_bytes * 2, sample);
387 if (devc->flag_reg & FLAG_RLE) {
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.
393 if (devc->sample[devc->num_bytes - 1] & 0x80) {
394 /* Clear the high bit. */
395 sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
396 devc->rle_count = sample;
397 devc->cnt_samples_rle += devc->rle_count;
398 sr_dbg("RLE count: %u.", devc->rle_count);
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;
410 if (num_channels < 4) {
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
418 * the number of probes.
421 memset(devc->tmp_sample, 0, 4);
422 for (i = 0; i < 4; i++) {
423 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
425 * This channel group was
426 * enabled, copy from received
429 devc->tmp_sample[i] = devc->sample[j++];
430 } else if (devc->flag_reg & FLAG_DEMUX && (i > 2)) {
431 /* group 2 & 3 get added to 0 & 1 */
432 devc->tmp_sample[i - 2] = devc->sample[j++];
435 memcpy(devc->sample, devc->tmp_sample, 4);
436 sr_spew("Expanded sample: 0x%.8x.", sample);
440 * the OLS sends its sample buffer backwards.
441 * store it in reverse order here, so we can dump
442 * this on the session bus later.
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),
449 memset(devc->sample, 0, 4);
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.
459 sr_dbg("Received %d bytes, %d samples, %d decompressed samples.",
460 devc->cnt_bytes, devc->cnt_samples,
461 devc->cnt_samples_rle);
462 if (devc->trigger_at != -1) {
464 * A trigger was set up, so we need to tell the frontend
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;
473 logic.data = devc->raw_sample_buf +
474 (devc->limit_samples - devc->num_samples) * 4;
475 sr_session_send(cb_data, &packet);
478 /* Send the trigger. */
479 packet.type = SR_DF_TRIGGER;
480 sr_session_send(cb_data, &packet);
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);
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);
491 /* no trigger was used */
492 packet.type = SR_DF_LOGIC;
493 packet.payload = &logic;
494 logic.length = devc->num_samples * 4;
496 logic.data = devc->raw_sample_buf +
497 (devc->limit_samples - devc->num_samples) * 4;
498 sr_session_send(cb_data, &packet);
500 g_free(devc->raw_sample_buf);
502 serial_flush(serial);
503 abort_acquisition(sdi);