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_probe *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_probe *)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_probe *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 || key == 0x00)
159 /* NULL-terminated string */
160 tmp_str = g_string_new("");
161 while (serial_read_blocking(serial, &tmp_c, 1) == 1 && tmp_c != '\0')
162 g_string_append_c(tmp_str, tmp_c);
163 sr_dbg("Got metadata key 0x%.2x value '%s'.",
168 devname = g_string_append(devname, tmp_str->str);
171 /* FPGA firmware version */
173 g_string_append(version, ", ");
174 g_string_append(version, "FPGA version ");
175 g_string_append(version, tmp_str->str);
178 /* Ancillary version */
180 g_string_append(version, ", ");
181 g_string_append(version, "Ancillary version ");
182 g_string_append(version, tmp_str->str);
185 sr_info("ols: unknown token 0x%.2x: '%s'",
186 token, tmp_str->str);
189 g_string_free(tmp_str, TRUE);
192 /* 32-bit unsigned integer */
193 if (serial_read_blocking(serial, &tmp_int, 4) != 4)
195 tmp_int = RB32(&tmp_int);
196 sr_dbg("Got metadata key 0x%.2x value 0x%.8x.",
200 /* Number of usable probes */
201 for (ui = 0; ui < tmp_int; ui++) {
202 if (!(probe = sr_probe_new(ui, SR_PROBE_LOGIC, TRUE,
203 ols_probe_names[ui])))
205 sdi->probes = g_slist_append(sdi->probes, probe);
209 /* Amount of sample memory available (bytes) */
210 devc->max_samples = tmp_int;
213 /* Amount of dynamic memory available (bytes) */
214 /* what is this for? */
217 /* Maximum sample rate (hz) */
218 devc->max_samplerate = tmp_int;
221 /* protocol version */
222 devc->protocol_version = tmp_int;
225 sr_info("Unknown token 0x%.2x: 0x%.8x.",
231 /* 8-bit unsigned integer */
232 if (serial_read_blocking(serial, &tmp_c, 1) != 1)
234 sr_dbg("Got metadata key 0x%.2x value 0x%.2x.",
238 /* Number of usable probes */
239 for (ui = 0; ui < tmp_c; ui++) {
240 if (!(probe = sr_probe_new(ui, SR_PROBE_LOGIC, TRUE,
241 ols_probe_names[ui])))
243 sdi->probes = g_slist_append(sdi->probes, probe);
247 /* protocol version */
248 devc->protocol_version = tmp_c;
251 sr_info("Unknown token 0x%.2x: 0x%.2x.",
262 sdi->model = devname->str;
263 sdi->version = version->str;
264 g_string_free(devname, FALSE);
265 g_string_free(version, FALSE);
270 SR_PRIV int ols_set_samplerate(const struct sr_dev_inst *sdi,
271 const uint64_t samplerate)
273 struct dev_context *devc;
276 if (devc->max_samplerate && samplerate > devc->max_samplerate)
277 return SR_ERR_SAMPLERATE;
279 if (samplerate > CLOCK_RATE) {
280 sr_info("Enabling demux mode.");
281 devc->flag_reg |= FLAG_DEMUX;
282 devc->flag_reg &= ~FLAG_FILTER;
283 devc->max_probes = NUM_PROBES / 2;
284 devc->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
286 sr_info("Disabling demux mode.");
287 devc->flag_reg &= ~FLAG_DEMUX;
288 devc->flag_reg |= FLAG_FILTER;
289 devc->max_probes = NUM_PROBES;
290 devc->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
293 /* Calculate actual samplerate used and complain if it is different
294 * from the requested.
296 devc->cur_samplerate = CLOCK_RATE / (devc->cur_samplerate_divider + 1);
297 if (devc->flag_reg & FLAG_DEMUX)
298 devc->cur_samplerate *= 2;
299 if (devc->cur_samplerate != samplerate)
300 sr_info("Can't match samplerate %" PRIu64 ", using %"
301 PRIu64 ".", samplerate, devc->cur_samplerate);
306 SR_PRIV void abort_acquisition(const struct sr_dev_inst *sdi)
308 struct sr_datafeed_packet packet;
309 struct sr_serial_dev_inst *serial;
312 serial_source_remove(serial);
314 /* Terminate session */
315 packet.type = SR_DF_END;
316 sr_session_send(sdi, &packet);
319 SR_PRIV int ols_receive_data(int fd, int revents, void *cb_data)
321 struct drv_context *drvc;
322 struct dev_context *devc;
323 struct sr_serial_dev_inst *serial;
324 struct sr_datafeed_packet packet;
325 struct sr_datafeed_logic logic;
326 struct sr_dev_inst *sdi;
329 int num_channels, offset, j;
336 /* Find this device's devc struct by its fd. */
338 for (l = drvc->instances; l; l = l->next) {
342 sp_get_port_handle(serial->data, &serial_fd);
348 /* Shouldn't happen. */
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(serial);
359 serial_source_add(serial, G_IO_IN, 30, ols_receive_data, cb_data);
360 devc->raw_sample_buf = g_try_malloc(devc->limit_samples * 4);
361 if (!devc->raw_sample_buf) {
362 sr_err("Sample buffer malloc failed.");
365 /* fill with 1010... for debugging */
366 memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4);
371 for (i = NUM_PROBES; 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)
381 /* Ignore it if we've read enough. */
382 if (devc->num_samples >= devc->limit_samples)
385 devc->sample[devc->num_bytes++] = byte;
386 sr_spew("Received byte 0x%.2x.", byte);
387 if (devc->num_bytes == num_channels) {
388 /* Got a full sample. Convert from the OLS's little-endian
389 * sample to the local format. */
390 sample = devc->sample[0] | (devc->sample[1] << 8) \
391 | (devc->sample[2] << 16) | (devc->sample[3] << 24);
392 sr_spew("Received sample 0x%.*x.", devc->num_bytes * 2, sample);
393 if (devc->flag_reg & FLAG_RLE) {
395 * In RLE mode the high bit of the sample is the
396 * "count" flag, meaning this sample is the number
397 * of times the previous sample occurred.
399 if (devc->sample[devc->num_bytes - 1] & 0x80) {
400 /* Clear the high bit. */
401 sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
402 devc->rle_count = sample;
403 sr_dbg("RLE count: %u.", devc->rle_count);
408 devc->num_samples += devc->rle_count + 1;
409 if (devc->num_samples > devc->limit_samples) {
410 /* Save us from overrunning the buffer. */
411 devc->rle_count -= devc->num_samples - devc->limit_samples;
412 devc->num_samples = devc->limit_samples;
415 if (num_channels < 4) {
417 * Some channel groups may have been turned
418 * off, to speed up transfer between the
419 * hardware and the PC. Expand that here before
420 * submitting it over the session bus --
421 * whatever is listening on the bus will be
422 * expecting a full 32-bit sample, based on
423 * the number of probes.
426 memset(devc->tmp_sample, 0, 4);
427 for (i = 0; i < 4; i++) {
428 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
430 * This channel group was
431 * enabled, copy from received
434 devc->tmp_sample[i] = devc->sample[j++];
435 } else if (devc->flag_reg & FLAG_DEMUX && (i > 2)) {
436 /* group 2 & 3 get added to 0 & 1 */
437 devc->tmp_sample[i - 2] = devc->sample[j++];
440 memcpy(devc->sample, devc->tmp_sample, 4);
441 sr_dbg("Full sample: 0x%.8x.", sample);
444 /* the OLS sends its sample buffer backwards.
445 * store it in reverse order here, so we can dump
446 * this on the session bus later.
448 offset = (devc->limit_samples - devc->num_samples) * 4;
449 for (i = 0; i <= devc->rle_count; i++) {
450 memcpy(devc->raw_sample_buf + offset + (i * 4),
453 memset(devc->sample, 0, 4);
459 * This is the main loop telling us a timeout was reached, or
460 * we've acquired all the samples we asked for -- we're done.
461 * Send the (properly-ordered) buffer to the frontend.
463 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);