2 * This file is part of the sigrok 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;
78 * Set up the probe mask for later configuration into the
81 probe_bit = 1 << (probe->index);
82 devc->probe_mask |= probe_bit;
87 /* Configure trigger mask and value. */
89 for (tc = probe->trigger; tc && *tc; tc++) {
90 devc->trigger_mask[stage] |= probe_bit;
92 devc->trigger_value[stage] |= probe_bit;
96 * TODO: Only supporting parallel mode, with
101 if (stage > devc->num_stages)
102 devc->num_stages = stage;
108 SR_PRIV uint32_t reverse16(uint32_t in)
112 out = (in & 0xff) << 8;
113 out |= (in & 0xff00) >> 8;
114 out |= (in & 0xff0000) << 8;
115 out |= (in & 0xff000000) >> 8;
120 SR_PRIV uint32_t reverse32(uint32_t in)
124 out = (in & 0xff) << 24;
125 out |= (in & 0xff00) << 8;
126 out |= (in & 0xff0000) >> 8;
127 out |= (in & 0xff000000) >> 24;
132 SR_PRIV struct dev_context *ols_dev_new(void)
134 struct dev_context *devc;
136 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
137 sr_err("Device context malloc failed.");
141 devc->trigger_at = -1;
142 devc->probe_mask = 0xffffffff;
143 devc->cur_samplerate = SR_KHZ(200);
149 SR_PRIV struct sr_dev_inst *get_metadata(struct sr_serial_dev_inst *serial)
151 struct sr_dev_inst *sdi;
152 struct dev_context *devc;
153 struct sr_probe *probe;
154 uint32_t tmp_int, ui;
155 uint8_t key, type, token;
156 GString *tmp_str, *devname, *version;
159 sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, NULL, NULL, NULL);
161 devc = ols_dev_new();
164 devname = g_string_new("");
165 version = g_string_new("");
169 if (serial_read(serial, &key, 1) != 1 || key == 0x00)
175 /* NULL-terminated string */
176 tmp_str = g_string_new("");
177 while (serial_read(serial, &tmp_c, 1) == 1 && tmp_c != '\0')
178 g_string_append_c(tmp_str, tmp_c);
179 sr_dbg("Got metadata key 0x%.2x value '%s'.",
184 devname = g_string_append(devname, tmp_str->str);
187 /* FPGA firmware version */
189 g_string_append(version, ", ");
190 g_string_append(version, "FPGA version ");
191 g_string_append(version, tmp_str->str);
194 /* Ancillary version */
196 g_string_append(version, ", ");
197 g_string_append(version, "Ancillary version ");
198 g_string_append(version, tmp_str->str);
201 sr_info("ols: unknown token 0x%.2x: '%s'",
202 token, tmp_str->str);
205 g_string_free(tmp_str, TRUE);
208 /* 32-bit unsigned integer */
209 if (serial_read(serial, &tmp_int, 4) != 4)
211 tmp_int = reverse32(tmp_int);
212 sr_dbg("Got metadata key 0x%.2x value 0x%.8x.",
216 /* Number of usable probes */
217 for (ui = 0; ui < tmp_int; ui++) {
218 if (!(probe = sr_probe_new(ui, SR_PROBE_LOGIC, TRUE,
219 ols_probe_names[ui])))
221 sdi->probes = g_slist_append(sdi->probes, probe);
225 /* Amount of sample memory available (bytes) */
226 devc->max_samples = tmp_int;
229 /* Amount of dynamic memory available (bytes) */
230 /* what is this for? */
233 /* Maximum sample rate (hz) */
234 devc->max_samplerate = tmp_int;
237 /* protocol version */
238 devc->protocol_version = tmp_int;
241 sr_info("Unknown token 0x%.2x: 0x%.8x.",
247 /* 8-bit unsigned integer */
248 if (serial_read(serial, &tmp_c, 1) != 1)
250 sr_dbg("Got metadata key 0x%.2x value 0x%.2x.",
254 /* Number of usable probes */
255 for (ui = 0; ui < tmp_c; ui++) {
256 if (!(probe = sr_probe_new(ui, SR_PROBE_LOGIC, TRUE,
257 ols_probe_names[ui])))
259 sdi->probes = g_slist_append(sdi->probes, probe);
263 /* protocol version */
264 devc->protocol_version = tmp_c;
267 sr_info("Unknown token 0x%.2x: 0x%.2x.",
278 sdi->model = devname->str;
279 sdi->version = version->str;
280 g_string_free(devname, FALSE);
281 g_string_free(version, FALSE);
286 SR_PRIV int ols_set_samplerate(const struct sr_dev_inst *sdi,
287 const uint64_t samplerate)
289 struct dev_context *devc;
292 if (devc->max_samplerate && samplerate > devc->max_samplerate)
293 return SR_ERR_SAMPLERATE;
295 if (samplerate > CLOCK_RATE) {
296 devc->flag_reg |= FLAG_DEMUX;
297 devc->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
299 devc->flag_reg &= ~FLAG_DEMUX;
300 devc->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
303 /* Calculate actual samplerate used and complain if it is different
304 * from the requested.
306 devc->cur_samplerate = CLOCK_RATE / (devc->cur_samplerate_divider + 1);
307 if (devc->flag_reg & FLAG_DEMUX)
308 devc->cur_samplerate *= 2;
309 if (devc->cur_samplerate != samplerate)
310 sr_info("Can't match samplerate %" PRIu64 ", using %"
311 PRIu64 ".", samplerate, devc->cur_samplerate);
316 SR_PRIV void abort_acquisition(const struct sr_dev_inst *sdi)
318 struct sr_datafeed_packet packet;
319 struct dev_context *devc;
322 sr_source_remove(devc->serial->fd);
324 /* Terminate session */
325 packet.type = SR_DF_END;
326 sr_session_send(sdi, &packet);
329 SR_PRIV int ols_receive_data(int fd, int revents, void *cb_data)
331 struct sr_datafeed_packet packet;
332 struct sr_datafeed_logic logic;
333 struct sr_dev_inst *sdi;
334 struct drv_context *drvc;
335 struct dev_context *devc;
338 int num_channels, offset, i, j;
343 /* Find this device's devc struct by its fd. */
345 for (l = drvc->instances; l; l = l->next) {
348 if (devc->serial->fd == fd)
353 /* Shouldn't happen. */
356 if (devc->num_transfers++ == 0) {
358 * First time round, means the device started sending data,
359 * and will not stop until done. If it stops sending for
360 * longer than it takes to send a byte, that means it's
361 * finished. We'll double that to 30ms to be sure...
363 sr_source_remove(fd);
364 sr_source_add(fd, G_IO_IN, 30, ols_receive_data, cb_data);
365 devc->raw_sample_buf = g_try_malloc(devc->limit_samples * 4);
366 if (!devc->raw_sample_buf) {
367 sr_err("Sample buffer malloc failed.");
370 /* fill with 1010... for debugging */
371 memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4);
375 for (i = 0x20; i > 0x02; i /= 2) {
376 if ((devc->flag_reg & i) == 0)
380 if (revents == G_IO_IN) {
381 if (serial_read(devc->serial, &byte, 1) != 1)
384 /* Ignore it if we've read enough. */
385 if (devc->num_samples >= devc->limit_samples)
388 devc->sample[devc->num_bytes++] = byte;
389 sr_dbg("Received byte 0x%.2x.", byte);
390 if (devc->num_bytes == num_channels) {
391 /* Got a full sample. */
392 sample = devc->sample[0] | (devc->sample[1] << 8) \
393 | (devc->sample[2] << 16) | (devc->sample[3] << 24);
394 sr_dbg("Received sample 0x%.*x.", devc->num_bytes * 2, sample);
395 if (devc->flag_reg & FLAG_RLE) {
397 * In RLE mode -1 should never come in as a
398 * sample, because bit 31 is the "count" flag.
400 if (devc->sample[devc->num_bytes - 1] & 0x80) {
401 devc->sample[devc->num_bytes - 1] &= 0x7f;
403 * FIXME: This will only work on
404 * little-endian systems.
406 devc->rle_count = sample;
407 sr_dbg("RLE count: %d.", devc->rle_count);
412 devc->num_samples += devc->rle_count + 1;
413 if (devc->num_samples > devc->limit_samples) {
414 /* Save us from overrunning the buffer. */
415 devc->rle_count -= devc->num_samples - devc->limit_samples;
416 devc->num_samples = devc->limit_samples;
419 if (num_channels < 4) {
421 * Some channel groups may have been turned
422 * off, to speed up transfer between the
423 * hardware and the PC. Expand that here before
424 * submitting it over the session bus --
425 * whatever is listening on the bus will be
426 * expecting a full 32-bit sample, based on
427 * the number of probes.
430 memset(devc->tmp_sample, 0, 4);
431 for (i = 0; i < 4; i++) {
432 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
434 * This channel group was
435 * enabled, copy from received
438 devc->tmp_sample[i] = devc->sample[j++];
441 memcpy(devc->sample, devc->tmp_sample, 4);
442 sr_dbg("Full sample: 0x%.8x.", sample);
445 /* the OLS sends its sample buffer backwards.
446 * store it in reverse order here, so we can dump
447 * this on the session bus later.
449 offset = (devc->limit_samples - devc->num_samples) * 4;
450 for (i = 0; i <= devc->rle_count; i++) {
451 memcpy(devc->raw_sample_buf + offset + (i * 4),
454 memset(devc->sample, 0, 4);
460 * This is the main loop telling us a timeout was reached, or
461 * we've acquired all the samples we asked for -- we're done.
462 * Send the (properly-ordered) buffer to the frontend.
464 if (devc->trigger_at != -1) {
465 /* a trigger was set up, so we need to tell the frontend
468 if (devc->trigger_at > 0) {
469 /* there are pre-trigger samples, send those first */
470 packet.type = SR_DF_LOGIC;
471 packet.payload = &logic;
472 logic.length = devc->trigger_at * 4;
474 logic.data = devc->raw_sample_buf +
475 (devc->limit_samples - devc->num_samples) * 4;
476 sr_session_send(cb_data, &packet);
479 /* send the trigger */
480 packet.type = SR_DF_TRIGGER;
481 sr_session_send(cb_data, &packet);
483 /* send post-trigger samples */
484 packet.type = SR_DF_LOGIC;
485 packet.payload = &logic;
486 logic.length = (devc->num_samples * 4) - (devc->trigger_at * 4);
488 logic.data = devc->raw_sample_buf + devc->trigger_at * 4 +
489 (devc->limit_samples - devc->num_samples) * 4;
490 sr_session_send(cb_data, &packet);
492 /* no trigger was used */
493 packet.type = SR_DF_LOGIC;
494 packet.payload = &logic;
495 logic.length = devc->num_samples * 4;
497 logic.data = devc->raw_sample_buf +
498 (devc->limit_samples - devc->num_samples) * 4;
499 sr_session_send(cb_data, &packet);
501 g_free(devc->raw_sample_buf);
503 serial_flush(devc->serial);
504 abort_acquisition(sdi);
505 serial_close(devc->serial);