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, uint32_t data)
44 sr_dbg("Sending cmd 0x%.2x data 0x%.8x.", command, data);
46 buf[1] = (data & 0xff000000) >> 24;
47 buf[2] = (data & 0xff0000) >> 16;
48 buf[3] = (data & 0xff00) >> 8;
50 if (serial_write_blocking(serial, buf, 5) != 5)
56 SR_PRIV int ols_configure_probes(const struct sr_dev_inst *sdi)
58 struct dev_context *devc;
59 const struct sr_probe *probe;
61 int probe_bit, stage, i;
67 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
68 devc->trigger_mask[i] = 0;
69 devc->trigger_value[i] = 0;
73 for (l = sdi->probes; l; l = l->next) {
74 probe = (const struct sr_probe *)l->data;
78 if (probe->index >= devc->max_probes) {
79 sr_err("Channels over the limit of %d\n", devc->max_probes);
84 * Set up the probe mask for later configuration into the
87 probe_bit = 1 << (probe->index);
88 devc->probe_mask |= probe_bit;
93 /* Configure trigger mask and value. */
95 for (tc = probe->trigger; tc && *tc; tc++) {
96 devc->trigger_mask[stage] |= probe_bit;
98 devc->trigger_value[stage] |= probe_bit;
102 * TODO: Only supporting parallel mode, with
107 if (stage > devc->num_stages)
108 devc->num_stages = stage - 1;
114 SR_PRIV uint32_t reverse16(uint32_t in)
118 out = (in & 0xff) << 8;
119 out |= (in & 0xff00) >> 8;
120 out |= (in & 0xff0000) << 8;
121 out |= (in & 0xff000000) >> 8;
126 SR_PRIV uint32_t reverse32(uint32_t in)
130 out = (in & 0xff) << 24;
131 out |= (in & 0xff00) << 8;
132 out |= (in & 0xff0000) >> 8;
133 out |= (in & 0xff000000) >> 24;
138 SR_PRIV struct dev_context *ols_dev_new(void)
140 struct dev_context *devc;
142 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
143 sr_err("Device context malloc failed.");
147 /* Device-specific settings */
148 devc->max_samples = devc->max_samplerate = devc->protocol_version = 0;
150 /* Acquisition settings */
151 devc->limit_samples = devc->capture_ratio = 0;
152 devc->trigger_at = -1;
153 devc->probe_mask = 0xffffffff;
159 SR_PRIV struct sr_dev_inst *get_metadata(struct sr_serial_dev_inst *serial)
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;
169 sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, NULL, NULL, NULL);
171 devc = ols_dev_new();
174 devname = g_string_new("");
175 version = g_string_new("");
179 if (serial_read_blocking(serial, &key, 1) != 1 || key == 0x00)
185 /* NULL-terminated string */
186 tmp_str = g_string_new("");
187 while (serial_read_blocking(serial, &tmp_c, 1) == 1 && tmp_c != '\0')
188 g_string_append_c(tmp_str, tmp_c);
189 sr_dbg("Got metadata key 0x%.2x value '%s'.",
194 devname = g_string_append(devname, tmp_str->str);
197 /* FPGA firmware version */
199 g_string_append(version, ", ");
200 g_string_append(version, "FPGA version ");
201 g_string_append(version, tmp_str->str);
204 /* Ancillary version */
206 g_string_append(version, ", ");
207 g_string_append(version, "Ancillary version ");
208 g_string_append(version, tmp_str->str);
211 sr_info("ols: unknown token 0x%.2x: '%s'",
212 token, tmp_str->str);
215 g_string_free(tmp_str, TRUE);
218 /* 32-bit unsigned integer */
219 if (serial_read_blocking(serial, &tmp_int, 4) != 4)
221 tmp_int = reverse32(tmp_int);
222 sr_dbg("Got metadata key 0x%.2x value 0x%.8x.",
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])))
231 sdi->probes = g_slist_append(sdi->probes, probe);
235 /* Amount of sample memory available (bytes) */
236 devc->max_samples = tmp_int;
239 /* Amount of dynamic memory available (bytes) */
240 /* what is this for? */
243 /* Maximum sample rate (hz) */
244 devc->max_samplerate = tmp_int;
247 /* protocol version */
248 devc->protocol_version = tmp_int;
251 sr_info("Unknown token 0x%.2x: 0x%.8x.",
257 /* 8-bit unsigned integer */
258 if (serial_read_blocking(serial, &tmp_c, 1) != 1)
260 sr_dbg("Got metadata key 0x%.2x value 0x%.2x.",
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])))
269 sdi->probes = g_slist_append(sdi->probes, probe);
273 /* protocol version */
274 devc->protocol_version = tmp_c;
277 sr_info("Unknown token 0x%.2x: 0x%.2x.",
288 sdi->model = devname->str;
289 sdi->version = version->str;
290 g_string_free(devname, FALSE);
291 g_string_free(version, FALSE);
296 SR_PRIV int ols_set_samplerate(const struct sr_dev_inst *sdi,
297 const uint64_t samplerate)
299 struct dev_context *devc;
302 if (devc->max_samplerate && samplerate > devc->max_samplerate)
303 return SR_ERR_SAMPLERATE;
305 if (samplerate > CLOCK_RATE) {
306 sr_info("Enabling demux mode.");
307 devc->flag_reg |= FLAG_DEMUX;
308 devc->flag_reg &= ~FLAG_FILTER;
309 devc->max_probes = NUM_PROBES / 2;
310 devc->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
312 sr_info("Disabling demux mode.");
313 devc->flag_reg &= ~FLAG_DEMUX;
314 devc->flag_reg |= FLAG_FILTER;
315 devc->max_probes = NUM_PROBES;
316 devc->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
319 /* Calculate actual samplerate used and complain if it is different
320 * from the requested.
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)
326 sr_info("Can't match samplerate %" PRIu64 ", using %"
327 PRIu64 ".", samplerate, devc->cur_samplerate);
332 SR_PRIV void abort_acquisition(const struct sr_dev_inst *sdi)
334 struct sr_datafeed_packet packet;
335 struct sr_serial_dev_inst *serial;
338 serial_source_remove(serial);
340 /* Terminate session */
341 packet.type = SR_DF_END;
342 sr_session_send(sdi, &packet);
345 SR_PRIV int ols_receive_data(int fd, int revents, void *cb_data)
347 struct drv_context *drvc;
348 struct dev_context *devc;
349 struct sr_serial_dev_inst *serial;
350 struct sr_datafeed_packet packet;
351 struct sr_datafeed_logic logic;
352 struct sr_dev_inst *sdi;
355 int num_channels, offset, j;
362 /* Find this device's devc struct by its fd. */
364 for (l = drvc->instances; l; l = l->next) {
368 sp_get_port_handle(serial->data, &serial_fd);
374 /* Shouldn't happen. */
377 if (devc->num_transfers++ == 0) {
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...
384 serial_source_remove(serial);
385 serial_source_add(serial, G_IO_IN, 30, ols_receive_data, cb_data);
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.");
391 /* fill with 1010... for debugging */
392 memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4);
397 for (i = NUM_PROBES; i > 0x02; i /= 2) {
398 if ((devc->flag_reg & i) == 0) {
403 if (revents == G_IO_IN && devc->num_samples < devc->limit_samples) {
404 if (serial_read_nonblocking(serial, &byte, 1) != 1)
407 /* Ignore it if we've read enough. */
408 if (devc->num_samples >= devc->limit_samples)
411 devc->sample[devc->num_bytes++] = byte;
412 sr_spew("Received byte 0x%.2x.", byte);
413 if (devc->num_bytes == num_channels) {
414 /* Got a full sample. Convert from the OLS's little-endian
415 * sample to the local format. */
416 sample = devc->sample[0] | (devc->sample[1] << 8) \
417 | (devc->sample[2] << 16) | (devc->sample[3] << 24);
418 sr_spew("Received sample 0x%.*x.", devc->num_bytes * 2, sample);
419 if (devc->flag_reg & FLAG_RLE) {
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.
425 if (devc->sample[devc->num_bytes - 1] & 0x80) {
426 /* Clear the high bit. */
427 sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
428 devc->rle_count = sample;
429 sr_dbg("RLE count: %u.", devc->rle_count);
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;
441 if (num_channels < 4) {
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.
452 memset(devc->tmp_sample, 0, 4);
453 for (i = 0; i < 4; i++) {
454 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
456 * This channel group was
457 * enabled, copy from received
460 devc->tmp_sample[i] = devc->sample[j++];
461 } else if (devc->flag_reg & FLAG_DEMUX && (i > 2)) {
462 /* group 2 & 3 get added to 0 & 1 */
463 devc->tmp_sample[i - 2] = devc->sample[j++];
466 memcpy(devc->sample, devc->tmp_sample, 4);
467 sr_dbg("Full sample: 0x%.8x.", sample);
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.
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),
479 memset(devc->sample, 0, 4);
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.
489 if (devc->trigger_at != -1) {
490 /* a trigger was set up, so we need to tell the frontend
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;
499 logic.data = devc->raw_sample_buf +
500 (devc->limit_samples - devc->num_samples) * 4;
501 sr_session_send(cb_data, &packet);
504 /* send the trigger */
505 packet.type = SR_DF_TRIGGER;
506 sr_session_send(cb_data, &packet);
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);
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);
517 /* no trigger was used */
518 packet.type = SR_DF_LOGIC;
519 packet.payload = &logic;
520 logic.length = devc->num_samples * 4;
522 logic.data = devc->raw_sample_buf +
523 (devc->limit_samples - devc->num_samples) * 4;
524 sr_session_send(cb_data, &packet);
526 g_free(devc->raw_sample_buf);
528 serial_flush(serial);
529 abort_acquisition(sdi);