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, serial_timeout(serial, 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, serial_timeout(serial, 1)) != 5)
57 /* Configures the channel mask based on which channels are enabled. */
58 SR_PRIV void ols_channel_mask(const struct sr_dev_inst *sdi)
60 struct dev_context *devc;
61 struct sr_channel *channel;
66 devc->channel_mask = 0;
67 for (l = sdi->channels; l; l = l->next) {
70 devc->channel_mask |= 1 << channel->index;
74 SR_PRIV int ols_convert_trigger(const struct sr_dev_inst *sdi)
76 struct dev_context *devc;
77 struct sr_trigger *trigger;
78 struct sr_trigger_stage *stage;
79 struct sr_trigger_match *match;
86 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
87 devc->trigger_mask[i] = 0;
88 devc->trigger_value[i] = 0;
91 if (!(trigger = sr_session_trigger_get(sdi->session)))
94 devc->num_stages = g_slist_length(trigger->stages);
95 if (devc->num_stages > NUM_TRIGGER_STAGES) {
96 sr_err("This device only supports %d trigger stages.",
101 for (l = trigger->stages; l; l = l->next) {
103 for (m = stage->matches; m; m = m->next) {
105 if (!match->channel->enabled)
106 /* Ignore disabled channels with a trigger. */
108 devc->trigger_mask[stage->stage] |= 1 << match->channel->index;
109 if (match->match == SR_TRIGGER_ONE)
110 devc->trigger_value[stage->stage] |= 1 << match->channel->index;
117 SR_PRIV struct dev_context *ols_dev_new(void)
119 struct dev_context *devc;
121 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
122 sr_err("Device context malloc failed.");
126 /* Device-specific settings */
127 devc->max_samples = devc->max_samplerate = devc->protocol_version = 0;
129 /* Acquisition settings */
130 devc->limit_samples = devc->capture_ratio = 0;
131 devc->trigger_at = -1;
132 devc->channel_mask = 0xffffffff;
138 SR_PRIV struct sr_dev_inst *get_metadata(struct sr_serial_dev_inst *serial)
140 struct sr_dev_inst *sdi;
141 struct dev_context *devc;
142 struct sr_channel *ch;
143 uint32_t tmp_int, ui;
144 uint8_t key, type, token;
146 GString *tmp_str, *devname, *version;
149 sdi = sr_dev_inst_new(SR_ST_INACTIVE, NULL, NULL, NULL);
151 devc = ols_dev_new();
154 devname = g_string_new("");
155 version = g_string_new("");
159 delay_ms = serial_timeout(serial, 1);
160 if (serial_read_blocking(serial, &key, 1, delay_ms) != 1)
163 sr_dbg("Got metadata key 0x00, metadata ends.");
170 /* NULL-terminated string */
171 tmp_str = g_string_new("");
172 delay_ms = serial_timeout(serial, 1);
173 while (serial_read_blocking(serial, &tmp_c, 1, delay_ms) == 1 && tmp_c != '\0')
174 g_string_append_c(tmp_str, tmp_c);
175 sr_dbg("Got metadata key 0x%.2x value '%s'.",
180 devname = g_string_append(devname, tmp_str->str);
183 /* FPGA firmware version */
185 g_string_append(version, ", ");
186 g_string_append(version, "FPGA version ");
187 g_string_append(version, tmp_str->str);
190 /* Ancillary version */
192 g_string_append(version, ", ");
193 g_string_append(version, "Ancillary version ");
194 g_string_append(version, tmp_str->str);
197 sr_info("ols: unknown token 0x%.2x: '%s'",
198 token, tmp_str->str);
201 g_string_free(tmp_str, TRUE);
204 /* 32-bit unsigned integer */
205 delay_ms = serial_timeout(serial, 4);
206 if (serial_read_blocking(serial, &tmp_int, 4, delay_ms) != 4)
208 tmp_int = RB32(&tmp_int);
209 sr_dbg("Got metadata key 0x%.2x value 0x%.8x.",
213 /* Number of usable channels */
214 for (ui = 0; ui < tmp_int; ui++) {
215 if (!(ch = sr_channel_new(ui, SR_CHANNEL_LOGIC, TRUE,
216 ols_channel_names[ui])))
218 sdi->channels = g_slist_append(sdi->channels, ch);
222 /* Amount of sample memory available (bytes) */
223 devc->max_samples = tmp_int;
226 /* Amount of dynamic memory available (bytes) */
227 /* what is this for? */
230 /* Maximum sample rate (hz) */
231 devc->max_samplerate = tmp_int;
234 /* protocol version */
235 devc->protocol_version = tmp_int;
238 sr_info("Unknown token 0x%.2x: 0x%.8x.",
244 /* 8-bit unsigned integer */
245 delay_ms = serial_timeout(serial, 1);
246 if (serial_read_blocking(serial, &tmp_c, 1, delay_ms) != 1)
248 sr_dbg("Got metadata key 0x%.2x value 0x%.2x.",
252 /* Number of usable channels */
253 for (ui = 0; ui < tmp_c; ui++) {
254 if (!(ch = sr_channel_new(ui, SR_CHANNEL_LOGIC, TRUE,
255 ols_channel_names[ui])))
257 sdi->channels = g_slist_append(sdi->channels, ch);
261 /* protocol version */
262 devc->protocol_version = tmp_c;
265 sr_info("Unknown token 0x%.2x: 0x%.2x.",
276 sdi->model = devname->str;
277 sdi->version = version->str;
278 g_string_free(devname, FALSE);
279 g_string_free(version, FALSE);
284 SR_PRIV int ols_set_samplerate(const struct sr_dev_inst *sdi,
285 const uint64_t samplerate)
287 struct dev_context *devc;
290 if (devc->max_samplerate && samplerate > devc->max_samplerate)
291 return SR_ERR_SAMPLERATE;
293 if (samplerate > CLOCK_RATE) {
294 sr_info("Enabling demux mode.");
295 devc->flag_reg |= FLAG_DEMUX;
296 devc->flag_reg &= ~FLAG_FILTER;
297 devc->max_channels = NUM_CHANNELS / 2;
298 devc->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
300 sr_info("Disabling demux mode.");
301 devc->flag_reg &= ~FLAG_DEMUX;
302 devc->flag_reg |= FLAG_FILTER;
303 devc->max_channels = NUM_CHANNELS;
304 devc->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
307 /* Calculate actual samplerate used and complain if it is different
308 * from the requested.
310 devc->cur_samplerate = CLOCK_RATE / (devc->cur_samplerate_divider + 1);
311 if (devc->flag_reg & FLAG_DEMUX)
312 devc->cur_samplerate *= 2;
313 if (devc->cur_samplerate != samplerate)
314 sr_info("Can't match samplerate %" PRIu64 ", using %"
315 PRIu64 ".", samplerate, devc->cur_samplerate);
320 SR_PRIV void abort_acquisition(const struct sr_dev_inst *sdi)
322 struct sr_datafeed_packet packet;
323 struct sr_serial_dev_inst *serial;
326 serial_source_remove(sdi->session, serial);
328 /* Terminate session */
329 packet.type = SR_DF_END;
330 sr_session_send(sdi, &packet);
333 SR_PRIV int ols_receive_data(int fd, int revents, void *cb_data)
335 struct dev_context *devc;
336 struct sr_dev_inst *sdi;
337 struct sr_serial_dev_inst *serial;
338 struct sr_datafeed_packet packet;
339 struct sr_datafeed_logic logic;
341 int num_ols_changrp, offset, j;
351 if (devc->num_transfers++ == 0) {
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...
358 serial_source_remove(sdi->session, serial);
359 serial_source_add(sdi->session, serial, G_IO_IN, 30,
360 ols_receive_data, cb_data);
361 devc->raw_sample_buf = g_try_malloc(devc->limit_samples * 4);
362 if (!devc->raw_sample_buf) {
363 sr_err("Sample buffer malloc failed.");
366 /* fill with 1010... for debugging */
367 memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4);
371 for (i = NUM_CHANNELS; i > 0x02; i /= 2) {
372 if ((devc->flag_reg & i) == 0) {
377 if (revents == G_IO_IN && devc->num_samples < devc->limit_samples) {
378 if (serial_read_nonblocking(serial, &byte, 1) != 1)
382 /* Ignore it if we've read enough. */
383 if (devc->num_samples >= devc->limit_samples)
386 devc->sample[devc->num_bytes++] = byte;
387 sr_spew("Received byte 0x%.2x.", byte);
388 if (devc->num_bytes == num_ols_changrp) {
390 devc->cnt_samples_rle++;
392 * Got a full sample. Convert from the OLS's little-endian
393 * sample to the local format.
395 sample = devc->sample[0] | (devc->sample[1] << 8) \
396 | (devc->sample[2] << 16) | (devc->sample[3] << 24);
397 sr_dbg("Received sample 0x%.*x.", devc->num_bytes * 2, sample);
398 if (devc->flag_reg & FLAG_RLE) {
400 * In RLE mode the high bit of the sample is the
401 * "count" flag, meaning this sample is the number
402 * of times the previous sample occurred.
404 if (devc->sample[devc->num_bytes - 1] & 0x80) {
405 /* Clear the high bit. */
406 sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
407 devc->rle_count = sample;
408 devc->cnt_samples_rle += devc->rle_count;
409 sr_dbg("RLE count: %u.", devc->rle_count);
414 devc->num_samples += devc->rle_count + 1;
415 if (devc->num_samples > devc->limit_samples) {
416 /* Save us from overrunning the buffer. */
417 devc->rle_count -= devc->num_samples - devc->limit_samples;
418 devc->num_samples = devc->limit_samples;
421 if (num_ols_changrp < 4) {
423 * Some channel groups may have been turned
424 * off, to speed up transfer between the
425 * hardware and the PC. Expand that here before
426 * submitting it over the session bus --
427 * whatever is listening on the bus will be
428 * expecting a full 32-bit sample, based on
429 * the number of channels.
432 memset(devc->tmp_sample, 0, 4);
433 for (i = 0; i < 4; i++) {
434 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
436 * This channel group was
437 * enabled, copy from received
440 devc->tmp_sample[i] = devc->sample[j++];
441 } else if (devc->flag_reg & FLAG_DEMUX && (i > 2)) {
442 /* group 2 & 3 get added to 0 & 1 */
443 devc->tmp_sample[i - 2] = devc->sample[j++];
446 memcpy(devc->sample, devc->tmp_sample, 4);
447 sr_spew("Expanded sample: 0x%.8x.", sample);
451 * the OLS sends its sample buffer backwards.
452 * store it in reverse order here, so we can dump
453 * this on the session bus later.
455 offset = (devc->limit_samples - devc->num_samples) * 4;
456 for (i = 0; i <= devc->rle_count; i++) {
457 memcpy(devc->raw_sample_buf + offset + (i * 4),
460 memset(devc->sample, 0, 4);
466 * This is the main loop telling us a timeout was reached, or
467 * we've acquired all the samples we asked for -- we're done.
468 * Send the (properly-ordered) buffer to the frontend.
470 sr_dbg("Received %d bytes, %d samples, %d decompressed samples.",
471 devc->cnt_bytes, devc->cnt_samples,
472 devc->cnt_samples_rle);
473 if (devc->trigger_at != -1) {
475 * A trigger was set up, so we need to tell the frontend
478 if (devc->trigger_at > 0) {
479 /* There are pre-trigger samples, send those first. */
480 packet.type = SR_DF_LOGIC;
481 packet.payload = &logic;
482 logic.length = devc->trigger_at * 4;
484 logic.data = devc->raw_sample_buf +
485 (devc->limit_samples - devc->num_samples) * 4;
486 sr_session_send(cb_data, &packet);
489 /* Send the trigger. */
490 packet.type = SR_DF_TRIGGER;
491 sr_session_send(cb_data, &packet);
493 /* Send post-trigger samples. */
494 packet.type = SR_DF_LOGIC;
495 packet.payload = &logic;
496 logic.length = (devc->num_samples * 4) - (devc->trigger_at * 4);
498 logic.data = devc->raw_sample_buf + devc->trigger_at * 4 +
499 (devc->limit_samples - devc->num_samples) * 4;
500 sr_session_send(cb_data, &packet);
502 /* no trigger was used */
503 packet.type = SR_DF_LOGIC;
504 packet.payload = &logic;
505 logic.length = devc->num_samples * 4;
507 logic.data = devc->raw_sample_buf +
508 (devc->limit_samples - devc->num_samples) * 4;
509 sr_session_send(cb_data, &packet);
511 g_free(devc->raw_sample_buf);
513 serial_flush(serial);
514 abort_acquisition(sdi);