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/>.
22 extern SR_PRIV struct sr_dev_driver ols_driver_info;
23 static struct sr_dev_driver *di = &ols_driver_info;
25 SR_PRIV int send_shortcommand(struct sr_serial_dev_inst *serial,
30 sr_dbg("Sending cmd 0x%.2x.", command);
32 if (serial_write(serial, buf, 1) != 1)
38 SR_PRIV int send_longcommand(struct sr_serial_dev_inst *serial,
39 uint8_t command, uint32_t data)
43 sr_dbg("Sending cmd 0x%.2x data 0x%.8x.", command, data);
45 buf[1] = (data & 0xff000000) >> 24;
46 buf[2] = (data & 0xff0000) >> 16;
47 buf[3] = (data & 0xff00) >> 8;
49 if (serial_write(serial, buf, 5) != 5)
55 SR_PRIV int ols_configure_probes(const struct sr_dev_inst *sdi)
57 struct dev_context *devc;
58 const struct sr_probe *probe;
60 int probe_bit, stage, i;
66 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
67 devc->trigger_mask[i] = 0;
68 devc->trigger_value[i] = 0;
72 for (l = sdi->probes; l; l = l->next) {
73 probe = (const struct sr_probe *)l->data;
77 if (probe->index >= devc->max_probes) {
78 sr_err("Channels over the limit of %d\n", devc->max_probes);
83 * Set up the probe mask for later configuration into the
86 probe_bit = 1 << (probe->index);
87 devc->probe_mask |= probe_bit;
92 /* Configure trigger mask and value. */
94 for (tc = probe->trigger; tc && *tc; tc++) {
95 devc->trigger_mask[stage] |= probe_bit;
97 devc->trigger_value[stage] |= probe_bit;
101 * TODO: Only supporting parallel mode, with
106 if (stage > devc->num_stages)
107 devc->num_stages = stage - 1;
113 SR_PRIV uint32_t reverse16(uint32_t in)
117 out = (in & 0xff) << 8;
118 out |= (in & 0xff00) >> 8;
119 out |= (in & 0xff0000) << 8;
120 out |= (in & 0xff000000) >> 8;
125 SR_PRIV uint32_t reverse32(uint32_t in)
129 out = (in & 0xff) << 24;
130 out |= (in & 0xff00) << 8;
131 out |= (in & 0xff0000) >> 8;
132 out |= (in & 0xff000000) >> 24;
137 SR_PRIV struct dev_context *ols_dev_new(void)
139 struct dev_context *devc;
141 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
142 sr_err("Device context malloc failed.");
146 /* Device-specific settings */
147 devc->max_samples = devc->max_samplerate = devc->protocol_version = 0;
149 /* Acquisition settings */
150 devc->limit_samples = devc->capture_ratio = 0;
151 devc->trigger_at = -1;
152 devc->probe_mask = 0xffffffff;
158 SR_PRIV struct sr_dev_inst *get_metadata(struct sr_serial_dev_inst *serial)
160 struct sr_dev_inst *sdi;
161 struct dev_context *devc;
162 struct sr_probe *probe;
163 uint32_t tmp_int, ui;
164 uint8_t key, type, token;
165 GString *tmp_str, *devname, *version;
168 sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, NULL, NULL, NULL);
170 devc = ols_dev_new();
173 devname = g_string_new("");
174 version = g_string_new("");
178 if (serial_read(serial, &key, 1) != 1 || key == 0x00)
184 /* NULL-terminated string */
185 tmp_str = g_string_new("");
186 while (serial_read(serial, &tmp_c, 1) == 1 && tmp_c != '\0')
187 g_string_append_c(tmp_str, tmp_c);
188 sr_dbg("Got metadata key 0x%.2x value '%s'.",
193 devname = g_string_append(devname, tmp_str->str);
196 /* FPGA firmware version */
198 g_string_append(version, ", ");
199 g_string_append(version, "FPGA version ");
200 g_string_append(version, tmp_str->str);
203 /* Ancillary version */
205 g_string_append(version, ", ");
206 g_string_append(version, "Ancillary version ");
207 g_string_append(version, tmp_str->str);
210 sr_info("ols: unknown token 0x%.2x: '%s'",
211 token, tmp_str->str);
214 g_string_free(tmp_str, TRUE);
217 /* 32-bit unsigned integer */
218 if (serial_read(serial, &tmp_int, 4) != 4)
220 tmp_int = reverse32(tmp_int);
221 sr_dbg("Got metadata key 0x%.2x value 0x%.8x.",
225 /* Number of usable probes */
226 for (ui = 0; ui < tmp_int; ui++) {
227 if (!(probe = sr_probe_new(ui, SR_PROBE_LOGIC, TRUE,
228 ols_probe_names[ui])))
230 sdi->probes = g_slist_append(sdi->probes, probe);
234 /* Amount of sample memory available (bytes) */
235 devc->max_samples = tmp_int;
238 /* Amount of dynamic memory available (bytes) */
239 /* what is this for? */
242 /* Maximum sample rate (hz) */
243 devc->max_samplerate = tmp_int;
246 /* protocol version */
247 devc->protocol_version = tmp_int;
250 sr_info("Unknown token 0x%.2x: 0x%.8x.",
256 /* 8-bit unsigned integer */
257 if (serial_read(serial, &tmp_c, 1) != 1)
259 sr_dbg("Got metadata key 0x%.2x value 0x%.2x.",
263 /* Number of usable probes */
264 for (ui = 0; ui < tmp_c; ui++) {
265 if (!(probe = sr_probe_new(ui, SR_PROBE_LOGIC, TRUE,
266 ols_probe_names[ui])))
268 sdi->probes = g_slist_append(sdi->probes, probe);
272 /* protocol version */
273 devc->protocol_version = tmp_c;
276 sr_info("Unknown token 0x%.2x: 0x%.2x.",
287 sdi->model = devname->str;
288 sdi->version = version->str;
289 g_string_free(devname, FALSE);
290 g_string_free(version, FALSE);
295 SR_PRIV int ols_set_samplerate(const struct sr_dev_inst *sdi,
296 const uint64_t samplerate)
298 struct dev_context *devc;
301 if (devc->max_samplerate && samplerate > devc->max_samplerate)
302 return SR_ERR_SAMPLERATE;
304 if (samplerate > CLOCK_RATE) {
305 sr_info("Enabling demux mode.");
306 devc->flag_reg |= FLAG_DEMUX;
307 devc->flag_reg &= ~FLAG_FILTER;
308 devc->max_probes = NUM_PROBES / 2;
309 devc->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
311 sr_info("Disabling demux mode.");
312 devc->flag_reg &= ~FLAG_DEMUX;
313 devc->flag_reg |= FLAG_FILTER;
314 devc->max_probes = NUM_PROBES;
315 devc->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
318 /* Calculate actual samplerate used and complain if it is different
319 * from the requested.
321 devc->cur_samplerate = CLOCK_RATE / (devc->cur_samplerate_divider + 1);
322 if (devc->flag_reg & FLAG_DEMUX)
323 devc->cur_samplerate *= 2;
324 if (devc->cur_samplerate != samplerate)
325 sr_info("Can't match samplerate %" PRIu64 ", using %"
326 PRIu64 ".", samplerate, devc->cur_samplerate);
331 SR_PRIV void abort_acquisition(const struct sr_dev_inst *sdi)
333 struct sr_datafeed_packet packet;
334 struct sr_serial_dev_inst *serial;
337 sr_source_remove(serial->fd);
339 /* Terminate session */
340 packet.type = SR_DF_END;
341 sr_session_send(sdi, &packet);
344 SR_PRIV int ols_receive_data(int fd, int revents, void *cb_data)
346 struct drv_context *drvc;
347 struct dev_context *devc;
348 struct sr_serial_dev_inst *serial;
349 struct sr_datafeed_packet packet;
350 struct sr_datafeed_logic logic;
351 struct sr_dev_inst *sdi;
354 int num_channels, offset, j;
360 /* Find this device's devc struct by its fd. */
362 for (l = drvc->instances; l; l = l->next) {
366 if (serial->fd == fd)
371 /* Shouldn't happen. */
374 if (devc->num_transfers++ == 0) {
376 * First time round, means the device started sending data,
377 * and will not stop until done. If it stops sending for
378 * longer than it takes to send a byte, that means it's
379 * finished. We'll double that to 30ms to be sure...
381 sr_source_remove(fd);
382 sr_source_add(fd, G_IO_IN, 30, ols_receive_data, cb_data);
383 devc->raw_sample_buf = g_try_malloc(devc->limit_samples * 4);
384 if (!devc->raw_sample_buf) {
385 sr_err("Sample buffer malloc failed.");
388 /* fill with 1010... for debugging */
389 memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4);
394 for (i = NUM_PROBES; i > 0x02; i /= 2) {
395 if ((devc->flag_reg & i) == 0) {
400 if (revents == G_IO_IN && devc->num_samples < devc->limit_samples) {
401 if (serial_read(serial, &byte, 1) != 1)
404 /* Ignore it if we've read enough. */
405 if (devc->num_samples >= devc->limit_samples)
408 devc->sample[devc->num_bytes++] = byte;
409 sr_dbg("Received byte 0x%.2x.", byte);
410 if (devc->num_bytes == num_channels) {
411 /* Got a full sample. Convert from the OLS's little-endian
412 * sample to the local format. */
413 sample = devc->sample[0] | (devc->sample[1] << 8) \
414 | (devc->sample[2] << 16) | (devc->sample[3] << 24);
415 sr_dbg("Received sample 0x%.*x.", devc->num_bytes * 2, sample);
416 if (devc->flag_reg & FLAG_RLE) {
418 * In RLE mode the high bit of the sample is the
419 * "count" flag, meaning this sample is the number
420 * of times the previous sample occurred.
422 if (devc->sample[devc->num_bytes - 1] & 0x80) {
423 /* Clear the high bit. */
424 sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
425 devc->rle_count = sample;
426 sr_dbg("RLE count: %u.", devc->rle_count);
431 devc->num_samples += devc->rle_count + 1;
432 if (devc->num_samples > devc->limit_samples) {
433 /* Save us from overrunning the buffer. */
434 devc->rle_count -= devc->num_samples - devc->limit_samples;
435 devc->num_samples = devc->limit_samples;
438 if (num_channels < 4) {
440 * Some channel groups may have been turned
441 * off, to speed up transfer between the
442 * hardware and the PC. Expand that here before
443 * submitting it over the session bus --
444 * whatever is listening on the bus will be
445 * expecting a full 32-bit sample, based on
446 * the number of probes.
449 memset(devc->tmp_sample, 0, 4);
450 for (i = 0; i < 4; i++) {
451 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
453 * This channel group was
454 * enabled, copy from received
457 devc->tmp_sample[i] = devc->sample[j++];
458 } else if (devc->flag_reg & FLAG_DEMUX && (i > 2)) {
459 /* group 2 & 3 get added to 0 & 1 */
460 devc->tmp_sample[i - 2] = devc->sample[j++];
463 memcpy(devc->sample, devc->tmp_sample, 4);
464 sr_dbg("Full sample: 0x%.8x.", sample);
467 /* the OLS sends its sample buffer backwards.
468 * store it in reverse order here, so we can dump
469 * this on the session bus later.
471 offset = (devc->limit_samples - devc->num_samples) * 4;
472 for (i = 0; i <= devc->rle_count; i++) {
473 memcpy(devc->raw_sample_buf + offset + (i * 4),
476 memset(devc->sample, 0, 4);
482 * This is the main loop telling us a timeout was reached, or
483 * we've acquired all the samples we asked for -- we're done.
484 * Send the (properly-ordered) buffer to the frontend.
486 if (devc->trigger_at != -1) {
487 /* a trigger was set up, so we need to tell the frontend
490 if (devc->trigger_at > 0) {
491 /* there are pre-trigger samples, send those first */
492 packet.type = SR_DF_LOGIC;
493 packet.payload = &logic;
494 logic.length = devc->trigger_at * 4;
496 logic.data = devc->raw_sample_buf +
497 (devc->limit_samples - devc->num_samples) * 4;
498 sr_session_send(cb_data, &packet);
501 /* send the trigger */
502 packet.type = SR_DF_TRIGGER;
503 sr_session_send(cb_data, &packet);
505 /* send post-trigger samples */
506 packet.type = SR_DF_LOGIC;
507 packet.payload = &logic;
508 logic.length = (devc->num_samples * 4) - (devc->trigger_at * 4);
510 logic.data = devc->raw_sample_buf + devc->trigger_at * 4 +
511 (devc->limit_samples - devc->num_samples) * 4;
512 sr_session_send(cb_data, &packet);
514 /* no trigger was used */
515 packet.type = SR_DF_LOGIC;
516 packet.payload = &logic;
517 logic.length = devc->num_samples * 4;
519 logic.data = devc->raw_sample_buf +
520 (devc->limit_samples - devc->num_samples) * 4;
521 sr_session_send(cb_data, &packet);
523 g_free(devc->raw_sample_buf);
525 serial_flush(serial);
526 abort_acquisition(sdi);